shownotification.https.window.js (4152B)
1 // META: script=/resources/testdriver.js 2 // META: script=/resources/testdriver-vendor.js 3 // META: script=resources/helpers.js 4 // META: script=resources/custom-data.js 5 6 "use strict"; 7 8 /** @type {ServiceWorkerRegistration} */ 9 let registration; 10 11 promise_setup(async () => { 12 await test_driver.set_permission({ name: "notifications" }, "granted"); 13 registration = await prepareActiveServiceWorker("noop-sw.js"); 14 }); 15 16 promise_test(async () => { 17 const notifications = await registration.getNotifications(); 18 assert_equals(notifications.length, 0, "Should return zero notification"); 19 }, "fetching no notifications"); 20 21 promise_test(async t => { 22 t.add_cleanup(closeAllNotifications); 23 await registration.showNotification(""); 24 const notifications = await registration.getNotifications(); 25 assert_equals(notifications.length, 1, "Should return one notification"); 26 assert_equals(notifications[0].title, "", "Should return an empty title"); 27 }, "fetching notification with an empty title"); 28 29 promise_test(async t => { 30 t.add_cleanup(closeAllNotifications); 31 await Promise.all([ 32 registration.showNotification("thunder", { tag: "fire" }), 33 registration.showNotification("bird", { tag: "fox" }), 34 registration.showNotification("supernova", { tag: "quantum" }), 35 ]); 36 const notifications = await registration.getNotifications({ tag: "quantum" }); 37 assert_equals( 38 notifications.length, 39 1, 40 "Should return only the matching notification" 41 ); 42 assert_equals(notifications[0].title, "supernova", "title should match"); 43 assert_equals(notifications[0].tag, "quantum", "tag should match"); 44 }, "fetching notification by tag filter"); 45 46 promise_test(async t => { 47 t.add_cleanup(closeAllNotifications); 48 await Promise.all([ 49 registration.showNotification("thunder", { tag: "moz" }), 50 registration.showNotification("bird", { tag: "moz" }), 51 ]); 52 const notifications = await registration.getNotifications({ tag: "moz" }); 53 assert_equals( 54 notifications.length, 55 1, 56 "Should return only the latest notification" 57 ); 58 }, "fetching same-tagged notification by tag filter"); 59 60 promise_test(async t => { 61 t.add_cleanup(closeAllNotifications); 62 await Promise.all([ 63 registration.showNotification("thunder"), 64 registration.showNotification("bird"), 65 registration.showNotification("supernova"), 66 ]); 67 const notifications = await registration.getNotifications(); 68 assert_equals(notifications.length, 3, "Should return three notifications"); 69 }, "fetching multiple notifications"); 70 71 // https://notifications.spec.whatwg.org/#dom-serviceworkerregistration-getnotifications 72 // Step 5.2: Let notifications be a list of all notifications in the list of 73 // notifications ... whose service worker registration is this ... 74 promise_test(async t => { 75 t.add_cleanup(closeAllNotifications); 76 const another = await navigator.serviceWorker.register("noop-sw.js", { scope: "./scope" }); 77 await registration.showNotification("Hello"); 78 const notifications = await another.getNotifications(); 79 assert_equals(notifications.length, 0, "Should return no notification"); 80 }, "fetching from another registration") 81 82 // https://notifications.spec.whatwg.org/#non-persistent-notification 83 // A non-persistent notification is a notification without an associated 84 // service worker registration. 85 promise_test(async t => { 86 t.add_cleanup(closeAllNotifications); 87 const nonPersistent = new Notification("Non-persistent"); 88 t.add_cleanup(() => nonPersistent.close()); 89 await registration.showNotification("Hello"); 90 const notifications = await registration.getNotifications(); 91 assert_equals(notifications.length, 1, "Should return a notification"); 92 assert_equals(notifications[0].title, "Hello", "Title should match"); 93 }, "fetching only persistent notifications") 94 95 promise_test(async t => { 96 t.add_cleanup(closeAllNotifications); 97 await registration.showNotification("Hello", { data: fakeCustomData }); 98 const notifications = await registration.getNotifications(); 99 assert_equals(notifications.length, 1, "Should return a notification"); 100 assert_custom_data(notifications[0].data); 101 }, "fetching a notification with custom data")