notification_worker.js (3365B)
1 /* eslint-disable mozilla/no-comparison-or-assignment-inside-ok */ 2 3 function ok(test, message) { 4 postMessage({ type: "ok", test, message }); 5 } 6 7 function is(a, b, message) { 8 postMessage({ type: "is", test1: a, test2: b, message }); 9 } 10 11 if (self.Notification) { 12 var steps = [ 13 function () { 14 ok(typeof Notification === "function", "Notification constructor exists"); 15 ok(Notification.permission, "Notification.permission exists"); 16 ok( 17 typeof Notification.requestPermission === "undefined", 18 "Notification.requestPermission should not exist" 19 ); 20 }, 21 22 function (done) { 23 var options = { 24 dir: "auto", 25 lang: "", 26 body: "This is a notification body", 27 tag: "sometag", 28 icon: "icon.png", 29 data: ["a complex object that should be", { structured: "cloned" }], 30 mozbehavior: { vibrationPattern: [30, 200, 30] }, 31 }; 32 var notification = new Notification("This is a title", options); 33 34 ok(notification !== undefined, "Notification exists"); 35 is(notification.onclick, null, "onclick() should be null"); 36 is(notification.onshow, null, "onshow() should be null"); 37 is(notification.onerror, null, "onerror() should be null"); 38 is(notification.onclose, null, "onclose() should be null"); 39 is(typeof notification.close, "function", "close() should exist"); 40 41 is(notification.dir, options.dir, "auto should get set"); 42 is(notification.lang, options.lang, "lang should get set"); 43 is(notification.body, options.body, "body should get set"); 44 is(notification.tag, options.tag, "tag should get set"); 45 is( 46 notification.icon, 47 new URL(options.icon, location.href).toString(), 48 "icon should get set" 49 ); 50 is( 51 notification.data[0], 52 "a complex object that should be", 53 "data item 0 should be a matching string" 54 ); 55 is( 56 notification.data[1].structured, 57 "cloned", 58 "data item 1 should be a matching object literal" 59 ); 60 61 // store notification in test context 62 this.notification = notification; 63 64 notification.onshow = function () { 65 ok(true, "onshow handler should be called"); 66 done(); 67 }; 68 }, 69 70 function (done) { 71 var notification = this.notification; 72 73 notification.onclose = function () { 74 ok(true, "onclose handler should be called"); 75 done(); 76 }; 77 78 notification.close(); 79 }, 80 ]; 81 82 onmessage = () => { 83 var context = {}; 84 (function executeRemainingTests(remainingTests) { 85 if (!remainingTests.length) { 86 postMessage({ type: "finish" }); 87 return; 88 } 89 90 var nextTest = remainingTests.shift(); 91 var finishTest = executeRemainingTests.bind(null, remainingTests); 92 var startTest = nextTest.call.bind(nextTest, context, finishTest); 93 94 try { 95 startTest(); 96 // if no callback was defined for test function, 97 // we must manually invoke finish to continue 98 if (nextTest.length === 0) { 99 finishTest(); 100 } 101 } catch (ex) { 102 ok(false, "Test threw exception! " + nextTest + " " + ex); 103 finishTest(); 104 } 105 })(steps); 106 }; 107 } else { 108 ok(true, "Notifications are not enabled in workers on the platform."); 109 postMessage({ type: "finish" }); 110 }