helpers.js (1489B)
1 function unregisterAllServiceWorker() { 2 return navigator.serviceWorker.getRegistrations().then(registrations => { 3 return Promise.all(registrations.map(r => r.unregister())); 4 }); 5 } 6 7 async function prepareActiveServiceWorker(script) { 8 await unregisterAllServiceWorker(); 9 const reg = await navigator.serviceWorker.register(script); 10 add_completion_callback(() => reg.unregister()); 11 await navigator.serviceWorker.ready; 12 return reg; 13 } 14 15 async function closeAllNotifications() { 16 for (const n of await registration.getNotifications()) { 17 n.close(); 18 } 19 } 20 21 async function trySettingPermission(perm) { 22 try { 23 await test_driver.set_permission({ name: "notifications" }, perm); 24 } catch { 25 // Not all implementations support this yet, but the permission may already be set to be able to continue 26 } 27 28 // Using Notification.permission instead of permissions.query() as 29 // some implementation without set_permission support overrides 30 // Notification.permission. 31 const permission = Notification.permission === "default" ? "prompt" : Notification.permission; 32 if (permission !== perm) { 33 throw new Error(`Should have the permission ${perm} to continue but found ${permission}`); 34 } 35 } 36 37 // Use this in service workers where activation is required e.g. when testing showNotification() 38 async function untilActivate() { 39 if (registration.active) { 40 return; 41 } 42 return new Promise(resolve => { 43 addEventListener("activate", resolve, { once: true }); 44 }); 45 }