serviceWorker-dedicated-worker.https.html (2983B)
1 <!DOCTYPE html> 2 <title>Service Worker: Check if dedicated workers are controlled</title> 3 <script src="/resources/testharness.js"></script> 4 <script src="/resources/testharnessreport.js"></script> 5 <script src="resources/utils.js"></script> 6 <script src="/common/utils.js"></script> 7 8 <body> 9 <script> 10 const frameUrl = './resources/serviceWorker-dedicated-worker-inner.html'; 11 const unregisterAllSW = async () => { 12 const regs = await navigator.serviceWorker.getRegistrations(); 13 return Promise.all(regs.map(reg => reg.unregister())); 14 }; 15 16 promise_test(async t => { 17 t.add_cleanup(unregisterAllSW); 18 const key = token(); 19 20 // Inside the fenced frame, the service worker is registered and fetch 21 // request is triggered from the dedicated worker to the url that is 22 // handled in the service worker. 23 const url = `${frameUrl}?useServiceWorkerInFencedFrame=true`; 24 attachFencedFrame(generateURL(url, [key])); 25 const result = await nextValueFromServer(key); 26 assert_equals(result, "OK"); 27 }, "Fenced frame's service workers can control fenced frame's dedicated workers"); 28 29 promise_test(async t => { 30 t.add_cleanup(unregisterAllSW); 31 const key = token(); 32 33 // Set a service worker in the fenced frame. Inside the fenced frame, a 34 // dedicated worker is created and triggers a fetch request. But we don't 35 // use the fetch request result in this test. This test will check if the 36 // dedicated worker in the parent frame is controlled by the SW in FF. 37 const url = `${frameUrl}?useServiceWorkerInFencedFrame=true`; 38 attachFencedFrame(generateURL(url, [key])); 39 await nextValueFromServer(key); 40 41 const checkIfWorkerIsControlled = async () => { 42 const dedicated_worker = new Worker('resources/serviceWorker-dedicated-worker.js'); 43 return new Promise((resolve, reject) => { 44 dedicated_worker.addEventListener('message', e => { 45 resolve(e.data) 46 }); 47 dedicated_worker.postMessage('fetch'); 48 }); 49 } 50 51 const result = await checkIfWorkerIsControlled() 52 assert_equals(result, "Not Found"); 53 }, "Fenced frame's service workers can not control the dedicated workers in the parent frame"); 54 55 promise_test(async t => { 56 t.add_cleanup(unregisterAllSW); 57 const key = token(); 58 59 // Register a service worker in the parent frame. 60 await navigator.serviceWorker.register('resources/serviceWorker-dedicated-worker-sw.js', { scope: '/' }); 61 await navigator.serviceWorker.ready; 62 63 // Inside the fenced frame, fetch request to unexisting URL is triggered 64 // from the dedicated worker. 65 attachFencedFrame(generateURL(frameUrl, [key])); 66 67 const result = await nextValueFromServer(key); 68 assert_equals(result, "Not Found"); 69 }, "Service workers in the parent frame of fenced frames can not control dedicated workers in fenced frames"); 70 </script> 71 </body>