claim-affect-other-registration.https.html (5524B)
1 <!doctype html> 2 <meta charset=utf-8> 3 <title>Service Worker: claim() should affect other registration</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 <script> 8 9 promise_test(function(t) { 10 var frame; 11 12 var init_scope = 'resources/blank.html?affect-other'; 13 var init_worker_url = 'resources/empty.js'; 14 var init_registration; 15 var init_workers = [undefined, undefined]; 16 17 var claim_scope = 'resources/blank.html?affect-other-registration'; 18 var claim_worker_url = 'resources/claim-worker.js'; 19 var claim_registration; 20 var claim_worker; 21 22 return Promise.resolve() 23 // Register the first service worker to init_scope. 24 .then(() => navigator.serviceWorker.register(init_worker_url + '?v1', 25 {scope: init_scope})) 26 .then(r => { 27 init_registration = r; 28 init_workers[0] = r.installing; 29 return Promise.resolve() 30 .then(() => wait_for_state(t, init_workers[0], 'activated')) 31 .then(() => assert_array_equals([init_registration.active, 32 init_registration.waiting, 33 init_registration.installing], 34 [init_workers[0], 35 null, 36 null], 37 'Wrong workers.')); 38 }) 39 40 // Create an iframe as the client of the first service worker of init_scope. 41 .then(() => with_iframe(claim_scope)) 42 .then(f => frame = f) 43 44 // Check the controller. 45 .then(() => frame.contentWindow.navigator.serviceWorker.getRegistration( 46 normalizeURL(init_scope))) 47 .then(r => assert_equals(frame.contentWindow.navigator.serviceWorker.controller, 48 r.active, 49 '.controller should belong to init_scope.')) 50 51 // Register the second service worker to init_scope. 52 .then(() => navigator.serviceWorker.register(init_worker_url + '?v2', 53 {scope: init_scope})) 54 .then(r => { 55 assert_equals(r, init_registration, 'Should be the same registration'); 56 init_workers[1] = r.installing; 57 return Promise.resolve() 58 .then(() => wait_for_state(t, init_workers[1], 'installed')) 59 .then(() => assert_array_equals([init_registration.active, 60 init_registration.waiting, 61 init_registration.installing], 62 [init_workers[0], 63 init_workers[1], 64 null], 65 'Wrong workers.')); 66 }) 67 68 // Register a service worker to claim_scope. 69 .then(() => navigator.serviceWorker.register(claim_worker_url, 70 {scope: claim_scope})) 71 .then(r => { 72 claim_registration = r; 73 claim_worker = r.installing; 74 return wait_for_state(t, claim_worker, 'activated') 75 }) 76 77 // Let claim_worker claim the created iframe. 78 .then(function() { 79 var channel = new MessageChannel(); 80 var saw_message = new Promise(function(resolve) { 81 channel.port1.onmessage = t.step_func(function(e) { 82 assert_equals(e.data, 'PASS', 83 'Worker call to claim() should fulfill.'); 84 resolve(); 85 }); 86 }); 87 88 claim_worker.postMessage({port: channel.port2}, [channel.port2]); 89 return saw_message; 90 }) 91 92 // Check the controller. 93 .then(() => frame.contentWindow.navigator.serviceWorker.getRegistration( 94 normalizeURL(claim_scope))) 95 .then(r => assert_equals(frame.contentWindow.navigator.serviceWorker.controller, 96 r.active, 97 '.controller should belong to claim_scope.')) 98 99 // Check the status of created registrations and service workers. 100 .then(() => wait_for_state(t, init_workers[1], 'activated')) 101 .then(() => { 102 assert_array_equals([claim_registration.active, 103 claim_registration.waiting, 104 claim_registration.installing], 105 [claim_worker, 106 null, 107 null], 108 'claim_worker should be the only worker.') 109 110 assert_array_equals([init_registration.active, 111 init_registration.waiting, 112 init_registration.installing], 113 [init_workers[1], 114 null, 115 null], 116 'The waiting sw should become the active worker.') 117 118 assert_array_equals([init_workers[0].state, 119 init_workers[1].state, 120 claim_worker.state], 121 ['redundant', 122 'activated', 123 'activated'], 124 'Wrong worker states.'); 125 }) 126 127 // Cleanup and finish testing. 128 .then(() => frame.remove()) 129 .then(() => Promise.all([ 130 init_registration.unregister(), 131 claim_registration.unregister() 132 ])) 133 .then(() => t.done()); 134 }, 'claim() should affect the originally controlling registration.'); 135 136 </script>