shownotification-resolve-manual.https.html (1748B)
1 <!DOCTYPE html> 2 <script src="/resources/testharness.js"></script> 3 <script src="/resources/testharnessreport.js"></script> 4 <body> 5 <script> 6 const worker = 'resources/shownotification-sw.js'; 7 8 function reset() { 9 return navigator.serviceWorker.getRegistrations().then(registrations => { 10 return Promise.all(registrations.map(r => r.unregister())); 11 }); 12 } 13 14 function getNewestWorker(reg) { 15 return reg.installing || reg.waiting || reg.active; 16 } 17 18 function registerSwAndGetWorker() { 19 return reset() 20 .then(() => navigator.serviceWorker.register(worker)) 21 .then(getNewestWorker); 22 } 23 24 promise_test(() => { 25 // Get notification permission 26 return Notification.requestPermission().then(permission => { 27 if (permission != "granted") { 28 throw Error('You must allow notifications for this origin before running this test.'); 29 } 30 return registerSwAndGetWorker(); 31 }).then(worker => { 32 return new Promise(resolve => { 33 // Wait for the service worker to post a message with type 'notification-data'. 34 navigator.serviceWorker.onmessage = event => { 35 if (event.data && event.data.type == 'notification-data') { 36 resolve(event.data); 37 navigator.serviceWorker.onmessage = null; 38 } 39 }; 40 41 // Ask the service worker to run the test. 42 worker.postMessage('test-shownotification'); 43 }) 44 }).then(result => { 45 assert_true(result.notificationReturned, `Notification appeared in getNotifications`); 46 // The icon is delayed by 2000ms, so showNotification should have taken at least 1900 to resolve. 47 assert_greater_than(result.resolveDuration, 1900, `showNotification appeared to wait for icon load`); 48 }); 49 }, 'showNotification resolves after icon fetch'); 50 51 </script> 52 </body>