NotificationTest.js (2951B)
1 /* global MockAlertsService, registerAndWaitForActive */ 2 3 var NotificationTest = (function () { 4 "use strict"; 5 6 function info(msg, name) { 7 SimpleTest.info("::Notification Tests::" + (name || ""), msg); 8 } 9 10 function executeTests(tests, callback) { 11 // context is `this` object in test functions 12 // it can be used to track data between tests 13 var context = {}; 14 15 (async function executeRemainingTests(remainingTests) { 16 if (!remainingTests.length) { 17 callback(); 18 return; 19 } 20 21 var nextTest = remainingTests.shift(); 22 var finishTest = executeRemainingTests.bind(null, remainingTests); 23 var startTest = nextTest.call.bind(nextTest, context, finishTest); 24 25 try { 26 await startTest(); 27 // if no callback was defined for test function, 28 // we must manually invoke finish to continue 29 if (nextTest.length === 0) { 30 finishTest(); 31 } 32 } catch (e) { 33 ok(false, `Test threw exception: ${e}`); 34 finishTest(); 35 } 36 })(tests); 37 } 38 39 // NotificationTest API 40 return { 41 run(tests) { 42 SimpleTest.waitForExplicitFinish(); 43 44 addLoadEvent(async function () { 45 executeTests(tests, function () { 46 SimpleTest.finish(); 47 }); 48 }); 49 }, 50 51 allowNotifications() { 52 return SpecialPowers.pushPermissions([ 53 { 54 type: "desktop-notification", 55 allow: SpecialPowers.Services.perms.ALLOW_ACTION, 56 context: document, 57 }, 58 ]); 59 }, 60 61 denyNotifications() { 62 return SpecialPowers.pushPermissions([ 63 { 64 type: "desktop-notification", 65 allow: SpecialPowers.Services.perms.DENY_ACTION, 66 context: document, 67 }, 68 ]); 69 }, 70 71 clickNotification() { 72 // TODO: how?? 73 }, 74 75 fireCloseEvent(title) { 76 window.dispatchEvent( 77 new CustomEvent("mock-notification-close-event", { 78 detail: { 79 title, 80 }, 81 }) 82 ); 83 }, 84 85 info, 86 87 payload: { 88 body: "Body", 89 tag: "fakeTag", 90 icon: "icon.jpg", 91 lang: "en-US", 92 dir: "ltr", 93 }, 94 }; 95 })(); 96 97 async function setupServiceWorker(src, scope) { 98 await NotificationTest.allowNotifications(); 99 await MockAlertsService.register(); 100 let registration = await registerAndWaitForActive(src, scope); 101 SimpleTest.registerCleanupFunction(async () => { 102 await registration.unregister(); 103 }); 104 return registration; 105 } 106 107 async function testFrame(src, args) { 108 let { promise, resolve } = Promise.withResolvers(); 109 let iframe = document.createElement("iframe"); 110 let serialized = encodeURIComponent(JSON.stringify(args)); 111 iframe.src = `${src}?args=${serialized}`; 112 window.callback = async function (data) { 113 window.callback = null; 114 document.body.removeChild(iframe); 115 iframe = null; 116 resolve(data); 117 }; 118 document.body.appendChild(iframe); 119 return await promise; 120 }