global-serviceworker.https.any.js (2545B)
1 // META: title=serviceWorker on service worker global 2 // META: global=serviceworker 3 4 test(() => { 5 assert_equals(registration.installing, null, 'registration.installing'); 6 assert_equals(registration.waiting, null, 'registration.waiting'); 7 assert_equals(registration.active, null, 'registration.active'); 8 assert_true('serviceWorker' in self, 'self.serviceWorker exists'); 9 assert_equals(serviceWorker.state, 'parsed', 'serviceWorker.state'); 10 assert_readonly(self, 'serviceWorker', `self.serviceWorker is read only`); 11 }, 'First run'); 12 13 // Cache this for later tests. 14 const initialServiceWorker = self.serviceWorker; 15 16 async_test((t) => { 17 assert_true('serviceWorker' in self, 'self.serviceWorker exists'); 18 serviceWorker.postMessage({ messageTest: true }); 19 20 // The rest of the test runs once this receives the above message. 21 addEventListener('message', t.step_func((event) => { 22 // Ignore unrelated messages. 23 if (!event.data.messageTest) return; 24 assert_equals(event.source, serviceWorker, 'event.source'); 25 t.done(); 26 })); 27 }, 'Can post message to self during startup'); 28 29 // The test is registered now so there isn't a race condition when collecting tests, but the asserts 30 // don't happen until the 'install' event fires. 31 async_test((t) => { 32 addEventListener('install', t.step_func_done(() => { 33 assert_true('serviceWorker' in self, 'self.serviceWorker exists'); 34 assert_equals(serviceWorker, initialServiceWorker, `self.serviceWorker hasn't changed`); 35 assert_equals(registration.installing, serviceWorker, 'registration.installing'); 36 assert_equals(registration.waiting, null, 'registration.waiting'); 37 assert_equals(registration.active, null, 'registration.active'); 38 assert_equals(serviceWorker.state, 'installing', 'serviceWorker.state'); 39 })); 40 }, 'During install'); 41 42 // The test is registered now so there isn't a race condition when collecting tests, but the asserts 43 // don't happen until the 'activate' event fires. 44 async_test((t) => { 45 addEventListener('activate', t.step_func_done(() => { 46 assert_true('serviceWorker' in self, 'self.serviceWorker exists'); 47 assert_equals(serviceWorker, initialServiceWorker, `self.serviceWorker hasn't changed`); 48 assert_equals(registration.installing, null, 'registration.installing'); 49 assert_equals(registration.waiting, null, 'registration.waiting'); 50 assert_equals(registration.active, serviceWorker, 'registration.active'); 51 assert_equals(serviceWorker.state, 'activating', 'serviceWorker.state'); 52 })); 53 }, 'During activate');