Bug 305085 - add a test for beforeunload uses, r=jaws
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Fri, 19 Dec 2014 21:41:57 +0000
changeset 223762 458431092bd3cc158014d06834bb6d1e5a5bd04c
parent 223761 e0c6c4e136eeec23f34541ded699abb86fdba642
child 223763 a23d5b6b72784f9d3c1b7823c55602e7c95911e0
push id10815
push usergijskruitbosch@gmail.com
push dateWed, 14 Jan 2015 17:48:35 +0000
treeherderfx-team@458431092bd3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjaws
bugs305085
milestone38.0a1
Bug 305085 - add a test for beforeunload uses, r=jaws
browser/base/content/test/general/browser.ini
browser/base/content/test/general/browser_beforeunload_duplicate_dialogs.js
browser/base/content/test/general/file_double_close_tab.html
browser/base/content/test/general/head.js
--- a/browser/base/content/test/general/browser.ini
+++ b/browser/base/content/test/general/browser.ini
@@ -121,16 +121,18 @@ skip-if = e10s # Bug 1093153 - no about:
 [browser_autocomplete_a11y_label.js]
 skip-if = e10s # Bug 1101993 - times out for unknown reasons when run in the dir (works on its own)
 [browser_autocomplete_no_title.js]
 [browser_autocomplete_autoselect.js]
 [browser_autocomplete_oldschool_wrap.js]
 [browser_autocomplete_tag_star_visibility.js]
 [browser_backButtonFitts.js]
 skip-if = os != "win" || e10s # The Fitts Law back button is only supported on Windows (bug 571454) / e10s - Bug 1099154: test touches content (attempts to add an event listener directly to the contentWindow)
+[browser_beforeunload_duplicate_dialogs.js]
+skip-if = e10s # bug 967873 means permitUnload doesn't work in e10s mode
 [browser_blob-channelname.js]
 [browser_bookmark_titles.js]
 skip-if = buildapp == 'mulet' || toolkit == "windows" || e10s # Disabled on Windows due to frequent failures (bugs 825739, 841341) / e10s - Bug 1094205 - places doesn't return the right thing in e10s mode, for some reason
 [browser_bug304198.js]
 skip-if = e10s
 [browser_bug321000.js]
 skip-if = true # browser_bug321000.js is disabled because newline handling is shaky (bug 592528)
 [browser_bug329212.js]
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/general/browser_beforeunload_duplicate_dialogs.js
@@ -0,0 +1,44 @@
+const TEST_PAGE = "http://mochi.test:8888/browser/browser/base/content/test/general/file_double_close_tab.html";
+
+let expectingDialog = false;
+function onTabModalDialogLoaded(node) {
+  ok(expectingDialog, "Should be expecting this dialog.");
+  expectingDialog = false;
+  // This accepts the dialog, closing it
+  node.Dialog.ui.button0.click();
+}
+
+
+// Listen for the dialog being created
+Services.obs.addObserver(onTabModalDialogLoaded, "tabmodal-dialog-loaded", false);
+registerCleanupFunction(() => {
+  Services.prefs.clearUserPref("browser.tabs.warnOnClose");
+  Services.obs.removeObserver(onTabModalDialogLoaded, "tabmodal-dialog-loaded");
+});
+
+add_task(function* closeLastTabInWindow() {
+  let newWin = yield promiseOpenAndLoadWindow({}, true);
+  let firstTab = newWin.gBrowser.selectedTab;
+  yield promiseTabLoadEvent(firstTab, TEST_PAGE);
+  let windowClosedPromise = promiseWindowWillBeClosed(newWin);
+  expectingDialog = true;
+  // close tab:
+  document.getAnonymousElementByAttribute(firstTab, "anonid", "close-button").click();
+  yield windowClosedPromise;
+  ok(!expectingDialog, "There should have been a dialog.");
+  ok(newWin.closed, "Window should be closed.");
+});
+
+add_task(function* closeWindowWithMultipleTabsIncludingOneBeforeUnload() {
+  Services.prefs.setBoolPref("browser.tabs.warnOnClose", false);
+  let newWin = yield promiseOpenAndLoadWindow({}, true);
+  let firstTab = newWin.gBrowser.selectedTab;
+  yield promiseTabLoadEvent(firstTab, TEST_PAGE);
+  yield promiseTabLoadEvent(newWin.gBrowser.addTab(), "http://example.com/");
+  let windowClosedPromise = promiseWindowWillBeClosed(newWin);
+  expectingDialog = true;
+  newWin.BrowserTryToCloseWindow();
+  yield windowClosedPromise;
+  ok(!expectingDialog, "There should have been a dialog.");
+  ok(newWin.closed, "Window should be closed.");
+});
--- a/browser/base/content/test/general/file_double_close_tab.html
+++ b/browser/base/content/test/general/file_double_close_tab.html
@@ -1,13 +1,13 @@
 <!DOCTYPE html>
 <html>
   <head>
     <meta charset="utf-8">
-    <title>Test for bug 1050638 - clicking tab close button twice should close tab even in beforeunload case</title>
+    <title>Test page that blocks beforeunload. Used in tests for bug 1050638 and bug 305085</title>
   </head>
   <body>
     This page will block beforeunload. It should still be user-closable at all times.
     <script>
       window.onbeforeunload = function() {
         return "stop";
       };
     </script>
--- a/browser/base/content/test/general/head.js
+++ b/browser/base/content/test/general/head.js
@@ -197,24 +197,31 @@ function resetBlocklist() {
 function whenNewWindowLoaded(aOptions, aCallback) {
   let win = OpenBrowserWindow(aOptions);
   win.addEventListener("load", function onLoad() {
     win.removeEventListener("load", onLoad, false);
     aCallback(win);
   }, false);
 }
 
+function promiseWindowWillBeClosed(win) {
+  return new Promise((resolve, reject) => {
+    Services.obs.addObserver(function observe(subject, topic) {
+      if (subject == win) {
+        Services.obs.removeObserver(observe, topic);
+        resolve();
+      }
+    }, "domwindowclosed", false);
+  });
+}
+
 function promiseWindowClosed(win) {
-  let deferred = Promise.defer();
-  win.addEventListener("unload", function onunload() {
-    win.removeEventListener("unload", onunload);
-    deferred.resolve();
-  });
+  let promise = promiseWindowWillBeClosed(win);
   win.close();
-  return deferred.promise;
+  return promise;
 }
 
 function promiseOpenAndLoadWindow(aOptions, aWaitForDelayedStartup=false) {
   let deferred = Promise.defer();
   let win = OpenBrowserWindow(aOptions);
   if (aWaitForDelayedStartup) {
     Services.obs.addObserver(function onDS(aSubject, aTopic, aData) {
       if (aSubject != win) {