Skip to content

Commit

Permalink
Bug 1930534 [wpt PR 49108] - Run ShadowRealm tests in multiple scopes…
Browse files Browse the repository at this point in the history
…, a=testonly

Automatic update from web-platform-tests
Change 'shadowrealm' global into a shorthand for all possible ShadowRealm scopes

In order to automatically run tests not only in a ShadowRealm created in a
window scope, but also in ShadowRealms created in other realms, change the
'shadowrealm' global type to a collection, and rename the existing
ShadowRealm handler to 'shadowrealm-in-window'.

--
Remove monkeypatch of globalThis.self in ShadowRealm

As per whatwg/html#9893, ShadowRealmGlobalScope
should have a `self` attribute already. There is no need to monkeypatch it
for the test harness.

--
Factor out JS code that will be common to multiple ShadowRealm handlers

We will add multiple ShadowRealm handlers, and they will all need to set
up certain global properties. Avoid repeating this code in each handler
as well as in idlharness-shadowrealm.js.

This should also increase readability, which is good since the ShadowRealm
setup code can be confusing.

--
Add 'shadowrealm-in-shadowrealm' global

This will add to any test with global=shadowrealm in its metadata, an
.any.shadowrealm-in-shadowrealm.html variant.

The test wrapper creates an outer ShadowRealm, which creates an inner
ShadowRealm and runs the tests inside that, relaying the results through
the outer ShadowRealm.

--
Add 'shadowrealm-in-dedicatedworker' global

This will add to any test with global=shadowrealm in its metadata, an
.any.shadowrealm-in-dedicatedworker.html variant.

The test loads an intermediate .any.worker-shadowrealm.js wrapper into a
Worker, and forwards the message port to the Worker's message port so that
fetch_tests_from_worker can receive the results.

--
Add 'shadowrealm-in-sharedworker' global

This will add to any test with global=shadowrealm in its metadata, an
.any.shadowrealm-in-sharedworker.html variant.

The test loads the same intermediate .any.worker-shadowrealm.js wrapper as
.any.shadowrealm-in-dedicatedworker.html, but populates a 'port' variable
with the port received from the connect event, instead of calling the
global postMessage since that won't work in a SharedWorker.

--
Add 'shadowrealm-in-serviceworker' global

This will add to any test with global=shadowrealm in its metadata, an
.any.shadowrealm-in-serviceworker.html variant.

We have to use a slightly different .any.serviceworker-shadowrealm.js
wrapper from the wrapper used for the other types of workers, because
dynamic import() is forbidden in ServiceWorker scopes. Instead, add a
utility function to set up a fakeDynamicImport() function inside the
ShadowRealm which uses the fetch adaptor to get the module's source text
and evaluate it in the shadowRealm.

Also add a case for ServiceWorkers to getPostMessageFunc(), which returns
a postMessage() drop-in replacement that broadcasts the message to all
clients, since test result messages from the ShadowRealm are not in
response to any particular message received by the ServiceWorker.

Note '.https.' needs to be added to the test path.

--
Add 'shadowrealm-in-audioworklet' global

This will add to any test with global=shadowrealm in its metadata, an
.any.shadowrealm-in-audioworklet.html variant.

The wrapper here is similar to the one for ServiceWorkers, since dynamic
import() is also forbidden in worklet scopes. But additionally fetch() is
not exposed, so we add a utility function to set up the ability to call
the window realm's fetch() through the AudioWorklet's message port.

We also add /resources/testharness-shadowrealm-audioworkletprocessor.js to
contain most of the AudioWorklet setup boilerplate, so that it isn't
written inline in serve.py.

Note '.https.' needs to be added to the test path.

--

wpt-commits: 9c8db8af89efbe0f67b215af2a6b49e9564e2971, 65a205aea5d02ff5bea7b1a0579287035d02d6c4, eb9c8e7259ef8bd5cca5019c1ca15ccd430e81dc, 3a20c56893472783b5e20c0d61cbb7b7b278cc6d, 7d8458ed291b139307430a102180c9a617d7876e, 42160ae827c863ac6787c8451fe377901c8f0652, 59367bb21d053abb9ed6de3cca5409486816acc9, 60d6c48e5fa76876bc3924b9d6185dfb56c9ab1c
wpt-pr: 49108
  • Loading branch information
ptomato authored and moz-wptsync-bot committed Nov 27, 2024
1 parent ee25329 commit df9f39e
Show file tree
Hide file tree
Showing 19 changed files with 491 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// META: script=/resources/testharness-shadowrealm-outer.js
// META: script=/resources/idlharness-shadowrealm.js
idl_test_shadowrealm(["compression"], ["streams"]);
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// META: script=/resources/testharness-shadowrealm-outer.js
// META: script=/resources/idlharness-shadowrealm.js

// https://console.spec.whatwg.org/
Expand Down
20 changes: 18 additions & 2 deletions testing/web-platform/tests/docs/writing-tests/testharness.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,25 @@ are:
* `jsshell`: to be run in a JavaScript shell, without access to the DOM
(currently only supported in SpiderMonkey, and skipped in wptrunner)
* `worker`: shorthand for the dedicated, shared, and service worker scopes
* `shadowrealm`: runs the test code in a
* `shadowrealm-in-window`: runs the test code in a
[ShadowRealm](https://github.com/tc39/proposal-shadowrealm) context hosted in
an ordinary Window context; to be run at <code><var>x</var>.any.shadowrealm.html</code>
an ordinary Window context; to be run at <code><var>x</var>.any.shadowrealm-in-window.html</code>
* `shadowrealm-in-shadowrealm`: runs the test code in a ShadowRealm context
hosted in another ShadowRealm context; to be run at
<code><var>x</var>.any.shadowrealm-in-shadowrealm.html</code>
* `shadowrealm-in-dedicatedworker`: runs the test code in a ShadowRealm context
hosted in a dedicated worker; to be run at
<code><var>x</var>.any.shadowrealm-in-dedicatedworker.html</code>
* `shadowrealm-in-sharedworker`: runs the test code in a ShadowRealm context
hosted in a shared worker; to be run at
<code><var>x</var>.any.shadowrealm-in-sharedworker.html</code>
* `shadowrealm-in-serviceworker`: runs the test code in a ShadowRealm context
hosted in a service worker; to be run at
<code><var>x</var>.https.any.shadowrealm-in-serviceworker.html</code>
* `shadowrealm-in-audioworklet`: runs the test code in a ShadowRealm context
hosted in an AudioWorklet processor; to be run at
<code><var>x</var>.https.any.shadowrealm-in-audioworklet.html</code>
* `shadowrealm`: shorthand for all of the ShadowRealm scopes

To check what scope your test is run from, you can use the following methods that will
be made available by the framework:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// META: script=/resources/testharness-shadowrealm-outer.js
// META: script=/resources/idlharness-shadowrealm.js
idl_test_shadowrealm(["dom"], ["html"]);
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// META: script=/resources/testharness-shadowrealm-outer.js
// META: script=/resources/idlharness-shadowrealm.js
idl_test_shadowrealm(["encoding"], ["streams"]);
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// META: script=/resources/testharness-shadowrealm-outer.js
// META: script=/resources/idlharness-shadowrealm.js
idl_test_shadowrealm(["hr-time"], ["html", "dom"]);
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// META: script=/resources/testharness-shadowrealm-outer.js
// META: script=/resources/idlharness-shadowrealm.js
idl_test_shadowrealm(["html"], ["wai-aria", "SVG", "cssom", "touch-events", "uievents", "dom", "xhr", "FileAPI", "mediacapture-streams", "performance-timeline"]);
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// META: script=/resources/testharness-shadowrealm-outer.js
// META: script=/resources/idlharness-shadowrealm.js
idl_test_shadowrealm(["performance-timeline"], ["hr-time", "dom"]);
47 changes: 19 additions & 28 deletions testing/web-platform/tests/resources/idlharness-shadowrealm.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* global shadowRealmEvalAsync */

// requires /resources/idlharness-shadowrealm-outer.js

// TODO: it would be nice to support `idl_array.add_objects`
function fetch_text(url) {
return fetch(url).then(function (r) {
Expand All @@ -23,38 +27,25 @@ function fetch_text(url) {
function idl_test_shadowrealm(srcs, deps) {
promise_setup(async t => {
const realm = new ShadowRealm();
// https://github.com/web-platform-tests/wpt/issues/31996
realm.evaluate("globalThis.self = globalThis; undefined;");

realm.evaluate(`
globalThis.self.GLOBAL = {
isWindow: function() { return false; },
isWorker: function() { return false; },
isShadowRealm: function() { return true; },
}; undefined;
`);
const specs = await Promise.all(srcs.concat(deps).map(spec => {
return fetch_text("/interfaces/" + spec + ".idl");
}));
const idls = JSON.stringify(specs);
await new Promise(
realm.evaluate(`(resolve,reject) => {
(async () => {
await import("/resources/testharness.js");
await import("/resources/WebIDLParser.js");
await import("/resources/idlharness.js");
const idls = ${idls};
const idl_array = new IdlArray();
for (let i = 0; i < ${srcs.length}; i++) {
idl_array.add_idls(idls[i]);
}
for (let i = ${srcs.length}; i < ${srcs.length + deps.length}; i++) {
idl_array.add_dependency_idls(idls[i]);
}
idl_array.test();
})().then(resolve, (e) => reject(e.toString()));
}`)
);
await shadowRealmEvalAsync(realm, `
await import("/resources/testharness-shadowrealm-inner.js");
await import("/resources/testharness.js");
await import("/resources/WebIDLParser.js");
await import("/resources/idlharness.js");
const idls = ${idls};
const idl_array = new IdlArray();
for (let i = 0; i < ${srcs.length}; i++) {
idl_array.add_idls(idls[i]);
}
for (let i = ${srcs.length}; i < ${srcs.length + deps.length}; i++) {
idl_array.add_dependency_idls(idls[i]);
}
idl_array.test();
`);
await fetch_tests_from_shadow_realm(realm);
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* AudioWorkletProcessor intended for hosting a ShadowRealm and running a test
* inside of that ShadowRealm.
*/
globalThis.TestRunner = class TestRunner extends AudioWorkletProcessor {
constructor() {
super();
this.createShadowRealmAndStartTests();
}

/**
* Fetch adaptor function intended as a drop-in replacement for fetchAdaptor()
* (see testharness-shadowrealm-outer.js), but it does not assume fetch() is
* present in the realm. Instead, it relies on setupFakeFetchOverMessagePort()
* having been called on the port on the other side of this.port's channel.
*/
fetchOverPortExecutor(resource) {
return (resolve, reject) => {
const listener = (event) => {
if (typeof event.data !== "string" || !event.data.startsWith("fetchResult::")) {
return;
}

const result = event.data.slice("fetchResult::".length);
if (result.startsWith("success::")) {
resolve(result.slice("success::".length));
} else {
reject(result.slice("fail::".length));
}

this.port.removeEventListener("message", listener);
}
this.port.addEventListener("message", listener);
this.port.start();
this.port.postMessage(`fetchRequest::${resource}`);
}
}

/**
* Async method, which is patched over in
* (test).any.audioworklet-shadowrealm.js; see serve.py
*/
async createShadowRealmAndStartTests() {
throw new Error("Forgot to overwrite this method!");
}

/** Overrides AudioWorkletProcessor.prototype.process() */
process() {
return false;
}
};
registerProcessor("test-runner", TestRunner);
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// testharness file with ShadowRealm utilities to be imported inside ShadowRealm

/**
* Set up all properties on the ShadowRealm's global object that tests will
* expect to be present.
*
* @param {string} queryString - string to use as value for location.search,
* used for subsetting some tests
* @param {function} fetchAdaptor - a function that takes a resource URI and
* returns a function which itself takes a (resolve, reject) pair from the
* hosting realm, and calls resolve with the text result of fetching the
* resource, or reject with a string indicating the error that occurred
*/
globalThis.setShadowRealmGlobalProperties = function (queryString, fetchAdaptor) {
globalThis.fetch_json = (resource) => {
const executor = fetchAdaptor(resource);
return new Promise(executor).then((s) => JSON.parse(s));
};

globalThis.location = { search: queryString };
};

globalThis.GLOBAL = {
isWindow: function() { return false; },
isWorker: function() { return false; },
isShadowRealm: function() { return true; },
};
127 changes: 127 additions & 0 deletions testing/web-platform/tests/resources/testharness-shadowrealm-outer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// testharness file with ShadowRealm utilities to be imported in the realm
// hosting the ShadowRealm

/**
* Convenience function for evaluating some async code in the ShadowRealm and
* waiting for the result.
*
* @param {ShadowRealm} realm - the ShadowRealm to evaluate the code in
* @param {string} asyncBody - the code to evaluate; will be put in the body of
* an async function, and must return a value explicitly if a value is to be
* returned to the hosting realm.
*/
globalThis.shadowRealmEvalAsync = function (realm, asyncBody) {
return new Promise(realm.evaluate(`
(resolve, reject) => {
(async () => {
${asyncBody}
})().then(resolve, (e) => reject(e.toString()));
}
`));
};

/**
* Convenience adaptor function for fetch() that can be passed to
* setShadowRealmGlobalProperties() (see testharness-shadowrealm-inner.js).
* Used to adapt the hosting realm's fetch(), if present, to fetch a resource
* and pass its text through the callable boundary to the ShadowRealm.
*/
globalThis.fetchAdaptor = (resource) => (resolve, reject) => {
fetch(resource)
.then(res => res.text())
.then(resolve, (e) => reject(e.toString()));
};

let sharedWorkerMessagePortPromise;
/**
* Used when the hosting realm is a worker. This value is a Promise that
* resolves to a function that posts a message to the worker's message port,
* just like postMessage(). The message port is only available asynchronously in
* SharedWorkers and ServiceWorkers.
*/
globalThis.getPostMessageFunc = async function () {
if (typeof postMessage === "function") {
return postMessage; // postMessage available directly in dedicated worker
}

if (typeof clients === "object") {
// Messages from the ShadowRealm are not in response to any message received
// from the ServiceWorker's client, so broadcast them to all clients
const allClients = await clients.matchAll({ includeUncontrolled: true });
return function broadcast(msg) {
allClients.map(client => client.postMessage(msg));
}
}

if (sharedWorkerMessagePortPromise) {
return await sharedWorkerMessagePortPromise;
}

throw new Error("getPostMessageFunc is intended for Worker scopes");
}

// Port available asynchronously in shared worker, but not via an async func
let savedResolver;
if (globalThis.constructor.name === "SharedWorkerGlobalScope") {
sharedWorkerMessagePortPromise = new Promise((resolve) => {
savedResolver = resolve;
});
addEventListener("connect", function (event) {
const port = event.ports[0];
savedResolver(port.postMessage.bind(port));
});
}

/**
* Used when the hosting realm does not permit dynamic import, e.g. in
* ServiceWorkers or AudioWorklets. Requires an adaptor function such as
* fetchAdaptor() above, or an equivalent if fetch() is not present in the
* hosting realm.
*
* @param {ShadowRealm} realm - the ShadowRealm in which to setup a
* fakeDynamicImport() global function.
* @param {function} adaptor - an adaptor function that does what fetchAdaptor()
* does.
*/
globalThis.setupFakeDynamicImportInShadowRealm = function(realm, adaptor) {
function fetchModuleTextExecutor(url) {
return (resolve, reject) => {
new Promise(adaptor(url))
.then(text => realm.evaluate(text + ";\nundefined"))
.then(resolve, (e) => reject(e.toString()));
}
}

realm.evaluate(`
(fetchModuleTextExecutor) => {
globalThis.fakeDynamicImport = function (url) {
return new Promise(fetchModuleTextExecutor(url));
}
}
`)(fetchModuleTextExecutor);
};

/**
* Used when the hosting realm does not expose fetch(), i.e. in worklets. The
* port on the other side of the channel needs to send messages starting with
* 'fetchRequest::' and listen for messages starting with 'fetchResult::'. See
* testharness-shadowrealm-audioworkletprocessor.js.
*
* @param {port} MessagePort - the message port on which to listen for fetch
* requests
*/
globalThis.setupFakeFetchOverMessagePort = function (port) {
port.addEventListener("message", (event) => {
if (typeof event.data !== "string" || !event.data.startsWith("fetchRequest::")) {
return;
}

fetch(event.data.slice("fetchRequest::".length))
.then(res => res.text())
.then(
text => port.postMessage(`fetchResult::success::${text}`),
error => port.postMessage(`fetchResult::fail::${error}`),
);
});
port.start();
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// META: script=/resources/testharness-shadowrealm-outer.js
// META: script=/resources/idlharness-shadowrealm.js
idl_test_shadowrealm(["streams"], ["dom"]);
21 changes: 20 additions & 1 deletion testing/web-platform/tests/tools/manifest/sourcefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,26 @@ class VariantData(TypedDict, total=False):
"dedicatedworker-module": {"suffix": ".any.worker-module.html"},
"worker": {"longhand": {"dedicatedworker", "sharedworker", "serviceworker"}},
"worker-module": {},
"shadowrealm": {},
"shadowrealm-in-window": {},
"shadowrealm-in-shadowrealm": {},
"shadowrealm-in-dedicatedworker": {},
"shadowrealm-in-sharedworker": {},
"shadowrealm-in-serviceworker": {
"force_https": True,
"suffix": ".https.any.shadowrealm-in-serviceworker.html",
},
"shadowrealm-in-audioworklet": {
"force_https": True,
"suffix": ".https.any.shadowrealm-in-audioworklet.html",
},
"shadowrealm": {"longhand": {
"shadowrealm-in-window",
"shadowrealm-in-shadowrealm",
"shadowrealm-in-dedicatedworker",
"shadowrealm-in-sharedworker",
"shadowrealm-in-serviceworker",
"shadowrealm-in-audioworklet",
}},
"jsshell": {"suffix": ".any.js"},
}

Expand Down
Loading

0 comments on commit df9f39e

Please sign in to comment.