Bug 1153289, add some tab switching methods to BrowserTestUtils.jsm, r=felipe

This commit is contained in:
Neil Deakin 2015-04-16 15:38:13 -04:00
Родитель af1ece91c4
Коммит 264ef51639
1 изменённых файлов: 66 добавлений и 5 удалений

Просмотреть файл

@ -51,15 +51,76 @@ this.BrowserTestUtils = {
* @rejects Any exception from taskFn is propagated.
*/
withNewTab: Task.async(function* (options, taskFn) {
let tab = options.gBrowser.addTab(options.url);
yield BrowserTestUtils.browserLoaded(tab.linkedBrowser);
options.gBrowser.selectedTab = tab;
let tab = yield BrowserTestUtils.openNewForegroundTab(options.gBrowser, options.url);
yield taskFn(tab.linkedBrowser);
options.gBrowser.removeTab(tab);
}),
/**
* Opens a new tab in the foreground.
*
* @param {tabbrowser} tabbrowser
* The tabbrowser to open the tab new in.
* @param {string} opening
* May be either a string URL to load in the tab, or a function that
* will be called to open a foreground tab. Defaults to "about:blank".
* @param {boolean} waitForLoad
* True to wait for the page in the new tab to load. Defaults to true.
*
* @return {Promise}
* Resolves when the tab is ready and loaded as necessary.
* @resolves The new tab.
*/
openNewForegroundTab(tabbrowser, opening = "about:blank", aWaitForLoad = true) {
let tab;
let promises = [
BrowserTestUtils.switchTab(tabbrowser, function () {
if (typeof opening == "function") {
opening();
tab = tabbrowser.selectedTab;
}
else {
tabbrowser.selectedTab = tab = tabbrowser.addTab(opening);
}
})
];
if (aWaitForLoad) {
promises.push(BrowserTestUtils.browserLoaded(tab.linkedBrowser));
}
return Promise.all(promises).then(() => tab);
},
/**
* Switches to a tab and resolves when it is ready.
*
* @param {tabbrowser} tabbrowser
* The tabbrowser.
* @param {tab} tab
* Either a tab element to switch to or a function to perform the switch.
*
* @return {Promise}
* Resolves when the tab has been switched to.
* @resolves The tab switched to.
*/
switchTab(tabbrowser, tab) {
let promise = new Promise(resolve => {
tabbrowser.addEventListener("TabSwitchDone", function onSwitch() {
tabbrowser.removeEventListener("TabSwitchDone", onSwitch);
TestUtils.executeSoon(() => resolve(tabbrowser.selectedTab));
});
});
if (typeof tab == "function") {
tab();
}
else {
tabbrowser.selectedTab = tab;
}
return promise;
},
/**
* Waits for an ongoing page load in a browser window to complete.
*