claim-shared-worker-fetch.https.html (2714B)
1 <!doctype html> 2 <meta charset=utf-8> 3 <title></title> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <script src="resources/test-helpers.sub.js"></script> 7 <body> 8 <script> 9 10 promise_test(function(t) { 11 var frame; 12 var resource = 'simple.txt'; 13 14 var worker; 15 var scope = 'resources/'; 16 var script = 'resources/claim-worker.js'; 17 18 return Promise.resolve() 19 // Create the test iframe with a shared worker. 20 .then(() => with_iframe('resources/claim-shared-worker-fetch-iframe.html')) 21 .then(f => frame = f) 22 23 // Check the controller and test with fetch in the shared worker. 24 .then(() => assert_equals(frame.contentWindow.navigator.controller, 25 undefined, 26 'Should have no controller.')) 27 .then(() => frame.contentWindow.fetch_in_shared_worker(resource)) 28 .then(response_text => assert_equals(response_text, 29 'a simple text file\n', 30 'fetch() should not be intercepted.')) 31 // Register a service worker. 32 .then(() => service_worker_unregister_and_register(t, script, scope)) 33 .then(r => { 34 t.add_cleanup(() => service_worker_unregister(t, scope)); 35 36 worker = r.installing; 37 38 return wait_for_state(t, worker, 'activated') 39 }) 40 // Let the service worker claim the iframe and the shared worker. 41 .then(() => { 42 var channel = new MessageChannel(); 43 var saw_message = new Promise(function(resolve) { 44 channel.port1.onmessage = t.step_func(function(e) { 45 assert_equals(e.data, 'PASS', 46 'Worker call to claim() should fulfill.'); 47 resolve(); 48 }); 49 }); 50 worker.postMessage({port: channel.port2}, [channel.port2]); 51 return saw_message; 52 }) 53 54 // Check the controller and test with fetch in the shared worker. 55 .then(() => frame.contentWindow.navigator.serviceWorker.getRegistration(scope)) 56 .then(r => assert_equals(frame.contentWindow.navigator.serviceWorker.controller, 57 r.active, 58 'Test iframe should be claimed.')) 59 // TODO(horo): Check the SharedWorker's navigator.seviceWorker.controller. 60 .then(() => frame.contentWindow.fetch_in_shared_worker(resource)) 61 .then(response_text => 62 assert_equals(response_text, 63 'Intercepted!', 64 'fetch() in the shared worker should be intercepted.')) 65 66 // Cleanup this testcase. 67 .then(() => frame.remove()); 68 }, 'fetch() in SharedWorker should be intercepted after the client is claimed.') 69 70 </script> 71 </body>