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. 4 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/browser/notification_service.h" 14 #include "content/public/browser/notification_types.h" 15 #include "content/public/common/url_constants.h" 16 17 using content::WebContents; 18 19 // Test crashes on Aura due to initiator's native view having no parent. 20 // http://crbug.com/104284 21 #if defined(USE_AURA) 22 #define MAYBE_GetOrCreatePreviewDialog DISABLED_GetOrCreatePreviewDialog 23 #define MAYBE_MultiplePreviewDialogs DISABLED_MultiplePreviewDialogs 24 #define MAYBE_ClearInitiatorDetails DISABLED_ClearInitiatorDetails 25 #else 26 #define MAYBE_GetOrCreatePreviewDialog GetOrCreatePreviewDialog 27 #define MAYBE_MultiplePreviewDialogs MultiplePreviewDialogs 28 #define MAYBE_ClearInitiatorDetails ClearInitiatorDetails 29 #endif 30 31 namespace printing { 32 33 typedef PrintPreviewTest PrintPreviewDialogControllerUnitTest; 34 35 // Create/Get a preview dialog for initiator. 36 TEST_F(PrintPreviewDialogControllerUnitTest, MAYBE_GetOrCreatePreviewDialog) { 37 // Lets start with one window with one tab. 38 EXPECT_EQ(1u, chrome::GetTotalBrowserCount()); 39 EXPECT_EQ(0, browser()->tab_strip_model()->count()); 40 chrome::NewTab(browser()); 41 EXPECT_EQ(1, browser()->tab_strip_model()->count()); 42 43 // Create a reference to initiator contents. 44 WebContents* initiator = browser()->tab_strip_model()->GetActiveWebContents(); 45 46 PrintPreviewDialogController* dialog_controller = 47 PrintPreviewDialogController::GetInstance(); 48 ASSERT_TRUE(dialog_controller); 49 50 // Get the preview dialog for initiator. 51 PrintViewManager::FromWebContents(initiator)->PrintPreviewNow(false); 52 WebContents* preview_dialog = 53 dialog_controller->GetOrCreatePreviewDialog(initiator); 54 55 // New print preview dialog is a constrained window, so the number of tabs is 56 // still 1. 57 EXPECT_EQ(1, browser()->tab_strip_model()->count()); 58 EXPECT_NE(initiator, preview_dialog); 59 60 // Get the print preview dialog for the same initiator. 61 WebContents* new_preview_dialog = 62 dialog_controller->GetOrCreatePreviewDialog(initiator); 63 64 // Preview dialog already exists. Tab count remains the same. 65 EXPECT_EQ(1, browser()->tab_strip_model()->count()); 66 67 // 1:1 relationship between initiator and preview dialog. 68 EXPECT_EQ(new_preview_dialog, preview_dialog); 69 } 70 71 // Tests multiple print preview dialogs exist in the same browser for different 72 // initiators. If a preview dialog already exists for an initiator, that 73 // initiator gets focused. 74 TEST_F(PrintPreviewDialogControllerUnitTest, MAYBE_MultiplePreviewDialogs) { 75 // Lets start with one window and two tabs. 76 EXPECT_EQ(1u, chrome::GetTotalBrowserCount()); 77 TabStripModel* tab_strip_model = browser()->tab_strip_model(); 78 ASSERT_TRUE(tab_strip_model); 79 80 EXPECT_EQ(0, tab_strip_model->count()); 81 82 // Create some new initiators. 83 chrome::NewTab(browser()); 84 WebContents* web_contents_1 = tab_strip_model->GetActiveWebContents(); 85 ASSERT_TRUE(web_contents_1); 86 87 chrome::NewTab(browser()); 88 WebContents* web_contents_2 = tab_strip_model->GetActiveWebContents(); 89 ASSERT_TRUE(web_contents_2); 90 EXPECT_EQ(2, tab_strip_model->count()); 91 92 PrintPreviewDialogController* dialog_controller = 93 PrintPreviewDialogController::GetInstance(); 94 ASSERT_TRUE(dialog_controller); 95 96 // Create preview dialog for |web_contents_1| 97 PrintViewManager::FromWebContents(web_contents_1)->PrintPreviewNow(false); 98 WebContents* preview_dialog_1 = 99 dialog_controller->GetOrCreatePreviewDialog(web_contents_1); 100 101 EXPECT_NE(web_contents_1, preview_dialog_1); 102 EXPECT_EQ(2, tab_strip_model->count()); 103 104 // Create preview dialog for |web_contents_2| 105 PrintViewManager::FromWebContents(web_contents_2)->PrintPreviewNow(false); 106 WebContents* preview_dialog_2 = 107 dialog_controller->GetOrCreatePreviewDialog(web_contents_2); 108 109 EXPECT_NE(web_contents_2, preview_dialog_2); 110 EXPECT_NE(preview_dialog_1, preview_dialog_2); 111 // 2 initiators and 2 preview dialogs exist in the same browser. The preview 112 // dialogs are constrained in their respective initiators. 113 EXPECT_EQ(2, tab_strip_model->count()); 114 115 int tab_1_index = tab_strip_model->GetIndexOfWebContents(web_contents_1); 116 int tab_2_index = tab_strip_model->GetIndexOfWebContents(web_contents_2); 117 int preview_dialog_1_index = 118 tab_strip_model->GetIndexOfWebContents(preview_dialog_1); 119 int preview_dialog_2_index = 120 tab_strip_model->GetIndexOfWebContents(preview_dialog_2); 121 122 // Constrained dialogs are not in the TabStripModel. 123 EXPECT_EQ(-1, preview_dialog_1_index); 124 EXPECT_EQ(-1, preview_dialog_2_index); 125 126 // Since |preview_dialog_2_index| was the most recently created dialog, its 127 // initiator should have focus. 128 EXPECT_EQ(tab_2_index, tab_strip_model->active_index()); 129 130 // When we get the preview dialog for |web_contents_1|, 131 // |preview_dialog_1| is activated and focused. 132 dialog_controller->GetOrCreatePreviewDialog(web_contents_1); 133 EXPECT_EQ(tab_1_index, tab_strip_model->active_index()); 134 } 135 136 // Check clearing the initiator details associated with a print preview dialog 137 // allows the initiator to create another print preview dialog. 138 TEST_F(PrintPreviewDialogControllerUnitTest, MAYBE_ClearInitiatorDetails) { 139 // Lets start with one window with one tab. 140 EXPECT_EQ(1u, chrome::GetTotalBrowserCount()); 141 EXPECT_EQ(0, browser()->tab_strip_model()->count()); 142 chrome::NewTab(browser()); 143 EXPECT_EQ(1, browser()->tab_strip_model()->count()); 144 145 // Create a reference to initiator contents. 146 WebContents* initiator = browser()->tab_strip_model()->GetActiveWebContents(); 147 148 PrintPreviewDialogController* dialog_controller = 149 PrintPreviewDialogController::GetInstance(); 150 ASSERT_TRUE(dialog_controller); 151 152 // Get the preview dialog for the initiator. 153 PrintViewManager::FromWebContents(initiator)->PrintPreviewNow(false); 154 WebContents* preview_dialog = 155 dialog_controller->GetOrCreatePreviewDialog(initiator); 156 157 // New print preview dialog is a constrained window, so the number of tabs is 158 // still 1. 159 EXPECT_EQ(1, browser()->tab_strip_model()->count()); 160 EXPECT_NE(initiator, preview_dialog); 161 162 // Clear the initiator details associated with the preview dialog. 163 dialog_controller->EraseInitiatorInfo(preview_dialog); 164 165 // Get a new print preview dialog for the initiator. 166 WebContents* new_preview_dialog = 167 dialog_controller->GetOrCreatePreviewDialog(initiator); 168 169 // New print preview dialog is a constrained window, so the number of tabs is 170 // still 1. 171 EXPECT_EQ(1, browser()->tab_strip_model()->count()); 172 // Verify a new print preview dialog has been created. 173 EXPECT_NE(new_preview_dialog, preview_dialog); 174 } 175 176 } // namespace printing 177