Bug 1476137 - Remove promiseWindowWillBeClosed and use BrowserTestUtils.domWindowClosed r=marco
authorsandeep <kumarsandeep2357@gmail.com>
Thu, 19 Jul 2018 11:45:15 +0530
changeset 821120 9fcf0dece71d6be939fe1b3b1361131bcdcec6f4
parent 821119 1ae4395e7cf7ea1c28781cec256cac08d7734ef1
child 821121 4a6d4d1b11fde4dd848f6c28137a557cea20faad
push id117018
push userbmo:sfoster@mozilla.com
push dateSat, 21 Jul 2018 04:05:10 +0000
reviewersmarco
bugs1476137
milestone63.0a1
Bug 1476137 - Remove promiseWindowWillBeClosed and use BrowserTestUtils.domWindowClosed r=marco MozReview-Commit-ID: 48y3wP65cm8
browser/base/content/test/general/browser_beforeunload_duplicate_dialogs.js
browser/base/content/test/general/browser_bug462673.js
browser/base/content/test/general/browser_save_link_when_window_navigates.js
browser/base/content/test/general/head.js
--- a/browser/base/content/test/general/browser_beforeunload_duplicate_dialogs.js
+++ b/browser/base/content/test/general/browser_beforeunload_duplicate_dialogs.js
@@ -26,45 +26,45 @@ registerCleanupFunction(() => {
   Services.prefs.clearUserPref("browser.tabs.warnOnClose");
   Services.obs.removeObserver(onTabModalDialogLoaded, "tabmodal-dialog-loaded");
 });
 
 add_task(async function closeLastTabInWindow() {
   let newWin = await promiseOpenAndLoadWindow({}, true);
   let firstTab = newWin.gBrowser.selectedTab;
   await promiseTabLoadEvent(firstTab, TEST_PAGE);
-  let windowClosedPromise = promiseWindowWillBeClosed(newWin);
+  let windowClosedPromise = BrowserTestUtils.domWindowClosed(newWin);
   expectingDialog = true;
   // close tab:
   document.getAnonymousElementByAttribute(firstTab, "anonid", "close-button").click();
   await windowClosedPromise;
   ok(!expectingDialog, "There should have been a dialog.");
   ok(newWin.closed, "Window should be closed.");
 });
 
 add_task(async function closeWindowWithMultipleTabsIncludingOneBeforeUnload() {
   Services.prefs.setBoolPref("browser.tabs.warnOnClose", false);
   let newWin = await promiseOpenAndLoadWindow({}, true);
   let firstTab = newWin.gBrowser.selectedTab;
   await promiseTabLoadEvent(firstTab, TEST_PAGE);
   await promiseTabLoadEvent(newWin.gBrowser.addTab(), "http://example.com/");
-  let windowClosedPromise = promiseWindowWillBeClosed(newWin);
+  let windowClosedPromise = BrowserTestUtils.domWindowClosed(newWin);
   expectingDialog = true;
   newWin.BrowserTryToCloseWindow();
   await windowClosedPromise;
   ok(!expectingDialog, "There should have been a dialog.");
   ok(newWin.closed, "Window should be closed.");
   Services.prefs.clearUserPref("browser.tabs.warnOnClose");
 });
 
 add_task(async function closeWindoWithSingleTabTwice() {
   let newWin = await promiseOpenAndLoadWindow({}, true);
   let firstTab = newWin.gBrowser.selectedTab;
   await promiseTabLoadEvent(firstTab, TEST_PAGE);
-  let windowClosedPromise = promiseWindowWillBeClosed(newWin);
+  let windowClosedPromise = BrowserTestUtils.domWindowClosed(newWin);
   expectingDialog = true;
   wantToClose = false;
   let firstDialogShownPromise = new Promise((resolve, reject) => { resolveDialogPromise = resolve; });
   document.getAnonymousElementByAttribute(firstTab, "anonid", "close-button").click();
   await firstDialogShownPromise;
   info("Got initial dialog, now trying again");
   expectingDialog = true;
   wantToClose = true;
--- a/browser/base/content/test/general/browser_bug462673.js
+++ b/browser/base/content/test/general/browser_bug462673.js
@@ -5,17 +5,17 @@ add_task(async function() {
   let tab = win.gBrowser.tabContainer.firstChild;
   await promiseTabLoadEvent(tab, getRootDirectory(gTestPath) + "test_bug462673.html");
 
   is(win.gBrowser.browsers.length, 2, "test_bug462673.html has opened a second tab");
   is(win.gBrowser.selectedTab, tab.nextSibling, "dependent tab is selected");
   win.gBrowser.removeTab(tab);
 
   // Closing a tab will also close its parent chrome window, but async
-  await promiseWindowWillBeClosed(win);
+  await BrowserTestUtils.domWindowClosed(win);
 });
 
 add_task(async function() {
   var win = openDialog(getBrowserURL(), "_blank", "chrome,all,dialog=no");
   await SimpleTest.promiseFocus(win);
 
   let tab = win.gBrowser.tabContainer.firstChild;
   await promiseTabLoadEvent(tab, getRootDirectory(gTestPath) + "test_bug462673.html");
--- a/browser/base/content/test/general/browser_save_link_when_window_navigates.js
+++ b/browser/base/content/test/general/browser_save_link_when_window_navigates.js
@@ -148,17 +148,17 @@ function test() {
     Services.ww.unregisterNotification(windowObserver);
     Services.prefs.clearUserPref(ALWAYS_DOWNLOAD_DIR_PREF);
     Services.prefs.clearUserPref(SAVE_PER_SITE_PREF);
     info("Finished running the cleanup code");
   });
 
   Services.prefs.setBoolPref(ALWAYS_DOWNLOAD_DIR_PREF, false);
   testOnWindow(undefined, function(win) {
-    let windowGonePromise = promiseWindowWillBeClosed(win);
+    let windowGonePromise = BrowserTestUtils.domWindowClosed(win);
     Services.prefs.setBoolPref(SAVE_PER_SITE_PREF, true);
     triggerSave(win, function() {
       windowGonePromise.then(function() {
         Services.prefs.setBoolPref(SAVE_PER_SITE_PREF, false);
         testOnWindow(undefined, function(win2) {
           triggerSave(win2, finish);
         });
       });
--- a/browser/base/content/test/general/head.js
+++ b/browser/base/content/test/general/head.js
@@ -1,14 +1,16 @@
 ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
 
 ChromeUtils.defineModuleGetter(this, "PlacesUtils",
   "resource://gre/modules/PlacesUtils.jsm");
 ChromeUtils.defineModuleGetter(this, "PlacesTestUtils",
   "resource://testing-common/PlacesTestUtils.jsm");
+ChromeUtils.defineModuleGetter(this, "BrowserTestUtils",
+  "resource://testing-common/BrowserTestUtils.jsm");
 ChromeUtils.defineModuleGetter(this, "TabCrashHandler",
   "resource:///modules/ContentCrashHandlers.jsm");
 
 /**
  * Wait for a <notification> to be closed then call the specified callback.
  */
 function waitForNotificationClose(notification, cb) {
   let parent = notification.parentNode;
@@ -200,29 +202,18 @@ function resetBlocklist() {
 
 function whenNewWindowLoaded(aOptions, aCallback) {
   let win = OpenBrowserWindow(aOptions);
   win.addEventListener("load", function() {
     aCallback(win);
   }, {once: true});
 }
 
-function promiseWindowWillBeClosed(win) {
-  return new Promise((resolve, reject) => {
-    Services.obs.addObserver(function observe(subject, topic) {
-      if (subject == win) {
-        Services.obs.removeObserver(observe, topic);
-        executeSoon(resolve);
-      }
-    }, "domwindowclosed");
-  });
-}
-
 function promiseWindowClosed(win) {
-  let promise = promiseWindowWillBeClosed(win);
+  let promise = BrowserTestUtils.domWindowClosed(win);
   win.close();
   return promise;
 }
 
 function promiseOpenAndLoadWindow(aOptions, aWaitForDelayedStartup = false) {
   return new Promise(resolve => {
     let win = OpenBrowserWindow(aOptions);
     if (aWaitForDelayedStartup) {