Support container.getRegistration() / getRegistrations() inside service workers
[WebKit-https.git] / LayoutTests / http / tests / workers / service / resources / ServiceWorkerGlobalScope_getRegistration-worker.js
1 let client = null;
2
3 function log(msg)
4 {
5     client.postMessage(msg);
6 }
7
8 async function runTest()
9 {
10     try {
11         let r = await navigator.serviceWorker.register("empty-worker.js", { scope: "/test" });
12         log("PASS: Registration succeeded");
13
14         let retrievedRegistration = await navigator.serviceWorker.getRegistration("/test");
15         if (r === retrievedRegistration)
16             log("PASS: getRegistration() returned the right registration");
17         else
18             log("FAIL: getRegistration() did not return the right registration");
19
20         let retrievedRegistrations = await navigator.serviceWorker.getRegistrations();
21         if (retrievedRegistrations.length === 2)
22             log("PASS: getRegistrations() returned 2 registrations");
23         else {
24             log("FAIL: getRegistrations() returned " + retrievedRegistrations.length + " registration(s)");
25             log("DONE");
26             return;
27         }
28
29         if (retrievedRegistrations[0] === self.registration)
30             log("PASS: getRegistrations()[0] is the right registration");
31         else
32             log("FAIL: getRegistrations()[0] is not the right registration");
33
34         if (retrievedRegistrations[1] === r)
35             log("PASS: getRegistrations()[1] is the right registration");
36         else
37             log("FAIL: getRegistrations()[1] is not the right registration");
38
39         log("DONE");
40     } catch (e) {
41         log("FAIL: " + e);
42         log("DONE");
43     }
44 }
45
46 self.addEventListener("message", (event) => {
47     client = event.source;
48     runTest();
49 });