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

This commit is contained in:
Tooru Fujisawa 2017-11-24 07:50:12 +09:00
parent d86f11e5b1
commit 9a3aa36ec4
8 changed files with 101 additions and 84 deletions

View File

@ -153,9 +153,7 @@ function delayedStartupFinished(aWindow) {
* @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);
}
/**

View File

@ -131,20 +131,27 @@ add_task(async function test_mixedcontent() {
* 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;

View File

@ -22,7 +22,7 @@ add_task(async function() {
});
// 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");
});
@ -33,7 +33,6 @@ add_task(async function() {
});
let openedBrowser = openedTab.linkedBrowser;
await BrowserTestUtils.browserLoaded(openedBrowser);
// Ensure that new file:// tab can be navigated to web content.
openedBrowser.loadURI("http://example.org/");

View File

@ -102,11 +102,18 @@ add_task(async function() {
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");
@ -138,11 +145,18 @@ add_task(async function() {
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");

View File

@ -30,7 +30,6 @@ add_task(async function test_view_source_in_tab() {
}, 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",
@ -58,7 +57,6 @@ add_task(async function test_view_source_in_window() {
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");

View File

@ -16,8 +16,6 @@ async function checkFrameSource() {
gBrowser.removeTab(sourceTab);
});
await waitForSourceLoaded(sourceTab);
let browser = gBrowser.selectedBrowser;
let textContent = await ContentTask.spawn(browser, {}, async function() {
return content.document.body.textContent;

View File

@ -47,27 +47,53 @@ function testViewSourceWindow(aURI, aTestCallback, 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;
}
/**
@ -77,16 +103,9 @@ function waitForViewSourceWindow() {
* @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);
});
}
/**
@ -107,20 +126,13 @@ async function openViewPartialSource(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;
});
}
/**
@ -145,15 +157,13 @@ async function openViewFrameSourceTab(aCSSSelector) {
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() {
@ -198,12 +208,7 @@ async function openDocumentSelect(aURI, aCSSSelector) {
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) {

View File

@ -6,14 +6,12 @@ add_task(async function() {
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");