Chromium Code Reviews| Index: chrome/browser/printing/print_preview_dialog_controller_unittest.cc |
| diff --git a/chrome/browser/printing/print_preview_dialog_controller_unittest.cc b/chrome/browser/printing/print_preview_dialog_controller_unittest.cc |
| index cae8b66e7db7c701ed2a98ee7f91213431421074..334d647aafd44ddc323629f4a9f319568329642f 100644 |
| --- a/chrome/browser/printing/print_preview_dialog_controller_unittest.cc |
| +++ b/chrome/browser/printing/print_preview_dialog_controller_unittest.cc |
| @@ -12,16 +12,82 @@ |
| #include "chrome/browser/ui/browser_finder.h" |
| #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" |
| +#include "content/public/browser/navigation_controller.h" |
| #include "content/public/browser/navigation_details.h" |
| +#include "content/public/browser/navigation_entry.h" |
| #include "content/public/browser/web_contents_delegate.h" |
| +#include "content/public/browser/web_contents_observer.h" |
| #include "content/public/common/url_constants.h" |
| #include "content/public/test/web_contents_tester.h" |
| using content::WebContents; |
| +using content::WebContentsObserver; |
| namespace { |
| // content::WebContentsDelegate destructor is protected: subclass for testing. |
| class TestWebContentsDelegate : public content::WebContentsDelegate {}; |
| + |
| +class PrintPreviewDialogDestroyedObserver : public WebContentsObserver { |
| + public: |
| + explicit PrintPreviewDialogDestroyedObserver(WebContents* dialog) |
| + : WebContentsObserver(dialog), |
| + dialog_destroyed_(false) { |
| + } |
| + ~PrintPreviewDialogDestroyedObserver() override {} |
| + |
| + bool dialog_destroyed() const { return dialog_destroyed_; } |
| + |
| + private: |
| + // content::WebContentsObserver implementation. |
| + void WebContentsDestroyed() override { dialog_destroyed_ = true; } |
| + |
| + bool dialog_destroyed_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogDestroyedObserver); |
| +}; |
| + |
| +class PrintPreviewDialogControllerForTest : |
| + public printing::PrintPreviewDialogController { |
| + public: |
| + PrintPreviewDialogControllerForTest() { |
| + next_dialog_ = 1; |
|
Lei Zhang
2016/08/17 21:27:55
Use the initializer list
|
| + } |
| + |
| + WebContents* GetOrCreatePreviewDialog(WebContents * initiator) override { |
| + DCHECK(initiator); |
| + |
| + // Get the print preview dialog for |initiator|. |
| + WebContents* preview_dialog = GetPrintPreviewForContents(initiator); |
| + if (!preview_dialog) { |
|
Lei Zhang
2016/08/17 21:27:55
I usually like to write:
if (foo) {
DoFoo();
} e
|
| + // Create a fake dialog and put it in the map, then observe initiator |
| + preview_dialog = reinterpret_cast<WebContents*>(next_dialog_++); |
| + preview_dialog_map_[preview_dialog] = initiator; |
| + AddObservers(initiator); |
| + } else { |
| + // Show the initiator holding the existing preview dialog. |
| + initiator->GetDelegate()->ActivateContents(initiator); |
| + } |
| + return preview_dialog; |
| + } |
| + |
| + private: |
| + ~PrintPreviewDialogControllerForTest() override {} |
| + |
| + // Override so that the controller doesn't try to close a fake web contents. |
| + void RemoveInitiator(WebContents* initiator) override { |
| + WebContents* preview_dialog = GetPrintPreviewForContents(initiator); |
| + DCHECK(preview_dialog); |
| + preview_dialog_map_[preview_dialog] = nullptr; |
| + RemoveObservers(initiator); |
| + printing::PrintViewManager::FromWebContents(initiator)->PrintPreviewDone(); |
| + } |
| + |
| + DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogControllerForTest); |
| + int next_dialog_; |
| +}; |
| + |
| + |
| + |
| } // namespace |
| namespace printing { |
| @@ -169,4 +235,213 @@ TEST_F(PrintPreviewDialogControllerUnitTest, ClearInitiatorDetails) { |
| EXPECT_NE(new_preview_dialog, preview_dialog); |
| } |
| +// Test that print preview dialogs close on navigation to new pages |
| +// and when navigating to old pages via fwd/back, but that auto navigation |
| +// (typed + address bar) to an existing page as occurs in gmail does not cause |
| +// the dialogs to close. |
| +TEST_F(PrintPreviewDialogControllerUnitTest, CloseDialogOnNavigation) { |
|
Lei Zhang
2016/08/17 21:27:55
I prefer this version mainly because we don't have
|
| + // Two similar URLs (same webpage, different URL fragments) |
| + GURL tiger_barb("https://www.google.com/#q=tiger+barb"); |
| + GURL tiger("https://www.google.com/#q=tiger"); |
| + |
| + // Set up by opening a new tab and getting web contents |
| + EXPECT_EQ(1u, chrome::GetTotalBrowserCount()); |
| + EXPECT_EQ(0, browser()->tab_strip_model()->count()); |
| + chrome::NewTab(browser()); |
| + EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
| + WebContents* web_contents = |
| + browser()->tab_strip_model()->GetActiveWebContents(); |
| + ASSERT_TRUE(web_contents); |
| + content::NavigationController& nav_controller = web_contents->GetController(); |
| + |
| + // Navigate to first page |
| + nav_controller.LoadURL(tiger, content::Referrer(), |
| + ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK), |
| + std::string()); |
| + CommitPendingLoad(&nav_controller); |
| + EXPECT_EQ(tiger, web_contents->GetLastCommittedURL()); |
| + |
| + // Get the preview dialog |
| + PrintPreviewDialogController* dialog_controller = |
| + PrintPreviewDialogController::GetInstance(); |
| + ASSERT_TRUE(dialog_controller); |
| + WebContents* tiger_preview_dialog = |
| + dialog_controller->GetOrCreatePreviewDialog(web_contents); |
| + PrintViewManager* manager = PrintViewManager::FromWebContents(web_contents); |
| + manager->PrintPreviewNow(false); |
| + |
| + // New print preview dialog is a constrained window, so the number of tabs is |
| + // still 1. |
| + EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
| + EXPECT_NE(web_contents, tiger_preview_dialog); |
| + PrintPreviewDialogDestroyedObserver tiger_destroyed(tiger_preview_dialog); |
| + |
| + // Navigate via link to a similar page. |
| + nav_controller.LoadURL(tiger_barb, content::Referrer(), |
| + ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK), |
| + std::string()); |
| + CommitPendingLoad(&nav_controller); |
| + |
| + // Check navigation was successful |
| + EXPECT_EQ(tiger_barb, web_contents->GetLastCommittedURL()); |
| + |
| + // Print preview now should return true as the navigation should have closed |
| + // |tiger_preview_dialog| and the previous dialog should have closed. |
| + EXPECT_TRUE(manager->PrintPreviewNow(false)); |
| + WebContents* tiger_barb_preview_dialog = |
| + dialog_controller->GetOrCreatePreviewDialog(web_contents); |
| + ASSERT_TRUE(tiger_barb_preview_dialog); |
| + |
| + // Check a new dialog was created - either the pointers should be different or |
| + // the previous web contents must have been destroyed. |
| + EXPECT_TRUE(tiger_destroyed.dialog_destroyed() || |
| + tiger_barb_preview_dialog != tiger_preview_dialog); |
| + EXPECT_NE(tiger_barb_preview_dialog, web_contents); |
| + PrintPreviewDialogDestroyedObserver tiger_barb_destroyed( |
| + tiger_barb_preview_dialog); |
| + |
| + // Now this returns false as |tiger_barb_preview_dialog| is open. |
| + EXPECT_FALSE(manager->PrintPreviewNow(false)); |
| + |
| + // Navigate with back button or ALT+LEFT ARROW to a similar page. |
| + nav_controller.GoBack(); |
| + CommitPendingLoad(&nav_controller); |
| + EXPECT_EQ(tiger, web_contents->GetLastCommittedURL()); |
| + EXPECT_TRUE(manager->PrintPreviewNow(false)); |
| + |
| + // Get new dialog |
| + WebContents* tiger_preview_dialog_2 = |
| + dialog_controller->GetOrCreatePreviewDialog(web_contents); |
| + ASSERT_TRUE(tiger_preview_dialog_2); |
| + |
| + // Verify this is a new dialog. |
| + EXPECT_TRUE(tiger_barb_destroyed.dialog_destroyed() || |
| + tiger_barb_preview_dialog != tiger_preview_dialog_2); |
| + EXPECT_NE(tiger_preview_dialog_2, web_contents); |
| + PrintPreviewDialogDestroyedObserver tiger_2_destroyed( |
| + tiger_preview_dialog_2); |
| + |
| + // Try to simulate Gmail navigation: Navigate to an existing page (via |
| + // Forward) but modify the navigation type while pending to look like an |
| + // address bar + typed transition (like Gmail auto navigation) |
| + nav_controller.GoForward(); |
| + nav_controller.GetPendingEntry()->SetTransitionType(ui::PageTransitionFromInt( |
| + ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)); |
| + CommitPendingLoad(&nav_controller); |
| + |
| + // Navigation successful |
| + EXPECT_EQ(tiger_barb, web_contents->GetLastCommittedURL()); |
| + |
| + // Print preview should not have changed due to this navigation type so print |
| + // preview now should return false, dialog is still alive, and the dialog |
| + // returned by GetOrCreatePreviewDialog should be the same as the earlier |
| + // dialog. |
| + EXPECT_FALSE(manager->PrintPreviewNow(false)); |
| + EXPECT_FALSE(tiger_2_destroyed.dialog_destroyed()); |
| + WebContents* tiger_preview_dialog_2b = |
| + dialog_controller->GetOrCreatePreviewDialog(web_contents); |
| + ASSERT_TRUE(tiger_preview_dialog_2b); |
| + EXPECT_EQ(tiger_preview_dialog_2b, tiger_preview_dialog_2); |
| + EXPECT_NE(tiger_preview_dialog_2b, web_contents); |
| +} |
| + |
| +// Test that print preview dialogs close on navigation to new pages |
| +// and when navigating to old pages via fwd/back, but that auto navigation |
| +// (typed + address bar) to an existing page as occurs in gmail does not cause |
| +// the dialogs to close. |
| +TEST_F(PrintPreviewDialogControllerUnitTest, CloseDialogOnNavigationV2) { |
| + // Two similar URLs (same webpage, different URL fragments) |
| + GURL tiger_barb("https://www.google.com/#q=tiger+barb"); |
| + GURL tiger("https://www.google.com/#q=tiger"); |
| + |
| + // Set up by opening a new tab and getting web contents |
| + EXPECT_EQ(1u, chrome::GetTotalBrowserCount()); |
| + EXPECT_EQ(0, browser()->tab_strip_model()->count()); |
| + chrome::NewTab(browser()); |
| + EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
| + WebContents* web_contents = |
| + browser()->tab_strip_model()->GetActiveWebContents(); |
| + ASSERT_TRUE(web_contents); |
| + content::NavigationController& nav_controller = web_contents->GetController(); |
| + |
| + // Navigate to first page |
| + nav_controller.LoadURL(tiger, content::Referrer(), |
| + ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK), |
| + std::string()); |
| + CommitPendingLoad(&nav_controller); |
| + EXPECT_EQ(tiger, web_contents->GetLastCommittedURL()); |
| + |
| + // Get the preview dialog |
| + PrintPreviewDialogControllerForTest* dialog_controller = |
| + new PrintPreviewDialogControllerForTest; |
| + ASSERT_TRUE(dialog_controller); |
| + PrintViewManager* manager = PrintViewManager::FromWebContents(web_contents); |
| + WebContents* tiger_preview_dialog = |
| + dialog_controller->GetOrCreatePreviewDialog(web_contents); |
| + manager->PrintPreviewNow(false); |
| + |
| + // New print preview dialog is a constrained window, so the number of tabs is |
| + // still 1. |
| + EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
| + EXPECT_NE(web_contents, tiger_preview_dialog); |
| + |
| + // Navigate via link to a similar page. |
| + nav_controller.LoadURL(tiger_barb, content::Referrer(), |
| + ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK), |
| + std::string()); |
| + CommitPendingLoad(&nav_controller); |
| + |
| + // Check navigation was successful |
| + EXPECT_EQ(tiger_barb, web_contents->GetLastCommittedURL()); |
| + |
| + // Print preview now should return true as the navigation should have closed |
| + // |tiger_preview_dialog| |
| + EXPECT_TRUE(manager->PrintPreviewNow(false)); |
| + WebContents* tiger_barb_preview_dialog = |
| + dialog_controller->GetOrCreatePreviewDialog(web_contents); |
| + ASSERT_TRUE(tiger_barb_preview_dialog); |
| + |
| + // Check a new dialog was created - pointers should be different. |
| + EXPECT_TRUE(tiger_barb_preview_dialog != tiger_preview_dialog); |
| + EXPECT_NE(tiger_barb_preview_dialog, web_contents); |
| + |
| + // Now this returns false as |tiger_barb_preview_dialog| is open. |
| + EXPECT_FALSE(manager->PrintPreviewNow(false)); |
| + |
| + // Navigate with back button or ALT+LEFT ARROW to a similar page. |
| + nav_controller.GoBack(); |
| + CommitPendingLoad(&nav_controller); |
| + EXPECT_EQ(tiger, web_contents->GetLastCommittedURL()); |
| + EXPECT_TRUE(manager->PrintPreviewNow(false)); |
| + |
| + // Get new dialog |
| + WebContents* tiger_preview_dialog_2 = |
| + dialog_controller->GetOrCreatePreviewDialog(web_contents); |
| + ASSERT_TRUE(tiger_preview_dialog_2); |
| + |
| + // Verify this is a new dialog. |
| + EXPECT_TRUE(tiger_barb_preview_dialog != tiger_preview_dialog_2); |
| + EXPECT_NE(tiger_preview_dialog_2, web_contents); |
| + |
| + // Try to simulate Gmail navigation: Navigate to an existing page (via |
| + // Forward) but modify the navigation type while pending to look like an |
| + // address bar + typed transition (like Gmail auto navigation) |
| + nav_controller.GoForward(); |
| + nav_controller.GetPendingEntry()->SetTransitionType(ui::PageTransitionFromInt( |
| + ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)); |
| + CommitPendingLoad(&nav_controller); |
| + |
| + // Navigation successful |
| + EXPECT_EQ(tiger_barb, web_contents->GetLastCommittedURL()); |
| + |
| + // Print preview should not have changed due to this navigation type so print |
| + // preview now should return false, and the dialog returned by |
| + // GetOrCreatePreviewDialog should be the same as the earlier dialog. |
| + EXPECT_FALSE(manager->PrintPreviewNow(false)); |
| + WebContents* tiger_preview_dialog_2b = |
| + dialog_controller->GetOrCreatePreviewDialog(web_contents); |
| + ASSERT_TRUE(tiger_preview_dialog_2b); |
| + EXPECT_EQ(tiger_preview_dialog_2b, tiger_preview_dialog_2); |
| + EXPECT_NE(tiger_preview_dialog_2b, web_contents); |
| +} |
| } // namespace printing |