Bug 1492179 - make browser_bug767836_perwindowpb.js not use CPOWs, and use modern async test tools instead of callbacks, r=mconley

Differential Revision: https://phabricator.services.mozilla.com/D6160

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Gijs Kruitbosch 2018-09-18 17:29:13 +00:00
Родитель 924a6f290b
Коммит a7981b4051
1 изменённых файлов: 51 добавлений и 67 удалений

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

@ -3,85 +3,69 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
function test() {
// initialization
waitForExplicitFinish();
let gNewTabService = Cc["@mozilla.org/browser/aboutnewtab-service;1"]
.getService(Ci.nsIAboutNewTabService);
let aboutNewTabService = Cc["@mozilla.org/browser/aboutnewtab-service;1"]
.getService(Ci.nsIAboutNewTabService);
async function doTest(isPrivate) {
let win = await BrowserTestUtils.openNewBrowserWindow({private: isPrivate});
let defaultURL = gNewTabService.newTabURL;
let newTabURL;
let testURL = "http://example.com/";
let defaultURL = aboutNewTabService.newTabURL;
let mode;
function doTest(aIsPrivateMode, aWindow, aCallback) {
openNewTab(aWindow, function() {
if (aIsPrivateMode) {
mode = "per window private browsing";
newTabURL = "about:privatebrowsing";
} else {
mode = "normal";
newTabURL = "about:newtab";
}
// Check the new tab opened while in normal/private mode
is(aWindow.gBrowser.selectedBrowser.currentURI.spec, newTabURL,
"URL of NewTab should be " + newTabURL + " in " + mode + " mode");
// Set the custom newtab url
aboutNewTabService.newTabURL = testURL;
is(aboutNewTabService.newTabURL, testURL, "Custom newtab url is set");
// Open a newtab after setting the custom newtab url
openNewTab(aWindow, function() {
is(aWindow.gBrowser.selectedBrowser.currentURI.spec, testURL,
"URL of NewTab should be the custom url");
// Clear the custom url.
aboutNewTabService.resetNewTabURL();
is(aboutNewTabService.newTabURL, defaultURL, "No custom newtab url is set");
aWindow.gBrowser.removeTab(aWindow.gBrowser.selectedTab);
aWindow.gBrowser.removeTab(aWindow.gBrowser.selectedTab);
aWindow.close();
aCallback();
});
});
let testURL = "http://example.com/";
if (isPrivate) {
mode = "per window private browsing";
newTabURL = "about:privatebrowsing";
} else {
mode = "normal";
newTabURL = "about:newtab";
}
function testOnWindow(aIsPrivate, aCallback) {
whenNewWindowLoaded({private: aIsPrivate}, function(win) {
executeSoon(() => aCallback(win));
});
}
await openNewTab(win, newTabURL);
// Check the new tab opened while in normal/private mode
is(win.gBrowser.selectedBrowser.currentURI.spec, newTabURL,
"URL of NewTab should be " + newTabURL + " in " + mode + " mode");
// check whether any custom new tab url has been configured
ok(!aboutNewTabService.overridden, "No custom newtab url is set");
// Set the custom newtab url
gNewTabService.newTabURL = testURL;
is(gNewTabService.newTabURL, testURL, "Custom newtab url is set");
// test normal mode
testOnWindow(false, function(aWindow) {
doTest(false, aWindow, function() {
// test private mode
testOnWindow(true, function(aWindow2) {
doTest(true, aWindow2, function() {
finish();
});
});
});
});
// Open a newtab after setting the custom newtab url
await openNewTab(win, testURL);
is(win.gBrowser.selectedBrowser.currentURI.spec, testURL,
"URL of NewTab should be the custom url");
// Clear the custom url.
gNewTabService.resetNewTabURL();
is(gNewTabService.newTabURL, defaultURL, "No custom newtab url is set");
win.gBrowser.removeTab(win.gBrowser.selectedTab);
win.gBrowser.removeTab(win.gBrowser.selectedTab);
await BrowserTestUtils.closeWindow(win);
}
function openNewTab(aWindow, aCallback) {
add_task(async function test_newTabService() {
// check whether any custom new tab url has been configured
ok(!gNewTabService.overridden, "No custom newtab url is set");
// test normal mode
await doTest(false);
// test private mode
await doTest(true);
});
async function openNewTab(aWindow, aExpectedURL) {
// Open a new tab
aWindow.BrowserOpenTab();
let browser = aWindow.gBrowser.selectedBrowser;
let doc = browser.contentDocumentAsCPOW;
if (doc && doc.readyState === "complete") {
executeSoon(aCallback);
return;
}
BrowserTestUtils.browserLoaded(browser).then(() => {
executeSoon(aCallback);
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;
});
if (!alreadyLoaded) {
await loadPromise;
}
}