Add optimization when updating a SW registration results in the exact same script
[WebKit-https.git] / LayoutTests / http / tests / workers / service / registration-updateViaCache-all.html
1 <html>
2 <head>
3 <script src="resources/sw-test-pre.js"></script>
4 </head>
5 <body>
6 <script>
7
8 function getRandomIdFromWorker(worker)
9 {
10     worker.postMessage("getRandomId");
11     return new Promise(function(resolve) {
12       navigator.serviceWorker.addEventListener('message', function(e) {
13             resolve(e.data);
14         });
15     });
16 }
17
18 async function test()
19 {
20     try {
21         let registration = await navigator.serviceWorker.register("resources/cacheable-script-worker.php", { updateViaCache: "all" });
22         let worker1 = registration.installing;
23         await waitForState(worker1, "activated");
24         let randomId1 = await getRandomIdFromWorker(worker1);
25
26         await registration.update();
27         if (registration.installing) {
28             log("FAIL: The new script should have come from the cache and thus be identical, in which case it would not have be reinstalled");
29             finishSWTest();
30         }
31  
32         let worker2 = registration.active;
33         let randomId2 = await getRandomIdFromWorker(worker2);
34
35         if (randomId1 === randomId2)
36             log("PASS: The worker script came from the network cache");
37         else
38             log("FAIL: The worker script did not come from the network cache");
39     } catch(e) {
40         log("Got exception: " + e);
41     }
42     finishSWTest();
43 }
44 test();
45 </script>
46 </body>
47 </html>