summaryrefslogtreecommitdiffstats
path: root/dom/origin-trials/tests/mochitest/common.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /dom/origin-trials/tests/mochitest/common.js
parentInitial commit. (diff)
downloadfirefox-esr-upstream.tar.xz
firefox-esr-upstream.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/origin-trials/tests/mochitest/common.js')
-rw-r--r--dom/origin-trials/tests/mochitest/common.js114
1 files changed, 114 insertions, 0 deletions
diff --git a/dom/origin-trials/tests/mochitest/common.js b/dom/origin-trials/tests/mochitest/common.js
new file mode 100644
index 0000000000..a38a975ea7
--- /dev/null
+++ b/dom/origin-trials/tests/mochitest/common.js
@@ -0,0 +1,114 @@
+/* import-globals-from ../../../../testing/mochitest/tests/SimpleTest/SimpleTest.js */
+
+// This would be a bit nicer with `self`, but Worklet doesn't have that, so
+// `globalThis` it is, see https://github.com/whatwg/html/issues/7696
+function workerReply(port) {
+ port.postMessage({
+ testTrialInterfaceExposed: !!globalThis.TestTrialInterface,
+ });
+}
+
+if (
+ globalThis.SharedWorkerGlobalScope &&
+ globalThis instanceof globalThis.SharedWorkerGlobalScope
+) {
+ globalThis.addEventListener("connect", function (e) {
+ const port = e.ports[0];
+ workerReply(port);
+ });
+} else if (
+ globalThis.WorkerGlobalScope &&
+ globalThis instanceof globalThis.WorkerGlobalScope
+) {
+ workerReply(globalThis);
+} else if (
+ globalThis.WorkletGlobalScope &&
+ globalThis instanceof globalThis.WorkletGlobalScope
+) {
+ class Processor extends AudioWorkletProcessor {
+ constructor() {
+ super();
+ this.port.start();
+ workerReply(this.port);
+ }
+
+ process(inputs, outputs, parameters) {
+ // Do nothing, output silence
+ return true;
+ }
+ }
+ registerProcessor("test-processor", Processor);
+}
+
+function assertTestTrialActive(shouldBeActive) {
+ add_task(async function () {
+ info("Main thread test: " + document.URL);
+ is(
+ !!navigator.testTrialGatedAttribute,
+ shouldBeActive,
+ "Should match active status for Navigator.testTrialControlledAttribute"
+ );
+ is(
+ !!self.TestTrialInterface,
+ shouldBeActive,
+ "Should match active status for TestTrialInterface"
+ );
+ if (shouldBeActive) {
+ ok(
+ new self.TestTrialInterface(),
+ "Should be able to construct interface"
+ );
+ }
+
+ function promiseWorkerWorkletMessage(target, context) {
+ info(`promiseWorkerWorkletMessage(${context})`);
+ return new Promise(resolve => {
+ target.addEventListener(
+ "message",
+ function (e) {
+ is(
+ e.data.testTrialInterfaceExposed,
+ shouldBeActive,
+ "Should work as expected in " + context
+ );
+ info(`got ${context} message`);
+ resolve();
+ },
+ { once: true }
+ );
+ });
+ }
+
+ {
+ info("Worker test");
+ const worker = new Worker("common.js");
+ await promiseWorkerWorkletMessage(worker, "worker");
+ worker.terminate();
+ }
+
+ {
+ info("SharedWorker test");
+ // We want a unique worker per page since the trial state depends on the
+ // creator document.
+ const worker = new SharedWorker("common.js", document.URL);
+ const promise = promiseWorkerWorkletMessage(worker.port, "shared worker");
+ worker.port.start();
+ await promise;
+ }
+
+ {
+ info("AudioWorklet test");
+ const audioContext = new AudioContext();
+ await audioContext.audioWorklet.addModule("common.js");
+ audioContext.resume();
+ const workletNode = new AudioWorkletNode(audioContext, "test-processor");
+ const promise = promiseWorkerWorkletMessage(workletNode.port, "worklet");
+ workletNode.port.start();
+ await promise;
+ await audioContext.close();
+ }
+
+ // FIXME(emilio): Add more tests.
+ // * Stuff hanging off Window or Document (bug 1757935).
+ });
+}