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 "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
8 #include "chrome/browser/printing/print_preview_dialog_controller.h" | 8 #include "chrome/browser/printing/print_preview_dialog_controller.h" |
9 #include "chrome/browser/ui/browser.h" | 9 #include "chrome/browser/ui/browser.h" |
10 #include "chrome/browser/ui/browser_commands.h" | 10 #include "chrome/browser/ui/browser_commands.h" |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 dialog_destroyed_ = true; | 96 dialog_destroyed_ = true; |
97 } | 97 } |
98 | 98 |
99 bool dialog_destroyed_; | 99 bool dialog_destroyed_; |
100 | 100 |
101 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogDestroyedObserver); | 101 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogDestroyedObserver); |
102 }; | 102 }; |
103 | 103 |
104 class PrintPreviewDialogControllerBrowserTest : public InProcessBrowserTest { | 104 class PrintPreviewDialogControllerBrowserTest : public InProcessBrowserTest { |
105 public: | 105 public: |
106 PrintPreviewDialogControllerBrowserTest() : initiator_tab_(NULL) {} | 106 PrintPreviewDialogControllerBrowserTest() : initiator_(NULL) {} |
107 virtual ~PrintPreviewDialogControllerBrowserTest() {} | 107 virtual ~PrintPreviewDialogControllerBrowserTest() {} |
108 | 108 |
109 WebContents* initiator_tab() { | 109 WebContents* initiator() { |
110 return initiator_tab_; | 110 return initiator_; |
111 } | 111 } |
112 | 112 |
113 void PrintPreview() { | 113 void PrintPreview() { |
114 base::RunLoop run_loop; | 114 base::RunLoop run_loop; |
115 request_preview_tab_observer()->set_quit_closure(run_loop.QuitClosure()); | 115 request_preview_tab_observer()->set_quit_closure(run_loop.QuitClosure()); |
116 chrome::Print(browser()); | 116 chrome::Print(browser()); |
117 run_loop.Run(); | 117 run_loop.Run(); |
118 } | 118 } |
119 | 119 |
120 WebContents* GetPrintPreviewDialog() { | 120 WebContents* GetPrintPreviewDialog() { |
121 printing::PrintPreviewDialogController* dialog_controller = | 121 printing::PrintPreviewDialogController* dialog_controller = |
122 printing::PrintPreviewDialogController::GetInstance(); | 122 printing::PrintPreviewDialogController::GetInstance(); |
123 return dialog_controller->GetPrintPreviewForContents(initiator_tab_); | 123 return dialog_controller->GetPrintPreviewForContents(initiator_); |
124 } | 124 } |
125 | 125 |
126 private: | 126 private: |
127 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 127 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
128 #if !defined(GOOGLE_CHROME_BUILD) | 128 #if !defined(GOOGLE_CHROME_BUILD) |
129 command_line->AppendSwitch(switches::kEnablePrintPreview); | 129 command_line->AppendSwitch(switches::kEnablePrintPreview); |
130 #endif | 130 #endif |
131 } | 131 } |
132 | 132 |
133 virtual void SetUpOnMainThread() OVERRIDE { | 133 virtual void SetUpOnMainThread() OVERRIDE { |
134 WebContents* first_tab = | 134 WebContents* first_tab = |
135 browser()->tab_strip_model()->GetActiveWebContents(); | 135 browser()->tab_strip_model()->GetActiveWebContents(); |
136 ASSERT_TRUE(first_tab); | 136 ASSERT_TRUE(first_tab); |
137 | 137 |
138 // Open a new tab so |cloned_tab_observer_| can see it first and attach a | 138 // Open a new tab so |cloned_tab_observer_| can see it first and attach a |
139 // RequestPrintPreviewObserver to it before the real | 139 // RequestPrintPreviewObserver to it before the real |
140 // PrintPreviewMessageHandler gets created. Thus enabling | 140 // PrintPreviewMessageHandler gets created. Thus enabling |
141 // RequestPrintPreviewObserver to get messages first for the purposes of | 141 // RequestPrintPreviewObserver to get messages first for the purposes of |
142 // this test. | 142 // this test. |
143 cloned_tab_observer_.reset(new PrintPreviewDialogClonedObserver(first_tab)); | 143 cloned_tab_observer_.reset(new PrintPreviewDialogClonedObserver(first_tab)); |
144 chrome::DuplicateTab(browser()); | 144 chrome::DuplicateTab(browser()); |
145 | 145 |
146 initiator_tab_ = browser()->tab_strip_model()->GetActiveWebContents(); | 146 initiator_ = browser()->tab_strip_model()->GetActiveWebContents(); |
147 ASSERT_TRUE(initiator_tab_); | 147 ASSERT_TRUE(initiator_); |
148 ASSERT_NE(first_tab, initiator_tab_); | 148 ASSERT_NE(first_tab, initiator_); |
149 } | 149 } |
150 | 150 |
151 virtual void CleanUpOnMainThread() OVERRIDE { | 151 virtual void CleanUpOnMainThread() OVERRIDE { |
152 cloned_tab_observer_.reset(); | 152 cloned_tab_observer_.reset(); |
153 initiator_tab_ = NULL; | 153 initiator_ = NULL; |
154 } | 154 } |
155 | 155 |
156 RequestPrintPreviewObserver* request_preview_tab_observer() { | 156 RequestPrintPreviewObserver* request_preview_tab_observer() { |
157 return cloned_tab_observer_->request_preview_tab_observer(); | 157 return cloned_tab_observer_->request_preview_tab_observer(); |
158 } | 158 } |
159 | 159 |
160 scoped_ptr<PrintPreviewDialogClonedObserver> cloned_tab_observer_; | 160 scoped_ptr<PrintPreviewDialogClonedObserver> cloned_tab_observer_; |
161 WebContents* initiator_tab_; | 161 WebContents* initiator_; |
162 | 162 |
163 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogControllerBrowserTest); | 163 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogControllerBrowserTest); |
164 }; | 164 }; |
165 | 165 |
166 // Test to verify that when a initiator tab navigates, we can create a new | 166 // Test to verify that when a initiator navigates, we can create a new preview |
167 // preview dialog for the new tab contents. | 167 // dialog for the new tab contents. |
168 IN_PROC_BROWSER_TEST_F(PrintPreviewDialogControllerBrowserTest, | 168 IN_PROC_BROWSER_TEST_F(PrintPreviewDialogControllerBrowserTest, |
169 NavigateFromInitiatorTab) { | 169 NavigateFromInitiatorTab) { |
170 // print for the first time. | 170 // print for the first time. |
171 PrintPreview(); | 171 PrintPreview(); |
172 | 172 |
173 // Get the preview dialog for the initiator tab. | 173 // Get the preview dialog for the initiator tab. |
174 WebContents* preview_dialog = GetPrintPreviewDialog(); | 174 WebContents* preview_dialog = GetPrintPreviewDialog(); |
175 | 175 |
176 // Check a new print preview dialog got created. | 176 // Check a new print preview dialog got created. |
177 ASSERT_TRUE(preview_dialog); | 177 ASSERT_TRUE(preview_dialog); |
178 ASSERT_NE(initiator_tab(), preview_dialog); | 178 ASSERT_NE(initiator(), preview_dialog); |
179 | 179 |
180 // Navigate in the initiator tab. Make sure navigating destroys the print | 180 // Navigate in the initiator tab. Make sure navigating destroys the print |
181 // preview dialog. | 181 // preview dialog. |
182 PrintPreviewDialogDestroyedObserver dialog_destroyed_observer(preview_dialog); | 182 PrintPreviewDialogDestroyedObserver dialog_destroyed_observer(preview_dialog); |
183 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL)); | 183 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL)); |
184 ASSERT_TRUE(dialog_destroyed_observer.dialog_destroyed()); | 184 ASSERT_TRUE(dialog_destroyed_observer.dialog_destroyed()); |
185 | 185 |
186 // Try printing again. | 186 // Try printing again. |
187 PrintPreview(); | 187 PrintPreview(); |
188 | 188 |
189 // Get the print preview dialog for the initiator tab. | 189 // Get the print preview dialog for the initiator tab. |
190 WebContents* new_preview_dialog = GetPrintPreviewDialog(); | 190 WebContents* new_preview_dialog = GetPrintPreviewDialog(); |
191 | 191 |
192 // Check a new preview dialog got created. | 192 // Check a new preview dialog got created. |
193 EXPECT_TRUE(new_preview_dialog); | 193 EXPECT_TRUE(new_preview_dialog); |
194 } | 194 } |
195 | 195 |
196 // Test to verify that after reloading the initiator tab, it creates a new | 196 // Test to verify that after reloading the initiator, it creates a new print |
197 // print preview dialog. | 197 // preview dialog. |
198 IN_PROC_BROWSER_TEST_F(PrintPreviewDialogControllerBrowserTest, | 198 IN_PROC_BROWSER_TEST_F(PrintPreviewDialogControllerBrowserTest, |
199 ReloadInitiatorTab) { | 199 ReloadInitiatorTab) { |
200 // print for the first time. | 200 // print for the first time. |
201 PrintPreview(); | 201 PrintPreview(); |
202 | 202 |
203 WebContents* preview_dialog = GetPrintPreviewDialog(); | 203 WebContents* preview_dialog = GetPrintPreviewDialog(); |
204 | 204 |
205 // Check a new print preview dialog got created. | 205 // Check a new print preview dialog got created. |
206 ASSERT_TRUE(preview_dialog); | 206 ASSERT_TRUE(preview_dialog); |
207 ASSERT_NE(initiator_tab(), preview_dialog); | 207 ASSERT_NE(initiator(), preview_dialog); |
208 | 208 |
209 // Reload the initiator tab. Make sure reloading destroys the print preview | 209 // Reload the initiator. Make sure reloading destroys the print preview |
210 // dialog. | 210 // dialog. |
211 PrintPreviewDialogDestroyedObserver dialog_destroyed_observer(preview_dialog); | 211 PrintPreviewDialogDestroyedObserver dialog_destroyed_observer(preview_dialog); |
212 content::WindowedNotificationObserver notification_observer( | 212 content::WindowedNotificationObserver notification_observer( |
213 content::NOTIFICATION_LOAD_STOP, | 213 content::NOTIFICATION_LOAD_STOP, |
214 content::NotificationService::AllSources()); | 214 content::NotificationService::AllSources()); |
215 chrome::Reload(browser(), CURRENT_TAB); | 215 chrome::Reload(browser(), CURRENT_TAB); |
216 notification_observer.Wait(); | 216 notification_observer.Wait(); |
217 ASSERT_TRUE(dialog_destroyed_observer.dialog_destroyed()); | 217 ASSERT_TRUE(dialog_destroyed_observer.dialog_destroyed()); |
218 | 218 |
219 // Try printing again. | 219 // Try printing again. |
220 PrintPreview(); | 220 PrintPreview(); |
221 | 221 |
222 // Create a preview dialog for the initiator tab. | 222 // Create a preview dialog for the initiator tab. |
223 WebContents* new_preview_dialog = GetPrintPreviewDialog(); | 223 WebContents* new_preview_dialog = GetPrintPreviewDialog(); |
224 EXPECT_TRUE(new_preview_dialog); | 224 EXPECT_TRUE(new_preview_dialog); |
225 } | 225 } |
OLD | NEW |