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" | |
15 #include "content/public/browser/navigation_details.h" | 16 #include "content/public/browser/navigation_details.h" |
17 #include "content/public/browser/navigation_entry.h" | |
16 #include "content/public/browser/web_contents_delegate.h" | 18 #include "content/public/browser/web_contents_delegate.h" |
19 #include "content/public/browser/web_contents_observer.h" | |
17 #include "content/public/common/url_constants.h" | 20 #include "content/public/common/url_constants.h" |
18 #include "content/public/test/web_contents_tester.h" | 21 #include "content/public/test/web_contents_tester.h" |
19 | 22 |
20 using content::WebContents; | 23 using content::WebContents; |
24 using content::WebContentsObserver; | |
21 | 25 |
22 namespace { | 26 namespace { |
23 // content::WebContentsDelegate destructor is protected: subclass for testing. | 27 // content::WebContentsDelegate destructor is protected: subclass for testing. |
24 class TestWebContentsDelegate : public content::WebContentsDelegate {}; | 28 class TestWebContentsDelegate : public content::WebContentsDelegate {}; |
29 | |
30 class PrintPreviewDialogDestroyedObserver : public WebContentsObserver { | |
31 public: | |
32 explicit PrintPreviewDialogDestroyedObserver(WebContents* dialog) | |
33 : WebContentsObserver(dialog), | |
34 dialog_destroyed_(false) { | |
35 } | |
36 ~PrintPreviewDialogDestroyedObserver() override {} | |
37 | |
38 bool dialog_destroyed() const { return dialog_destroyed_; } | |
39 | |
40 private: | |
41 // content::WebContentsObserver implementation. | |
42 void WebContentsDestroyed() override { dialog_destroyed_ = true; } | |
43 | |
44 bool dialog_destroyed_; | |
45 | |
46 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogDestroyedObserver); | |
47 }; | |
48 | |
49 class PrintPreviewDialogControllerForTest : | |
50 public printing::PrintPreviewDialogController { | |
51 public: | |
52 PrintPreviewDialogControllerForTest() { | |
53 next_dialog_ = 1; | |
Lei Zhang
2016/08/17 21:27:55
Use the initializer list
| |
54 } | |
55 | |
56 WebContents* GetOrCreatePreviewDialog(WebContents * initiator) override { | |
57 DCHECK(initiator); | |
58 | |
59 // Get the print preview dialog for |initiator|. | |
60 WebContents* preview_dialog = GetPrintPreviewForContents(initiator); | |
61 if (!preview_dialog) { | |
Lei Zhang
2016/08/17 21:27:55
I usually like to write:
if (foo) {
DoFoo();
} e
| |
62 // Create a fake dialog and put it in the map, then observe initiator | |
63 preview_dialog = reinterpret_cast<WebContents*>(next_dialog_++); | |
64 preview_dialog_map_[preview_dialog] = initiator; | |
65 AddObservers(initiator); | |
66 } else { | |
67 // Show the initiator holding the existing preview dialog. | |
68 initiator->GetDelegate()->ActivateContents(initiator); | |
69 } | |
70 return preview_dialog; | |
71 } | |
72 | |
73 private: | |
74 ~PrintPreviewDialogControllerForTest() override {} | |
75 | |
76 // Override so that the controller doesn't try to close a fake web contents. | |
77 void RemoveInitiator(WebContents* initiator) override { | |
78 WebContents* preview_dialog = GetPrintPreviewForContents(initiator); | |
79 DCHECK(preview_dialog); | |
80 preview_dialog_map_[preview_dialog] = nullptr; | |
81 RemoveObservers(initiator); | |
82 printing::PrintViewManager::FromWebContents(initiator)->PrintPreviewDone(); | |
83 } | |
84 | |
85 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogControllerForTest); | |
86 int next_dialog_; | |
87 }; | |
88 | |
89 | |
90 | |
25 } // namespace | 91 } // namespace |
26 | 92 |
27 namespace printing { | 93 namespace printing { |
28 | 94 |
29 using PrintPreviewDialogControllerUnitTest = PrintPreviewTest; | 95 using PrintPreviewDialogControllerUnitTest = PrintPreviewTest; |
30 | 96 |
31 // Create/Get a preview dialog for initiator. | 97 // Create/Get a preview dialog for initiator. |
32 TEST_F(PrintPreviewDialogControllerUnitTest, GetOrCreatePreviewDialog) { | 98 TEST_F(PrintPreviewDialogControllerUnitTest, GetOrCreatePreviewDialog) { |
33 // Lets start with one window with one tab. | 99 // Lets start with one window with one tab. |
34 EXPECT_EQ(1u, chrome::GetTotalBrowserCount()); | 100 EXPECT_EQ(1u, chrome::GetTotalBrowserCount()); |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
162 WebContents* new_preview_dialog = | 228 WebContents* new_preview_dialog = |
163 dialog_controller->GetOrCreatePreviewDialog(initiator); | 229 dialog_controller->GetOrCreatePreviewDialog(initiator); |
164 | 230 |
165 // New print preview dialog is a constrained window, so the number of tabs is | 231 // New print preview dialog is a constrained window, so the number of tabs is |
166 // still 1. | 232 // still 1. |
167 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | 233 EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
168 // Verify a new print preview dialog has been created. | 234 // Verify a new print preview dialog has been created. |
169 EXPECT_NE(new_preview_dialog, preview_dialog); | 235 EXPECT_NE(new_preview_dialog, preview_dialog); |
170 } | 236 } |
171 | 237 |
238 // Test that print preview dialogs close on navigation to new pages | |
239 // and when navigating to old pages via fwd/back, but that auto navigation | |
240 // (typed + address bar) to an existing page as occurs in gmail does not cause | |
241 // the dialogs to close. | |
242 TEST_F(PrintPreviewDialogControllerUnitTest, CloseDialogOnNavigation) { | |
Lei Zhang
2016/08/17 21:27:55
I prefer this version mainly because we don't have
| |
243 // Two similar URLs (same webpage, different URL fragments) | |
244 GURL tiger_barb("https://www.google.com/#q=tiger+barb"); | |
245 GURL tiger("https://www.google.com/#q=tiger"); | |
246 | |
247 // Set up by opening a new tab and getting web contents | |
248 EXPECT_EQ(1u, chrome::GetTotalBrowserCount()); | |
249 EXPECT_EQ(0, browser()->tab_strip_model()->count()); | |
250 chrome::NewTab(browser()); | |
251 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | |
252 WebContents* web_contents = | |
253 browser()->tab_strip_model()->GetActiveWebContents(); | |
254 ASSERT_TRUE(web_contents); | |
255 content::NavigationController& nav_controller = web_contents->GetController(); | |
256 | |
257 // Navigate to first page | |
258 nav_controller.LoadURL(tiger, content::Referrer(), | |
259 ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK), | |
260 std::string()); | |
261 CommitPendingLoad(&nav_controller); | |
262 EXPECT_EQ(tiger, web_contents->GetLastCommittedURL()); | |
263 | |
264 // Get the preview dialog | |
265 PrintPreviewDialogController* dialog_controller = | |
266 PrintPreviewDialogController::GetInstance(); | |
267 ASSERT_TRUE(dialog_controller); | |
268 WebContents* tiger_preview_dialog = | |
269 dialog_controller->GetOrCreatePreviewDialog(web_contents); | |
270 PrintViewManager* manager = PrintViewManager::FromWebContents(web_contents); | |
271 manager->PrintPreviewNow(false); | |
272 | |
273 // New print preview dialog is a constrained window, so the number of tabs is | |
274 // still 1. | |
275 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | |
276 EXPECT_NE(web_contents, tiger_preview_dialog); | |
277 PrintPreviewDialogDestroyedObserver tiger_destroyed(tiger_preview_dialog); | |
278 | |
279 // Navigate via link to a similar page. | |
280 nav_controller.LoadURL(tiger_barb, content::Referrer(), | |
281 ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK), | |
282 std::string()); | |
283 CommitPendingLoad(&nav_controller); | |
284 | |
285 // Check navigation was successful | |
286 EXPECT_EQ(tiger_barb, web_contents->GetLastCommittedURL()); | |
287 | |
288 // Print preview now should return true as the navigation should have closed | |
289 // |tiger_preview_dialog| and the previous dialog should have closed. | |
290 EXPECT_TRUE(manager->PrintPreviewNow(false)); | |
291 WebContents* tiger_barb_preview_dialog = | |
292 dialog_controller->GetOrCreatePreviewDialog(web_contents); | |
293 ASSERT_TRUE(tiger_barb_preview_dialog); | |
294 | |
295 // Check a new dialog was created - either the pointers should be different or | |
296 // the previous web contents must have been destroyed. | |
297 EXPECT_TRUE(tiger_destroyed.dialog_destroyed() || | |
298 tiger_barb_preview_dialog != tiger_preview_dialog); | |
299 EXPECT_NE(tiger_barb_preview_dialog, web_contents); | |
300 PrintPreviewDialogDestroyedObserver tiger_barb_destroyed( | |
301 tiger_barb_preview_dialog); | |
302 | |
303 // Now this returns false as |tiger_barb_preview_dialog| is open. | |
304 EXPECT_FALSE(manager->PrintPreviewNow(false)); | |
305 | |
306 // Navigate with back button or ALT+LEFT ARROW to a similar page. | |
307 nav_controller.GoBack(); | |
308 CommitPendingLoad(&nav_controller); | |
309 EXPECT_EQ(tiger, web_contents->GetLastCommittedURL()); | |
310 EXPECT_TRUE(manager->PrintPreviewNow(false)); | |
311 | |
312 // Get new dialog | |
313 WebContents* tiger_preview_dialog_2 = | |
314 dialog_controller->GetOrCreatePreviewDialog(web_contents); | |
315 ASSERT_TRUE(tiger_preview_dialog_2); | |
316 | |
317 // Verify this is a new dialog. | |
318 EXPECT_TRUE(tiger_barb_destroyed.dialog_destroyed() || | |
319 tiger_barb_preview_dialog != tiger_preview_dialog_2); | |
320 EXPECT_NE(tiger_preview_dialog_2, web_contents); | |
321 PrintPreviewDialogDestroyedObserver tiger_2_destroyed( | |
322 tiger_preview_dialog_2); | |
323 | |
324 // Try to simulate Gmail navigation: Navigate to an existing page (via | |
325 // Forward) but modify the navigation type while pending to look like an | |
326 // address bar + typed transition (like Gmail auto navigation) | |
327 nav_controller.GoForward(); | |
328 nav_controller.GetPendingEntry()->SetTransitionType(ui::PageTransitionFromInt( | |
329 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)); | |
330 CommitPendingLoad(&nav_controller); | |
331 | |
332 // Navigation successful | |
333 EXPECT_EQ(tiger_barb, web_contents->GetLastCommittedURL()); | |
334 | |
335 // Print preview should not have changed due to this navigation type so print | |
336 // preview now should return false, dialog is still alive, and the dialog | |
337 // returned by GetOrCreatePreviewDialog should be the same as the earlier | |
338 // dialog. | |
339 EXPECT_FALSE(manager->PrintPreviewNow(false)); | |
340 EXPECT_FALSE(tiger_2_destroyed.dialog_destroyed()); | |
341 WebContents* tiger_preview_dialog_2b = | |
342 dialog_controller->GetOrCreatePreviewDialog(web_contents); | |
343 ASSERT_TRUE(tiger_preview_dialog_2b); | |
344 EXPECT_EQ(tiger_preview_dialog_2b, tiger_preview_dialog_2); | |
345 EXPECT_NE(tiger_preview_dialog_2b, web_contents); | |
346 } | |
347 | |
348 // Test that print preview dialogs close on navigation to new pages | |
349 // and when navigating to old pages via fwd/back, but that auto navigation | |
350 // (typed + address bar) to an existing page as occurs in gmail does not cause | |
351 // the dialogs to close. | |
352 TEST_F(PrintPreviewDialogControllerUnitTest, CloseDialogOnNavigationV2) { | |
353 // Two similar URLs (same webpage, different URL fragments) | |
354 GURL tiger_barb("https://www.google.com/#q=tiger+barb"); | |
355 GURL tiger("https://www.google.com/#q=tiger"); | |
356 | |
357 // Set up by opening a new tab and getting web contents | |
358 EXPECT_EQ(1u, chrome::GetTotalBrowserCount()); | |
359 EXPECT_EQ(0, browser()->tab_strip_model()->count()); | |
360 chrome::NewTab(browser()); | |
361 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | |
362 WebContents* web_contents = | |
363 browser()->tab_strip_model()->GetActiveWebContents(); | |
364 ASSERT_TRUE(web_contents); | |
365 content::NavigationController& nav_controller = web_contents->GetController(); | |
366 | |
367 // Navigate to first page | |
368 nav_controller.LoadURL(tiger, content::Referrer(), | |
369 ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK), | |
370 std::string()); | |
371 CommitPendingLoad(&nav_controller); | |
372 EXPECT_EQ(tiger, web_contents->GetLastCommittedURL()); | |
373 | |
374 // Get the preview dialog | |
375 PrintPreviewDialogControllerForTest* dialog_controller = | |
376 new PrintPreviewDialogControllerForTest; | |
377 ASSERT_TRUE(dialog_controller); | |
378 PrintViewManager* manager = PrintViewManager::FromWebContents(web_contents); | |
379 WebContents* tiger_preview_dialog = | |
380 dialog_controller->GetOrCreatePreviewDialog(web_contents); | |
381 manager->PrintPreviewNow(false); | |
382 | |
383 // New print preview dialog is a constrained window, so the number of tabs is | |
384 // still 1. | |
385 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | |
386 EXPECT_NE(web_contents, tiger_preview_dialog); | |
387 | |
388 // Navigate via link to a similar page. | |
389 nav_controller.LoadURL(tiger_barb, content::Referrer(), | |
390 ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK), | |
391 std::string()); | |
392 CommitPendingLoad(&nav_controller); | |
393 | |
394 // Check navigation was successful | |
395 EXPECT_EQ(tiger_barb, web_contents->GetLastCommittedURL()); | |
396 | |
397 // Print preview now should return true as the navigation should have closed | |
398 // |tiger_preview_dialog| | |
399 EXPECT_TRUE(manager->PrintPreviewNow(false)); | |
400 WebContents* tiger_barb_preview_dialog = | |
401 dialog_controller->GetOrCreatePreviewDialog(web_contents); | |
402 ASSERT_TRUE(tiger_barb_preview_dialog); | |
403 | |
404 // Check a new dialog was created - pointers should be different. | |
405 EXPECT_TRUE(tiger_barb_preview_dialog != tiger_preview_dialog); | |
406 EXPECT_NE(tiger_barb_preview_dialog, web_contents); | |
407 | |
408 // Now this returns false as |tiger_barb_preview_dialog| is open. | |
409 EXPECT_FALSE(manager->PrintPreviewNow(false)); | |
410 | |
411 // Navigate with back button or ALT+LEFT ARROW to a similar page. | |
412 nav_controller.GoBack(); | |
413 CommitPendingLoad(&nav_controller); | |
414 EXPECT_EQ(tiger, web_contents->GetLastCommittedURL()); | |
415 EXPECT_TRUE(manager->PrintPreviewNow(false)); | |
416 | |
417 // Get new dialog | |
418 WebContents* tiger_preview_dialog_2 = | |
419 dialog_controller->GetOrCreatePreviewDialog(web_contents); | |
420 ASSERT_TRUE(tiger_preview_dialog_2); | |
421 | |
422 // Verify this is a new dialog. | |
423 EXPECT_TRUE(tiger_barb_preview_dialog != tiger_preview_dialog_2); | |
424 EXPECT_NE(tiger_preview_dialog_2, web_contents); | |
425 | |
426 // Try to simulate Gmail navigation: Navigate to an existing page (via | |
427 // Forward) but modify the navigation type while pending to look like an | |
428 // address bar + typed transition (like Gmail auto navigation) | |
429 nav_controller.GoForward(); | |
430 nav_controller.GetPendingEntry()->SetTransitionType(ui::PageTransitionFromInt( | |
431 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)); | |
432 CommitPendingLoad(&nav_controller); | |
433 | |
434 // Navigation successful | |
435 EXPECT_EQ(tiger_barb, web_contents->GetLastCommittedURL()); | |
436 | |
437 // Print preview should not have changed due to this navigation type so print | |
438 // preview now should return false, and the dialog returned by | |
439 // GetOrCreatePreviewDialog should be the same as the earlier dialog. | |
440 EXPECT_FALSE(manager->PrintPreviewNow(false)); | |
441 WebContents* tiger_preview_dialog_2b = | |
442 dialog_controller->GetOrCreatePreviewDialog(web_contents); | |
443 ASSERT_TRUE(tiger_preview_dialog_2b); | |
444 EXPECT_EQ(tiger_preview_dialog_2b, tiger_preview_dialog_2); | |
445 EXPECT_NE(tiger_preview_dialog_2b, web_contents); | |
446 } | |
172 } // namespace printing | 447 } // namespace printing |
OLD | NEW |