test_notificationdb_module.js (3911B)
1 let { MemoryNotificationDB } = ChromeUtils.importESModule( 2 "moz-src:///dom/notification/MemoryNotificationDB.sys.mjs" 3 ); 4 5 add_task(async function test_delete_all() { 6 const db = new MemoryNotificationDB(); 7 8 // Save three notifications 9 const origin = "https://example.com"; 10 const scope = "https://example.com/"; 11 await db.taskSave({ 12 origin, 13 notification: { 14 id: "foo", 15 tag: "foo", 16 serviceWorkerRegistrationScope: scope, 17 }, 18 }); 19 await db.taskSave({ 20 origin, 21 notification: { 22 id: "bar", 23 tag: "bar", 24 serviceWorkerRegistrationScope: scope, 25 }, 26 }); 27 await db.taskSave({ 28 origin, 29 notification: { 30 id: "baz", 31 tag: "baz", 32 serviceWorkerRegistrationScope: scope, 33 }, 34 }); 35 36 // And also one with different origin 37 const origin2 = "https://example.org"; 38 const scope2 = "https://example.org/"; 39 await db.taskSave({ 40 origin: origin2, 41 notification: { 42 id: "foo2", 43 tag: "foo", 44 serviceWorkerRegistrationScope: scope2, 45 }, 46 }); 47 48 // We should have three notifications 49 let notifications = await db.taskGetAll({ origin, scope }); 50 Assert.equal(notifications.length, 3, "Should get three notifications"); 51 Assert.deepEqual( 52 notifications.map(n => n.id), 53 ["foo", "bar", "baz"], 54 "Should have three different notifications" 55 ); 56 57 // By tag 58 notifications = await db.taskGetAll({ origin, scope, tag: "foo" }); 59 Assert.equal(notifications.length, 1, "Should get one notification"); 60 Assert.equal(notifications[0].id, "foo", "Should get the expected ID"); 61 62 // By tag for different origin 63 notifications = await db.taskGetAll({ 64 origin: origin2, 65 scope: scope2, 66 tag: "foo", 67 }); 68 Assert.equal(notifications.length, 1, "Should get one notification"); 69 Assert.equal(notifications[0].id, "foo2", "Should get the expected ID"); 70 71 // Now remove all except bar/baz, meaning foo should be removed 72 await db.taskDeleteAllExcept({ ids: ["bar", "baz"] }); 73 notifications = await db.taskGetAll({ origin, scope }); 74 Assert.equal(notifications.length, 2, "Should get two notifications"); 75 Assert.deepEqual( 76 notifications.map(n => n.id), 77 ["bar", "baz"], 78 "Should have two different notifications" 79 ); 80 81 // Bar should be removed from tag too 82 notifications = await db.taskGetAll({ origin, scope, tag: "foo" }); 83 Assert.equal(notifications.length, 0, "Should get zero notification"); 84 85 // Now remove everything 86 await db.taskDeleteAllExcept({ ids: [] }); 87 notifications = await db.taskGetAll({ origin, scope }); 88 Assert.equal(notifications.length, 0, "Should get zero notification"); 89 notifications = await db.taskGetAll({ origin, scope, tag: "bar" }); 90 Assert.equal(notifications.length, 0, "Should get zero notification"); 91 notifications = await db.taskGetAll({ origin: origin2, scope: scope2 }); 92 Assert.equal(notifications.length, 0, "Should get zero notification"); 93 notifications = await db.taskGetAll({ 94 origin: origin2, 95 scope: scope2, 96 tag: "foo", 97 }); 98 Assert.equal(notifications.length, 0, "Should get zero notification"); 99 }); 100 101 add_task(async function test_delete_all() { 102 const db = new MemoryNotificationDB(); 103 const { notifications, byTag } = db.testGetRawMap(); 104 105 // Save a notification 106 const origin = "https://example.com"; 107 const scope = "https://example.com/"; 108 await db.taskSave({ 109 origin, 110 notification: { 111 id: "foo", 112 tag: "foo", 113 serviceWorkerRegistrationScope: scope, 114 }, 115 }); 116 117 Assert.ok(origin in notifications, "notification map should include origin"); 118 Assert.ok(origin in byTag, "notification tag map should include origin"); 119 120 await db.taskDelete({ 121 origin, 122 id: "foo", 123 }); 124 Assert.ok( 125 !(origin in notifications), 126 "notification map should not include origin" 127 ); 128 Assert.ok( 129 !(origin in byTag), 130 "notification tag map should not include origin" 131 ); 132 });