1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/printing/print_preview_dialog_controller.h"
6 #include "chrome/browser/printing/print_preview_test.h"
7 #include "chrome/browser/printing/print_view_manager.h"
8 #include "chrome/browser/ui/browser_commands.h"
9 #include "chrome/browser/ui/browser_finder.h"
10 #include "chrome/browser/ui/tabs/tab_strip_model.h"
11 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h"
12 #include "content/public/browser/navigation_details.h"
13 #include "content/public/common/url_constants.h"
15 using content::WebContents
;
17 // Test crashes on Aura due to initiator's native view having no parent.
18 // http://crbug.com/104284
20 #define MAYBE_GetOrCreatePreviewDialog DISABLED_GetOrCreatePreviewDialog
21 #define MAYBE_MultiplePreviewDialogs DISABLED_MultiplePreviewDialogs
22 #define MAYBE_ClearInitiatorDetails DISABLED_ClearInitiatorDetails
24 #define MAYBE_GetOrCreatePreviewDialog GetOrCreatePreviewDialog
25 #define MAYBE_MultiplePreviewDialogs MultiplePreviewDialogs
26 #define MAYBE_ClearInitiatorDetails ClearInitiatorDetails
31 typedef PrintPreviewTest PrintPreviewDialogControllerUnitTest
;
33 // Create/Get a preview dialog for initiator.
34 TEST_F(PrintPreviewDialogControllerUnitTest
, MAYBE_GetOrCreatePreviewDialog
) {
35 // Lets start with one window with one tab.
36 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
37 EXPECT_EQ(0, browser()->tab_strip_model()->count());
38 chrome::NewTab(browser());
39 EXPECT_EQ(1, browser()->tab_strip_model()->count());
41 // Create a reference to initiator contents.
42 WebContents
* initiator
= browser()->tab_strip_model()->GetActiveWebContents();
44 PrintPreviewDialogController
* dialog_controller
=
45 PrintPreviewDialogController::GetInstance();
46 ASSERT_TRUE(dialog_controller
);
48 // Get the preview dialog for initiator.
49 PrintViewManager::FromWebContents(initiator
)->PrintPreviewNow(false);
50 WebContents
* preview_dialog
=
51 dialog_controller
->GetOrCreatePreviewDialog(initiator
);
53 // New print preview dialog is a constrained window, so the number of tabs is
55 EXPECT_EQ(1, browser()->tab_strip_model()->count());
56 EXPECT_NE(initiator
, preview_dialog
);
58 // Get the print preview dialog for the same initiator.
59 WebContents
* new_preview_dialog
=
60 dialog_controller
->GetOrCreatePreviewDialog(initiator
);
62 // Preview dialog already exists. Tab count remains the same.
63 EXPECT_EQ(1, browser()->tab_strip_model()->count());
65 // 1:1 relationship between initiator and preview dialog.
66 EXPECT_EQ(new_preview_dialog
, preview_dialog
);
69 // Tests multiple print preview dialogs exist in the same browser for different
70 // initiators. If a preview dialog already exists for an initiator, that
71 // initiator gets focused.
72 TEST_F(PrintPreviewDialogControllerUnitTest
, MAYBE_MultiplePreviewDialogs
) {
73 // Lets start with one window and two tabs.
74 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
75 TabStripModel
* tab_strip_model
= browser()->tab_strip_model();
76 ASSERT_TRUE(tab_strip_model
);
78 EXPECT_EQ(0, tab_strip_model
->count());
80 // Create some new initiators.
81 chrome::NewTab(browser());
82 WebContents
* web_contents_1
= tab_strip_model
->GetActiveWebContents();
83 ASSERT_TRUE(web_contents_1
);
85 chrome::NewTab(browser());
86 WebContents
* web_contents_2
= tab_strip_model
->GetActiveWebContents();
87 ASSERT_TRUE(web_contents_2
);
88 EXPECT_EQ(2, tab_strip_model
->count());
90 PrintPreviewDialogController
* dialog_controller
=
91 PrintPreviewDialogController::GetInstance();
92 ASSERT_TRUE(dialog_controller
);
94 // Create preview dialog for |web_contents_1|
95 PrintViewManager::FromWebContents(web_contents_1
)->PrintPreviewNow(false);
96 WebContents
* preview_dialog_1
=
97 dialog_controller
->GetOrCreatePreviewDialog(web_contents_1
);
99 EXPECT_NE(web_contents_1
, preview_dialog_1
);
100 EXPECT_EQ(2, tab_strip_model
->count());
102 // Create preview dialog for |web_contents_2|
103 PrintViewManager::FromWebContents(web_contents_2
)->PrintPreviewNow(false);
104 WebContents
* preview_dialog_2
=
105 dialog_controller
->GetOrCreatePreviewDialog(web_contents_2
);
107 EXPECT_NE(web_contents_2
, preview_dialog_2
);
108 EXPECT_NE(preview_dialog_1
, preview_dialog_2
);
109 // 2 initiators and 2 preview dialogs exist in the same browser. The preview
110 // dialogs are constrained in their respective initiators.
111 EXPECT_EQ(2, tab_strip_model
->count());
113 int tab_1_index
= tab_strip_model
->GetIndexOfWebContents(web_contents_1
);
114 int tab_2_index
= tab_strip_model
->GetIndexOfWebContents(web_contents_2
);
115 int preview_dialog_1_index
=
116 tab_strip_model
->GetIndexOfWebContents(preview_dialog_1
);
117 int preview_dialog_2_index
=
118 tab_strip_model
->GetIndexOfWebContents(preview_dialog_2
);
120 // Constrained dialogs are not in the TabStripModel.
121 EXPECT_EQ(-1, preview_dialog_1_index
);
122 EXPECT_EQ(-1, preview_dialog_2_index
);
124 // Since |preview_dialog_2_index| was the most recently created dialog, its
125 // initiator should have focus.
126 EXPECT_EQ(tab_2_index
, tab_strip_model
->active_index());
128 // When we get the preview dialog for |web_contents_1|,
129 // |preview_dialog_1| is activated and focused.
130 dialog_controller
->GetOrCreatePreviewDialog(web_contents_1
);
131 EXPECT_EQ(tab_1_index
, tab_strip_model
->active_index());
134 // Check clearing the initiator details associated with a print preview dialog
135 // allows the initiator to create another print preview dialog.
136 TEST_F(PrintPreviewDialogControllerUnitTest
, MAYBE_ClearInitiatorDetails
) {
137 // Lets start with one window with one tab.
138 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
139 EXPECT_EQ(0, browser()->tab_strip_model()->count());
140 chrome::NewTab(browser());
141 EXPECT_EQ(1, browser()->tab_strip_model()->count());
143 // Create a reference to initiator contents.
144 WebContents
* initiator
= browser()->tab_strip_model()->GetActiveWebContents();
146 PrintPreviewDialogController
* dialog_controller
=
147 PrintPreviewDialogController::GetInstance();
148 ASSERT_TRUE(dialog_controller
);
150 // Get the preview dialog for the initiator.
151 PrintViewManager::FromWebContents(initiator
)->PrintPreviewNow(false);
152 WebContents
* preview_dialog
=
153 dialog_controller
->GetOrCreatePreviewDialog(initiator
);
155 // New print preview dialog is a constrained window, so the number of tabs is
157 EXPECT_EQ(1, browser()->tab_strip_model()->count());
158 EXPECT_NE(initiator
, preview_dialog
);
160 // Clear the initiator details associated with the preview dialog.
161 dialog_controller
->EraseInitiatorInfo(preview_dialog
);
163 // Get a new print preview dialog for the initiator.
164 WebContents
* new_preview_dialog
=
165 dialog_controller
->GetOrCreatePreviewDialog(initiator
);
167 // New print preview dialog is a constrained window, so the number of tabs is
169 EXPECT_EQ(1, browser()->tab_strip_model()->count());
170 // Verify a new print preview dialog has been created.
171 EXPECT_NE(new_preview_dialog
, preview_dialog
);
174 } // namespace printing