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
;
19 typedef PrintPreviewTest PrintPreviewDialogControllerUnitTest
;
21 // Create/Get a preview dialog for initiator.
22 TEST_F(PrintPreviewDialogControllerUnitTest
, GetOrCreatePreviewDialog
) {
23 // Lets start with one window with one tab.
24 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
25 EXPECT_EQ(0, browser()->tab_strip_model()->count());
26 chrome::NewTab(browser());
27 EXPECT_EQ(1, browser()->tab_strip_model()->count());
29 // Create a reference to initiator contents.
30 WebContents
* initiator
= browser()->tab_strip_model()->GetActiveWebContents();
32 PrintPreviewDialogController
* dialog_controller
=
33 PrintPreviewDialogController::GetInstance();
34 ASSERT_TRUE(dialog_controller
);
36 // Get the preview dialog for initiator.
37 PrintViewManager::FromWebContents(initiator
)->PrintPreviewNow(false);
38 WebContents
* preview_dialog
=
39 dialog_controller
->GetOrCreatePreviewDialog(initiator
);
41 // New print preview dialog is a constrained window, so the number of tabs is
43 EXPECT_EQ(1, browser()->tab_strip_model()->count());
44 EXPECT_NE(initiator
, preview_dialog
);
46 // Get the print preview dialog for the same initiator.
47 WebContents
* new_preview_dialog
=
48 dialog_controller
->GetOrCreatePreviewDialog(initiator
);
50 // Preview dialog already exists. Tab count remains the same.
51 EXPECT_EQ(1, browser()->tab_strip_model()->count());
53 // 1:1 relationship between initiator and preview dialog.
54 EXPECT_EQ(new_preview_dialog
, preview_dialog
);
57 // Tests multiple print preview dialogs exist in the same browser for different
58 // initiators. If a preview dialog already exists for an initiator, that
59 // initiator gets focused.
60 TEST_F(PrintPreviewDialogControllerUnitTest
, MultiplePreviewDialogs
) {
61 // Lets start with one window and two tabs.
62 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
63 TabStripModel
* tab_strip_model
= browser()->tab_strip_model();
64 ASSERT_TRUE(tab_strip_model
);
66 EXPECT_EQ(0, tab_strip_model
->count());
68 // Create some new initiators.
69 chrome::NewTab(browser());
70 WebContents
* web_contents_1
= tab_strip_model
->GetActiveWebContents();
71 ASSERT_TRUE(web_contents_1
);
73 chrome::NewTab(browser());
74 WebContents
* web_contents_2
= tab_strip_model
->GetActiveWebContents();
75 ASSERT_TRUE(web_contents_2
);
76 EXPECT_EQ(2, tab_strip_model
->count());
78 PrintPreviewDialogController
* dialog_controller
=
79 PrintPreviewDialogController::GetInstance();
80 ASSERT_TRUE(dialog_controller
);
82 // Create preview dialog for |web_contents_1|
83 PrintViewManager::FromWebContents(web_contents_1
)->PrintPreviewNow(false);
84 WebContents
* preview_dialog_1
=
85 dialog_controller
->GetOrCreatePreviewDialog(web_contents_1
);
87 EXPECT_NE(web_contents_1
, preview_dialog_1
);
88 EXPECT_EQ(2, tab_strip_model
->count());
90 // Create preview dialog for |web_contents_2|
91 PrintViewManager::FromWebContents(web_contents_2
)->PrintPreviewNow(false);
92 WebContents
* preview_dialog_2
=
93 dialog_controller
->GetOrCreatePreviewDialog(web_contents_2
);
95 EXPECT_NE(web_contents_2
, preview_dialog_2
);
96 EXPECT_NE(preview_dialog_1
, preview_dialog_2
);
97 // 2 initiators and 2 preview dialogs exist in the same browser. The preview
98 // dialogs are constrained in their respective initiators.
99 EXPECT_EQ(2, tab_strip_model
->count());
101 int tab_1_index
= tab_strip_model
->GetIndexOfWebContents(web_contents_1
);
102 int tab_2_index
= tab_strip_model
->GetIndexOfWebContents(web_contents_2
);
103 int preview_dialog_1_index
=
104 tab_strip_model
->GetIndexOfWebContents(preview_dialog_1
);
105 int preview_dialog_2_index
=
106 tab_strip_model
->GetIndexOfWebContents(preview_dialog_2
);
108 // Constrained dialogs are not in the TabStripModel.
109 EXPECT_EQ(-1, preview_dialog_1_index
);
110 EXPECT_EQ(-1, preview_dialog_2_index
);
112 // Since |preview_dialog_2_index| was the most recently created dialog, its
113 // initiator should have focus.
114 EXPECT_EQ(tab_2_index
, tab_strip_model
->active_index());
116 // When we get the preview dialog for |web_contents_1|,
117 // |preview_dialog_1| is activated and focused.
118 dialog_controller
->GetOrCreatePreviewDialog(web_contents_1
);
119 EXPECT_EQ(tab_1_index
, tab_strip_model
->active_index());
122 // Check clearing the initiator details associated with a print preview dialog
123 // allows the initiator to create another print preview dialog.
124 TEST_F(PrintPreviewDialogControllerUnitTest
, ClearInitiatorDetails
) {
125 // Lets start with one window with one tab.
126 EXPECT_EQ(1u, chrome::GetTotalBrowserCount());
127 EXPECT_EQ(0, browser()->tab_strip_model()->count());
128 chrome::NewTab(browser());
129 EXPECT_EQ(1, browser()->tab_strip_model()->count());
131 // Create a reference to initiator contents.
132 WebContents
* initiator
= browser()->tab_strip_model()->GetActiveWebContents();
134 PrintPreviewDialogController
* dialog_controller
=
135 PrintPreviewDialogController::GetInstance();
136 ASSERT_TRUE(dialog_controller
);
138 // Get the preview dialog for the initiator.
139 PrintViewManager::FromWebContents(initiator
)->PrintPreviewNow(false);
140 WebContents
* preview_dialog
=
141 dialog_controller
->GetOrCreatePreviewDialog(initiator
);
143 // New print preview dialog is a constrained window, so the number of tabs is
145 EXPECT_EQ(1, browser()->tab_strip_model()->count());
146 EXPECT_NE(initiator
, preview_dialog
);
148 // Clear the initiator details associated with the preview dialog.
149 dialog_controller
->EraseInitiatorInfo(preview_dialog
);
151 // Get a new print preview dialog for the initiator.
152 WebContents
* new_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 // Verify a new print preview dialog has been created.
159 EXPECT_NE(new_preview_dialog
, preview_dialog
);
162 } // namespace printing