test_notification_serviceworker_get.html (3201B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>ServiceWorkerRegistration.getNotifications() on main thread and worker thread.</title> 5 <script src="/tests/SimpleTest/SimpleTest.js"></script> 6 <script src="MockAlertsService.js"></script> 7 <script src="NotificationTest.js"></script> 8 <script src="/tests/SimpleTest/GleanTest.js"></script> 9 <link rel="stylesheet" href="/tests/SimpleTest/test.css" /> 10 </head> 11 <body> 12 <p id="display"></p> 13 <div id="content" style="display: none"></div> 14 <pre id="test"></pre> 15 <script type="text/javascript"> 16 17 SimpleTest.requestFlakyTimeout("untriaged"); 18 19 function testFrame(src) { 20 return new Promise(resolve => { 21 var iframe = document.createElement("iframe"); 22 iframe.src = src; 23 window.callback = function(result) { 24 iframe.src = "about:blank"; 25 document.body.removeChild(iframe); 26 iframe = null; 27 SpecialPowers.exactGC(function() { 28 resolve(result); 29 }); 30 }; 31 document.body.appendChild(iframe); 32 }); 33 } 34 35 function registerSW() { 36 return testFrame('notification/register.html').then(function() { 37 ok(true, "Registered service worker."); 38 }); 39 } 40 41 async function unregisterSW() { 42 const reg = await navigator.serviceWorker.getRegistration("./notification/"); 43 await reg.unregister(); 44 } 45 46 async function testDismiss() { 47 await GleanTest.testResetFOG(); 48 49 // Dismissed persistent notifications should be removed from the 50 // notification list. 51 const reg = await navigator.serviceWorker.getRegistration("./notification/"); 52 await reg.showNotification("This is a notification that will be closed", { tag: "dismiss" }); 53 const showCount = await GleanTest.webNotification.showOrigin.first_party.testGetValue(); 54 is(showCount, 1, "Notification first party show attempt counter should increment once."); 55 56 const notifications = await reg.getNotifications(); 57 is(notifications.length, 1, "There should be one visible notification"); 58 is(notifications[0].tag, "dismiss", "Tag should match"); 59 60 // Simulate dismissing the notification by using the alerts service 61 // directly, instead of `Notification#close`. 62 const ids = await MockAlertsService.getNotificationIds(); 63 is(ids.length, 1, "Only one notification registered in the service"); 64 65 await MockAlertsService.closeNotification(ids[0]); 66 67 // Make sure dismissed notifications are no longer retrieved. 68 is((await reg.getNotifications()).length, 0, "There should be no more stored notifications"); 69 } 70 71 function testGetWorker() { 72 todo(false, "navigator.serviceWorker is not available on workers yet"); 73 return Promise.resolve(); 74 } 75 76 SimpleTest.waitForExplicitFinish(); 77 78 SpecialPowers.pushPrefEnv({"set": [ 79 ["dom.serviceWorkers.exemptFromPerDomainMax", true], 80 ["dom.serviceWorkers.enabled", true], 81 ["dom.serviceWorkers.testing.enabled", true], 82 ]}, function() { 83 registerSW() 84 .then(NotificationTest.allowNotifications) 85 .then(() => MockAlertsService.register()) 86 .then(testGetWorker) 87 .then(testDismiss) 88 .then(unregisterSW) 89 .then(function() { 90 SimpleTest.finish(); 91 }); 92 }); 93 </script> 94 </body> 95 </html>