Bug 1292239 - Add utility to BrowserTestUtils to wait for load of selected browser in a brand new window. r=Felipe, a=test-only
authorMike Conley <mconley@mozilla.com>
Thu, 04 Aug 2016 15:48:31 -0400
changeset 349944 3a73ccd4e90e826d14b54d084d0fb3768271796c
parent 349943 95e4986d9803655d61e2975b9f06fdaf1937c79f
child 349945 458d5f0a55bbb2423091a2108cab633321a64d26
push id1230
push userjlund@mozilla.com
push dateMon, 31 Oct 2016 18:13:35 +0000
treeherdermozilla-release@5e06e3766db2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersFelipe, test-only
bugs1292239
milestone50.0a2
Bug 1292239 - Add utility to BrowserTestUtils to wait for load of selected browser in a brand new window. r=Felipe, a=test-only MozReview-Commit-ID: IOChbNs8b3y
testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm
--- a/testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm
+++ b/testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm
@@ -200,16 +200,37 @@ this.BrowserTestUtils = {
           mm.removeMessageListener("browser-test-utils:loadEvent", onLoad);
           resolve(msg.data.url);
         }
       });
     });
   },
 
   /**
+   * Waits for the selected browser to load in a new window. This
+   * is most useful when you've got a window that might not have
+   * loaded its DOM yet, and where you can't easily use browserLoaded
+   * on gBrowser.selectedBrowser since gBrowser doesn't yet exist.
+   *
+   * @param {win}
+   *        A newly opened window for which we're waiting for the
+   *        first browser load.
+   *
+   * @return {Promise}
+   * @resolves Once the selected browser fires its load event.
+   */
+  firstBrowserLoaded(win) {
+    let mm = win.messageManager;
+    return this.waitForMessage(mm, "browser-test-utils:loadEvent", (msg) => {
+      let selectedBrowser = win.gBrowser.selectedBrowser;
+      return msg.target == selectedBrowser;
+    });
+  },
+
+  /**
    * Waits for the web progress listener associated with this tab to fire a
    * STATE_STOP for the toplevel document.
    *
    * @param {xul:browser} browser
    *        A xul:browser.
    *
    * @return {Promise}
    * @resolves When STATE_STOP reaches the tab's progress listener
@@ -428,21 +449,21 @@ this.BrowserTestUtils = {
 
     let win = Services.ww.openWindow(
       null, Services.prefs.getCharPref("browser.chromeURL"), "_blank",
       features, argString);
 
     // Wait for browser-delayed-startup-finished notification, it indicates
     // that the window has loaded completely and is ready to be used for
     // testing.
-    yield this.waitForEvent(win, "load");
     let startupPromise =
       TestUtils.topicObserved("browser-delayed-startup-finished",
                               subject => subject == win).then(() => win);
-    let loadPromise = this.browserLoaded(win.gBrowser.selectedBrowser);
+
+    let loadPromise = this.firstBrowserLoaded(win);
 
     yield startupPromise;
     yield loadPromise;
 
     return win;
   }),
 
   /**