OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_dialog_controller.h" | 5 #include "chrome/browser/printing/print_preview_dialog_controller.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "chrome/browser/printing/print_preview_test.h" | 9 #include "chrome/browser/printing/print_preview_test.h" |
10 #include "chrome/browser/printing/print_view_manager.h" | 10 #include "chrome/browser/printing/print_view_manager.h" |
11 #include "chrome/browser/ui/browser_commands.h" | 11 #include "chrome/browser/ui/browser_commands.h" |
12 #include "chrome/browser/ui/browser_finder.h" | 12 #include "chrome/browser/ui/browser_finder.h" |
13 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 13 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
14 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" | 14 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" |
15 #include "content/public/browser/navigation_controller.h" | |
16 #include "content/public/browser/navigation_details.h" | 15 #include "content/public/browser/navigation_details.h" |
17 #include "content/public/browser/navigation_entry.h" | |
18 #include "content/public/browser/web_contents_delegate.h" | 16 #include "content/public/browser/web_contents_delegate.h" |
19 #include "content/public/common/url_constants.h" | 17 #include "content/public/common/url_constants.h" |
20 #include "content/public/test/web_contents_tester.h" | 18 #include "content/public/test/web_contents_tester.h" |
21 | 19 |
22 using content::WebContents; | 20 using content::WebContents; |
23 | 21 |
24 namespace { | 22 namespace { |
25 // content::WebContentsDelegate destructor is protected: subclass for testing. | 23 // content::WebContentsDelegate destructor is protected: subclass for testing. |
26 class TestWebContentsDelegate : public content::WebContentsDelegate {}; | 24 class TestWebContentsDelegate : public content::WebContentsDelegate {}; |
27 } // namespace | 25 } // namespace |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 WebContents* new_preview_dialog = | 162 WebContents* new_preview_dialog = |
165 dialog_controller->GetOrCreatePreviewDialog(initiator); | 163 dialog_controller->GetOrCreatePreviewDialog(initiator); |
166 | 164 |
167 // New print preview dialog is a constrained window, so the number of tabs is | 165 // New print preview dialog is a constrained window, so the number of tabs is |
168 // still 1. | 166 // still 1. |
169 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | 167 EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
170 // Verify a new print preview dialog has been created. | 168 // Verify a new print preview dialog has been created. |
171 EXPECT_NE(new_preview_dialog, preview_dialog); | 169 EXPECT_NE(new_preview_dialog, preview_dialog); |
172 } | 170 } |
173 | 171 |
174 // Test that print preview dialogs close on navigation to new pages | |
175 // and when navigating to old pages via fwd/back, but that auto navigation | |
176 // (typed + address bar) to an existing page as occurs in gmail does not cause | |
177 // the dialogs to close. | |
178 TEST_F(PrintPreviewDialogControllerUnitTest, CloseDialogOnNavigation) { | |
179 // Two similar URLs (same webpage, different URL fragments) | |
180 GURL tiger_barb("https://www.google.com/#q=tiger+barb"); | |
181 GURL tiger("https://www.google.com/#q=tiger"); | |
182 | |
183 // Set up by opening a new tab and getting web contents | |
184 EXPECT_EQ(1u, chrome::GetTotalBrowserCount()); | |
185 EXPECT_EQ(0, browser()->tab_strip_model()->count()); | |
186 chrome::NewTab(browser()); | |
187 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | |
188 WebContents* web_contents = | |
189 browser()->tab_strip_model()->GetActiveWebContents(); | |
190 ASSERT_TRUE(web_contents); | |
191 content::NavigationController& nav_controller = web_contents->GetController(); | |
192 | |
193 // Navigate to first page | |
194 nav_controller.LoadURL(tiger, content::Referrer(), | |
195 ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK), | |
196 std::string()); | |
197 CommitPendingLoad(&nav_controller); | |
198 | |
199 // Get the preview dialog | |
200 PrintPreviewDialogController* dialog_controller = | |
201 PrintPreviewDialogController::GetInstance(); | |
202 ASSERT_TRUE(dialog_controller); | |
203 PrintViewManager* manager = PrintViewManager::FromWebContents(web_contents); | |
204 manager->PrintPreviewNow(false); | |
205 WebContents* tiger_preview_dialog = | |
206 dialog_controller->GetOrCreatePreviewDialog(web_contents); | |
207 | |
208 // New print preview dialog is a constrained window, so the number of tabs is | |
209 // still 1. | |
210 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | |
211 EXPECT_NE(web_contents, tiger_preview_dialog); | |
212 | |
213 // Navigate via link to a similar page. | |
214 nav_controller.LoadURL(tiger_barb, content::Referrer(), | |
215 ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK), | |
216 std::string()); | |
217 CommitPendingLoad(&nav_controller); | |
218 | |
219 // Print preview now should return true as the navigation should have closed | |
220 // |tiger_preview_dialog| | |
221 EXPECT_TRUE(manager->PrintPreviewNow(false)); | |
222 WebContents* tiger_barb_preview_dialog = | |
223 dialog_controller->GetOrCreatePreviewDialog(web_contents); | |
224 | |
225 // Check that new dialog was created | |
226 ASSERT_TRUE(tiger_barb_preview_dialog); | |
227 EXPECT_NE(tiger_preview_dialog, tiger_barb_preview_dialog); | |
228 EXPECT_NE(tiger_barb_preview_dialog, web_contents); | |
229 | |
230 // Now this returns false as |tiger_barb_preview_dialog| is open. | |
231 EXPECT_FALSE(manager->PrintPreviewNow(false)); | |
232 | |
233 // Navigate with back button or ALT+LEFT ARROW to a similar page. | |
234 nav_controller.GoBack(); | |
235 CommitPendingLoad(&nav_controller); | |
236 EXPECT_TRUE(manager->PrintPreviewNow(false)); | |
237 WebContents* tiger_preview_dialog_2 = | |
238 dialog_controller->GetOrCreatePreviewDialog(web_contents); | |
239 ASSERT_TRUE(tiger_preview_dialog_2); | |
240 EXPECT_NE(tiger_barb_preview_dialog, tiger_preview_dialog_2); | |
241 EXPECT_NE(tiger_preview_dialog_2, web_contents); | |
242 | |
243 // Try to simulate Gmail navigation: Navigate to an existing page (via | |
244 // Forward) but modify the navigation type while pending to look like an | |
245 // address bar + typed transition (like Gmail auto navigation) | |
246 nav_controller.GoForward(); | |
247 nav_controller.GetPendingEntry()->SetTransitionType(ui::PageTransitionFromInt( | |
248 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)); | |
249 CommitPendingLoad(&nav_controller); | |
250 | |
251 // Print preview should not have changed due to this navigation type so print | |
252 // preview now should return false and the dialogs should be the same. | |
253 EXPECT_FALSE(manager->PrintPreviewNow(false)); | |
254 WebContents* tiger_preview_dialog_2b = | |
255 dialog_controller->GetOrCreatePreviewDialog(web_contents); | |
256 ASSERT_TRUE(tiger_preview_dialog_2b); | |
257 EXPECT_EQ(tiger_preview_dialog_2b, tiger_preview_dialog_2); | |
258 EXPECT_NE(tiger_preview_dialog_2b, web_contents); | |
259 } | |
260 | |
261 } // namespace printing | 172 } // namespace printing |
OLD | NEW |