diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /dom/tests/mochitest/fetch/worker_wrapper.js | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/tests/mochitest/fetch/worker_wrapper.js')
-rw-r--r-- | dom/tests/mochitest/fetch/worker_wrapper.js | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/dom/tests/mochitest/fetch/worker_wrapper.js b/dom/tests/mochitest/fetch/worker_wrapper.js new file mode 100644 index 0000000000..72d00db0e1 --- /dev/null +++ b/dom/tests/mochitest/fetch/worker_wrapper.js @@ -0,0 +1,85 @@ +importScripts("utils.js"); + +function getScriptUrl() { + return new URL(location.href).searchParams.get("script"); +} + +importScripts(getScriptUrl()); + +var client; +var context; + +function ok(a, msg) { + client.postMessage({ + type: "status", + status: !!a, + msg: a + ": " + msg, + context, + }); +} + +function is(a, b, msg) { + client.postMessage({ + type: "status", + status: a === b, + msg: a + " === " + b + ": " + msg, + context, + }); +} + +addEventListener("message", function workerWrapperOnMessage(e) { + removeEventListener("message", workerWrapperOnMessage); + var data = e.data; + + function runTestAndReportToClient(event) { + var done = function (res) { + client.postMessage({ type: "finish", context }); + return res; + }; + + try { + // runTest() is provided by the test. + var result = runTest().then(done, done); + if ("waitUntil" in event) { + event.waitUntil(result); + } + } catch (e) { + client.postMessage({ + type: "status", + status: false, + msg: "worker failed to run " + data.script + "; error: " + e.message, + context, + }); + done(); + } + } + + if ("ServiceWorker" in self) { + // Fetch requests from a service worker are not intercepted. + self.isSWPresent = false; + + e.waitUntil( + self.clients + .matchAll({ includeUncontrolled: true }) + .then(function (clients) { + for (var i = 0; i < clients.length; ++i) { + if (clients[i].url.indexOf("message_receiver.html") > -1) { + client = clients[i]; + break; + } + } + if (!client) { + dump( + "We couldn't find the message_receiver window, the test will fail\n" + ); + } + context = "ServiceWorker"; + runTestAndReportToClient(e); + }) + ); + } else { + client = self; + context = "Worker"; + runTestAndReportToClient(e); + } +}); |