registration-schedule-job.https.html (3737B)
1 <!DOCTYPE html> 2 <meta charset="utf-8"> 3 <meta name=timeout content=long> 4 <title>Service Worker: Schedule Job algorithm</title> 5 <script src="/resources/testharness.js"></script> 6 <script src="resources/testharness-helpers.js"></script> 7 <script src="/resources/testharnessreport.js"></script> 8 <script src="resources/test-helpers.sub.js"></script> 9 <script> 10 // Tests for https://w3c.github.io/ServiceWorker/#schedule-job-algorithm 11 // Non-equivalent register jobs should not be coalesced. 12 const scope = 'resources/'; 13 const script1 = 'resources/empty.js'; 14 const script2 = 'resources/empty.js?change'; 15 16 async function cleanup() { 17 const registration = await navigator.serviceWorker.getRegistration(scope); 18 if (registration) 19 await registration.unregister(); 20 } 21 22 function absolute_url(url) { 23 return new URL(url, self.location).toString(); 24 } 25 26 // Test that a change to `script` starts a new register job. 27 promise_test(async t => { 28 await cleanup(); 29 t.add_cleanup(cleanup); 30 31 // Make a registration. 32 const registration = await 33 navigator.serviceWorker.register(script1, {scope}); 34 35 // Schedule two more register jobs. 36 navigator.serviceWorker.register(script1, {scope}); 37 await navigator.serviceWorker.register(script2, {scope}); 38 39 // The jobs should not have been coalesced. 40 const worker = get_newest_worker(registration); 41 assert_equals(worker.scriptURL, absolute_url(script2)); 42 }, 'different scriptURL'); 43 44 // Test that a change to `updateViaCache` starts a new register job. 45 promise_test(async t => { 46 await cleanup(); 47 t.add_cleanup(cleanup); 48 49 // Check defaults. 50 const registration = await 51 navigator.serviceWorker.register(script1, {scope}); 52 assert_equals(registration.updateViaCache, 'imports'); 53 54 // Schedule two more register jobs. 55 navigator.serviceWorker.register(script1, {scope}); 56 await navigator.serviceWorker.register(script1, {scope, 57 updateViaCache: 'none'}); 58 59 // The jobs should not have been coalesced. 60 assert_equals(registration.updateViaCache, 'none'); 61 }, 'different updateViaCache'); 62 63 // Test that a change to `type` starts a new register job. 64 promise_test(async t => { 65 await cleanup(); 66 t.add_cleanup(cleanup); 67 68 const scriptForTypeCheck = 'resources/type-check-worker.js'; 69 // Check defaults. 70 const registration = await 71 navigator.serviceWorker.register(scriptForTypeCheck, {scope}); 72 73 let worker_type = await new Promise((resolve) => { 74 navigator.serviceWorker.onmessage = (event) => { 75 resolve(event.data); 76 }; 77 // The jobs should not have been coalesced. get_newest_worker() helps the 78 // test fail with stable output on browers that incorrectly coalesce 79 // register jobs, since then sometimes registration is not a new worker as 80 // expected. 81 const worker = get_newest_worker(registration); 82 // The argument of postMessage doesn't matter for this case. 83 worker.postMessage(''); 84 }); 85 86 assert_equals(worker_type, 'classic'); 87 88 // Schedule two more register jobs. 89 navigator.serviceWorker.register(scriptForTypeCheck, {scope}); 90 await navigator.serviceWorker.register(scriptForTypeCheck, {scope, type: 'module'}); 91 92 worker_type = await new Promise((resolve) => { 93 navigator.serviceWorker.onmessage = (event) => { 94 resolve(event.data); 95 }; 96 // The jobs should not have been coalesced. get_newest_worker() helps the 97 // test fail with stable output on browers that incorrectly coalesce 98 // register jobs, since then sometimes registration is not a new worker as 99 // expected. 100 const worker = get_newest_worker(registration); 101 // The argument of postMessage doesn't matter for this case. 102 worker.postMessage(''); 103 }); 104 105 assert_equals(worker_type, 'module'); 106 }, 'different type'); 107 </script>