getnotifications-sw.js (1446B)
1 importScripts("/resources/testharness.js"); 2 importScripts("resources/helpers.js"); 3 4 async function cleanup() { 5 for (const n of await registration.getNotifications()) { 6 n.close(); 7 } 8 } 9 10 async function test_notification(t, title) { 11 t.add_cleanup(cleanup); 12 13 const notifications = await registration.getNotifications(); 14 15 assert_equals( 16 notifications.length, 17 1, 18 "There should be one stored notification" 19 ); 20 const notification = notifications[0]; 21 assert_true(notification instanceof Notification, "Should be a Notification"); 22 assert_equals(notification.title, title, "Title should match"); 23 } 24 25 async function postAll(data) { 26 const clients = await self.clients.matchAll({ includeUncontrolled: true }); 27 assert_true(clients.length > 0, "clients.length"); 28 for (const client of clients) { 29 client.postMessage(data); 30 } 31 } 32 33 promise_setup(async () => { 34 await untilActivate(); 35 }); 36 37 promise_test(async t => { 38 await new Promise((resolve, reject) => { 39 self.addEventListener("message", ev => { 40 if (ev.data === "notification-created") { 41 resolve(); 42 } 43 }); 44 postAll("notification-create").catch(reject); 45 }); 46 await test_notification(t, "Created from window"); 47 }, "Get notification created from window"); 48 49 promise_test(async t => { 50 await registration.showNotification("Created here"); 51 await test_notification(t, "Created here"); 52 }, "Create and get notification within service worker");