summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/test/utils.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /dom/serviceworkers/test/utils.js
parentInitial commit. (diff)
downloadfirefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz
firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/serviceworkers/test/utils.js')
-rw-r--r--dom/serviceworkers/test/utils.js85
1 files changed, 85 insertions, 0 deletions
diff --git a/dom/serviceworkers/test/utils.js b/dom/serviceworkers/test/utils.js
new file mode 100644
index 0000000000..15ab203972
--- /dev/null
+++ b/dom/serviceworkers/test/utils.js
@@ -0,0 +1,85 @@
+function waitForState(worker, state, context) {
+ return new Promise(resolve => {
+ function onStateChange() {
+ if (worker.state === state) {
+ worker.removeEventListener("statechange", onStateChange);
+ resolve(context);
+ }
+ }
+
+ // First add an event listener, so we won't miss any change that happens
+ // before we check the current state.
+ worker.addEventListener("statechange", onStateChange);
+
+ // Now check if the worker is already in the desired state.
+ onStateChange();
+ });
+}
+
+/**
+ * Helper for browser tests to issue register calls from the content global and
+ * wait for the SW to progress to the active state, as most tests desire.
+ * From the ContentTask.spawn, use via
+ * `content.wrappedJSObject.registerAndWaitForActive`.
+ */
+async function registerAndWaitForActive(...args) {
+ console.log("...calling register");
+ const reg = await navigator.serviceWorker.register(...args);
+ // Unless registration resurrection happens, the SW should be in the
+ // installing slot.
+ console.log("...waiting for activation");
+ await waitForState(reg.installing, "activated", reg);
+ console.log("...activated!");
+ return reg;
+}
+
+/**
+ * Helper to create an iframe with the given URL and return the first
+ * postMessage payload received. This is intended to be used when creating
+ * cross-origin iframes.
+ *
+ * A promise will be returned that resolves with the payload of the postMessage
+ * call.
+ */
+function createIframeAndWaitForMessage(url) {
+ const iframe = document.createElement("iframe");
+ document.body.appendChild(iframe);
+ return new Promise(resolve => {
+ window.addEventListener(
+ "message",
+ event => {
+ resolve(event.data);
+ },
+ { once: true }
+ );
+ iframe.src = url;
+ });
+}
+
+/**
+ * Helper to create a nested iframe into the iframe created by
+ * createIframeAndWaitForMessage().
+ *
+ * A promise will be returned that resolves with the payload of the postMessage
+ * call.
+ */
+function createNestedIframeAndWaitForMessage(url) {
+ const iframe = document.getElementsByTagName("iframe")[0];
+ iframe.contentWindow.postMessage("create nested iframe", "*");
+ return new Promise(resolve => {
+ window.addEventListener(
+ "message",
+ event => {
+ resolve(event.data);
+ },
+ { once: true }
+ );
+ });
+}
+
+async function unregisterAll() {
+ const registrations = await navigator.serviceWorker.getRegistrations();
+ for (const reg of registrations) {
+ await reg.unregister();
+ }
+}