Bug 1416153 - Part 0: Fix tests that uses BrowserTestUtils.waitForNewTab to perform the registration of the next event handler instantly inside the new tab event handler. r=Gijs
authorTooru Fujisawa <arai_a@mac.com>
Fri, 24 Nov 2017 07:50:12 +0900
changeset 393691 976f45726ed5e5ca5e6961f5a8d6abeaa41a3637
parent 393669 c841dbfc685301fe4d6163e841ec06f8139d8b94
child 393692 76288affab86b424af5be018b4e25c7e8254e522
push id32973
push usercbrindusan@mozilla.com
push dateSat, 25 Nov 2017 21:19:04 +0000
treeherdermozilla-central@f045ac9f76cf [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersGijs
bugs1416153
milestone59.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 1416153 - Part 0: Fix tests that uses BrowserTestUtils.waitForNewTab to perform the registration of the next event handler instantly inside the new tab event handler. r=Gijs
browser/base/content/test/referrer/head.js
browser/base/content/test/siteIdentity/browser_insecureLoginForms.js
browser/base/content/test/tabs/browser_opened_file_tab_navigated_to_web.js
browser/extensions/shield-recipe-client/test/browser/browser_Heartbeat.js
toolkit/components/viewsource/test/browser/browser_open_docgroup.js
toolkit/components/viewsource/test/browser/browser_srcdoc.js
toolkit/components/viewsource/test/browser/head.js
toolkit/mozapps/extensions/test/xpinstall/browser_bug638292.js
--- a/browser/base/content/test/referrer/head.js
+++ b/browser/base/content/test/referrer/head.js
@@ -148,19 +148,17 @@ function delayedStartupFinished(aWindow)
 
 /**
  * Waits for some (any) tab to load. The caller triggers the load.
  * @param aWindow The window where to wait for a tab to load.
  * @return {Promise}
  * @resolves With the tab once it's loaded.
  */
 function someTabLoaded(aWindow) {
-  return BrowserTestUtils.waitForNewTab(gTestWindow.gBrowser).then((tab) => {
-    return BrowserTestUtils.browserStopped(tab.linkedBrowser).then(() => tab);
-  });
+  return BrowserTestUtils.waitForNewTab(gTestWindow.gBrowser, null, true);
 }
 
 /**
  * Waits for a new window to open and load. The caller triggers the open.
  * @return {Promise}
  * @resolves With the new window once it's open and loaded.
  */
 function newWindowOpened() {
--- a/browser/base/content/test/siteIdentity/browser_insecureLoginForms.js
+++ b/browser/base/content/test/siteIdentity/browser_insecureLoginForms.js
@@ -126,30 +126,37 @@ add_task(async function test_mixedconten
 
   gBrowser.removeTab(tab);
 });
 
 /**
  * Checks that insecure window.opener does not trigger a warning.
  */
 add_task(async function test_ignoring_window_opener() {
-  let newTabURL = "https://example.com" + TEST_URL_PATH + "form_basic.html";
   let path = getRootDirectory(gTestPath)
     .replace("chrome://mochitests/content", "http://example.com");
   let url = path + "insecure_opener.html";
 
   await BrowserTestUtils.withNewTab(url, async function(browser) {
     // Clicking the link will spawn a new tab.
-    let loaded = BrowserTestUtils.waitForNewTab(gBrowser, newTabURL);
+    let stateChangePromise;
+    let tabOpenPromise = new Promise(resolve => {
+      gBrowser.tabContainer.addEventListener("TabOpen", event => {
+        let tab = event.target;
+        let newTabBrowser = tab.linkedBrowser;
+        stateChangePromise = waitForInsecureLoginFormsStateChange(newTabBrowser, 2);
+        resolve(tab);
+      }, { once: true });
+    });
+
     await ContentTask.spawn(browser, {}, function() {
       content.document.getElementById("link").click();
     });
-    let tab = await loaded;
-    browser = tab.linkedBrowser;
-    await waitForInsecureLoginFormsStateChange(browser, 2);
+    let tab = await tabOpenPromise;
+    await stateChangePromise;
 
     // Open the identity popup.
     let { gIdentityHandler } = gBrowser.ownerGlobal;
     let promisePanelOpen = BrowserTestUtils.waitForEvent(gIdentityHandler._identityPopup, "popupshown");
     gIdentityHandler._identityBox.click();
     await promisePanelOpen;
 
     ok(Array.every(document.getElementById("identity-popup-mainView")
--- a/browser/base/content/test/tabs/browser_opened_file_tab_navigated_to_web.js
+++ b/browser/base/content/test/tabs/browser_opened_file_tab_navigated_to_web.js
@@ -17,27 +17,26 @@ add_task(async function() {
 
   // Open first file:// page.
   let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, uriString);
   registerCleanupFunction(async function() {
     await BrowserTestUtils.removeTab(tab);
   });
 
   // Open new file:// tab from JavaScript in first file:// page.
-  let promiseTabOpened = BrowserTestUtils.waitForNewTab(gBrowser, openedUriString);
+  let promiseTabOpened = BrowserTestUtils.waitForNewTab(gBrowser, openedUriString, true);
   await ContentTask.spawn(tab.linkedBrowser, openedUriString, uri => {
     content.open(uri, "_blank");
   });
 
   let openedTab = await promiseTabOpened;
   registerCleanupFunction(async function() {
     await BrowserTestUtils.removeTab(openedTab);
   });
 
   let openedBrowser = openedTab.linkedBrowser;
-  await BrowserTestUtils.browserLoaded(openedBrowser);
 
   // Ensure that new file:// tab can be navigated to web content.
   openedBrowser.loadURI("http://example.org/");
   let href = await BrowserTestUtils.browserLoaded(openedBrowser);
   is(href, "http://example.org/",
      "Check that new file:// page has navigated successfully to web content");
 });
--- a/browser/extensions/shield-recipe-client/test/browser/browser_Heartbeat.js
+++ b/browser/extensions/shield-recipe-client/test/browser/browser_Heartbeat.js
@@ -97,21 +97,28 @@ add_task(async function() {
   Assert.equal(notificationBox.childElementCount, preCount + 1, "Correct number of notifications open");
   Assert.equal(hb.notice.querySelectorAll(".star-x").length, 5, "Correct number of stars");
   Assert.equal(hb.notice.querySelectorAll(".notification-button").length, 0, "Engagement button not shown");
   Assert.equal(learnMoreEl.href, "https://example.org/learnmore", "Learn more url correct");
   Assert.equal(learnMoreEl.value, "Learn More", "Learn more label correct");
   Assert.equal(messageEl.textContent, "test", "Message is correct");
 
   // Check that when clicking the learn more link, a tab opens with the right URL
-  const tabOpenPromise = BrowserTestUtils.waitForNewTab(targetWindow.gBrowser);
+  let loadedPromise;
+  const tabOpenPromise = new Promise(resolve => {
+    gBrowser.tabContainer.addEventListener("TabOpen", event => {
+      let tab = event.target;
+      loadedPromise = BrowserTestUtils.browserLoaded(
+        tab.linkedBrowser, true, url => url && url !== "about:blank");
+      resolve(tab);
+    }, { once: true });
+  });
   learnMoreEl.click();
   const tab = await tabOpenPromise;
-  const tabUrl = await BrowserTestUtils.browserLoaded(
-    tab.linkedBrowser, true, url => url && url !== "about:blank");
+  const tabUrl = await loadedPromise;
 
   Assert.equal(tabUrl, "https://example.org/learnmore", "Learn more link opened the right url");
 
   const telemetrySentPromise = assertTelemetrySent(hb, ["offeredTS", "learnMoreTS", "closedTS"]);
   // Close notification to trigger telemetry to be sent
   await closeAllNotifications(targetWindow, notificationBox);
   await telemetrySentPromise;
   await BrowserTestUtils.removeTab(tab);
@@ -133,21 +140,28 @@ add_task(async function() {
   });
   const engagementButton = hb.notice.querySelector(".notification-button");
 
   Assert.equal(hb.notice.querySelectorAll(".star-x").length, 0, "Stars not shown");
   Assert.ok(engagementButton, "Engagement button added");
   Assert.equal(engagementButton.label, "Click me!", "Engagement button has correct label");
 
   const engagementEl = hb.notice.querySelector(".notification-button");
-  const tabOpenPromise = BrowserTestUtils.waitForNewTab(targetWindow.gBrowser);
+  let loadedPromise;
+  const tabOpenPromise = new Promise(resolve => {
+    gBrowser.tabContainer.addEventListener("TabOpen", event => {
+      let tab = event.target;
+      loadedPromise = BrowserTestUtils.browserLoaded(
+        tab.linkedBrowser, true, url => url && url !== "about:blank");
+      resolve(tab);
+    }, { once: true });
+  });
   engagementEl.click();
   const tab = await tabOpenPromise;
-  const tabUrl = await BrowserTestUtils.browserLoaded(
-        tab.linkedBrowser, true, url => url && url !== "about:blank");
+  const tabUrl = await loadedPromise;
   // the postAnswer url gets query parameters appended onto the end, so use Assert.startsWith instead of Assert.equal
   Assert.ok(tabUrl.startsWith("https://example.org/postAnswer"), "Engagement button opened the right url");
 
   const telemetrySentPromise = assertTelemetrySent(hb, ["offeredTS", "engagedTS", "closedTS"]);
   // Close notification to trigger telemetry to be sent
   await closeAllNotifications(targetWindow, notificationBox);
   await telemetrySentPromise;
   await BrowserTestUtils.removeTab(tab);
--- a/toolkit/components/viewsource/test/browser/browser_open_docgroup.js
+++ b/toolkit/components/viewsource/test/browser/browser_open_docgroup.js
@@ -25,17 +25,16 @@ add_task(async function test_view_source
   });
 
   await BrowserTestUtils.withNewTab({
     gBrowser,
     url: "http://example.com",
   }, async function(browser) {
     let sourceTab = await openViewSource(browser);
     let sourceBrowser = sourceTab.linkedBrowser;
-    await waitForSourceLoaded(sourceBrowser);
 
     await ContentTask.spawn(sourceBrowser, null, async function() {
       Assert.equal(content.document.body.id, "viewsource",
                    "View source mode enabled");
     });
 
     await BrowserTestUtils.removeTab(sourceTab);
   });
@@ -53,17 +52,16 @@ add_task(async function test_view_source
     ],
   });
 
   await BrowserTestUtils.withNewTab({
     gBrowser,
     url: "http://example.com",
   }, async function(browser) {
     let sourceWin = await openViewSource(browser);
-    await waitForSourceLoaded(sourceWin);
     await ContentTask.spawn(sourceWin.gBrowser, null, async function() {
       Assert.equal(content.document.body.id, "viewsource",
                    "View source mode enabled");
     });
 
     await closeViewSourceWindow(sourceWin);
   });
 
--- a/toolkit/components/viewsource/test/browser/browser_srcdoc.js
+++ b/toolkit/components/viewsource/test/browser/browser_srcdoc.js
@@ -11,18 +11,16 @@ add_task(async function() {
 });
 
 async function checkFrameSource() {
   let sourceTab = await openViewFrameSourceTab("#f");
   registerCleanupFunction(function() {
     gBrowser.removeTab(sourceTab);
   });
 
-  await waitForSourceLoaded(sourceTab);
-
   let browser = gBrowser.selectedBrowser;
   let textContent = await ContentTask.spawn(browser, {}, async function() {
     return content.document.body.textContent;
   });
   is(textContent, frameSource, "Correct content loaded");
   let id = await ContentTask.spawn(browser, {}, async function() {
     return content.document.body.id;
   });
--- a/toolkit/components/viewsource/test/browser/head.js
+++ b/toolkit/components/viewsource/test/browser/head.js
@@ -42,56 +42,75 @@ function closeViewSourceWindow(aWindow, 
 
 function testViewSourceWindow(aURI, aTestCallback, aCloseCallback) {
   openViewSourceWindow(aURI, function(aWindow) {
     aTestCallback(aWindow);
     closeViewSourceWindow(aWindow, aCloseCallback);
   });
 }
 
-function waitForViewSourceWindow() {
-  return new Promise(resolve => {
-    let windowListener = {
-      onOpenWindow(xulWindow) {
-        let win = xulWindow.QueryInterface(Ci.nsIInterfaceRequestor)
-                           .getInterface(Ci.nsIDOMWindow);
-        win.addEventListener("load", function() {
-          if (win.document.documentElement.getAttribute("windowtype") !=
-              WINDOW_TYPE) {
-            return;
-          }
-          // Found the window
-          resolve(win);
-          Services.wm.removeListener(windowListener);
-        }, {once: true});
-      },
-      onCloseWindow() {},
-      onWindowTitleChange() {}
-    };
-    Services.wm.addListener(windowListener);
-  });
+/**
+ * Wait for view source tab or window after calling given function to open it.
+ *
+ * @param open - a function to open view source.
+ * @returns the new tab or window which shows the source.
+ */
+async function waitForViewSourceTabOrWindow(open) {
+  let sourceLoadedPromise;
+  let tabOrWindowPromise;
+  if (Services.prefs.getBoolPref("view_source.tab")) {
+    tabOrWindowPromise = new Promise(resolve => {
+      gBrowser.tabContainer.addEventListener("TabOpen", event => {
+        let tab = event.target;
+        sourceLoadedPromise = waitForSourceLoaded(tab);
+        resolve(tab);
+      }, { once: true });
+    });
+  } else {
+    tabOrWindowPromise = new Promise(resolve => {
+      let windowListener = {
+        onOpenWindow(xulWindow) {
+          let win = xulWindow.QueryInterface(Ci.nsIInterfaceRequestor)
+              .getInterface(Ci.nsIDOMWindow);
+          win.addEventListener("load", function() {
+            if (win.document.documentElement.getAttribute("windowtype") !=
+                WINDOW_TYPE) {
+              return;
+            }
+            // Found the window
+            sourceLoadedPromise = waitForSourceLoaded(win);
+            resolve(win);
+            Services.wm.removeListener(windowListener);
+          }, {once: true});
+        },
+        onCloseWindow() {},
+        onWindowTitleChange() {}
+      };
+      Services.wm.addListener(windowListener);
+    });
+  }
+
+  await open();
+
+  let tabOrWindow = await tabOrWindowPromise;
+  await sourceLoadedPromise;
+
+  return tabOrWindow;
 }
 
 /**
  * Opens view source for a browser.
  *
  * @param browser - the <xul:browser> to open view source for.
  * @returns the new tab or window which shows the source.
  */
 function openViewSource(browser) {
-  let openPromise;
-  if (Services.prefs.getBoolPref("view_source.tab")) {
-    openPromise = BrowserTestUtils.waitForNewTab(gBrowser, null);
-  } else {
-    openPromise = waitForViewSourceWindow();
-  }
-
-  window.BrowserViewSource(browser);
-
-  return openPromise;
+  return waitForViewSourceTabOrWindow(() => {
+    window.BrowserViewSource(browser);
+  });
 }
 
 /**
  * Opens a view source tab / window for a selection (View Selection Source)
  * within the currently selected browser in gBrowser.
  *
  * @param aCSSSelector - used to specify a node within the selection to
  *                       view the source of. It is expected that this node is
@@ -102,30 +121,23 @@ async function openViewPartialSource(aCS
   let contentAreaContextMenuPopup =
     document.getElementById("contentAreaContextMenu");
   let popupShownPromise =
     BrowserTestUtils.waitForEvent(contentAreaContextMenuPopup, "popupshown");
   await BrowserTestUtils.synthesizeMouseAtCenter(aCSSSelector,
           { type: "contextmenu", button: 2 }, gBrowser.selectedBrowser);
   await popupShownPromise;
 
-  let openPromise;
-  if (Services.prefs.getBoolPref("view_source.tab")) {
-    openPromise = BrowserTestUtils.waitForNewTab(gBrowser, null);
-  } else {
-    openPromise = waitForViewSourceWindow();
-  }
-
-  let popupHiddenPromise =
-    BrowserTestUtils.waitForEvent(contentAreaContextMenuPopup, "popuphidden");
-  let item = document.getElementById("context-viewpartialsource-selection");
-  EventUtils.synthesizeMouseAtCenter(item, {});
-  await popupHiddenPromise;
-
-  return openPromise;
+  return waitForViewSourceTabOrWindow(async () => {
+    let popupHiddenPromise =
+        BrowserTestUtils.waitForEvent(contentAreaContextMenuPopup, "popuphidden");
+    let item = document.getElementById("context-viewpartialsource-selection");
+    EventUtils.synthesizeMouseAtCenter(item, {});
+    await popupHiddenPromise;
+  });
 }
 
 /**
  * Opens a view source tab for a frame (View Frame Source) within the
  * currently selected browser in gBrowser.
  *
  * @param aCSSSelector - used to specify the frame to view the source of.
  * @returns the new tab which shows the source.
@@ -140,25 +152,23 @@ async function openViewFrameSourceTab(aC
   await popupShownPromise;
 
   let frameContextMenu = document.getElementById("frame");
   popupShownPromise =
     BrowserTestUtils.waitForEvent(frameContextMenu, "popupshown");
   EventUtils.synthesizeMouseAtCenter(frameContextMenu, {});
   await popupShownPromise;
 
-  let newTabPromise = BrowserTestUtils.waitForNewTab(gBrowser, null);
-
-  let popupHiddenPromise =
-    BrowserTestUtils.waitForEvent(frameContextMenu, "popuphidden");
-  let item = document.getElementById("context-viewframesource");
-  EventUtils.synthesizeMouseAtCenter(item, {});
-  await popupHiddenPromise;
-
-  return newTabPromise;
+  return waitForViewSourceTabOrWindow(async () => {
+    let popupHiddenPromise =
+        BrowserTestUtils.waitForEvent(frameContextMenu, "popuphidden");
+    let item = document.getElementById("context-viewframesource");
+    EventUtils.synthesizeMouseAtCenter(item, {});
+    await popupHiddenPromise;
+  });
 }
 
 registerCleanupFunction(function() {
   var windows = Services.wm.getEnumerator(WINDOW_TYPE);
   ok(!windows.hasMoreElements(), "No remaining view source windows still open");
   while (windows.hasMoreElements())
     windows.getNext().close();
 });
@@ -193,22 +203,17 @@ async function openDocumentSelect(aURI, 
     gBrowser.removeTab(tab);
   });
 
   await ContentTask.spawn(gBrowser.selectedBrowser, { selector: aCSSSelector }, async function(arg) {
     let element = content.document.querySelector(arg.selector);
     content.getSelection().selectAllChildren(element);
   });
 
-  let tabOrWindow = await openViewPartialSource(aCSSSelector);
-
-  // Wait until the source has been loaded.
-  await waitForSourceLoaded(tabOrWindow);
-
-  return tabOrWindow;
+  return openViewPartialSource(aCSSSelector);
 }
 
 function pushPrefs(...aPrefs) {
   return SpecialPowers.pushPrefEnv({"set": aPrefs});
 }
 
 function waitForPrefChange(pref) {
   let deferred = PromiseUtils.defer();
--- a/toolkit/mozapps/extensions/test/xpinstall/browser_bug638292.js
+++ b/toolkit/mozapps/extensions/test/xpinstall/browser_bug638292.js
@@ -1,24 +1,22 @@
 // ----------------------------------------------------------------------------
 // Test whether an InstallTrigger.enabled is working
 add_task(async function() {
   let testtab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TESTROOT + "bug638292.html");
 
   async function verify(link, button) {
     info("Clicking " + link);
 
-    let waitForNewTabPromise = BrowserTestUtils.waitForNewTab(gBrowser);
+    let loadedPromise = BrowserTestUtils.waitForNewTab(gBrowser, null, true);
 
     await BrowserTestUtils.synthesizeMouseAtCenter("#" + link, { button },
                                                    gBrowser.selectedBrowser);
 
-    let newtab = await waitForNewTabPromise;
-
-    await BrowserTestUtils.browserLoaded(newtab.linkedBrowser);
+    let newtab = await loadedPromise;
 
     let result = await ContentTask.spawn(newtab.linkedBrowser, { }, async function() {
       return (content.document.getElementById("enabled").textContent == "true");
     });
 
     ok(result, "installTrigger for " + link + " should have been enabled");
 
     // Focus the old tab (link3 is opened in the background)