Index: chrome/browser/unload_browsertest.cc |
diff --git a/chrome/browser/unload_browsertest.cc b/chrome/browser/unload_browsertest.cc |
index 7aaeb067adde895d71ad57071776ea0daf26cd35..8b1e42458cd00135f034f393e86914bf1be0079b 100644 |
--- a/chrome/browser/unload_browsertest.cc |
+++ b/chrome/browser/unload_browsertest.cc |
@@ -204,6 +204,24 @@ class UnloadTest : public InProcessBrowserTest { |
else |
js_dialog->native_dialog()->CancelAppModalDialog(); |
} |
+ |
+ // Prepares a WebContents or Browser for a beforeunload dialog. |
+ // |
+ // This comprises two things. First, it makes sure that the beforeunload hang |
+ // monitor will not trigger, otherwise timeout might prevent the dialog from |
+ // appearing. See https://crbug.com/519646 . |
+ // |
+ // Second, beforeunload dialogs require a user gesture, so it makes one. |
+ void PrepareForDialog(content::WebContents* web_contents) { |
+ web_contents->GetMainFrame()->DisableBeforeUnloadHangMonitorForTesting(); |
+ for (auto* frame : web_contents->GetAllFrames()) |
+ frame->ExecuteJavaScriptWithUserGestureForTests(base::string16()); |
+ } |
+ |
+ void PrepareForDialog(Browser* browser) { |
+ for (int i = 0; i < browser->tab_strip_model()->count(); i++) |
+ PrepareForDialog(browser->tab_strip_model()->GetWebContentsAt(i)); |
+ } |
}; |
// Navigate to a page with an infinite unload handler. |
@@ -282,13 +300,7 @@ IN_PROC_BROWSER_TEST_F(UnloadTest, DISABLED_BrowserCloseUnload) { |
// OK in the beforeunload confirm dialog. |
IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserCloseBeforeUnloadOK) { |
NavigateToDataURL(BEFORE_UNLOAD_HTML, "beforeunload"); |
- // Disable the hang monitor, otherwise there will be a race between the |
- // beforeunload dialog and the beforeunload hang timer. |
- browser() |
- ->tab_strip_model() |
- ->GetActiveWebContents() |
- ->GetMainFrame() |
- ->DisableBeforeUnloadHangMonitorForTesting(); |
+ PrepareForDialog(browser()); |
content::WindowedNotificationObserver window_observer( |
chrome::NOTIFICATION_BROWSER_CLOSED, |
@@ -303,14 +315,7 @@ IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserCloseBeforeUnloadOK) { |
// If this test flakes, reopen http://crbug.com/123110 |
IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserCloseBeforeUnloadCancel) { |
NavigateToDataURL(BEFORE_UNLOAD_HTML, "beforeunload"); |
- // Disable the hang monitor, otherwise there will be a race between the |
- // beforeunload dialog and the beforeunload hang timer. |
- browser() |
- ->tab_strip_model() |
- ->GetActiveWebContents() |
- ->GetMainFrame() |
- ->DisableBeforeUnloadHangMonitorForTesting(); |
- |
+ PrepareForDialog(browser()); |
chrome::CloseWindow(browser()); |
// We wait for the title to change after cancelling the closure of browser |
@@ -356,13 +361,7 @@ IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserListCloseNoUnloadListeners) { |
// beforeunload handler and clicking Leave in the beforeunload confirm dialog. |
IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserListCloseBeforeUnloadOK) { |
NavigateToDataURL(BEFORE_UNLOAD_HTML, "beforeunload"); |
- // Disable the hang monitor, otherwise there will be a race between the |
- // beforeunload dialog and the beforeunload hang timer. |
- browser() |
- ->tab_strip_model() |
- ->GetActiveWebContents() |
- ->GetMainFrame() |
- ->DisableBeforeUnloadHangMonitorForTesting(); |
+ PrepareForDialog(browser()); |
content::WindowedNotificationObserver window_observer( |
chrome::NOTIFICATION_BROWSER_CLOSED, |
@@ -383,13 +382,7 @@ IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserListCloseBeforeUnloadOK) { |
// beforeunload handler and clicking Stay in the beforeunload confirm dialog. |
IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserListCloseBeforeUnloadCancel) { |
NavigateToDataURL(BEFORE_UNLOAD_HTML, "beforeunload"); |
- // Disable the hang monitor, otherwise there will be a race between the |
- // beforeunload dialog and the beforeunload hang timer. |
- browser() |
- ->tab_strip_model() |
- ->GetActiveWebContents() |
- ->GetMainFrame() |
- ->DisableBeforeUnloadHangMonitorForTesting(); |
+ PrepareForDialog(browser()); |
UnloadResults unload_results; |
BrowserList::CloseAllBrowsersWithProfile( |
@@ -425,13 +418,7 @@ IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserListCloseBeforeUnloadCancel) { |
// beforeunload handler and clicking Leave in the beforeunload confirm dialog. |
IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserListDoubleCloseBeforeUnloadOK) { |
NavigateToDataURL(BEFORE_UNLOAD_HTML, "beforeunload"); |
- // Disable the hang monitor, otherwise there will be a race between the |
- // beforeunload dialog and the beforeunload hang timer. |
- browser() |
- ->tab_strip_model() |
- ->GetActiveWebContents() |
- ->GetMainFrame() |
- ->DisableBeforeUnloadHangMonitorForTesting(); |
+ PrepareForDialog(browser()); |
content::WindowedNotificationObserver window_observer( |
chrome::NOTIFICATION_BROWSER_CLOSED, |
@@ -457,13 +444,7 @@ IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserListDoubleCloseBeforeUnloadOK) { |
// beforeunload handler and clicking Stay in the beforeunload confirm dialog. |
IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserListDoubleCloseBeforeUnloadCancel) { |
NavigateToDataURL(BEFORE_UNLOAD_HTML, "beforeunload"); |
- // Disable the hang monitor, otherwise there will be a race between the |
- // beforeunload dialog and the beforeunload hang timer. |
- browser() |
- ->tab_strip_model() |
- ->GetActiveWebContents() |
- ->GetMainFrame() |
- ->DisableBeforeUnloadHangMonitorForTesting(); |
+ PrepareForDialog(browser()); |
UnloadResults unload_results; |
BrowserList::CloseAllBrowsersWithProfile( |
@@ -514,13 +495,7 @@ IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserTerminateBeforeUnload) { |
// If this flakes, use http://crbug.com/32615 and http://crbug.com/45675 |
IN_PROC_BROWSER_TEST_F(UnloadTest, BrowserCloseWithInnerFocusedFrame) { |
NavigateToDataURL(INNER_FRAME_WITH_FOCUS_HTML, "innerframewithfocus"); |
- // Disable the hang monitor, otherwise there will be a race between the |
- // beforeunload dialog and the beforeunload hang timer. |
- browser() |
- ->tab_strip_model() |
- ->GetActiveWebContents() |
- ->GetMainFrame() |
- ->DisableBeforeUnloadHangMonitorForTesting(); |
+ PrepareForDialog(browser()); |
content::WindowedNotificationObserver window_observer( |
chrome::NOTIFICATION_BROWSER_CLOSED, |
@@ -895,6 +870,7 @@ IN_PROC_BROWSER_TEST_F(FastUnloadTest, |
return; |
NavigateToDataURL(BEFORE_UNLOAD_HTML, "beforeunload"); |
+ PrepareForDialog(browser()); |
content::WebContents* beforeunload_contents = |
browser()->tab_strip_model()->GetActiveWebContents(); |
@@ -927,6 +903,7 @@ IN_PROC_BROWSER_TEST_F(FastUnloadTest, |
IN_PROC_BROWSER_TEST_F(FastUnloadTest, |
BrowserListForceCloseWithBeforeUnloadWithFastUnload) { |
NavigateToDataURL(BEFORE_UNLOAD_HTML, "beforeunload"); |
+ PrepareForDialog(browser()); |
content::WindowedNotificationObserver window_observer( |
chrome::NOTIFICATION_BROWSER_CLOSED, |
@@ -945,6 +922,7 @@ IN_PROC_BROWSER_TEST_F(FastUnloadTest, |
IN_PROC_BROWSER_TEST_F(FastUnloadTest, |
BrowserListForceCloseAfterNormalCloseWithFastUnload) { |
NavigateToDataURL(BEFORE_UNLOAD_HTML, "beforeunload"); |
+ PrepareForDialog(browser()); |
content::WindowedNotificationObserver window_observer( |
chrome::NOTIFICATION_BROWSER_CLOSED, |