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 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "chrome/browser/printing/print_preview_test.h" | 10 #include "chrome/browser/printing/print_preview_test.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | 63 EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
64 | 64 |
65 // Create a reference to initiator contents. | 65 // Create a reference to initiator contents. |
66 WebContents* initiator = browser()->tab_strip_model()->GetActiveWebContents(); | 66 WebContents* initiator = browser()->tab_strip_model()->GetActiveWebContents(); |
67 | 67 |
68 PrintPreviewDialogController* dialog_controller = | 68 PrintPreviewDialogController* dialog_controller = |
69 PrintPreviewDialogController::GetInstance(); | 69 PrintPreviewDialogController::GetInstance(); |
70 ASSERT_TRUE(dialog_controller); | 70 ASSERT_TRUE(dialog_controller); |
71 | 71 |
72 // Get the preview dialog for initiator. | 72 // Get the preview dialog for initiator. |
73 PrintViewManager::FromWebContents(initiator)->PrintPreviewNow( | 73 PrintViewManager::FromWebContents(initiator)->PrintPreviewNow(false); |
74 initiator->GetMainFrame(), false); | |
75 WebContents* preview_dialog = | 74 WebContents* preview_dialog = |
76 dialog_controller->GetOrCreatePreviewDialog(initiator); | 75 dialog_controller->GetOrCreatePreviewDialog(initiator); |
77 | 76 |
78 // New print preview dialog is a constrained window, so the number of tabs is | 77 // New print preview dialog is a constrained window, so the number of tabs is |
79 // still 1. | 78 // still 1. |
80 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | 79 EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
81 EXPECT_NE(initiator, preview_dialog); | 80 EXPECT_NE(initiator, preview_dialog); |
82 | 81 |
83 // Get the print preview dialog for the same initiator. | 82 // Get the print preview dialog for the same initiator. |
84 WebContents* new_preview_dialog = | 83 WebContents* new_preview_dialog = |
(...skipping 25 matching lines...) Expand all Loading... |
110 chrome::NewTab(browser()); | 109 chrome::NewTab(browser()); |
111 WebContents* web_contents_2 = tab_strip_model->GetActiveWebContents(); | 110 WebContents* web_contents_2 = tab_strip_model->GetActiveWebContents(); |
112 ASSERT_TRUE(web_contents_2); | 111 ASSERT_TRUE(web_contents_2); |
113 EXPECT_EQ(2, tab_strip_model->count()); | 112 EXPECT_EQ(2, tab_strip_model->count()); |
114 | 113 |
115 PrintPreviewDialogController* dialog_controller = | 114 PrintPreviewDialogController* dialog_controller = |
116 PrintPreviewDialogController::GetInstance(); | 115 PrintPreviewDialogController::GetInstance(); |
117 ASSERT_TRUE(dialog_controller); | 116 ASSERT_TRUE(dialog_controller); |
118 | 117 |
119 // Create preview dialog for |web_contents_1| | 118 // Create preview dialog for |web_contents_1| |
120 PrintViewManager::FromWebContents(web_contents_1) | 119 PrintViewManager::FromWebContents(web_contents_1)->PrintPreviewNow(false); |
121 ->PrintPreviewNow(web_contents_1->GetMainFrame(), false); | |
122 WebContents* preview_dialog_1 = | 120 WebContents* preview_dialog_1 = |
123 dialog_controller->GetOrCreatePreviewDialog(web_contents_1); | 121 dialog_controller->GetOrCreatePreviewDialog(web_contents_1); |
124 | 122 |
125 EXPECT_NE(web_contents_1, preview_dialog_1); | 123 EXPECT_NE(web_contents_1, preview_dialog_1); |
126 EXPECT_EQ(2, tab_strip_model->count()); | 124 EXPECT_EQ(2, tab_strip_model->count()); |
127 | 125 |
128 // Create preview dialog for |web_contents_2| | 126 // Create preview dialog for |web_contents_2| |
129 PrintViewManager::FromWebContents(web_contents_2) | 127 PrintViewManager::FromWebContents(web_contents_2)->PrintPreviewNow(false); |
130 ->PrintPreviewNow(web_contents_2->GetMainFrame(), false); | |
131 WebContents* preview_dialog_2 = | 128 WebContents* preview_dialog_2 = |
132 dialog_controller->GetOrCreatePreviewDialog(web_contents_2); | 129 dialog_controller->GetOrCreatePreviewDialog(web_contents_2); |
133 | 130 |
134 EXPECT_NE(web_contents_2, preview_dialog_2); | 131 EXPECT_NE(web_contents_2, preview_dialog_2); |
135 EXPECT_NE(preview_dialog_1, preview_dialog_2); | 132 EXPECT_NE(preview_dialog_1, preview_dialog_2); |
136 // 2 initiators and 2 preview dialogs exist in the same browser. The preview | 133 // 2 initiators and 2 preview dialogs exist in the same browser. The preview |
137 // dialogs are constrained in their respective initiators. | 134 // dialogs are constrained in their respective initiators. |
138 EXPECT_EQ(2, tab_strip_model->count()); | 135 EXPECT_EQ(2, tab_strip_model->count()); |
139 | 136 |
140 int tab_1_index = tab_strip_model->GetIndexOfWebContents(web_contents_1); | 137 int tab_1_index = tab_strip_model->GetIndexOfWebContents(web_contents_1); |
(...skipping 27 matching lines...) Expand all Loading... |
168 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | 165 EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
169 | 166 |
170 // Create a reference to initiator contents. | 167 // Create a reference to initiator contents. |
171 WebContents* initiator = browser()->tab_strip_model()->GetActiveWebContents(); | 168 WebContents* initiator = browser()->tab_strip_model()->GetActiveWebContents(); |
172 | 169 |
173 PrintPreviewDialogController* dialog_controller = | 170 PrintPreviewDialogController* dialog_controller = |
174 PrintPreviewDialogController::GetInstance(); | 171 PrintPreviewDialogController::GetInstance(); |
175 ASSERT_TRUE(dialog_controller); | 172 ASSERT_TRUE(dialog_controller); |
176 | 173 |
177 // Get the preview dialog for the initiator. | 174 // Get the preview dialog for the initiator. |
178 PrintViewManager::FromWebContents(initiator)->PrintPreviewNow( | 175 PrintViewManager::FromWebContents(initiator)->PrintPreviewNow(false); |
179 initiator->GetMainFrame(), false); | |
180 WebContents* preview_dialog = | 176 WebContents* preview_dialog = |
181 dialog_controller->GetOrCreatePreviewDialog(initiator); | 177 dialog_controller->GetOrCreatePreviewDialog(initiator); |
182 | 178 |
183 // New print preview dialog is a constrained window, so the number of tabs is | 179 // New print preview dialog is a constrained window, so the number of tabs is |
184 // still 1. | 180 // still 1. |
185 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | 181 EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
186 EXPECT_NE(initiator, preview_dialog); | 182 EXPECT_NE(initiator, preview_dialog); |
187 | 183 |
188 // Clear the initiator details associated with the preview dialog. | 184 // Clear the initiator details associated with the preview dialog. |
189 dialog_controller->EraseInitiatorInfo(preview_dialog); | 185 dialog_controller->EraseInitiatorInfo(preview_dialog); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 CommitPendingLoad(&nav_controller); | 221 CommitPendingLoad(&nav_controller); |
226 EXPECT_EQ(tiger, web_contents->GetLastCommittedURL()); | 222 EXPECT_EQ(tiger, web_contents->GetLastCommittedURL()); |
227 | 223 |
228 // Get the preview dialog | 224 // Get the preview dialog |
229 PrintPreviewDialogController* dialog_controller = | 225 PrintPreviewDialogController* dialog_controller = |
230 PrintPreviewDialogController::GetInstance(); | 226 PrintPreviewDialogController::GetInstance(); |
231 ASSERT_TRUE(dialog_controller); | 227 ASSERT_TRUE(dialog_controller); |
232 WebContents* tiger_preview_dialog = | 228 WebContents* tiger_preview_dialog = |
233 dialog_controller->GetOrCreatePreviewDialog(web_contents); | 229 dialog_controller->GetOrCreatePreviewDialog(web_contents); |
234 PrintViewManager* manager = PrintViewManager::FromWebContents(web_contents); | 230 PrintViewManager* manager = PrintViewManager::FromWebContents(web_contents); |
235 manager->PrintPreviewNow(web_contents->GetMainFrame(), false); | 231 manager->PrintPreviewNow(false); |
236 | 232 |
237 // New print preview dialog is a constrained window, so the number of tabs is | 233 // New print preview dialog is a constrained window, so the number of tabs is |
238 // still 1. | 234 // still 1. |
239 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | 235 EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
240 EXPECT_NE(web_contents, tiger_preview_dialog); | 236 EXPECT_NE(web_contents, tiger_preview_dialog); |
241 PrintPreviewDialogDestroyedObserver tiger_destroyed(tiger_preview_dialog); | 237 PrintPreviewDialogDestroyedObserver tiger_destroyed(tiger_preview_dialog); |
242 | 238 |
243 // Navigate via link to a similar page. | 239 // Navigate via link to a similar page. |
244 nav_controller.LoadURL(tiger_barb, content::Referrer(), | 240 nav_controller.LoadURL(tiger_barb, content::Referrer(), |
245 ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK), | 241 ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK), |
246 std::string()); | 242 std::string()); |
247 CommitPendingLoad(&nav_controller); | 243 CommitPendingLoad(&nav_controller); |
248 | 244 |
249 // Check navigation was successful | 245 // Check navigation was successful |
250 EXPECT_EQ(tiger_barb, web_contents->GetLastCommittedURL()); | 246 EXPECT_EQ(tiger_barb, web_contents->GetLastCommittedURL()); |
251 | 247 |
252 // Print preview now should return true as the navigation should have closed | 248 // Print preview now should return true as the navigation should have closed |
253 // |tiger_preview_dialog| and the previous dialog should have closed. | 249 // |tiger_preview_dialog| and the previous dialog should have closed. |
254 EXPECT_TRUE(manager->PrintPreviewNow(web_contents->GetMainFrame(), false)); | 250 EXPECT_TRUE(manager->PrintPreviewNow(false)); |
255 WebContents* tiger_barb_preview_dialog = | 251 WebContents* tiger_barb_preview_dialog = |
256 dialog_controller->GetOrCreatePreviewDialog(web_contents); | 252 dialog_controller->GetOrCreatePreviewDialog(web_contents); |
257 ASSERT_TRUE(tiger_barb_preview_dialog); | 253 ASSERT_TRUE(tiger_barb_preview_dialog); |
258 | 254 |
259 // Check a new dialog was created - either the pointers should be different or | 255 // Check a new dialog was created - either the pointers should be different or |
260 // the previous web contents must have been destroyed. | 256 // the previous web contents must have been destroyed. |
261 EXPECT_TRUE(tiger_destroyed.dialog_destroyed() || | 257 EXPECT_TRUE(tiger_destroyed.dialog_destroyed() || |
262 tiger_barb_preview_dialog != tiger_preview_dialog); | 258 tiger_barb_preview_dialog != tiger_preview_dialog); |
263 EXPECT_NE(tiger_barb_preview_dialog, web_contents); | 259 EXPECT_NE(tiger_barb_preview_dialog, web_contents); |
264 PrintPreviewDialogDestroyedObserver tiger_barb_destroyed( | 260 PrintPreviewDialogDestroyedObserver tiger_barb_destroyed( |
265 tiger_barb_preview_dialog); | 261 tiger_barb_preview_dialog); |
266 | 262 |
267 // Now this returns false as |tiger_barb_preview_dialog| is open. | 263 // Now this returns false as |tiger_barb_preview_dialog| is open. |
268 EXPECT_FALSE(manager->PrintPreviewNow(web_contents->GetMainFrame(), false)); | 264 EXPECT_FALSE(manager->PrintPreviewNow(false)); |
269 | 265 |
270 // Navigate with back button or ALT+LEFT ARROW to a similar page. | 266 // Navigate with back button or ALT+LEFT ARROW to a similar page. |
271 nav_controller.GoBack(); | 267 nav_controller.GoBack(); |
272 CommitPendingLoad(&nav_controller); | 268 CommitPendingLoad(&nav_controller); |
273 EXPECT_EQ(tiger, web_contents->GetLastCommittedURL()); | 269 EXPECT_EQ(tiger, web_contents->GetLastCommittedURL()); |
274 EXPECT_TRUE(manager->PrintPreviewNow(web_contents->GetMainFrame(), false)); | 270 EXPECT_TRUE(manager->PrintPreviewNow(false)); |
275 | 271 |
276 // Get new dialog | 272 // Get new dialog |
277 WebContents* tiger_preview_dialog_2 = | 273 WebContents* tiger_preview_dialog_2 = |
278 dialog_controller->GetOrCreatePreviewDialog(web_contents); | 274 dialog_controller->GetOrCreatePreviewDialog(web_contents); |
279 ASSERT_TRUE(tiger_preview_dialog_2); | 275 ASSERT_TRUE(tiger_preview_dialog_2); |
280 | 276 |
281 // Verify this is a new dialog. | 277 // Verify this is a new dialog. |
282 EXPECT_TRUE(tiger_barb_destroyed.dialog_destroyed() || | 278 EXPECT_TRUE(tiger_barb_destroyed.dialog_destroyed() || |
283 tiger_barb_preview_dialog != tiger_preview_dialog_2); | 279 tiger_barb_preview_dialog != tiger_preview_dialog_2); |
284 EXPECT_NE(tiger_preview_dialog_2, web_contents); | 280 EXPECT_NE(tiger_preview_dialog_2, web_contents); |
285 PrintPreviewDialogDestroyedObserver tiger_2_destroyed( | 281 PrintPreviewDialogDestroyedObserver tiger_2_destroyed( |
286 tiger_preview_dialog_2); | 282 tiger_preview_dialog_2); |
287 | 283 |
288 // Try to simulate Gmail navigation: Navigate to an existing page (via | 284 // Try to simulate Gmail navigation: Navigate to an existing page (via |
289 // Forward) but modify the navigation type while pending to look like an | 285 // Forward) but modify the navigation type while pending to look like an |
290 // address bar + typed transition (like Gmail auto navigation) | 286 // address bar + typed transition (like Gmail auto navigation) |
291 nav_controller.GoForward(); | 287 nav_controller.GoForward(); |
292 nav_controller.GetPendingEntry()->SetTransitionType(ui::PageTransitionFromInt( | 288 nav_controller.GetPendingEntry()->SetTransitionType(ui::PageTransitionFromInt( |
293 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)); | 289 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)); |
294 CommitPendingLoad(&nav_controller); | 290 CommitPendingLoad(&nav_controller); |
295 | 291 |
296 // Navigation successful | 292 // Navigation successful |
297 EXPECT_EQ(tiger_barb, web_contents->GetLastCommittedURL()); | 293 EXPECT_EQ(tiger_barb, web_contents->GetLastCommittedURL()); |
298 | 294 |
299 // Print preview should not have changed due to this navigation type so print | 295 // Print preview should not have changed due to this navigation type so print |
300 // preview now should return false, dialog is still alive, and the dialog | 296 // preview now should return false, dialog is still alive, and the dialog |
301 // returned by GetOrCreatePreviewDialog should be the same as the earlier | 297 // returned by GetOrCreatePreviewDialog should be the same as the earlier |
302 // dialog. | 298 // dialog. |
303 EXPECT_FALSE(manager->PrintPreviewNow(web_contents->GetMainFrame(), false)); | 299 EXPECT_FALSE(manager->PrintPreviewNow(false)); |
304 EXPECT_FALSE(tiger_2_destroyed.dialog_destroyed()); | 300 EXPECT_FALSE(tiger_2_destroyed.dialog_destroyed()); |
305 WebContents* tiger_preview_dialog_2b = | 301 WebContents* tiger_preview_dialog_2b = |
306 dialog_controller->GetOrCreatePreviewDialog(web_contents); | 302 dialog_controller->GetOrCreatePreviewDialog(web_contents); |
307 ASSERT_TRUE(tiger_preview_dialog_2b); | 303 ASSERT_TRUE(tiger_preview_dialog_2b); |
308 EXPECT_EQ(tiger_preview_dialog_2b, tiger_preview_dialog_2); | 304 EXPECT_EQ(tiger_preview_dialog_2b, tiger_preview_dialog_2); |
309 EXPECT_NE(tiger_preview_dialog_2b, web_contents); | 305 EXPECT_NE(tiger_preview_dialog_2b, web_contents); |
310 } | 306 } |
311 | 307 |
312 // Tests preview dialog controller cleans up correctly and does not throw errors | 308 // Tests preview dialog controller cleans up correctly and does not throw errors |
313 // on a renderer process crash. Checks that the renderer process closed | 309 // on a renderer process crash. Checks that the renderer process closed |
314 // notification is still received even if one of two preview dialogs with the | 310 // notification is still received even if one of two preview dialogs with the |
315 // same renderer process host is closed before the process "crashes". | 311 // same renderer process host is closed before the process "crashes". |
316 TEST_F(PrintPreviewDialogControllerUnitTest, MultiplePreviewDialogsClose) { | 312 TEST_F(PrintPreviewDialogControllerUnitTest, MultiplePreviewDialogsClose) { |
317 // Set up the browser. | 313 // Set up the browser. |
318 EXPECT_EQ(1u, chrome::GetTotalBrowserCount()); | 314 EXPECT_EQ(1u, chrome::GetTotalBrowserCount()); |
319 TabStripModel* tab_strip_model = browser()->tab_strip_model(); | 315 TabStripModel* tab_strip_model = browser()->tab_strip_model(); |
320 ASSERT_TRUE(tab_strip_model); | 316 ASSERT_TRUE(tab_strip_model); |
321 EXPECT_EQ(0, tab_strip_model->count()); | 317 EXPECT_EQ(0, tab_strip_model->count()); |
322 | 318 |
323 // Create a new tab with contents |web_contents_1| | 319 // Create a new tab with contents |web_contents_1| |
324 chrome::NewTab(browser()); | 320 chrome::NewTab(browser()); |
325 WebContents* web_contents_1 = tab_strip_model->GetActiveWebContents(); | 321 WebContents* web_contents_1 = tab_strip_model->GetActiveWebContents(); |
326 ASSERT_TRUE(web_contents_1); | 322 ASSERT_TRUE(web_contents_1); |
327 EXPECT_EQ(1, tab_strip_model->count()); | 323 EXPECT_EQ(1, tab_strip_model->count()); |
328 PrintPreviewDialogController* dialog_controller = | 324 PrintPreviewDialogController* dialog_controller = |
329 PrintPreviewDialogController::GetInstance(); | 325 PrintPreviewDialogController::GetInstance(); |
330 ASSERT_TRUE(dialog_controller); | 326 ASSERT_TRUE(dialog_controller); |
331 | 327 |
332 // Create preview dialog for |web_contents_1|. Should not create a new tab. | 328 // Create preview dialog for |web_contents_1|. Should not create a new tab. |
333 PrintViewManager::FromWebContents(web_contents_1) | 329 PrintViewManager::FromWebContents(web_contents_1)->PrintPreviewNow(false); |
334 ->PrintPreviewNow(web_contents_1->GetMainFrame(), false); | |
335 WebContents* preview_dialog_1 = | 330 WebContents* preview_dialog_1 = |
336 dialog_controller->GetOrCreatePreviewDialog(web_contents_1); | 331 dialog_controller->GetOrCreatePreviewDialog(web_contents_1); |
337 EXPECT_NE(web_contents_1, preview_dialog_1); | 332 EXPECT_NE(web_contents_1, preview_dialog_1); |
338 EXPECT_EQ(1, tab_strip_model->count()); | 333 EXPECT_EQ(1, tab_strip_model->count()); |
339 | 334 |
340 // Create a new tab with contents |web_contents_2| | 335 // Create a new tab with contents |web_contents_2| |
341 chrome::NewTab(browser()); | 336 chrome::NewTab(browser()); |
342 WebContents* web_contents_2 = tab_strip_model->GetActiveWebContents(); | 337 WebContents* web_contents_2 = tab_strip_model->GetActiveWebContents(); |
343 ASSERT_TRUE(web_contents_2); | 338 ASSERT_TRUE(web_contents_2); |
344 EXPECT_EQ(2, tab_strip_model->count()); | 339 EXPECT_EQ(2, tab_strip_model->count()); |
345 | 340 |
346 // Create preview dialog for |web_contents_2| | 341 // Create preview dialog for |web_contents_2| |
347 PrintViewManager::FromWebContents(web_contents_2) | 342 PrintViewManager::FromWebContents(web_contents_2)->PrintPreviewNow(false); |
348 ->PrintPreviewNow(web_contents_2->GetMainFrame(), false); | |
349 WebContents* preview_dialog_2 = | 343 WebContents* preview_dialog_2 = |
350 dialog_controller->GetOrCreatePreviewDialog(web_contents_2); | 344 dialog_controller->GetOrCreatePreviewDialog(web_contents_2); |
351 EXPECT_NE(web_contents_2, preview_dialog_2); | 345 EXPECT_NE(web_contents_2, preview_dialog_2); |
352 EXPECT_NE(preview_dialog_1, preview_dialog_2); | 346 EXPECT_NE(preview_dialog_1, preview_dialog_2); |
353 | 347 |
354 // 2 initiators and 2 preview dialogs exist in the same browser. The preview | 348 // 2 initiators and 2 preview dialogs exist in the same browser. The preview |
355 // dialogs are constrained in their respective initiators. | 349 // dialogs are constrained in their respective initiators. |
356 EXPECT_EQ(2, tab_strip_model->count()); | 350 EXPECT_EQ(2, tab_strip_model->count()); |
357 | 351 |
358 // Close |web_contents_1|'s tab | 352 // Close |web_contents_1|'s tab |
359 int tab_1_index = tab_strip_model->GetIndexOfWebContents(web_contents_1); | 353 int tab_1_index = tab_strip_model->GetIndexOfWebContents(web_contents_1); |
360 tab_strip_model->CloseWebContentsAt(tab_1_index, 0); | 354 tab_strip_model->CloseWebContentsAt(tab_1_index, 0); |
361 EXPECT_EQ(1, tab_strip_model->count()); | 355 EXPECT_EQ(1, tab_strip_model->count()); |
362 | 356 |
363 // Simulate a crash of the render process host for |web_contents_2|. Print | 357 // Simulate a crash of the render process host for |web_contents_2|. Print |
364 // preview controller should exit cleanly and not crash. | 358 // preview controller should exit cleanly and not crash. |
365 content::MockRenderProcessHost* rph = | 359 content::MockRenderProcessHost* rph = |
366 static_cast<content::MockRenderProcessHost*>( | 360 static_cast<content::MockRenderProcessHost*>( |
367 web_contents_2->GetRenderViewHost()->GetProcess()); | 361 web_contents_2->GetRenderViewHost()->GetProcess()); |
368 rph->SimulateCrash(); | 362 rph->SimulateCrash(); |
369 } | 363 } |
370 } // namespace printing | 364 } // namespace printing |
OLD | NEW |