Bug 1402707 - Change BrowserTestUtils.waitForNewTab to allow waiting for tabs to open a new URL when the tab might not be the next one opened. r?mak
MozReview-Commit-ID: HNFEC6BuBit
--- a/testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm
+++ b/testing/mochitest/BrowserTestUtils/BrowserTestUtils.jsm
@@ -365,39 +365,45 @@ this.BrowserTestUtils = {
};
browser.addProgressListener(wpl);
this._webProgressListeners.add(wpl);
dump("Waiting for browser load" + (expectedURI ? (" of " + expectedURI) : "") + "\n");
});
},
/**
- * Waits for the next tab to open and load a given URL.
+ * Waits for a tab to open and load a given URL.
*
* The method doesn't wait for the tab contents to load.
*
* @param {tabbrowser} tabbrowser
* The tabbrowser to look for the next new tab in.
* @param {string} url
* A string URL to look for in the new tab. If null, allows any non-blank URL.
* @param {boolean} waitForLoad
* True to wait for the page in the new tab to load. Defaults to false.
+ * @param {boolean} waitForAnyTab
+ * True to wait for the url to be loaded in any new tab, not just the next
+ * one opened.
*
* @return {Promise}
* @resolves With the {xul:tab} when a tab is opened and its location changes
* to the given URL and optionally that browser has loaded.
*
* NB: this method will not work if you open a new tab with e.g. BrowserOpenTab
* and the tab does not load a URL, because no onLocationChange will fire.
*/
- waitForNewTab(tabbrowser, url, waitForLoad = false) {
+ waitForNewTab(tabbrowser, url, waitForLoad = false, waitForAnyTab = false) {
let urlMatches = url ? (urlToMatch) => urlToMatch == url
: (urlToMatch) => urlToMatch != "about:blank";
return new Promise((resolve, reject) => {
- tabbrowser.tabContainer.addEventListener("TabOpen", function(openEvent) {
+ tabbrowser.tabContainer.addEventListener("TabOpen", function tabOpenListener(openEvent) {
+ if (!waitForAnyTab) {
+ tabbrowser.tabContainer.removeEventListener("TabOpen", tabOpenListener);
+ }
let newTab = openEvent.target;
let newBrowser = newTab.linkedBrowser;
let result;
if (waitForLoad) {
// If waiting for load, resolve with promise for that, which when load
// completes resolves to the new tab.
result = BrowserTestUtils.browserLoaded(newBrowser, false, urlMatches)
.then(() => newTab);
@@ -412,24 +418,25 @@ this.BrowserTestUtils = {
if (aBrowser != newBrowser) {
return;
}
// Check that new location is the URL we want.
if (!urlMatches(aBrowser.currentURI.spec)) {
return;
}
-
+ if (waitForAnyTab) {
+ tabbrowser.tabContainer.removeEventListener("TabOpen", tabOpenListener);
+ }
tabbrowser.removeTabsProgressListener(progressListener);
resolve(result);
},
};
tabbrowser.addTabsProgressListener(progressListener);
-
- }, {once: true});
+ });
});
},
/**
* Waits for onLocationChange.
*
* @param {tabbrowser} tabbrowser
* The tabbrowser to wait for the location change on.