Add support for unregistering a service worker
[WebKit-https.git] / LayoutTests / http / tests / workers / service / resources / basic-unregister.js
1 function done()
2 {
3     finishSWTest();
4 }
5
6 async function test()
7 {
8     try {
9         if (!internals.hasServiceWorkerRegisteredForOrigin(self.origin))
10             log("PASS: There is initially no service worker registered for the origin");
11         else
12             log("FAIL: There is initially a service worker registered for the origin");
13
14         let registration = await navigator.serviceWorker.register("resources/basic-fetch-worker.js", { });
15         if (registration.scope === "https://127.0.0.1:8443/workers/service/")
16             log("PASS: registration scope is " + registration.scope);
17         else
18             log("FAIL: registration scope is " + registration.scope);
19  
20         if (internals.hasServiceWorkerRegisteredForOrigin(self.origin))
21             log("PASS: There is a service worker registered for the origin");
22         else
23             log("FAIL: There is no service worker registered for the origin");
24
25         let unregistrationResult = await registration.unregister();
26         if (unregistrationResult)
27             log("PASS: Unregistration was successful");
28         else
29             log("FAIL: Unregistration failed");
30
31         if (!internals.hasServiceWorkerRegisteredForOrigin(self.origin))
32             log("PASS: There is no service worker registered for the origin");
33         else
34             log("FAIL: There is a service worker registered for the origin");
35
36         unregistrationResult = await registration.unregister();
37         if (!unregistrationResult)
38             log("PASS: Unregistration failed as expected");
39         else
40             log("FAIL: Unregistration succeeded unexpectedly");
41         
42         if (!internals.hasServiceWorkerRegisteredForOrigin(self.origin))
43             log("PASS: There is no service worker registered for the origin");
44         else
45             log("FAIL: There is a service worker registered for the origin");
46
47         registration = await navigator.serviceWorker.register("resources/basic-fetch-worker.js", { });
48         if (registration.scope === "https://127.0.0.1:8443/workers/service/")
49             log("PASS: registration scope is " + registration.scope);
50         else
51             log("FAIL: registration scope is " + registration.scope);
52
53         if (internals.hasServiceWorkerRegisteredForOrigin(self.origin))
54             log("PASS: There is a service worker registered for the origin");
55         else
56             log("FAIL: There is no service worker registered for the origin");
57     } catch(e) {
58         console.log("Got exception: " + e);
59     }
60     finishSWTest();
61 }
62
63 test();