Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Side by Side Diff: chrome/browser/ui/browser_browsertest.cc

Issue 2787393003: Fix races in tests waiting for beforeunload dialogs. (Closed)
Patch Set: rev Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/browser/unload_browsertest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 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/ui/browser.h" 5 #include "chrome/browser/ui/browser.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <memory> 10 #include <memory>
(...skipping 601 matching lines...) Expand 10 before | Expand all | Expand 10 after
612 EXPECT_FALSE(contents->GetRenderProcessHost()->IgnoreInputEvents()); 612 EXPECT_FALSE(contents->GetRenderProcessHost()->IgnoreInputEvents());
613 } 613 }
614 614
615 // Make sure that dialogs are closed after a renderer process dies, and that 615 // Make sure that dialogs are closed after a renderer process dies, and that
616 // subsequent navigations work. See http://crbug/com/343265. 616 // subsequent navigations work. See http://crbug/com/343265.
617 IN_PROC_BROWSER_TEST_F(BrowserTest, SadTabCancelsDialogs) { 617 IN_PROC_BROWSER_TEST_F(BrowserTest, SadTabCancelsDialogs) {
618 ASSERT_TRUE(embedded_test_server()->Start()); 618 ASSERT_TRUE(embedded_test_server()->Start());
619 host_resolver()->AddRule("www.example.com", "127.0.0.1"); 619 host_resolver()->AddRule("www.example.com", "127.0.0.1");
620 GURL beforeunload_url(embedded_test_server()->GetURL("/beforeunload.html")); 620 GURL beforeunload_url(embedded_test_server()->GetURL("/beforeunload.html"));
621 ui_test_utils::NavigateToURL(browser(), beforeunload_url); 621 ui_test_utils::NavigateToURL(browser(), beforeunload_url);
622 WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
623 // Disable the hang monitor, otherwise there will be a race between the
624 // beforeunload dialog and the beforeunload hang timer.
625 contents->GetMainFrame()->DisableBeforeUnloadHangMonitorForTesting();
622 626
623 // Start a navigation to trigger the beforeunload dialog. 627 // Start a navigation to trigger the beforeunload dialog.
624 WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
625 contents->GetMainFrame()->ExecuteJavaScriptForTests( 628 contents->GetMainFrame()->ExecuteJavaScriptForTests(
626 ASCIIToUTF16("window.location.href = 'about:blank'")); 629 ASCIIToUTF16("window.location.href = 'about:blank'"));
627 AppModalDialog* alert = ui_test_utils::WaitForAppModalDialog(); 630 AppModalDialog* alert = ui_test_utils::WaitForAppModalDialog();
628 EXPECT_TRUE(alert->IsValid()); 631 EXPECT_TRUE(alert->IsValid());
629 AppModalDialogQueue* dialog_queue = AppModalDialogQueue::GetInstance(); 632 AppModalDialogQueue* dialog_queue = AppModalDialogQueue::GetInstance();
630 EXPECT_TRUE(dialog_queue->HasActiveDialog()); 633 EXPECT_TRUE(dialog_queue->HasActiveDialog());
631 634
632 // Crash the renderer process and ensure the dialog is gone. 635 // Crash the renderer process and ensure the dialog is gone.
633 content::RenderProcessHost* child_process = contents->GetRenderProcessHost(); 636 content::RenderProcessHost* child_process = contents->GetRenderProcessHost();
634 content::RenderProcessHostWatcher crash_observer( 637 content::RenderProcessHostWatcher crash_observer(
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
698 EXPECT_FALSE(dialog_queue->HasActiveDialog()); 701 EXPECT_FALSE(dialog_queue->HasActiveDialog());
699 702
700 interstitial->DontProceed(); 703 interstitial->DontProceed();
701 } 704 }
702 705
703 // Test for crbug.com/22004. Reloading a page with a before unload handler and 706 // Test for crbug.com/22004. Reloading a page with a before unload handler and
704 // then canceling the dialog should not leave the throbber spinning. 707 // then canceling the dialog should not leave the throbber spinning.
705 IN_PROC_BROWSER_TEST_F(BrowserTest, ReloadThenCancelBeforeUnload) { 708 IN_PROC_BROWSER_TEST_F(BrowserTest, ReloadThenCancelBeforeUnload) {
706 GURL url(std::string("data:text/html,") + kBeforeUnloadHTML); 709 GURL url(std::string("data:text/html,") + kBeforeUnloadHTML);
707 ui_test_utils::NavigateToURL(browser(), url); 710 ui_test_utils::NavigateToURL(browser(), url);
711 WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
712 // Disable the hang monitor, otherwise there will be a race between the
713 // beforeunload dialog and the beforeunload hang timer.
714 contents->GetMainFrame()->DisableBeforeUnloadHangMonitorForTesting();
708 715
709 // Navigate to another page, but click cancel in the dialog. Make sure that 716 // Navigate to another page, but click cancel in the dialog. Make sure that
710 // the throbber stops spinning. 717 // the throbber stops spinning.
711 chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB); 718 chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB);
712 AppModalDialog* alert = ui_test_utils::WaitForAppModalDialog(); 719 AppModalDialog* alert = ui_test_utils::WaitForAppModalDialog();
713 720
714 FailedCommitWatcher watcher( 721 FailedCommitWatcher watcher(contents);
715 browser()->tab_strip_model()->GetActiveWebContents());
716 alert->CloseModalDialog(); 722 alert->CloseModalDialog();
717 if (content::IsBrowserSideNavigationEnabled()) 723 if (content::IsBrowserSideNavigationEnabled())
718 watcher.Wait(); 724 watcher.Wait();
719 EXPECT_FALSE( 725 EXPECT_FALSE(contents->IsLoading());
720 browser()->tab_strip_model()->GetActiveWebContents()->IsLoading());
721 726
722 // Clear the beforeunload handler so the test can easily exit. 727 // Clear the beforeunload handler so the test can easily exit.
723 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()-> 728 contents->GetMainFrame()->ExecuteJavaScriptForTests(
724 ExecuteJavaScriptForTests(ASCIIToUTF16("onbeforeunload=null;")); 729 ASCIIToUTF16("onbeforeunload=null;"));
725 } 730 }
726 731
727 class RedirectObserver : public content::WebContentsObserver { 732 class RedirectObserver : public content::WebContentsObserver {
728 public: 733 public:
729 explicit RedirectObserver(content::WebContents* web_contents) 734 explicit RedirectObserver(content::WebContents* web_contents)
730 : WebContentsObserver(web_contents), 735 : WebContentsObserver(web_contents),
731 transition_(ui::PageTransition::PAGE_TRANSITION_LINK) { 736 transition_(ui::PageTransition::PAGE_TRANSITION_LINK) {
732 } 737 }
733 738
734 void DidFinishNavigation( 739 void DidFinishNavigation(
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
820 825
821 // Temporarily replace ContentBrowserClient with one that will cause a 826 // Temporarily replace ContentBrowserClient with one that will cause a
822 // process swap on all redirects to HTTPS URLs. 827 // process swap on all redirects to HTTPS URLs.
823 TransferHttpsRedirectsContentBrowserClient new_client; 828 TransferHttpsRedirectsContentBrowserClient new_client;
824 content::ContentBrowserClient* old_client = 829 content::ContentBrowserClient* old_client =
825 SetBrowserClientForTesting(&new_client); 830 SetBrowserClientForTesting(&new_client);
826 831
827 // Navigate to a page with a beforeunload handler. 832 // Navigate to a page with a beforeunload handler.
828 GURL url(embedded_test_server()->GetURL("/beforeunload.html")); 833 GURL url(embedded_test_server()->GetURL("/beforeunload.html"));
829 ui_test_utils::NavigateToURL(browser(), url); 834 ui_test_utils::NavigateToURL(browser(), url);
835 WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
836 // Disable the hang monitor, otherwise there will be a race between the
837 // beforeunload dialog and the beforeunload hang timer.
838 contents->GetMainFrame()->DisableBeforeUnloadHangMonitorForTesting();
Nico 2017/04/05 19:26:00 Shouldn't you do this before the navigation to the
830 839
831 // Navigate to a URL that redirects to another process and approve the 840 // Navigate to a URL that redirects to another process and approve the
832 // beforeunload dialog that pops up. 841 // beforeunload dialog that pops up.
833 content::WindowedNotificationObserver nav_observer( 842 content::WindowedNotificationObserver nav_observer(
834 content::NOTIFICATION_NAV_ENTRY_COMMITTED, 843 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
835 content::NotificationService::AllSources()); 844 content::NotificationService::AllSources());
836 GURL https_url(https_test_server.GetURL("/title1.html")); 845 GURL https_url(https_test_server.GetURL("/title1.html"));
837 GURL redirect_url( 846 GURL redirect_url(
838 embedded_test_server()->GetURL("/server-redirect?" + https_url.spec())); 847 embedded_test_server()->GetURL("/server-redirect?" + https_url.spec()));
839 browser()->OpenURL(OpenURLParams(redirect_url, Referrer(), 848 browser()->OpenURL(OpenURLParams(redirect_url, Referrer(),
840 WindowOpenDisposition::CURRENT_TAB, 849 WindowOpenDisposition::CURRENT_TAB,
841 ui::PAGE_TRANSITION_TYPED, false)); 850 ui::PAGE_TRANSITION_TYPED, false));
842 AppModalDialog* alert = ui_test_utils::WaitForAppModalDialog(); 851 AppModalDialog* alert = ui_test_utils::WaitForAppModalDialog();
843 EXPECT_TRUE( 852 EXPECT_TRUE(
844 static_cast<JavaScriptAppModalDialog*>(alert)->is_before_unload_dialog()); 853 static_cast<JavaScriptAppModalDialog*>(alert)->is_before_unload_dialog());
845 alert->native_dialog()->AcceptAppModalDialog(); 854 alert->native_dialog()->AcceptAppModalDialog();
846 nav_observer.Wait(); 855 nav_observer.Wait();
847 856
848 // Restore previous browser client. 857 // Restore previous browser client.
849 SetBrowserClientForTesting(old_client); 858 SetBrowserClientForTesting(old_client);
850 } 859 }
851 860
852 // Test for crbug.com/80401. Canceling a before unload dialog should reset 861 // Test for crbug.com/80401. Canceling a before unload dialog should reset
853 // the URL to the previous page's URL. 862 // the URL to the previous page's URL.
854 IN_PROC_BROWSER_TEST_F(BrowserTest, CancelBeforeUnloadResetsURL) { 863 IN_PROC_BROWSER_TEST_F(BrowserTest, CancelBeforeUnloadResetsURL) {
855 GURL url(ui_test_utils::GetTestUrl(base::FilePath( 864 GURL url(ui_test_utils::GetTestUrl(base::FilePath(
856 base::FilePath::kCurrentDirectory), base::FilePath(kBeforeUnloadFile))); 865 base::FilePath::kCurrentDirectory), base::FilePath(kBeforeUnloadFile)));
857 ui_test_utils::NavigateToURL(browser(), url); 866 ui_test_utils::NavigateToURL(browser(), url);
867 WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
868 // Disable the hang monitor, otherwise there will be a race between the
869 // beforeunload dialog and the beforeunload hang timer.
870 contents->GetMainFrame()->DisableBeforeUnloadHangMonitorForTesting();
858 871
859 // Navigate to a page that triggers a cross-site transition. 872 // Navigate to a page that triggers a cross-site transition.
860 ASSERT_TRUE(embedded_test_server()->Start()); 873 ASSERT_TRUE(embedded_test_server()->Start());
861 GURL url2(embedded_test_server()->GetURL("/title1.html")); 874 GURL url2(embedded_test_server()->GetURL("/title1.html"));
862 browser()->OpenURL(OpenURLParams(url2, Referrer(), 875 browser()->OpenURL(OpenURLParams(url2, Referrer(),
863 WindowOpenDisposition::CURRENT_TAB, 876 WindowOpenDisposition::CURRENT_TAB,
864 ui::PAGE_TRANSITION_TYPED, false)); 877 ui::PAGE_TRANSITION_TYPED, false));
865 878
866 content::WindowedNotificationObserver host_destroyed_observer( 879 content::WindowedNotificationObserver host_destroyed_observer(
867 content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED, 880 content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED,
868 content::NotificationService::AllSources()); 881 content::NotificationService::AllSources());
869 882
870 // Cancel the dialog. 883 // Cancel the dialog.
871 AppModalDialog* alert = ui_test_utils::WaitForAppModalDialog(); 884 AppModalDialog* alert = ui_test_utils::WaitForAppModalDialog();
872 FailedCommitWatcher watcher( 885 FailedCommitWatcher watcher(contents);
873 browser()->tab_strip_model()->GetActiveWebContents());
874 alert->CloseModalDialog(); 886 alert->CloseModalDialog();
875 if (content::IsBrowserSideNavigationEnabled()) 887 if (content::IsBrowserSideNavigationEnabled())
876 watcher.Wait(); 888 watcher.Wait();
877 EXPECT_FALSE( 889 EXPECT_FALSE(contents->IsLoading());
878 browser()->tab_strip_model()->GetActiveWebContents()->IsLoading());
879 890
880 // Verify there are no pending history items after the dialog is cancelled. 891 // Verify there are no pending history items after the dialog is cancelled.
881 // (see crbug.com/93858) 892 // (see crbug.com/93858)
882 NavigationEntry* entry = browser()->tab_strip_model()-> 893 NavigationEntry* entry = contents->GetController().GetPendingEntry();
883 GetActiveWebContents()->GetController().GetPendingEntry();
884 EXPECT_EQ(NULL, entry); 894 EXPECT_EQ(NULL, entry);
885 895
886 // Wait for the ShouldClose_ACK to arrive. We can detect it by waiting for 896 // Wait for the ShouldClose_ACK to arrive. We can detect it by waiting for
887 // the pending RVH to be destroyed. 897 // the pending RVH to be destroyed.
888 host_destroyed_observer.Wait(); 898 host_destroyed_observer.Wait();
889 EXPECT_EQ(url, browser()->toolbar_model()->GetURL()); 899 EXPECT_EQ(url, browser()->toolbar_model()->GetURL());
890 900
891 // Clear the beforeunload handler so the test can easily exit. 901 // Clear the beforeunload handler so the test can easily exit.
892 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()-> 902 contents->GetMainFrame()->ExecuteJavaScriptForTests(
893 ExecuteJavaScriptForTests(ASCIIToUTF16("onbeforeunload=null;")); 903 ASCIIToUTF16("onbeforeunload=null;"));
894 } 904 }
895 905
896 // Test for crbug.com/11647. A page closed with window.close() should not have 906 // Test for crbug.com/11647. A page closed with window.close() should not have
897 // two beforeunload dialogs shown. 907 // two beforeunload dialogs shown.
898 // http://crbug.com/410891 908 // http://crbug.com/410891
899 IN_PROC_BROWSER_TEST_F(BrowserTest, 909 IN_PROC_BROWSER_TEST_F(BrowserTest,
900 DISABLED_SingleBeforeUnloadAfterWindowClose) { 910 DISABLED_SingleBeforeUnloadAfterWindowClose) {
901 browser() 911 browser()
902 ->tab_strip_model() 912 ->tab_strip_model()
903 ->GetActiveWebContents() 913 ->GetActiveWebContents()
(...skipping 12 matching lines...) Expand all
916 ASCIIToUTF16("w.close(); alert('bar');")); 926 ASCIIToUTF16("w.close(); alert('bar');"));
917 AppModalDialog* alert = ui_test_utils::WaitForAppModalDialog(); 927 AppModalDialog* alert = ui_test_utils::WaitForAppModalDialog();
918 alert->native_dialog()->AcceptAppModalDialog(); 928 alert->native_dialog()->AcceptAppModalDialog();
919 929
920 alert = ui_test_utils::WaitForAppModalDialog(); 930 alert = ui_test_utils::WaitForAppModalDialog();
921 EXPECT_FALSE(static_cast<JavaScriptAppModalDialog*>(alert)-> 931 EXPECT_FALSE(static_cast<JavaScriptAppModalDialog*>(alert)->
922 is_before_unload_dialog()); 932 is_before_unload_dialog());
923 alert->native_dialog()->AcceptAppModalDialog(); 933 alert->native_dialog()->AcceptAppModalDialog();
924 } 934 }
925 935
926 // BrowserTest.BeforeUnloadVsBeforeReload times out on Windows.
927 // http://crbug.com/130411
928 #if defined(OS_WIN)
929 #define MAYBE_BeforeUnloadVsBeforeReload DISABLED_BeforeUnloadVsBeforeReload
930 #else
931 #define MAYBE_BeforeUnloadVsBeforeReload BeforeUnloadVsBeforeReload
932 #endif
933
934 // Test that when a page has an onbeforeunload handler, reloading a page shows a 936 // Test that when a page has an onbeforeunload handler, reloading a page shows a
935 // different dialog than navigating to a different page. 937 // different dialog than navigating to a different page.
936 IN_PROC_BROWSER_TEST_F(BrowserTest, MAYBE_BeforeUnloadVsBeforeReload) { 938 IN_PROC_BROWSER_TEST_F(BrowserTest, BeforeUnloadVsBeforeReload) {
937 GURL url(std::string("data:text/html,") + kBeforeUnloadHTML); 939 GURL url(std::string("data:text/html,") + kBeforeUnloadHTML);
938 ui_test_utils::NavigateToURL(browser(), url); 940 ui_test_utils::NavigateToURL(browser(), url);
941 WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
942 // Disable the hang monitor, otherwise there will be a race between the
943 // beforeunload dialog and the beforeunload hang timer.
944 contents->GetMainFrame()->DisableBeforeUnloadHangMonitorForTesting();
939 945
940 // Reload the page, and check that we get a "before reload" dialog. 946 // Reload the page, and check that we get a "before reload" dialog.
941 chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB); 947 chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB);
942 AppModalDialog* alert = ui_test_utils::WaitForAppModalDialog(); 948 AppModalDialog* alert = ui_test_utils::WaitForAppModalDialog();
943 EXPECT_TRUE(static_cast<JavaScriptAppModalDialog*>(alert)->is_reload()); 949 EXPECT_TRUE(static_cast<JavaScriptAppModalDialog*>(alert)->is_reload());
944 950
945 // Cancel the reload. 951 // Cancel the reload.
946 FailedCommitWatcher watcher( 952 FailedCommitWatcher watcher(contents);
947 browser()->tab_strip_model()->GetActiveWebContents());
948 alert->native_dialog()->CancelAppModalDialog(); 953 alert->native_dialog()->CancelAppModalDialog();
949 if (content::IsBrowserSideNavigationEnabled()) 954 if (content::IsBrowserSideNavigationEnabled())
950 watcher.Wait(); 955 watcher.Wait();
951 956
952 // Navigate to another url, and check that we get a "before unload" dialog. 957 // Navigate to another url, and check that we get a "before unload" dialog.
953 GURL url2(url::kAboutBlankURL); 958 GURL url2(url::kAboutBlankURL);
954 browser()->OpenURL(OpenURLParams(url2, Referrer(), 959 browser()->OpenURL(OpenURLParams(url2, Referrer(),
955 WindowOpenDisposition::CURRENT_TAB, 960 WindowOpenDisposition::CURRENT_TAB,
956 ui::PAGE_TRANSITION_TYPED, false)); 961 ui::PAGE_TRANSITION_TYPED, false));
957 962
(...skipping 2037 matching lines...) Expand 10 before | Expand all | Expand 10 after
2995 Browser* browser = new Browser(params); 3000 Browser* browser = new Browser(params);
2996 gfx::Rect bounds = browser->window()->GetBounds(); 3001 gfx::Rect bounds = browser->window()->GetBounds();
2997 3002
2998 // Should be EXPECT_EQ, but this width is inconsistent across platforms. 3003 // Should be EXPECT_EQ, but this width is inconsistent across platforms.
2999 // See https://crbug.com/567925. 3004 // See https://crbug.com/567925.
3000 EXPECT_GE(bounds.width(), 100); 3005 EXPECT_GE(bounds.width(), 100);
3001 EXPECT_EQ(122, bounds.height()); 3006 EXPECT_EQ(122, bounds.height());
3002 browser->window()->Close(); 3007 browser->window()->Close();
3003 } 3008 }
3004 } 3009 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/unload_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698