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/browser/notification_service.h"
14 #include "content/public/browser/notification_types.h"
15 #include "content/public/common/url_constants.h"
17 using content::WebContents
;
19 // Test crashes on Aura due to initiator tab's native view having no parent.
20 // http://crbug.com/104284
22 #define MAYBE_GetOrCreatePreviewDialog DISABLED_GetOrCreatePreviewDialog
23 #define MAYBE_MultiplePreviewDialogs DISABLED_MultiplePreviewDialogs
24 #define MAYBE_ClearInitiatorTabDetails DISABLED_ClearInitiatorTabDetails
26 #define MAYBE_GetOrCreatePreviewDialog GetOrCreatePreviewDialog
27 #define MAYBE_MultiplePreviewDialogs MultiplePreviewDialogs
28 #define MAYBE_ClearInitiatorTabDetails ClearInitiatorTabDetails
33 typedef PrintPreviewTest PrintPreviewDialogControllerUnitTest
;
35 // Create/Get a preview dialog for initiator tab.
36 TEST_F(PrintPreviewDialogControllerUnitTest
, MAYBE_GetOrCreatePreviewDialog
) {
37 // Lets start with one window with one tab.
38 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
39 EXPECT_EQ(0, browser()->tab_strip_model()->count());
40 chrome::NewTab(browser());
41 EXPECT_EQ(1, browser()->tab_strip_model()->count());
43 // Create a reference to initiator tab contents.
44 WebContents
* initiator_tab
=
45 browser()->tab_strip_model()->GetActiveWebContents();
47 PrintPreviewDialogController
* dialog_controller
=
48 PrintPreviewDialogController::GetInstance();
49 ASSERT_TRUE(dialog_controller
);
51 // Get the preview dialog for initiator tab.
52 PrintViewManager::FromWebContents(initiator_tab
)->PrintPreviewNow(false);
53 WebContents
* preview_dialog
=
54 dialog_controller
->GetOrCreatePreviewDialog(initiator_tab
);
56 // New print preview dialog is a constrained window, so the number of tabs is
58 EXPECT_EQ(1, browser()->tab_strip_model()->count());
59 EXPECT_NE(initiator_tab
, preview_dialog
);
61 // Get the print preview dialog for the same initiator tab.
62 WebContents
* new_preview_dialog
=
63 dialog_controller
->GetOrCreatePreviewDialog(initiator_tab
);
65 // Preview dialog already exists. Tab count remains the same.
66 EXPECT_EQ(1, browser()->tab_strip_model()->count());
68 // 1:1 relationship between initiator tab and preview dialog.
69 EXPECT_EQ(new_preview_dialog
, preview_dialog
);
72 // Tests multiple print preview dialogs exist in the same browser for
73 // different initiator tabs. If a preview dialog already exists for an
74 // initiator tab, that initiator tab gets focused.
75 TEST_F(PrintPreviewDialogControllerUnitTest
, MAYBE_MultiplePreviewDialogs
) {
76 // Lets start with one window and two tabs.
77 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
78 TabStripModel
* tab_strip_model
= browser()->tab_strip_model();
79 ASSERT_TRUE(tab_strip_model
);
81 EXPECT_EQ(0, tab_strip_model
->count());
83 // Create some new initiator tabs.
84 chrome::NewTab(browser());
85 WebContents
* web_contents_1
= tab_strip_model
->GetActiveWebContents();
86 ASSERT_TRUE(web_contents_1
);
88 chrome::NewTab(browser());
89 WebContents
* web_contents_2
= tab_strip_model
->GetActiveWebContents();
90 ASSERT_TRUE(web_contents_2
);
91 EXPECT_EQ(2, tab_strip_model
->count());
93 PrintPreviewDialogController
* dialog_controller
=
94 PrintPreviewDialogController::GetInstance();
95 ASSERT_TRUE(dialog_controller
);
97 // Create preview dialog for |web_contents_1|
98 PrintViewManager::FromWebContents(web_contents_1
)->PrintPreviewNow(false);
99 WebContents
* preview_dialog_1
=
100 dialog_controller
->GetOrCreatePreviewDialog(web_contents_1
);
102 EXPECT_NE(web_contents_1
, preview_dialog_1
);
103 EXPECT_EQ(2, tab_strip_model
->count());
105 // Create preview dialog for |web_contents_2|
106 PrintViewManager::FromWebContents(web_contents_2
)->PrintPreviewNow(false);
107 WebContents
* preview_dialog_2
=
108 dialog_controller
->GetOrCreatePreviewDialog(web_contents_2
);
110 EXPECT_NE(web_contents_2
, preview_dialog_2
);
111 EXPECT_NE(preview_dialog_1
, preview_dialog_2
);
112 // 2 initiator tabs and 2 preview dialogs exist in the same browser.
113 // The preview dialogs are constrained in their respective initiator tabs.
114 EXPECT_EQ(2, tab_strip_model
->count());
116 int tab_1_index
= tab_strip_model
->GetIndexOfWebContents(web_contents_1
);
117 int tab_2_index
= tab_strip_model
->GetIndexOfWebContents(web_contents_2
);
118 int preview_dialog_1_index
=
119 tab_strip_model
->GetIndexOfWebContents(preview_dialog_1
);
120 int preview_dialog_2_index
=
121 tab_strip_model
->GetIndexOfWebContents(preview_dialog_2
);
123 // Constrained dialogs are not in the TabStripModel.
124 EXPECT_EQ(-1, preview_dialog_1_index
);
125 EXPECT_EQ(-1, preview_dialog_2_index
);
127 // Since |preview_dialog_2_index| was the most recently created dialog, its
128 // initiator tab should have focus.
129 EXPECT_EQ(tab_2_index
, tab_strip_model
->active_index());
131 // When we get the preview dialog for |web_contents_1|,
132 // |preview_dialog_1| is activated and focused.
133 dialog_controller
->GetOrCreatePreviewDialog(web_contents_1
);
134 EXPECT_EQ(tab_1_index
, tab_strip_model
->active_index());
137 // Check clearing the initiator tab details associated with a print preview
138 // dialog allows the initiator tab to create another print preview dialog.
139 TEST_F(PrintPreviewDialogControllerUnitTest
, MAYBE_ClearInitiatorTabDetails
) {
140 // Lets start with one window with one tab.
141 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
142 EXPECT_EQ(0, browser()->tab_strip_model()->count());
143 chrome::NewTab(browser());
144 EXPECT_EQ(1, browser()->tab_strip_model()->count());
146 // Create a reference to initiator tab contents.
147 WebContents
* initiator_tab
=
148 browser()->tab_strip_model()->GetActiveWebContents();
150 PrintPreviewDialogController
* dialog_controller
=
151 PrintPreviewDialogController::GetInstance();
152 ASSERT_TRUE(dialog_controller
);
154 // Get the preview dialog for the initiator tab.
155 PrintViewManager::FromWebContents(initiator_tab
)->PrintPreviewNow(false);
156 WebContents
* preview_dialog
=
157 dialog_controller
->GetOrCreatePreviewDialog(initiator_tab
);
159 // New print preview dialog is a constrained window, so the number of tabs is
161 EXPECT_EQ(1, browser()->tab_strip_model()->count());
162 EXPECT_NE(initiator_tab
, preview_dialog
);
164 // Clear the initiator tab details associated with the preview dialog.
165 dialog_controller
->EraseInitiatorTabInfo(preview_dialog
);
167 // Get a new print preview dialog for the initiator tab.
168 WebContents
* new_preview_dialog
=
169 dialog_controller
->GetOrCreatePreviewDialog(initiator_tab
);
171 // New print preview dialog is a constrained window, so the number of tabs is
173 EXPECT_EQ(1, browser()->tab_strip_model()->count());
174 // Verify a new print preview dialog has been created.
175 EXPECT_NE(new_preview_dialog
, preview_dialog
);
178 } // namespace printing