OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/printing/print_preview_tab_controller.h" | 5 #include "chrome/browser/printing/print_preview_tab_controller.h" |
6 #include "chrome/browser/ui/browser.h" | 6 #include "chrome/browser/ui/browser.h" |
7 #include "chrome/browser/ui/browser_list.h" | 7 #include "chrome/browser/ui/browser_list.h" |
8 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 8 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
9 #include "chrome/common/url_constants.h" | 9 #include "chrome/common/url_constants.h" |
10 #include "chrome/test/base/in_process_browser_test.h" | 10 #include "chrome/test/base/in_process_browser_test.h" |
11 #include "chrome/test/base/ui_test_utils.h" | 11 #include "chrome/test/base/ui_test_utils.h" |
12 #include "content/browser/tab_contents/tab_contents.h" | 12 #include "content/browser/tab_contents/tab_contents.h" |
13 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 typedef InProcessBrowserTest PrintPreviewTabControllerBrowserTest; | 17 typedef InProcessBrowserTest PrintPreviewTabControllerBrowserTest; |
18 | 18 |
19 // Test to verify that when a preview tab navigates, we can create a new print | |
20 // preview tab for both initiator tab and new preview tab contents. | |
21 IN_PROC_BROWSER_TEST_F(PrintPreviewTabControllerBrowserTest, | |
22 NavigateFromPrintPreviewTab) { | |
23 ASSERT_TRUE(browser()); | |
24 BrowserList::SetLastActive(browser()); | |
25 ASSERT_TRUE(BrowserList::GetLastActive()); | |
26 | |
27 // Lets start with one window with one tab. | |
28 EXPECT_EQ(1u, BrowserList::size()); | |
29 EXPECT_EQ(1, browser()->tab_count()); | |
30 | |
31 // Create a reference to initiator tab contents. | |
32 TabContentsWrapper* initiator_tab = | |
33 browser()->GetSelectedTabContentsWrapper(); | |
34 ASSERT_TRUE(initiator_tab); | |
35 | |
36 scoped_refptr<printing::PrintPreviewTabController> | |
37 tab_controller(new printing::PrintPreviewTabController()); | |
38 ASSERT_TRUE(tab_controller); | |
39 | |
40 // Get the preview tab for initiator tab. | |
41 TabContentsWrapper* preview_tab = | |
42 tab_controller->GetOrCreatePreviewTab(initiator_tab); | |
43 | |
44 // New print preview tab is created. Current focus is on preview tab. | |
45 EXPECT_EQ(2, browser()->tab_count()); | |
46 EXPECT_NE(initiator_tab, preview_tab); | |
47 | |
48 GURL url(chrome::kAboutBlankURL); | |
49 ui_test_utils::NavigateToURL(browser(), url); | |
50 EXPECT_EQ(url, preview_tab->tab_contents()->GetURL()); | |
51 | |
52 // Get the print preview tab for initiator tab. | |
53 TabContentsWrapper* new_preview_tab = | |
54 tab_controller->GetOrCreatePreviewTab(initiator_tab); | |
55 | |
56 // New preview tab is created. | |
57 EXPECT_EQ(3, browser()->tab_count()); | |
58 EXPECT_NE(new_preview_tab, preview_tab); | |
59 | |
60 // Get the print preview tab for old preview tab. | |
61 TabContentsWrapper* newest_preview_tab = | |
62 tab_controller->GetOrCreatePreviewTab(preview_tab); | |
63 | |
64 // Newest preview tab is created and the previously created preview tab is not | |
65 // merely activated. | |
66 EXPECT_EQ(4, browser()->tab_count()); | |
67 EXPECT_NE(newest_preview_tab, new_preview_tab); | |
68 } | |
69 | |
70 // Test to verify that when a initiator tab navigates, we can create a new | 19 // Test to verify that when a initiator tab navigates, we can create a new |
71 // preview tab for the new tab contents. But we cannot create a preview tab for | 20 // preview tab for the new tab contents. |
72 // the old preview tab. | |
73 IN_PROC_BROWSER_TEST_F(PrintPreviewTabControllerBrowserTest, | 21 IN_PROC_BROWSER_TEST_F(PrintPreviewTabControllerBrowserTest, |
74 NavigateFromInitiatorTab) { | 22 NavigateFromInitiatorTab) { |
75 ASSERT_TRUE(browser()); | 23 ASSERT_TRUE(browser()); |
76 BrowserList::SetLastActive(browser()); | 24 BrowserList::SetLastActive(browser()); |
77 ASSERT_TRUE(BrowserList::GetLastActive()); | 25 ASSERT_TRUE(BrowserList::GetLastActive()); |
78 | 26 |
79 // Lets start with one window with one tab. | 27 // Lets start with one window with one tab. |
80 EXPECT_EQ(1u, BrowserList::size()); | 28 EXPECT_EQ(1u, BrowserList::size()); |
81 EXPECT_EQ(1, browser()->tab_count()); | 29 EXPECT_EQ(1, browser()->tab_count()); |
82 | 30 |
83 // Create a reference to initiator tab contents. | 31 // Create a reference to initiator tab contents. |
84 TabContentsWrapper* initiator_tab = | 32 TabContentsWrapper* initiator_tab = |
85 browser()->GetSelectedTabContentsWrapper(); | 33 browser()->GetSelectedTabContentsWrapper(); |
86 ASSERT_TRUE(initiator_tab); | 34 ASSERT_TRUE(initiator_tab); |
87 | 35 |
88 scoped_refptr<printing::PrintPreviewTabController> | 36 printing::PrintPreviewTabController* tab_controller = |
89 tab_controller(new printing::PrintPreviewTabController()); | 37 printing::PrintPreviewTabController::GetInstance(); |
90 ASSERT_TRUE(tab_controller); | 38 ASSERT_TRUE(tab_controller); |
91 | 39 |
92 // Get the preview tab for initiator tab. | 40 // Get the preview tab for initiator tab. |
93 TabContentsWrapper* preview_tab = | 41 TabContentsWrapper* preview_tab = |
94 tab_controller->GetOrCreatePreviewTab(initiator_tab); | 42 tab_controller->GetOrCreatePreviewTab(initiator_tab); |
95 | 43 |
96 // New print preview tab is created. Current focus is on preview tab. | 44 // New print preview tab is created. |
97 EXPECT_EQ(2, browser()->tab_count()); | 45 EXPECT_EQ(1, browser()->tab_count()); |
98 EXPECT_NE(initiator_tab, preview_tab); | 46 EXPECT_NE(initiator_tab, preview_tab); |
99 | 47 |
100 // Activate initiator tab. | 48 // Navigate in the initiator tab. |
101 browser()->ActivateTabAt(0, true); | |
102 GURL url(chrome::kChromeUINewTabURL); | 49 GURL url(chrome::kChromeUINewTabURL); |
103 ui_test_utils::NavigateToURL(browser(), url); | 50 ui_test_utils::NavigateToURL(browser(), url); |
104 | 51 |
105 // Get the print preview tab for initiator tab. | 52 // Get the print preview tab for initiator tab. |
106 TabContentsWrapper* new_preview_tab = | 53 TabContentsWrapper* new_preview_tab = |
107 tab_controller->GetOrCreatePreviewTab(initiator_tab); | 54 tab_controller->GetOrCreatePreviewTab(initiator_tab); |
108 | 55 |
109 // New preview tab is created. | 56 // New preview tab is created. |
110 EXPECT_EQ(3, browser()->tab_count()); | 57 EXPECT_EQ(1, browser()->tab_count()); |
111 EXPECT_NE(new_preview_tab, preview_tab); | 58 EXPECT_NE(new_preview_tab, preview_tab); |
112 | |
113 // Get the print preview tab for old preview tab. | |
114 TabContentsWrapper* newest_preview_tab = | |
115 tab_controller->GetOrCreatePreviewTab(preview_tab); | |
116 | |
117 // Make sure preview tab is not created for |preview_tab|. | |
118 EXPECT_EQ(3, browser()->tab_count()); | |
119 EXPECT_EQ(newest_preview_tab, preview_tab); | |
120 } | 59 } |
121 | 60 |
122 // Test to verify that even after reloading initiator tab and preview tab, | 61 // Test to verify that after reloading the initiator tab, it creates a new |
123 // their association exists. | 62 // print preview tab. |
63 // This test is flaky because sometimes |new_preview_tab| could be a new tab, | |
64 // yet have the same address as |preview_tab|. http://crbug.com/XXX | |
kmadhusu
2011/10/14 00:21:09
Add a valid bug #.
Lei Zhang
2011/10/14 20:31:27
Done.
| |
124 IN_PROC_BROWSER_TEST_F(PrintPreviewTabControllerBrowserTest, | 65 IN_PROC_BROWSER_TEST_F(PrintPreviewTabControllerBrowserTest, |
125 ReloadInitiatorTabAndPreviewTab) { | 66 FLAKY_ReloadInitiatorTab) { |
126 ASSERT_TRUE(browser()); | 67 ASSERT_TRUE(browser()); |
127 BrowserList::SetLastActive(browser()); | 68 BrowserList::SetLastActive(browser()); |
128 ASSERT_TRUE(BrowserList::GetLastActive()); | 69 ASSERT_TRUE(BrowserList::GetLastActive()); |
129 | 70 |
130 // Lets start with one window with one tab. | 71 // Lets start with one window with one tab. |
131 EXPECT_EQ(1u, BrowserList::size()); | 72 EXPECT_EQ(1u, BrowserList::size()); |
132 EXPECT_EQ(1, browser()->tab_count()); | 73 EXPECT_EQ(1, browser()->tab_count()); |
133 | 74 |
134 // Create a reference to initiator tab contents. | 75 // Create a reference to initiator tab contents. |
135 TabContentsWrapper* initiator_tab = | 76 TabContentsWrapper* initiator_tab = |
136 browser()->GetSelectedTabContentsWrapper(); | 77 browser()->GetSelectedTabContentsWrapper(); |
137 ASSERT_TRUE(initiator_tab); | 78 ASSERT_TRUE(initiator_tab); |
138 | 79 |
139 scoped_refptr<printing::PrintPreviewTabController> | 80 printing::PrintPreviewTabController* tab_controller = |
140 tab_controller(new printing::PrintPreviewTabController()); | 81 printing::PrintPreviewTabController::GetInstance(); |
141 ASSERT_TRUE(tab_controller); | 82 ASSERT_TRUE(tab_controller); |
142 | 83 |
143 // Get the preview tab for initiator tab. | 84 // Get the preview tab for initiator tab. |
144 TabContentsWrapper* preview_tab = | 85 TabContentsWrapper* preview_tab = |
145 tab_controller->GetOrCreatePreviewTab(initiator_tab); | 86 tab_controller->GetOrCreatePreviewTab(initiator_tab); |
146 | 87 |
147 // New print preview tab is created. Current focus is on preview tab. | 88 // New print preview tab is created. |
148 EXPECT_EQ(2, browser()->tab_count()); | 89 EXPECT_EQ(1, browser()->tab_count()); |
149 EXPECT_NE(initiator_tab, preview_tab); | 90 EXPECT_NE(initiator_tab, preview_tab); |
150 | 91 |
151 // Activate initiator tab and reload. | 92 // Reload the initiator tab. |
152 browser()->ActivateTabAt(0, true); | |
153 browser()->Reload(CURRENT_TAB); | 93 browser()->Reload(CURRENT_TAB); |
94 ui_test_utils::WaitForNavigation(&initiator_tab->controller()); | |
154 | 95 |
155 // Get the print preview tab for initiator tab. | 96 // Get the print preview tab for initiator tab. |
156 TabContentsWrapper* new_preview_tab = | 97 TabContentsWrapper* new_preview_tab = |
157 tab_controller->GetOrCreatePreviewTab(initiator_tab); | 98 tab_controller->GetOrCreatePreviewTab(initiator_tab); |
158 | 99 |
159 // Old preview tab is activated. | 100 EXPECT_EQ(1, browser()->tab_count()); |
160 EXPECT_EQ(2, browser()->tab_count()); | 101 EXPECT_NE(new_preview_tab, preview_tab); |
161 EXPECT_EQ(new_preview_tab, preview_tab); | |
162 | |
163 // Reload preview tab. | |
164 browser()->Reload(CURRENT_TAB); | |
165 // Get the print preview tab for old preview tab. | |
166 TabContentsWrapper* newest_preview_tab = | |
167 tab_controller->GetOrCreatePreviewTab(preview_tab); | |
168 | |
169 // Make sure new preview tab is not created for |preview_tab|. | |
170 EXPECT_EQ(2, browser()->tab_count()); | |
171 EXPECT_EQ(newest_preview_tab, preview_tab); | |
172 } | |
173 | |
174 // Test that print preview tabs are placed correctly. | |
175 IN_PROC_BROWSER_TEST_F(PrintPreviewTabControllerBrowserTest, | |
176 OpenPreviewTabInCorrectPosition) { | |
177 const int kTabCount = 4; | |
178 // Create kTabCount - 1 tabs since we start with 1 tab already. | |
179 for (int i = 0; i < kTabCount - 1; ++i) { | |
180 browser::NavigateParams p(browser(), GURL(), content::PAGE_TRANSITION_LINK); | |
181 p.disposition = NEW_FOREGROUND_TAB; | |
182 browser::Navigate(&p); | |
183 } | |
184 EXPECT_EQ(kTabCount, browser()->tab_count()); | |
185 | |
186 // Create a print preview tab. | |
187 scoped_refptr<printing::PrintPreviewTabController> | |
188 tab_controller(new printing::PrintPreviewTabController()); | |
189 ASSERT_TRUE(tab_controller); | |
190 | |
191 const int kInitiatorTabIndex = 1; | |
192 TabContentsWrapper* initiator_tab = | |
193 browser()->GetTabContentsWrapperAt(kInitiatorTabIndex); | |
194 ASSERT_TRUE(initiator_tab); | |
195 TabContentsWrapper* preview_tab = | |
196 tab_controller->GetOrCreatePreviewTab(initiator_tab); | |
197 EXPECT_TRUE(preview_tab); | |
198 | |
199 // Check the preview tab's location. | |
200 EXPECT_EQ(preview_tab, | |
201 browser()->GetTabContentsWrapperAt(kInitiatorTabIndex + 1)); | |
202 EXPECT_EQ(preview_tab, browser()->GetSelectedTabContentsWrapper()); | |
203 } | |
204 | |
205 // Test that print preview tabs created by pop-up windows are placed correctly. | |
206 IN_PROC_BROWSER_TEST_F(PrintPreviewTabControllerBrowserTest, | |
207 OpenPreviewTabFromPopupInCorrectPosition) { | |
208 const int kTabCount = 4; | |
209 // Create kTabCount - 1 tabs since we start with 1 tab already. | |
210 for (int i = 0; i < kTabCount - 1; ++i) { | |
211 browser::NavigateParams p(browser(), GURL(), content::PAGE_TRANSITION_LINK); | |
212 p.disposition = NEW_FOREGROUND_TAB; | |
213 browser::Navigate(&p); | |
214 } | |
215 EXPECT_EQ(kTabCount, browser()->tab_count()); | |
216 | |
217 // Create a popup | |
218 browser::NavigateParams p(browser(), GURL(), content::PAGE_TRANSITION_LINK); | |
219 p.disposition = NEW_POPUP; | |
220 ui_test_utils::NavigateToURL(&p); | |
221 | |
222 | |
223 #if defined(OS_CHROMEOS) | |
224 // Navigate() should have opened a new tab on CrOS. | |
225 EXPECT_EQ(browser(), p.browser); | |
226 EXPECT_EQ(Browser::TYPE_TABBED, p.browser->type()); | |
227 #else | |
228 // Navigate() should have opened a new popup window. | |
229 EXPECT_NE(browser(), p.browser); | |
230 EXPECT_EQ(Browser::TYPE_POPUP, p.browser->type()); | |
231 #endif | |
232 ASSERT_TRUE(p.target_contents); | |
233 | |
234 // Create a print preview tab. | |
235 scoped_refptr<printing::PrintPreviewTabController> | |
236 tab_controller(new printing::PrintPreviewTabController()); | |
237 ASSERT_TRUE(tab_controller); | |
238 | |
239 TabContentsWrapper* preview_tab = | |
240 tab_controller->GetOrCreatePreviewTab(p.target_contents); | |
241 EXPECT_TRUE(preview_tab); | |
242 | |
243 int tab_position = kTabCount; | |
244 #if defined(OS_CHROMEOS) | |
245 // Increment position since CrOS opened a new tab instead of a popup. | |
246 tab_position++; | |
247 #endif | |
248 | |
249 // Check the preview tab's location. | |
250 EXPECT_EQ(preview_tab, browser()->GetTabContentsWrapperAt(tab_position)); | |
251 EXPECT_EQ(preview_tab, browser()->GetSelectedTabContentsWrapper()); | |
252 } | 102 } |
253 | 103 |
254 } // namespace | 104 } // namespace |
OLD | NEW |