test_bad_script_cache.html (3192B)
1 <!-- 2 Any copyright is dedicated to the Public Domain. 3 http://creativecommons.org/publicdomain/zero/1.0/ 4 --> 5 <!DOCTYPE HTML> 6 <html> 7 <head> 8 <title>Test updating a service worker with a bad script cache.</title> 9 <script src="/tests/SimpleTest/SimpleTest.js"></script> 10 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 11 </head> 12 <body> 13 <script src='utils.js'></script> 14 <script class="testbody" type="text/javascript"> 15 16 async function deleteCaches(cacheStorage) { 17 let keyList = await cacheStorage.keys(); 18 let promiseList = []; 19 keyList.forEach(key => { 20 promiseList.push(cacheStorage.delete(key)); 21 }); 22 return await Promise.all(keyList); 23 } 24 25 function waitForUpdate(reg) { 26 return new Promise(resolve => { 27 reg.addEventListener('updatefound', resolve, { once: true }); 28 }); 29 } 30 31 async function runTest() { 32 let reg; 33 try { 34 const script = 'update_worker.sjs'; 35 const scope = 'bad-script-cache'; 36 37 reg = await navigator.serviceWorker.register(script, { scope }); 38 await waitForState(reg.installing, 'activated'); 39 40 // Verify the service worker script cache has the worker script stored. 41 let chromeCaches = SpecialPowers.createChromeCache('chrome', window.origin); 42 let scriptURL = new URL(script, window.location.href); 43 let response = await chromeCaches.match(scriptURL.href); 44 is(response.url, scriptURL.href, 'worker script should be stored'); 45 46 // Force delete the service worker script out from under the service worker. 47 // Note: Prefs are set to kill the SW thread immediately on idle. 48 await deleteCaches(chromeCaches); 49 50 // Verify the service script cache no longer knows about the worker script. 51 response = await chromeCaches.match(scriptURL.href); 52 is(response, undefined, 'worker script should not be stored'); 53 54 // Force an update and wait for it to fire an update event. 55 reg.update(); 56 await waitForUpdate(reg); 57 await waitForState(reg.installing, 'activated'); 58 59 // Verify that the script cache knows about the worker script again. 60 response = await chromeCaches.match(scriptURL.href); 61 is(response.url, scriptURL.href, 'worker script should be stored'); 62 } catch (e) { 63 ok(false, e); 64 } 65 if (reg) { 66 await reg.unregister(); 67 } 68 69 // If this test is run on windows and the process shuts down immediately after, then 70 // we may fail to remove some of the Cache API body files. This is because the GC 71 // runs late causing Cache API to cleanup after shutdown begins. It seems something 72 // during shutdown scans these files and conflicts with removing the file on windows. 73 // 74 // To avoid this we perform an explict GC here to ensure that Cache API can cleanup 75 // earlier. 76 await new Promise(resolve => SpecialPowers.exactGC(resolve)); 77 78 SimpleTest.finish(); 79 } 80 81 SimpleTest.waitForExplicitFinish(); 82 SpecialPowers.pushPrefEnv({"set": [ 83 // standard prefs 84 ["dom.serviceWorkers.exemptFromPerDomainMax", true], 85 ["dom.serviceWorkers.enabled", true], 86 ["dom.serviceWorkers.testing.enabled", true], 87 88 // immediately kill the service worker thread when idle 89 ["dom.serviceWorkers.idle_timeout", 0], 90 91 ]}, runTest); 92 </script> 93 </pre> 94 </body> 95 </html>