diff options
Diffstat (limited to 'devtools/client/aboutdebugging/test/browser/resources/service-workers')
8 files changed, 232 insertions, 0 deletions
diff --git a/devtools/client/aboutdebugging/test/browser/resources/service-workers/controlled-sw.html b/devtools/client/aboutdebugging/test/browser/resources/service-workers/controlled-sw.html new file mode 100644 index 0000000000..98d3bffd95 --- /dev/null +++ b/devtools/client/aboutdebugging/test/browser/resources/service-workers/controlled-sw.html @@ -0,0 +1,38 @@ +<!DOCTYPE HTML> +<html> +<head> + <meta charset="UTF-8"> + <title>Service worker controlled</title> +</head> +<body> +<script type="text/javascript"> + +"use strict"; + +let registration; + +const registerServiceWorker = async function() { + try { + registration = await navigator.serviceWorker.register("controlled-sw.js"); + dump("Controlled service worker registered\n"); + } catch (e) { + dump("Controlled service worker not registered: " + e + "\n"); + } +}; + +// Helper called from helper-serviceworker.js to unregister the service worker. +window.getRegistration = function() { + return registration; +}; + +// Called from browser_aboutdebugging_serviceworker_status.js +window.installServiceWorker = function() { + registration.installing.postMessage("install-service-worker"); +}; + +// Register the service worker. +registerServiceWorker(); + +</script> +</body> +</html> diff --git a/devtools/client/aboutdebugging/test/browser/resources/service-workers/controlled-sw.js b/devtools/client/aboutdebugging/test/browser/resources/service-workers/controlled-sw.js new file mode 100644 index 0000000000..0a6d9cfdc6 --- /dev/null +++ b/devtools/client/aboutdebugging/test/browser/resources/service-workers/controlled-sw.js @@ -0,0 +1,31 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* eslint-env worker */ + +"use strict"; + +// Copied from shared-head.js +function waitUntil(predicate, interval = 10) { + if (predicate()) { + return Promise.resolve(true); + } + return new Promise(resolve => { + setTimeout(function () { + waitUntil(predicate, interval).then(() => resolve(true)); + }, interval); + }); +} + +// This flag will be flipped from controlled-sw.html::installServiceWorker() +let canInstall = false; +self.addEventListener("message", function (event) { + if (event.data === "install-service-worker") { + canInstall = true; + } +}); + +// Wait for the canInstall flag to be flipped before completing the install. +self.addEventListener("install", function (event) { + event.waitUntil(waitUntil(() => canInstall)); +}); diff --git a/devtools/client/aboutdebugging/test/browser/resources/service-workers/empty-sw.html b/devtools/client/aboutdebugging/test/browser/resources/service-workers/empty-sw.html new file mode 100644 index 0000000000..ab862743a7 --- /dev/null +++ b/devtools/client/aboutdebugging/test/browser/resources/service-workers/empty-sw.html @@ -0,0 +1,30 @@ +<!DOCTYPE HTML> +<html> +<head> + <meta charset="UTF-8"> + <title>Service worker test</title> +</head> +<body> +<script type="text/javascript"> +"use strict"; + +let registration; + +const registerServiceWorker = async function() { + try { + registration = await navigator.serviceWorker.register("empty-sw.js"); + dump("Empty service worker registered\n"); + } catch (e) { + dump("Empty service worker not registered: " + e + "\n"); + } +}; + +// Helper called from helper-serviceworker.js to unregister the service worker. +window.getRegistration = function() { + return registration; +}; +// Register the service worker. +registerServiceWorker(); +</script> +</body> +</html> diff --git a/devtools/client/aboutdebugging/test/browser/resources/service-workers/empty-sw.js b/devtools/client/aboutdebugging/test/browser/resources/service-workers/empty-sw.js new file mode 100644 index 0000000000..1e7226402c --- /dev/null +++ b/devtools/client/aboutdebugging/test/browser/resources/service-workers/empty-sw.js @@ -0,0 +1 @@ +// Empty, just test registering. diff --git a/devtools/client/aboutdebugging/test/browser/resources/service-workers/fetch-sw.html b/devtools/client/aboutdebugging/test/browser/resources/service-workers/fetch-sw.html new file mode 100644 index 0000000000..a1bb218341 --- /dev/null +++ b/devtools/client/aboutdebugging/test/browser/resources/service-workers/fetch-sw.html @@ -0,0 +1,30 @@ +<!DOCTYPE HTML> +<html> +<head> + <meta charset="UTF-8"> + <title>Service worker test</title> +</head> +<body> +<script type="text/javascript"> +"use strict"; + +let registration; + +const registerServiceWorker = async function() { + try { + registration = await navigator.serviceWorker.register("fetch-sw.js"); + dump("Empty service worker registered\n"); + } catch (e) { + dump("Empty service worker not registered: " + e + "\n"); + } +}; + +// Helper called from helper-serviceworker.js to unregister the service worker. +window.getRegistration = function() { + return registration; +}; +// Register the service worker. +registerServiceWorker(); +</script> +</body> +</html> diff --git a/devtools/client/aboutdebugging/test/browser/resources/service-workers/fetch-sw.js b/devtools/client/aboutdebugging/test/browser/resources/service-workers/fetch-sw.js new file mode 100644 index 0000000000..de6ee1fb32 --- /dev/null +++ b/devtools/client/aboutdebugging/test/browser/resources/service-workers/fetch-sw.js @@ -0,0 +1,6 @@ +"use strict"; + +// Bug 1328293 +self.onfetch = function (event) { + // do nothing. +}; diff --git a/devtools/client/aboutdebugging/test/browser/resources/service-workers/push-sw.html b/devtools/client/aboutdebugging/test/browser/resources/service-workers/push-sw.html new file mode 100644 index 0000000000..bf5b0b0b0a --- /dev/null +++ b/devtools/client/aboutdebugging/test/browser/resources/service-workers/push-sw.html @@ -0,0 +1,61 @@ +<!DOCTYPE HTML> +<html> +<head> + <meta charset="UTF-8"> + <title>Service worker push test</title> +</head> +<body> +<script type="text/javascript"> + +"use strict"; + +let registration; +let subscription; + +const registerServiceWorker = async function() { + const perm = { type: "desktop-notification", allow: true, context: document }; + await SpecialPowers.pushPermissions([perm]); + + try { + registration = await navigator.serviceWorker.register("push-sw.js"); + dump("Push service worker registered\n"); + } catch (e) { + dump("Push service worker not registered: " + e + "\n"); + } +}; + +// Helper called from helper-serviceworker.js to unregister the service worker. +window.getRegistration = function() { + return registration; +}; + +// Helper called from browser_aboutdebugging_serviceworker_pushservice_url.js +window.subscribeToPush = async function() { + try { + subscription = await registration.pushManager.subscribe(); + dump("SW subscribed to push: " + subscription.endpoint + "\n"); + } catch (e) { + dump("SW not subscribed to push: " + e + "\n"); + } +}; + +// Helper called from browser_aboutdebugging_serviceworker_pushservice_url.js +window.unsubscribeToPush = async function() { + subscription.unsubscribe(); +}; + +// Expose a promise to wait until the service worker is claimed. +window.onSwClaimed = new Promise(resolve => { + navigator.serviceWorker.addEventListener("message", function(event) { + if (event.data == "sw-claimed") { + resolve(); + } + }); +}); + +// Register the service worker. +registerServiceWorker(); + +</script> +</body> +</html> diff --git a/devtools/client/aboutdebugging/test/browser/resources/service-workers/push-sw.js b/devtools/client/aboutdebugging/test/browser/resources/service-workers/push-sw.js new file mode 100644 index 0000000000..1231697ddd --- /dev/null +++ b/devtools/client/aboutdebugging/test/browser/resources/service-workers/push-sw.js @@ -0,0 +1,35 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* eslint-env worker */ +/* global clients */ + +"use strict"; + +// Send a message to all controlled windows. +function postMessage(message) { + return clients.matchAll().then(function (clientlist) { + clientlist.forEach(function (client) { + client.postMessage(message); + }); + }); +} + +// Don't wait for the next page load to become the active service worker. +self.addEventListener("install", function (event) { + event.waitUntil(self.skipWaiting()); +}); + +// Claim control over the currently open test page when activating. +self.addEventListener("activate", function (event) { + event.waitUntil( + self.clients.claim().then(function () { + return postMessage("sw-claimed"); + }) + ); +}); + +// Forward all "push" events to the controlled window. +self.addEventListener("push", function (event) { + event.waitUntil(postMessage("sw-pushed")); +}); |