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

Unified Diff: chrome/browser/ui/browser_browsertest.cc

Issue 2801813005: Only show a beforeunload dialog if a frame has had a user gesture since its load. (Closed)
Patch Set: aw 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/browser_browsertest.cc
diff --git a/chrome/browser/ui/browser_browsertest.cc b/chrome/browser/ui/browser_browsertest.cc
index 639da6e2daafaf56a8a96c6ae14a6c110451f378..257c1d410a98f43737db40841ce44f13907223a1 100644
--- a/chrome/browser/ui/browser_browsertest.cc
+++ b/chrome/browser/ui/browser_browsertest.cc
@@ -624,6 +624,12 @@ IN_PROC_BROWSER_TEST_F(BrowserTest, SadTabCancelsDialogs) {
// beforeunload dialog and the beforeunload hang timer.
contents->GetMainFrame()->DisableBeforeUnloadHangMonitorForTesting();
+ // JavaScript onbeforeunload dialogs require a user gesture.
Nico 2017/04/07 20:05:28 You're adding this exactly in the tests where you
Charlie Reis 2017/04/10 06:20:11 It does seem like the majority of cases would bene
Avi (use Gerrit) 2017/05/05 15:30:04 Done.
+ for (auto* frame :
+ browser()->tab_strip_model()->GetActiveWebContents()->GetAllFrames()) {
+ frame->ExecuteJavaScriptWithUserGestureForTests(base::string16());
+ }
+
// Start a navigation to trigger the beforeunload dialog.
contents->GetMainFrame()->ExecuteJavaScriptForTests(
ASCIIToUTF16("window.location.href = 'about:blank'"));
@@ -713,6 +719,12 @@ IN_PROC_BROWSER_TEST_F(BrowserTest, ReloadThenCancelBeforeUnload) {
// beforeunload dialog and the beforeunload hang timer.
contents->GetMainFrame()->DisableBeforeUnloadHangMonitorForTesting();
+ // JavaScript onbeforeunload dialogs require a user gesture.
+ for (auto* frame :
+ browser()->tab_strip_model()->GetActiveWebContents()->GetAllFrames()) {
+ frame->ExecuteJavaScriptWithUserGestureForTests(base::string16());
+ }
+
// Navigate to another page, but click cancel in the dialog. Make sure that
// the throbber stops spinning.
chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB);
@@ -837,6 +849,12 @@ IN_PROC_BROWSER_TEST_F(BrowserTest, SingleBeforeUnloadAfterRedirect) {
// beforeunload dialog and the beforeunload hang timer.
contents->GetMainFrame()->DisableBeforeUnloadHangMonitorForTesting();
+ // JavaScript onbeforeunload dialogs require a user gesture.
+ for (auto* frame :
+ browser()->tab_strip_model()->GetActiveWebContents()->GetAllFrames()) {
+ frame->ExecuteJavaScriptWithUserGestureForTests(base::string16());
+ }
+
// Navigate to a URL that redirects to another process and approve the
// beforeunload dialog that pops up.
content::WindowedNotificationObserver nav_observer(
@@ -869,6 +887,12 @@ IN_PROC_BROWSER_TEST_F(BrowserTest, CancelBeforeUnloadResetsURL) {
// beforeunload dialog and the beforeunload hang timer.
contents->GetMainFrame()->DisableBeforeUnloadHangMonitorForTesting();
+ // JavaScript onbeforeunload dialogs require a user gesture.
+ for (auto* frame :
+ browser()->tab_strip_model()->GetActiveWebContents()->GetAllFrames()) {
+ frame->ExecuteJavaScriptWithUserGestureForTests(base::string16());
+ }
+
// Navigate to a page that triggers a cross-site transition.
ASSERT_TRUE(embedded_test_server()->Start());
GURL url2(embedded_test_server()->GetURL("/title1.html"));
@@ -943,6 +967,12 @@ IN_PROC_BROWSER_TEST_F(BrowserTest, BeforeUnloadVsBeforeReload) {
// beforeunload dialog and the beforeunload hang timer.
contents->GetMainFrame()->DisableBeforeUnloadHangMonitorForTesting();
+ // JavaScript onbeforeunload dialogs require a user gesture.
+ for (auto* frame :
+ browser()->tab_strip_model()->GetActiveWebContents()->GetAllFrames()) {
+ frame->ExecuteJavaScriptWithUserGestureForTests(base::string16());
+ }
+
// Reload the page, and check that we get a "before reload" dialog.
chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB);
AppModalDialog* alert = ui_test_utils::WaitForAppModalDialog();
@@ -1013,6 +1043,12 @@ IN_PROC_BROWSER_TEST_F(BeforeUnloadAtQuitWithTwoWindows,
GURL url(std::string("data:text/html,") + kBeforeUnloadHTML);
ui_test_utils::NavigateToURL(browser(), url);
+ // JavaScript onbeforeunload dialogs require a user gesture.
+ for (auto* frame :
+ browser()->tab_strip_model()->GetActiveWebContents()->GetAllFrames()) {
+ frame->ExecuteJavaScriptWithUserGestureForTests(base::string16());
+ }
+
// Open a second browser window at about:blank.
ui_test_utils::BrowserAddedObserver browser_added_observer;
chrome::NewEmptyWindow(browser()->profile());

Powered by Google App Engine
This is Rietveld 408576698