Bug 1095236 - Simplify browser_test_new_window_from_content.js to use BrowserTestUtils. r=mrbkap

--HG--
extra : commitid : Ia8rS8t1WAn
extra : rebase_source : e5a9f99eb8c900dacb363658973050f78320be29
extra : histedit_source : 28435fa0ad3292ca096f79b53c91ef9ea6305ffd
This commit is contained in:
Mike Conley 2015-10-02 19:44:48 -04:00
Родитель c617698d29
Коммит b98d93ada9
4 изменённых файлов: 36 добавлений и 197 удалений

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

@ -26,7 +26,6 @@ skip-if= buildapp == 'mulet'
skip-if = (toolkit == 'android' || buildapp == 'b2g' || buildapp == 'mulet')
support-files =
test_new_window_from_content_child.html
test_new_window_from_content_child.js
[browser_webapps_permissions.js]
# TODO: Re-enable permissions tests on Mac, bug 795334
skip-if = buildapp != "b2g"

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

@ -35,10 +35,7 @@
each preference.
*/
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/Promise.jsm");
Cu.import("resource://gre/modules/Task.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
const kXULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
const kContentDoc = "http://www.example.com/browser/dom/tests/browser/test_new_window_from_content_child.html";
@ -91,117 +88,8 @@ registerCleanupFunction(function() {
Services.prefs.setIntPref(kNewWindowPrefKey, originalNewWindowPref);
Services.prefs.setIntPref(kNewWindowRestrictionPrefKey,
originalNewWindowRestrictionPref);
// If there are any content tabs leftover, make sure they're not going to
// block exiting with onbeforeunload.
for (let tab of gBrowser.tabs) {
let browser = gBrowser.getBrowserForTab(tab);
if (browser.contentDocument.location == kContentDoc) {
closeTab(tab);
}
}
});
/**
* WindowOpenListener is a very simple nsIWindowMediatorListener that
* listens for a new window opening to aExpectedURI. It has two Promises
* attached to it - openPromise and closePromise. As you'd expect,
* openPromise resolves when the window is opened, and closePromise
* resolves if and when a window with the same URI closes. There is
* no attempt to make sure that it's the same window opening and
* closing - we just use the URI.
*
* @param aExpectedURI the URI to watch for in a new window.
* @return nsIWindowMediatorListener
*/
function WindowOpenListener(aExpectedURI) {
this._openDeferred = Promise.defer();
this._closeDeferred = Promise.defer();
this._expectedURI = aExpectedURI;
}
WindowOpenListener.prototype = {
get openPromise() {
return this._openDeferred.promise;
},
get closePromise() {
return this._closeDeferred.promise;
},
onOpenWindow: function(aXULWindow) {
let domWindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindow);
let location = domWindow.document.location;
if (location == this._expectedURI) {
let deferred = this._openDeferred;
domWindow.addEventListener("load", function onWindowLoad() {
domWindow.removeEventListener("load", onWindowLoad);
deferred.resolve(domWindow);
})
}
},
onCloseWindow: function(aXULWindow) {
this._closeDeferred.resolve();
},
onWindowTitleChange: function(aXULWindow, aNewTitle) {},
QueryInterface: XPCOMUtils.generateQI([Ci.nsIWindowMediatorListener])
};
/**
* Adds the testing tab, and injects our frame script which
* allows us to send click events to links and other things.
*
* @return a Promise that resolves once the tab is loaded and ready.
*/
function loadAndSelectTestTab() {
let tab = gBrowser.addTab(kContentDoc);
gBrowser.selectedTab = tab;
let browser = gBrowser.getBrowserForTab(tab);
browser.messageManager.loadFrameScript(kContentScript, false);
let deferred = Promise.defer();
browser.addEventListener("DOMContentLoaded", function onBrowserLoad(aEvent) {
browser.removeEventListener("DOMContentLoaded", onBrowserLoad);
deferred.resolve(tab);
});
return deferred.promise;
}
/**
* Clears the onbeforeunload event handler from the testing tab,
* and then closes the tab.
*
* @param aTab the testing tab to close.
* @param a Promise that resolves once the tab has been closed.
*/
function closeTab(aTab) {
let deferred = Promise.defer();
let browserMM = gBrowser.getBrowserForTab(aTab).messageManager;
browserMM.sendAsyncMessage("TEST:allow-unload");
browserMM.addMessageListener("TEST:allow-unload:done", function(aMessage) {
gBrowser.removeTab(aTab);
deferred.resolve();
})
return deferred.promise;
}
/**
* Sends a click event on some item into a tab.
*
* @param aTab the tab to send the click event to
* @param aItemId the item within the tab content to click.
*/
function clickInTab(aTab, aItemId) {
let browserMM = gBrowser.getBrowserForTab(aTab).messageManager;
browserMM.sendAsyncMessage("TEST:click-item", {
details: aItemId,
});
}
/**
* For some expectation when a link is clicked, creates and
* returns a Promise that resolves when that expectation is
@ -213,62 +101,38 @@ function clickInTab(aTab, aItemId) {
* occurred - for example, if a new window was opened, this function
* closes it before resolving.
*
* @param aTab the tab with the test document
* @param aBrowser the <xul:browser> with the test document
* @param aExpectation one of kSameTab, kNewWin, or kNewTab.
* @return a Promise that resolves when the expectation is fulfilled,
* and cleaned up after.
*/
function prepareForResult(aTab, aExpectation) {
let deferred = Promise.defer();
let browser = gBrowser.getBrowserForTab(aTab);
function prepareForResult(aBrowser, aExpectation) {
switch(aExpectation) {
case kSameTab:
// We expect about:blank to be loaded in the current tab. In order
// to prevent us needing to reload the document and content script
// after browsing away, we'll detect the attempt by using onbeforeunload,
// and then cancel the unload. It's a hack, but it's also a pretty
// cheap way of detecting when we're browsing away in the test tab.
// The onbeforeunload event handler is set in the content script automatically.
browser.addEventListener("DOMWillOpenModalDialog", function onModalDialog() {
browser.removeEventListener("DOMWillOpenModalDialog", onModalDialog, true);
executeSoon(() => {
let stack = browser.parentNode;
let dialogs = stack.getElementsByTagNameNS(kXULNS, "tabmodalprompt");
dialogs[0].ui.button1.click()
deferred.resolve();
})
}, true);
return Task.spawn(function*() {
yield BrowserTestUtils.browserLoaded(aBrowser);
is(aBrowser.currentURI.spec, "about:robots", "Should be at about:robots");
// Now put the browser back where it came from
yield BrowserTestUtils.loadURI(aBrowser, kContentDoc);
yield BrowserTestUtils.browserLoaded(aBrowser);
});
break;
case kNewWin:
let listener = new WindowOpenListener("about:blank");
Services.wm.addListener(listener);
info("Waiting for a new about:blank window");
listener.openPromise.then(function(aWindow) {
info("Got the new about:blank window - closing it.");
executeSoon(() => {
aWindow.close();
});
listener.closePromise.then(() => {
info("New about:blank window closed!");
Services.wm.removeListener(listener);
deferred.resolve();
});
return Task.spawn(function*() {
let newWin = yield BrowserTestUtils.waitForNewWindow();
let newBrowser = newWin.gBrowser.selectedBrowser;
yield BrowserTestUtils.browserLoaded(newBrowser);
is(newBrowser.currentURI.spec, "about:robots", "Should be at about:robots");
yield BrowserTestUtils.closeWindow(newWin);
});
break;
case kNewTab:
gBrowser.tabContainer.addEventListener("TabOpen", function onTabOpen(aEvent) {
let newTab = aEvent.target;
let newBrowser = gBrowser.getBrowserForTab(newTab);
if (newBrowser.contentDocument.location.href == "about:blank") {
gBrowser.tabContainer.removeEventListener("TabOpen", onTabOpen);
executeSoon(() => {
gBrowser.removeTab(newTab);
deferred.resolve();
})
}
})
return Task.spawn(function*() {
let newTab = yield BrowserTestUtils.waitForNewTab(gBrowser);
is(newTab.linkedBrowser.currentURI.spec, "about:robots",
"Should be at about:robots");
yield BrowserTestUtils.removeTab(newTab);
});
break;
default:
ok(false, "prepareForResult can't handle an expectation of " + aExpectation)
@ -283,14 +147,16 @@ function prepareForResult(aTab, aExpectation) {
* perform as specified in the supplied aMatrix (kWinOpenDefault,
* for example).
*
* @param aLinkId the id of the link within the testing page to test.
* @param aLinkSelector a selector for the link within the testing page to click.
* @param aMatrix a testing matrix for the
* browser.link.open_newwindow and browser.link.open_newwindow.restriction
* prefs to test against. See kWinOpenDefault for an example.
*/
function testLinkWithMatrix(aLinkId, aMatrix) {
return Task.spawn(function* () {
let tab = yield loadAndSelectTestTab();
function testLinkWithMatrix(aLinkSelector, aMatrix) {
return BrowserTestUtils.withNewTab({
gBrowser,
url: kContentDoc,
}, function*(browser) {
// This nested for-loop is unravelling the matrix const
// we set up, and gives us three things through each tick
// of the inner loop:
@ -298,7 +164,6 @@ function testLinkWithMatrix(aLinkId, aMatrix) {
// 2) newWindowRestPref: a browser.link.open_newwindow.restriction pref to try
// 3) expectation: what we expect the click outcome on this link to be,
// which will either be kSameTab, kNewWin or kNewTab.
for (let newWindowPref in aMatrix) {
let expectations = aMatrix[newWindowPref];
for (let i = 0; i < expectations.length; ++i) {
@ -307,28 +172,27 @@ function testLinkWithMatrix(aLinkId, aMatrix) {
Services.prefs.setIntPref("browser.link.open_newwindow", newWindowPref);
Services.prefs.setIntPref("browser.link.open_newwindow.restriction", newWindowRestPref);
info("Clicking on " + aLinkId);
info("Clicking on " + aLinkSelector);
info("Testing with browser.link.open_newwindow = " + newWindowPref + " and " +
"browser.link.open_newwindow.restriction = " + newWindowRestPref);
info("Expecting: " + expectation);
let resultPromise = prepareForResult(tab, expectation);
clickInTab(tab, aLinkId);
let resultPromise = prepareForResult(browser, expectation);
BrowserTestUtils.synthesizeMouseAtCenter(aLinkSelector, {}, browser);
yield resultPromise;
ok(true, "Got expectation: " + expectation);
info("Got expectation: " + expectation);
}
}
yield closeTab(tab);
});
}
add_task(function* test_window_open_with_defaults() {
yield testLinkWithMatrix("winOpenDefault", kWinOpenDefault);
yield testLinkWithMatrix("#winOpenDefault", kWinOpenDefault);
});
add_task(function* test_window_open_with_non_defaults() {
yield testLinkWithMatrix("winOpenNonDefault", kWinOpenNonDefault);
yield testLinkWithMatrix("#winOpenNonDefault", kWinOpenNonDefault);
});
add_task(function* test_target__blank() {
yield testLinkWithMatrix("targetBlank", kTargetBlank);
yield testLinkWithMatrix("#targetBlank", kTargetBlank);
});

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

@ -6,18 +6,13 @@
<body>
<p><a id="winOpenDefault" href="#" onclick="return openWindow();">Open a new window via window.open with default features.</a></p>
<p><a id="winOpenNonDefault" href="#" onclick="return openWindow('resizable=no, toolbar=no, scrollbars=no, menubar=no, status=no, directories=no, height=100, width=500');">Open a new window via window.open with non-default features.</a></p>
<p><a id="targetBlank" href="about:blank" target="_blank">Open a new window via target="_blank".</a></p>
<p><a id="targetBlank" href="about:robots" target="_blank">Open a new window via target="_blank".</a></p>
</body>
</html>
<script>
function openWindow(aFeatures="") {
window.open("about:blank", "_blank", aFeatures);
window.open("about:robots", "_blank", aFeatures);
return false;
}
window.onbeforeunload = function(aEvent) {
return "We should not browse away from this document.";
}
</script>

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

@ -1,19 +0,0 @@
// A hacky mechanism for catching and detecting that we're attempting
// to browse away is by setting the onbeforeunload event handler. We
// detect this dialog opening in the parent test script, and dismiss
// it.
function handleClickItem(aMessage) {
let itemId = aMessage.data.details;
content.console.log("Getting item with ID: " + itemId);
let item = content.document.getElementById(itemId);
item.click();
}
function handleAllowUnload(aMessage) {
content.onbeforeunload = null;
sendSyncMessage("TEST:allow-unload:done");
}
addMessageListener("TEST:click-item", handleClickItem);
addMessageListener("TEST:allow-unload", handleAllowUnload);