| 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 #if defined(OS_POSIX) | 5 #if defined(OS_POSIX) |
| 6 #include <signal.h> | 6 #include <signal.h> |
| 7 #endif | 7 #endif |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 #include "chrome/browser/chrome_notification_types.h" | 14 #include "chrome/browser/chrome_notification_types.h" |
| 15 #include "chrome/browser/net/url_request_mock_util.h" | 15 #include "chrome/browser/net/url_request_mock_util.h" |
| 16 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
| 17 #include "chrome/browser/ui/browser_commands.h" | 17 #include "chrome/browser/ui/browser_commands.h" |
| 18 #include "chrome/browser/ui/browser_list.h" | 18 #include "chrome/browser/ui/browser_list.h" |
| 19 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 19 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 20 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
| 21 #include "chrome/test/base/in_process_browser_test.h" | 21 #include "chrome/test/base/in_process_browser_test.h" |
| 22 #include "chrome/test/base/ui_test_utils.h" | 22 #include "chrome/test/base/ui_test_utils.h" |
| 23 #include "components/app_modal/javascript_app_modal_dialog.h" | 23 #include "components/app_modal/javascript_app_modal_dialog.h" |
| 24 #include "components/app_modal/native_app_modal_dialog.h" | 24 #include "components/app_modal/native_app_modal_dialog.h" |
| 25 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
| 26 #include "content/public/browser/notification_service.h" | 26 #include "content/public/browser/notification_service.h" |
| 27 #include "content/public/browser/render_frame_host.h" |
| 27 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
| 28 #include "content/public/common/content_switches.h" | 29 #include "content/public/common/content_switches.h" |
| 29 #include "content/public/test/browser_test_utils.h" | 30 #include "content/public/test/browser_test_utils.h" |
| 30 #include "net/test/embedded_test_server/embedded_test_server.h" | 31 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 31 #include "net/test/url_request/url_request_mock_http_job.h" | 32 #include "net/test/url_request/url_request_mock_http_job.h" |
| 32 #include "net/url_request/url_request_test_util.h" | 33 #include "net/url_request/url_request_test_util.h" |
| 33 | 34 |
| 34 #if defined(OS_WIN) | 35 #if defined(OS_WIN) |
| 35 // For version specific disabled tests below (http://crbug.com/267597). | 36 // For version specific disabled tests below (http://crbug.com/267597). |
| 36 #include "base/win/windows_version.h" | 37 #include "base/win/windows_version.h" |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 // Tests closing the browser on a page with an unload listener registered. | 272 // Tests closing the browser on a page with an unload listener registered. |
| 272 // Test marked as flaky in http://crbug.com/51698 | 273 // Test marked as flaky in http://crbug.com/51698 |
| 273 IN_PROC_BROWSER_TEST_F(UnloadTest, DISABLED_BrowserCloseUnload) { | 274 IN_PROC_BROWSER_TEST_F(UnloadTest, DISABLED_BrowserCloseUnload) { |
| 274 LoadUrlAndQuitBrowser(UNLOAD_HTML, "unload"); | 275 LoadUrlAndQuitBrowser(UNLOAD_HTML, "unload"); |
| 275 } | 276 } |
| 276 | 277 |
| 277 // Tests closing the browser with a beforeunload handler and clicking | 278 // Tests closing the browser with a beforeunload handler and clicking |
| 278 // OK in the beforeunload confirm dialog. | 279 // OK in the beforeunload confirm dialog. |
| 279 IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserCloseBeforeUnloadOK) { | 280 IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserCloseBeforeUnloadOK) { |
| 280 NavigateToDataURL(BEFORE_UNLOAD_HTML, "beforeunload"); | 281 NavigateToDataURL(BEFORE_UNLOAD_HTML, "beforeunload"); |
| 282 // Disable the hang monitor, otherwise there will be a race between the |
| 283 // beforeunload dialog and the beforeunload hang timer. |
| 284 browser() |
| 285 ->tab_strip_model() |
| 286 ->GetActiveWebContents() |
| 287 ->GetMainFrame() |
| 288 ->DisableBeforeUnloadHangMonitorForTesting(); |
| 281 | 289 |
| 282 content::WindowedNotificationObserver window_observer( | 290 content::WindowedNotificationObserver window_observer( |
| 283 chrome::NOTIFICATION_BROWSER_CLOSED, | 291 chrome::NOTIFICATION_BROWSER_CLOSED, |
| 284 content::NotificationService::AllSources()); | 292 content::NotificationService::AllSources()); |
| 285 chrome::CloseWindow(browser()); | 293 chrome::CloseWindow(browser()); |
| 286 ClickModalDialogButton(true); | 294 ClickModalDialogButton(true); |
| 287 window_observer.Wait(); | 295 window_observer.Wait(); |
| 288 } | 296 } |
| 289 | 297 |
| 290 // Tests closing the browser with a beforeunload handler and clicking | 298 // Tests closing the browser with a beforeunload handler and clicking |
| 291 // CANCEL in the beforeunload confirm dialog. | 299 // CANCEL in the beforeunload confirm dialog. |
| 292 // If this test flakes, reopen http://crbug.com/123110 | 300 // If this test flakes, reopen http://crbug.com/123110 |
| 293 IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserCloseBeforeUnloadCancel) { | 301 IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserCloseBeforeUnloadCancel) { |
| 294 NavigateToDataURL(BEFORE_UNLOAD_HTML, "beforeunload"); | 302 NavigateToDataURL(BEFORE_UNLOAD_HTML, "beforeunload"); |
| 303 // Disable the hang monitor, otherwise there will be a race between the |
| 304 // beforeunload dialog and the beforeunload hang timer. |
| 305 browser() |
| 306 ->tab_strip_model() |
| 307 ->GetActiveWebContents() |
| 308 ->GetMainFrame() |
| 309 ->DisableBeforeUnloadHangMonitorForTesting(); |
| 310 |
| 295 chrome::CloseWindow(browser()); | 311 chrome::CloseWindow(browser()); |
| 296 | 312 |
| 297 // We wait for the title to change after cancelling the closure of browser | 313 // We wait for the title to change after cancelling the closure of browser |
| 298 // window, to ensure that in-flight IPCs from the renderer reach the browser. | 314 // window, to ensure that in-flight IPCs from the renderer reach the browser. |
| 299 // Otherwise the browser won't put up the beforeunload dialog because it's | 315 // Otherwise the browser won't put up the beforeunload dialog because it's |
| 300 // waiting for an ack from the renderer. | 316 // waiting for an ack from the renderer. |
| 301 base::string16 expected_title = base::ASCIIToUTF16("cancelled"); | 317 base::string16 expected_title = base::ASCIIToUTF16("cancelled"); |
| 302 content::TitleWatcher title_watcher( | 318 content::TitleWatcher title_watcher( |
| 303 browser()->tab_strip_model()->GetActiveWebContents(), expected_title); | 319 browser()->tab_strip_model()->GetActiveWebContents(), expected_title); |
| 304 ClickModalDialogButton(false); | 320 ClickModalDialogButton(false); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 330 false); | 346 false); |
| 331 window_observer.Wait(); | 347 window_observer.Wait(); |
| 332 EXPECT_EQ(1, unload_results.get_successes()); | 348 EXPECT_EQ(1, unload_results.get_successes()); |
| 333 EXPECT_EQ(0, unload_results.get_aborts()); | 349 EXPECT_EQ(0, unload_results.get_aborts()); |
| 334 } | 350 } |
| 335 | 351 |
| 336 // Tests closing the browser by BrowserList::CloseAllBrowsersWithProfile, with a | 352 // Tests closing the browser by BrowserList::CloseAllBrowsersWithProfile, with a |
| 337 // beforeunload handler and clicking Leave in the beforeunload confirm dialog. | 353 // beforeunload handler and clicking Leave in the beforeunload confirm dialog. |
| 338 IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserListCloseBeforeUnloadOK) { | 354 IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserListCloseBeforeUnloadOK) { |
| 339 NavigateToDataURL(BEFORE_UNLOAD_HTML, "beforeunload"); | 355 NavigateToDataURL(BEFORE_UNLOAD_HTML, "beforeunload"); |
| 356 // Disable the hang monitor, otherwise there will be a race between the |
| 357 // beforeunload dialog and the beforeunload hang timer. |
| 358 browser() |
| 359 ->tab_strip_model() |
| 360 ->GetActiveWebContents() |
| 361 ->GetMainFrame() |
| 362 ->DisableBeforeUnloadHangMonitorForTesting(); |
| 340 | 363 |
| 341 content::WindowedNotificationObserver window_observer( | 364 content::WindowedNotificationObserver window_observer( |
| 342 chrome::NOTIFICATION_BROWSER_CLOSED, | 365 chrome::NOTIFICATION_BROWSER_CLOSED, |
| 343 content::NotificationService::AllSources()); | 366 content::NotificationService::AllSources()); |
| 344 UnloadResults unload_results; | 367 UnloadResults unload_results; |
| 345 BrowserList::CloseAllBrowsersWithProfile( | 368 BrowserList::CloseAllBrowsersWithProfile( |
| 346 browser()->profile(), | 369 browser()->profile(), |
| 347 base::Bind(&UnloadResults::AddSuccess, base::Unretained(&unload_results)), | 370 base::Bind(&UnloadResults::AddSuccess, base::Unretained(&unload_results)), |
| 348 base::Bind(&UnloadResults::AddAbort, base::Unretained(&unload_results)), | 371 base::Bind(&UnloadResults::AddAbort, base::Unretained(&unload_results)), |
| 349 false); | 372 false); |
| 350 ClickModalDialogButton(true); | 373 ClickModalDialogButton(true); |
| 351 window_observer.Wait(); | 374 window_observer.Wait(); |
| 352 EXPECT_EQ(1, unload_results.get_successes()); | 375 EXPECT_EQ(1, unload_results.get_successes()); |
| 353 EXPECT_EQ(0, unload_results.get_aborts()); | 376 EXPECT_EQ(0, unload_results.get_aborts()); |
| 354 } | 377 } |
| 355 | 378 |
| 356 // Tests closing the browser by BrowserList::CloseAllBrowsersWithProfile, with a | 379 // Tests closing the browser by BrowserList::CloseAllBrowsersWithProfile, with a |
| 357 // beforeunload handler and clicking Stay in the beforeunload confirm dialog. | 380 // beforeunload handler and clicking Stay in the beforeunload confirm dialog. |
| 358 IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserListCloseBeforeUnloadCancel) { | 381 IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserListCloseBeforeUnloadCancel) { |
| 359 NavigateToDataURL(BEFORE_UNLOAD_HTML, "beforeunload"); | 382 NavigateToDataURL(BEFORE_UNLOAD_HTML, "beforeunload"); |
| 383 // Disable the hang monitor, otherwise there will be a race between the |
| 384 // beforeunload dialog and the beforeunload hang timer. |
| 385 browser() |
| 386 ->tab_strip_model() |
| 387 ->GetActiveWebContents() |
| 388 ->GetMainFrame() |
| 389 ->DisableBeforeUnloadHangMonitorForTesting(); |
| 360 | 390 |
| 361 UnloadResults unload_results; | 391 UnloadResults unload_results; |
| 362 BrowserList::CloseAllBrowsersWithProfile( | 392 BrowserList::CloseAllBrowsersWithProfile( |
| 363 browser()->profile(), | 393 browser()->profile(), |
| 364 base::Bind(&UnloadResults::AddSuccess, base::Unretained(&unload_results)), | 394 base::Bind(&UnloadResults::AddSuccess, base::Unretained(&unload_results)), |
| 365 base::Bind(&UnloadResults::AddAbort, base::Unretained(&unload_results)), | 395 base::Bind(&UnloadResults::AddAbort, base::Unretained(&unload_results)), |
| 366 false); | 396 false); |
| 367 | 397 |
| 368 // We wait for the title to change after cancelling the closure of browser | 398 // We wait for the title to change after cancelling the closure of browser |
| 369 // window, to ensure that in-flight IPCs from the renderer reach the browser. | 399 // window, to ensure that in-flight IPCs from the renderer reach the browser. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 385 content::NotificationService::AllSources()); | 415 content::NotificationService::AllSources()); |
| 386 chrome::CloseWindow(browser()); | 416 chrome::CloseWindow(browser()); |
| 387 ClickModalDialogButton(true); | 417 ClickModalDialogButton(true); |
| 388 window_observer.Wait(); | 418 window_observer.Wait(); |
| 389 } | 419 } |
| 390 | 420 |
| 391 // Tests double calls to BrowserList::CloseAllBrowsersWithProfile, with a | 421 // Tests double calls to BrowserList::CloseAllBrowsersWithProfile, with a |
| 392 // beforeunload handler and clicking Leave in the beforeunload confirm dialog. | 422 // beforeunload handler and clicking Leave in the beforeunload confirm dialog. |
| 393 IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserListDoubleCloseBeforeUnloadOK) { | 423 IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserListDoubleCloseBeforeUnloadOK) { |
| 394 NavigateToDataURL(BEFORE_UNLOAD_HTML, "beforeunload"); | 424 NavigateToDataURL(BEFORE_UNLOAD_HTML, "beforeunload"); |
| 425 // Disable the hang monitor, otherwise there will be a race between the |
| 426 // beforeunload dialog and the beforeunload hang timer. |
| 427 browser() |
| 428 ->tab_strip_model() |
| 429 ->GetActiveWebContents() |
| 430 ->GetMainFrame() |
| 431 ->DisableBeforeUnloadHangMonitorForTesting(); |
| 395 | 432 |
| 396 content::WindowedNotificationObserver window_observer( | 433 content::WindowedNotificationObserver window_observer( |
| 397 chrome::NOTIFICATION_BROWSER_CLOSED, | 434 chrome::NOTIFICATION_BROWSER_CLOSED, |
| 398 content::NotificationService::AllSources()); | 435 content::NotificationService::AllSources()); |
| 399 UnloadResults unload_results; | 436 UnloadResults unload_results; |
| 400 BrowserList::CloseAllBrowsersWithProfile( | 437 BrowserList::CloseAllBrowsersWithProfile( |
| 401 browser()->profile(), | 438 browser()->profile(), |
| 402 base::Bind(&UnloadResults::AddSuccess, base::Unretained(&unload_results)), | 439 base::Bind(&UnloadResults::AddSuccess, base::Unretained(&unload_results)), |
| 403 base::Bind(&UnloadResults::AddAbort, base::Unretained(&unload_results)), | 440 base::Bind(&UnloadResults::AddAbort, base::Unretained(&unload_results)), |
| 404 false); | 441 false); |
| 405 BrowserList::CloseAllBrowsersWithProfile( | 442 BrowserList::CloseAllBrowsersWithProfile( |
| 406 browser()->profile(), | 443 browser()->profile(), |
| 407 base::Bind(&UnloadResults::AddSuccess, base::Unretained(&unload_results)), | 444 base::Bind(&UnloadResults::AddSuccess, base::Unretained(&unload_results)), |
| 408 base::Bind(&UnloadResults::AddAbort, base::Unretained(&unload_results)), | 445 base::Bind(&UnloadResults::AddAbort, base::Unretained(&unload_results)), |
| 409 false); | 446 false); |
| 410 ClickModalDialogButton(true); | 447 ClickModalDialogButton(true); |
| 411 window_observer.Wait(); | 448 window_observer.Wait(); |
| 412 EXPECT_EQ(1, unload_results.get_successes()); | 449 EXPECT_EQ(1, unload_results.get_successes()); |
| 413 EXPECT_EQ(0, unload_results.get_aborts()); | 450 EXPECT_EQ(0, unload_results.get_aborts()); |
| 414 } | 451 } |
| 415 | 452 |
| 416 // Tests double calls to BrowserList::CloseAllBrowsersWithProfile, with a | 453 // Tests double calls to BrowserList::CloseAllBrowsersWithProfile, with a |
| 417 // beforeunload handler and clicking Stay in the beforeunload confirm dialog. | 454 // beforeunload handler and clicking Stay in the beforeunload confirm dialog. |
| 418 IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserListDoubleCloseBeforeUnloadCancel) { | 455 IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserListDoubleCloseBeforeUnloadCancel) { |
| 419 NavigateToDataURL(BEFORE_UNLOAD_HTML, "beforeunload"); | 456 NavigateToDataURL(BEFORE_UNLOAD_HTML, "beforeunload"); |
| 457 // Disable the hang monitor, otherwise there will be a race between the |
| 458 // beforeunload dialog and the beforeunload hang timer. |
| 459 browser() |
| 460 ->tab_strip_model() |
| 461 ->GetActiveWebContents() |
| 462 ->GetMainFrame() |
| 463 ->DisableBeforeUnloadHangMonitorForTesting(); |
| 420 | 464 |
| 421 UnloadResults unload_results; | 465 UnloadResults unload_results; |
| 422 BrowserList::CloseAllBrowsersWithProfile( | 466 BrowserList::CloseAllBrowsersWithProfile( |
| 423 browser()->profile(), | 467 browser()->profile(), |
| 424 base::Bind(&UnloadResults::AddSuccess, base::Unretained(&unload_results)), | 468 base::Bind(&UnloadResults::AddSuccess, base::Unretained(&unload_results)), |
| 425 base::Bind(&UnloadResults::AddAbort, base::Unretained(&unload_results)), | 469 base::Bind(&UnloadResults::AddAbort, base::Unretained(&unload_results)), |
| 426 false); | 470 false); |
| 427 BrowserList::CloseAllBrowsersWithProfile( | 471 BrowserList::CloseAllBrowsersWithProfile( |
| 428 browser()->profile(), | 472 browser()->profile(), |
| 429 base::Bind(&UnloadResults::AddSuccess, base::Unretained(&unload_results)), | 473 base::Bind(&UnloadResults::AddSuccess, base::Unretained(&unload_results)), |
| (...skipping 30 matching lines...) Expand all Loading... |
| 460 NavigateToDataURL(BEFORE_UNLOAD_HTML, "beforeunload"); | 504 NavigateToDataURL(BEFORE_UNLOAD_HTML, "beforeunload"); |
| 461 EXPECT_EQ(kill(base::GetCurrentProcessHandle(), SIGTERM), 0); | 505 EXPECT_EQ(kill(base::GetCurrentProcessHandle(), SIGTERM), 0); |
| 462 } | 506 } |
| 463 #endif | 507 #endif |
| 464 | 508 |
| 465 // Tests closing the browser and clicking OK in the beforeunload confirm dialog | 509 // Tests closing the browser and clicking OK in the beforeunload confirm dialog |
| 466 // if an inner frame has the focus. | 510 // if an inner frame has the focus. |
| 467 // If this flakes, use http://crbug.com/32615 and http://crbug.com/45675 | 511 // If this flakes, use http://crbug.com/32615 and http://crbug.com/45675 |
| 468 IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserCloseWithInnerFocusedFrame) { | 512 IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserCloseWithInnerFocusedFrame) { |
| 469 NavigateToDataURL(INNER_FRAME_WITH_FOCUS_HTML, "innerframewithfocus"); | 513 NavigateToDataURL(INNER_FRAME_WITH_FOCUS_HTML, "innerframewithfocus"); |
| 514 // Disable the hang monitor, otherwise there will be a race between the |
| 515 // beforeunload dialog and the beforeunload hang timer. |
| 516 browser() |
| 517 ->tab_strip_model() |
| 518 ->GetActiveWebContents() |
| 519 ->GetMainFrame() |
| 520 ->DisableBeforeUnloadHangMonitorForTesting(); |
| 470 | 521 |
| 471 content::WindowedNotificationObserver window_observer( | 522 content::WindowedNotificationObserver window_observer( |
| 472 chrome::NOTIFICATION_BROWSER_CLOSED, | 523 chrome::NOTIFICATION_BROWSER_CLOSED, |
| 473 content::NotificationService::AllSources()); | 524 content::NotificationService::AllSources()); |
| 474 chrome::CloseWindow(browser()); | 525 chrome::CloseWindow(browser()); |
| 475 ClickModalDialogButton(true); | 526 ClickModalDialogButton(true); |
| 476 window_observer.Wait(); | 527 window_observer.Wait(); |
| 477 } | 528 } |
| 478 | 529 |
| 479 // Tests closing the browser with a beforeunload handler that takes forever | 530 // Tests closing the browser with a beforeunload handler that takes forever |
| (...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 906 base::Bind(&UnloadResults::AddSuccess, base::Unretained(&unload_results)), | 957 base::Bind(&UnloadResults::AddSuccess, base::Unretained(&unload_results)), |
| 907 base::Bind(&UnloadResults::AddAbort, base::Unretained(&unload_results)), | 958 base::Bind(&UnloadResults::AddAbort, base::Unretained(&unload_results)), |
| 908 true); | 959 true); |
| 909 window_observer.Wait(); | 960 window_observer.Wait(); |
| 910 EXPECT_EQ(1, unload_results.get_successes()); | 961 EXPECT_EQ(1, unload_results.get_successes()); |
| 911 EXPECT_EQ(0, unload_results.get_aborts()); | 962 EXPECT_EQ(0, unload_results.get_aborts()); |
| 912 } | 963 } |
| 913 | 964 |
| 914 // TODO(ojan): Add tests for unload/beforeunload that have multiple tabs | 965 // TODO(ojan): Add tests for unload/beforeunload that have multiple tabs |
| 915 // and multiple windows. | 966 // and multiple windows. |
| OLD | NEW |