fune/toolkit/components/extensions/test/xpcshell/test_ext_contentscript.js
Boris Zbarsky 2e5c884144 Bug 1489308 part 5. Align the work we do on document.open with the spec. r=mccr8,smaug
The main behavior changes are:

1) We no longer create a new Window when doing document.open().  We use the
same Window but remove all the event listeners on it and on the existing DOM
tree before removing the document's existing kids.

2) We no longer create a new session history entry.  The existing one always
gets replaced instead.

3) We now support document.open on documents that are not in a Window.

The reasons for the various test changes are as follows:

The change to browser_modifiedclick_inherit_principal.js is because we no
longer set the docshell to a wyciwyg URL when document.open() happens and the
test was depending on that to terminate.

browser_wyciwyg_urlbarCopying.js is being removed because it's trying to test
wyciwyg URIs, which no longer exist.

The changes in docshell/test/navigation are because document.open() no longer
affects session history.  One of the tests was testing the interactions there
and is being removed; another is being repurposed to just test that
document.open() does not affect history.length.

The change to test_x-frame-options.html is because document.open() now removes
event listeners on the window, which it didn't use to do (and in the specific
case in this test reused the existing inner too, so the listener was still
around in practice).  The new behavior matches other browsers.

The removal of test_bug172261.html is because document.open() no longer affects
session history, so you can't go back across it or forward to the "opened"
state, so the situation that test is trying to test no longer exists.

The changes to test_bug255820.html are because reloading a document after
document.open() will now just load the URL of the document that was the entry
document for the open() call, not reload the written content.  So there's not
much point testing reload behavior, and in this test it was just reloading the
toplevel test file inside the frames.

The change to test_bug346659.html is because now we no longer create a new
Window on document.open().

The change to test_bug1232829.html is because document.open() (implicit in this
test) no longer adds history entries, so the back() was just leaving the test
page instead of going back across the document.open().  The test is a
crashtest in practice, so might still be testing something useful about how
document.open() interacts with animations.

The change to test_bug715739.html is because the URL of the document after
document.open() is now the URL of the entry document, not a wyciwyg URL, so
reload() has different behavior than it used to.

The change to test_bug329869.html is because now when we go back we're
reloading the original document we had, not doing a wyciwyg load, and the
security info now doesn't include the untrusted script.

The changes to the wpt expectations are removing a bunch of expected failures
now that we pass those tests and disabling some tests that are fundamentally
racy and hence fail randomly.  The latter all have github issues filed for the
test problem.

The change to testing/web-platform/tests/common/object-association.js is fixing
tests that were not matching the spec (and were failing in other browsers).

The change to parser-uses-registry-of-owner-document.html is fixing tests that
were not matching the spec (and were failing in other browsers).

The change to document-write.tentative.html is because the test was buggy: it
was using the same iframe element for all its tests and racing loads from some
tests against API calls from other tests, etc.  It's a wonder it ever managed
to pass, independent of these patches (and in fact it doesn't pass according to
wpt.fyi data, even in Firefox).

The changes in html/browsers/history/the-history-interface are because
document.open() no longer adds history entries.  The test was failing in all
other browsers for the same reason.

The changes in html/browsers/history/the-location-interface are because
reloading a document.open()-created thing now loads the URL of the page that
was the entry document for the open() call.  The test was failing in all other
browsers.

The change to reload_document_open_write.html is because we now reload the url
of the document that entered the script that called open() when we reload, not
the written content.  Other browsers were failing this test too; Gecko with
the old document.open implementation was the only one that passed.

The change to http-refresh.py is to fix a test bug: it was not returning a
Content-Type header, so we were putting up helper app dialogs, etc.

The change to test_ext_contentscript.js is because we no create a new global
for document.open() calls.  Kris Maglione OKed this part.

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

--HG--
extra : moz-landing-system : lando
2019-02-27 23:24:48 +00:00

236 lines
7.2 KiB
JavaScript

"use strict";
const server = createHttpServer();
server.registerDirectory("/data/", do_get_file("data"));
const BASE_URL = `http://localhost:${server.identity.primaryPort}/data`;
// ExtensionContent.jsm needs to know when it's running from xpcshell,
// to use the right timeout for content scripts executed at document_idle.
ExtensionTestUtils.mockAppInfo();
add_task(async function test_contentscript_runAt() {
function background() {
browser.runtime.onMessage.addListener(([msg, expectedStates, readyState], sender) => {
if (msg == "chrome-namespace-ok") {
browser.test.sendMessage(msg);
return;
}
browser.test.assertEq("script-run", msg, "message type is correct");
browser.test.assertTrue(expectedStates.includes(readyState),
`readyState "${readyState}" is one of [${expectedStates}]`);
browser.test.sendMessage("script-run-" + expectedStates[0]);
});
}
function contentScriptStart() {
browser.runtime.sendMessage(["script-run", ["loading"], document.readyState]);
}
function contentScriptEnd() {
browser.runtime.sendMessage(["script-run", ["interactive", "complete"], document.readyState]);
}
function contentScriptIdle() {
browser.runtime.sendMessage(["script-run", ["complete"], document.readyState]);
}
function contentScript() {
let manifest = browser.runtime.getManifest();
void manifest.applications.gecko.id;
browser.runtime.sendMessage(["chrome-namespace-ok"]);
}
let extensionData = {
manifest: {
applications: {gecko: {id: "contentscript@tests.mozilla.org"}},
content_scripts: [
{
"matches": ["http://*/*/file_sample.html"],
"js": ["content_script_start.js"],
"run_at": "document_start",
},
{
"matches": ["http://*/*/file_sample.html"],
"js": ["content_script_end.js"],
"run_at": "document_end",
},
{
"matches": ["http://*/*/file_sample.html"],
"js": ["content_script_idle.js"],
"run_at": "document_idle",
},
{
"matches": ["http://*/*/file_sample.html"],
"js": ["content_script_idle.js"],
// Test default `run_at`.
},
{
"matches": ["http://*/*/file_sample.html"],
"js": ["content_script.js"],
"run_at": "document_idle",
},
],
},
background,
files: {
"content_script_start.js": contentScriptStart,
"content_script_end.js": contentScriptEnd,
"content_script_idle.js": contentScriptIdle,
"content_script.js": contentScript,
},
};
let extension = ExtensionTestUtils.loadExtension(extensionData);
let loadingCount = 0;
let interactiveCount = 0;
let completeCount = 0;
extension.onMessage("script-run-loading", () => { loadingCount++; });
extension.onMessage("script-run-interactive", () => { interactiveCount++; });
let completePromise = new Promise(resolve => {
extension.onMessage("script-run-complete", () => {
completeCount++;
if (completeCount > 1) {
resolve();
}
});
});
let chromeNamespacePromise = extension.awaitMessage("chrome-namespace-ok");
await extension.startup();
let contentPage = await ExtensionTestUtils.loadContentPage(`${BASE_URL}/file_sample.html`);
await Promise.all([completePromise, chromeNamespacePromise]);
await contentPage.close();
equal(loadingCount, 1, "document_start script ran exactly once");
equal(interactiveCount, 1, "document_end script ran exactly once");
equal(completeCount, 2, "document_idle script ran exactly twice");
await extension.unload();
});
add_task(async function test_contentscript_window_open() {
if (AppConstants.DEBUG && ExtensionTestUtils.remoteContentScripts) {
return;
}
let script = async () => {
/* globals x */
browser.test.assertEq(1, x, "Should only run once");
if (top !== window) {
// Wait for our parent page to load, then set a timeout to wait for the
// document.open call, so we make sure to not tear down the extension
// until after we've done the document.open.
await new Promise(resolve => {
top.addEventListener("load",
() => setTimeout(resolve, 0),
{once: true});
});
}
browser.test.sendMessage("content-script", [location.href, top === window]);
};
let extension = ExtensionTestUtils.loadExtension({
manifest: {
applications: {gecko: {id: "contentscript@tests.mozilla.org"}},
content_scripts: [
{
"matches": ["<all_urls>"],
"js": ["content_script.js"],
"run_at": "document_start",
"match_about_blank": true,
"all_frames": true,
},
],
},
files: {
"content_script.js": `
var x = (x || 0) + 1;
(${script})();
`,
},
});
await extension.startup();
let url = `${BASE_URL}/file_document_open.html`;
let contentPage = await ExtensionTestUtils.loadContentPage(url);
let [pageURL, pageIsTop] = await extension.awaitMessage("content-script");
// Sometimes we get a content script load for the initial about:blank
// top level frame here, sometimes we don't. Either way is fine, as long as we
// don't get two loads into the same document.open() document.
if (pageURL === "about:blank") {
equal(pageIsTop, true);
[pageURL, pageIsTop] = await extension.awaitMessage("content-script");
}
Assert.deepEqual([pageURL, pageIsTop], [url, true]);
let [frameURL, isTop] = await extension.awaitMessage("content-script");
Assert.deepEqual([frameURL, isTop], [url, false]);
await contentPage.close();
await extension.unload();
});
// This test verify that a cached script is still able to catch the document
// while it is still loading (when we do not block the document parsing as
// we do for a non cached script).
add_task(async function test_cached_contentscript_on_document_start() {
let extension = ExtensionTestUtils.loadExtension({
manifest: {
content_scripts: [
{
"matches": ["http://localhost/*/file_document_open.html"],
"js": ["content_script.js"],
"run_at": "document_start",
},
],
},
files: {
"content_script.js": `
browser.test.sendMessage("content-script-loaded", {
url: window.location.href,
documentReadyState: document.readyState,
});
`,
},
});
await extension.startup();
let url = `${BASE_URL}/file_document_open.html`;
let contentPage = await ExtensionTestUtils.loadContentPage(url);
let msg = await extension.awaitMessage("content-script-loaded");
Assert.deepEqual(msg, {
url,
documentReadyState: "loading",
}, "Got the expected url and document.readyState from a non cached script");
// Reload the page and check that the cached content script is still able to
// run on document_start.
await contentPage.loadURL(url);
let msgFromCached = await extension.awaitMessage("content-script-loaded");
Assert.deepEqual(msgFromCached, {
url,
documentReadyState: "loading",
}, "Got the expected url and document.readyState from a cached script");
await extension.unload();
await contentPage.close();
});