Bug 1492782 - modernize a few tests and reduce CPOW usage in browser/base/content/test/general/, r=mconley
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Thu, 20 Sep 2018 14:17:32 +0000
changeset 437452 e5781c87acb5468a9fd79e0d3c02748597fafcb6
parent 437451 83eda6b67672b552fa453529410972e11d3cf250
child 437453 40ade9ef77d00a003328bb3bc9853d74bd3567ea
push id34683
push userapavel@mozilla.com
push dateThu, 20 Sep 2018 21:54:05 +0000
treeherdermozilla-central@4d3cd0ab7277 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmconley
bugs1492782
milestone64.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1492782 - modernize a few tests and reduce CPOW usage in browser/base/content/test/general/, r=mconley Differential Revision: https://phabricator.services.mozilla.com/D6375
browser/base/content/test/general/browser_bug481560.js
browser/base/content/test/general/browser_bug763468_perwindowpb.js
browser/base/content/test/general/head.js
--- a/browser/base/content/test/general/browser_bug481560.js
+++ b/browser/base/content/test/general/browser_bug481560.js
@@ -1,21 +1,16 @@
-function test() {
-  waitForExplicitFinish();
-
-  whenNewWindowLoaded(null, function(win) {
-    waitForFocus(function() {
-      function onTabClose() {
-        ok(false, "shouldn't have gotten the TabClose event for the last tab");
-      }
-      var tab = win.gBrowser.selectedTab;
-      tab.addEventListener("TabClose", onTabClose);
+add_task(async function testTabCloseShortcut() {
+  let win = await BrowserTestUtils.openNewBrowserWindow();
+  await SimpleTest.promiseFocus(win);
 
-      EventUtils.synthesizeKey("w", { accelKey: true }, win);
-
-      ok(win.closed, "accel+w closed the window immediately");
+  function onTabClose() {
+    ok(false, "shouldn't have gotten the TabClose event for the last tab");
+  }
+  var tab = win.gBrowser.selectedTab;
+  tab.addEventListener("TabClose", onTabClose);
 
-      tab.removeEventListener("TabClose", onTabClose);
+  EventUtils.synthesizeKey("w", { accelKey: true }, win);
 
-      finish();
-    }, win);
-  });
-}
+  ok(win.closed, "accel+w closed the window immediately");
+
+  tab.removeEventListener("TabClose", onTabClose);
+});
--- a/browser/base/content/test/general/browser_bug763468_perwindowpb.js
+++ b/browser/base/content/test/general/browser_bug763468_perwindowpb.js
@@ -1,64 +1,53 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 "use strict";
 
 // This test makes sure that opening a new tab in private browsing mode opens about:privatebrowsing
-function test() {
-  // initialization
-  waitForExplicitFinish();
+add_task(async function testPBNewTab() {
+  registerCleanupFunction(async function() {
+    for (let win of windowsToClose) {
+      await BrowserTestUtils.closeWindow(win);
+    }
+  });
 
   let windowsToClose = [];
-  let newTabURL;
-  let mode;
+
+  async function doTest(aIsPrivateMode) {
+    let newTabURL;
+    let mode;
+    let win = await BrowserTestUtils.openNewBrowserWindow({private: aIsPrivateMode});
+    windowsToClose.push(win);
 
-  function doTest(aIsPrivateMode, aWindow, aCallback) {
-    whenNewTabLoaded(aWindow, function() {
-      if (aIsPrivateMode) {
-        mode = "per window private browsing";
-        newTabURL = "about:privatebrowsing";
-      } else {
-        mode = "normal";
-        newTabURL = "about:newtab";
-      }
+    if (aIsPrivateMode) {
+      mode = "per window private browsing";
+      newTabURL = "about:privatebrowsing";
+    } else {
+      mode = "normal";
+      newTabURL = "about:newtab";
+    }
+    await openNewTab(win, newTabURL);
 
-      is(aWindow.gBrowser.currentURI.spec, newTabURL,
-        "URL of NewTab should be " + newTabURL + " in " + mode + " mode");
-
-      aWindow.gBrowser.removeTab(aWindow.gBrowser.selectedTab);
-      aCallback();
-    });
+    is(win.gBrowser.currentURI.spec, newTabURL,
+      "URL of NewTab should be " + newTabURL + " in " + mode + " mode");
   }
 
-  function testOnWindow(aOptions, aCallback) {
-    whenNewWindowLoaded(aOptions, function(aWin) {
-      windowsToClose.push(aWin);
-      // execute should only be called when need, like when you are opening
-      // web pages on the test. If calling executeSoon() is not necesary, then
-      // call whenNewWindowLoaded() instead of testOnWindow() on your test.
-      executeSoon(() => aCallback(aWin));
-    });
-  }
+  await doTest(false);
+  await doTest(true);
+  await doTest(false);
+});
 
-  // this function is called after calling finish() on the test.
-  registerCleanupFunction(function() {
-    windowsToClose.forEach(function(aWin) {
-      aWin.close();
-    });
+async function openNewTab(aWindow, aExpectedURL) {
+  // Open a new tab
+  aWindow.BrowserOpenTab();
+
+  let browser = aWindow.gBrowser.selectedBrowser;
+  let loadPromise = BrowserTestUtils.browserLoaded(browser, false, aExpectedURL);
+  let alreadyLoaded = await ContentTask.spawn(browser, aExpectedURL, url => {
+    let doc = content.document;
+    return doc && doc.readyState === "complete" && doc.location.href == url;
   });
-
-  // test first when not on private mode
-  testOnWindow({}, function(aWin) {
-    doTest(false, aWin, function() {
-      // then test when on private mode
-      testOnWindow({private: true}, function(aWin2) {
-        doTest(true, aWin2, function() {
-          // then test again when not on private mode
-          testOnWindow({}, function(aWin3) {
-            doTest(false, aWin3, finish);
-          });
-        });
-      });
-    });
-  });
+  if (!alreadyLoaded) {
+    await loadPromise;
+  }
 }
--- a/browser/base/content/test/general/head.js
+++ b/browser/base/content/test/general/head.js
@@ -195,23 +195,16 @@ function setAndUpdateBlocklist(aURL, aCa
   Services.prefs.setCharPref("extensions.blocklist.url", aURL);
   updateBlocklist(aCallback);
 }
 
 function resetBlocklist() {
   Services.prefs.setCharPref("extensions.blocklist.url", _originalTestBlocklistURL);
 }
 
-function whenNewWindowLoaded(aOptions, aCallback) {
-  let win = OpenBrowserWindow(aOptions);
-  win.addEventListener("load", function() {
-    aCallback(win);
-  }, {once: true});
-}
-
 function promiseWindowClosed(win) {
   let promise = BrowserTestUtils.domWindowClosed(win);
   win.close();
   return promise;
 }
 
 function promiseOpenAndLoadWindow(aOptions, aWaitForDelayedStartup = false) {
   return new Promise(resolve => {
@@ -228,27 +221,30 @@ function promiseOpenAndLoadWindow(aOptio
     } else {
       win.addEventListener("load", function() {
         resolve(win);
       }, {once: true});
     }
   });
 }
 
-function whenNewTabLoaded(aWindow, aCallback) {
+async function whenNewTabLoaded(aWindow, aCallback) {
   aWindow.BrowserOpenTab();
 
+  let expectedURL = aboutNewTabService.newTabURL;
   let browser = aWindow.gBrowser.selectedBrowser;
-  let doc = browser.contentDocumentAsCPOW;
-  if (doc && doc.readyState === "complete") {
-    aCallback();
-    return;
+  let loadPromise = BrowserTestUtils.browserLoaded(browser, false, expectedURL);
+  let alreadyLoaded = await ContentTask.spawn(browser, expectedURL, url => {
+    let doc = content.document;
+    return doc && doc.readyState === "complete" && doc.location.href == url;
+  });
+  if (!alreadyLoaded) {
+    await loadPromise;
   }
-
-  whenTabLoaded(aWindow.gBrowser.selectedTab, aCallback);
+  aCallback();
 }
 
 function whenTabLoaded(aTab, aCallback) {
   promiseTabLoadEvent(aTab).then(aCallback);
 }
 
 function promiseTabLoaded(aTab) {
   return new Promise(resolve => {