test_notificationdb.js (6803B)
1 "use strict"; 2 3 /** 4 * @import {NotificationDB} from "../../NotificationDB.sys.mjs" 5 * @type {NotificationDB} 6 */ 7 let db; 8 9 /** @type {NotificationDB} */ 10 let memoryDb; 11 12 add_setup(async function run_test() { 13 do_get_profile(); 14 15 db = ChromeUtils.importESModule( 16 "moz-src:///dom/notification/NotificationDB.sys.mjs" 17 ).db; 18 19 let { MemoryNotificationDB } = ChromeUtils.importESModule( 20 "moz-src:///dom/notification/MemoryNotificationDB.sys.mjs" 21 ); 22 memoryDb = new MemoryNotificationDB(); 23 }); 24 25 // Get one notification, none exists 26 add_task(async function test_get_none() { 27 let notifications = await db.queueTask("getall", { 28 origin: systemNotification.origin, 29 }); 30 31 Assert.equal(0, notifications.length); 32 }); 33 34 add_task(async function test_send_and_get_one() { 35 // Store one notification 36 await db.queueTask("save", { 37 origin: systemNotification.origin, 38 notification: systemNotification, 39 }); 40 41 // Get one notification, one exists 42 let notifications = await db.queueTask("getall", { 43 origin: systemNotification.origin, 44 }); 45 46 Assert.equal(1, notifications.length); 47 // compare the content 48 compareNotification(systemNotification, notifications[0]); 49 }); 50 51 add_task(async function test_delete_one_get_none_again() { 52 // Delete one notification 53 await db.queueTask("delete", { 54 origin: systemNotification.origin, 55 id: systemNotification.id, 56 }); 57 58 // Get one notification, none exists 59 let notifications = await db.queueTask("getall", { 60 origin: systemNotification.origin, 61 }); 62 Assert.equal(0, notifications.length); 63 }); 64 65 // Delete one notification that do not exists anymore 66 add_task(async function test_delete_one_nonexistent() { 67 await db.queueTask("delete", { 68 origin: systemNotification.origin, 69 id: systemNotification.id, 70 }); 71 }); 72 73 // Store two notifications with the same id 74 add_task(async function test_send_two_get_one() { 75 await db.queueTask("save", { 76 origin: systemNotification.origin, 77 notification: systemNotification, 78 }); 79 80 await db.queueTask("save", { 81 origin: systemNotification.origin, 82 notification: systemNotification, 83 }); 84 85 let notifications = await db.queueTask("getall", { 86 origin: systemNotification.origin, 87 }); 88 Assert.equal(1, notifications.length); 89 // compare the content 90 compareNotification(systemNotification, notifications[0]); 91 }); 92 93 // Delete previous notification 94 add_task(async function test_delete_previous() { 95 await db.queueTask("delete", { 96 origin: systemNotification.origin, 97 id: systemNotification.id, 98 }); 99 }); 100 101 // Store two notifications from same origin with the same tag 102 add_task(async function test_send_two_get_one() { 103 let tag = "voicemail"; 104 105 let systemNotification1 = getNotificationObject( 106 "system", 107 "{f271f9ee-3955-4c10-b1f2-af552fb270ee}", 108 tag 109 ); 110 let systemNotification2 = getNotificationObject( 111 "system", 112 "{8ef9a628-f0f4-44b4-820d-c117573c33e3}", 113 tag 114 ); 115 116 await db.queueTask("save", { 117 origin: systemNotification1.origin, 118 notification: systemNotification1, 119 }); 120 121 await db.queueTask("save", { 122 origin: systemNotification2.origin, 123 notification: systemNotification2, 124 }); 125 126 let notifications = await db.queueTask("getall", { 127 origin: systemNotification1.origin, 128 }); 129 Assert.equal(1, notifications.length); 130 // compare the content 131 compareNotification(systemNotification2, notifications[0]); 132 }); 133 134 // Delete previous notification 135 add_task(async function test_delete_previous() { 136 await db.queueTask("delete", { 137 origin: systemNotification.origin, 138 id: "{8ef9a628-f0f4-44b4-820d-c117573c33e3}", 139 }); 140 }); 141 142 // Store two notifications from two origins with the same tag 143 add_task(async function test_send_two_get_two() { 144 let tag = "voicemail"; 145 146 let systemNotification1 = systemNotification; 147 systemNotification1.tag = tag; 148 149 let calendarNotification2 = calendarNotification; 150 calendarNotification2.tag = tag; 151 152 await db.queueTask("save", { 153 origin: systemNotification1.origin, 154 notification: systemNotification1, 155 }); 156 157 await db.queueTask("save", { 158 origin: calendarNotification2.origin, 159 notification: calendarNotification2, 160 }); 161 162 // Trigger getall for each origin 163 let notifications = await db.queueTask("getall", { 164 origin: systemNotification1.origin, 165 }); 166 167 // one notification per origin 168 Assert.equal(1, notifications.length); 169 // first call should be system notification 170 compareNotification(systemNotification1, notifications[0]); 171 172 notifications = await db.queueTask("getall", { 173 origin: calendarNotification2.origin, 174 }); 175 176 // one notification per origin 177 Assert.equal(1, notifications.length); 178 // second and last call should be calendar notification 179 compareNotification(calendarNotification2, notifications[0]); 180 }); 181 182 // Cleanup previous notification 183 add_task(async function test_delete_previous() { 184 await db.queueTask("delete", { 185 origin: systemNotification.origin, 186 id: "{2bc883bf-2809-4432-b0f4-f54e10372764}", 187 }); 188 }); 189 190 add_task(async function test_notification_onDiskPersistence() { 191 let verifyDisk = async function (expectedId) { 192 const NOTIFICATION_STORE_PATH = PathUtils.join( 193 PathUtils.profileDir, 194 "notificationstore.json" 195 ); 196 197 const onDiskNotificationStr = await IOUtils.readUTF8( 198 NOTIFICATION_STORE_PATH 199 ); 200 return onDiskNotificationStr.includes(expectedId); 201 }; 202 203 let persistedNotification = getNotificationObject( 204 systemNotification.origin, 205 "{315aaf98-6c72-48fe-8e2c-a841e1b00027}", 206 "" /* tag */, 207 true /* scope */ 208 ); 209 210 await db.queueTask("save", { 211 origin: persistedNotification.origin, 212 notification: persistedNotification, 213 }); 214 Assert.ok(await verifyDisk(persistedNotification.id)); 215 216 let nonPersistedNotification = getNotificationObject( 217 systemNotification.origin, 218 "{8110ed62-303f-4f9b-a257-a62487aaa09c}", 219 "" /* tag */, 220 true /* scope */ 221 ); 222 223 await memoryDb.queueTask("save", { 224 origin: nonPersistedNotification.origin, 225 notification: nonPersistedNotification, 226 }); 227 228 // memoryonly notification must not exist on disk. 229 Assert.ok(!(await verifyDisk(nonPersistedNotification.id))); 230 231 let verifyMemory = function (notifications, expectedId) { 232 return notifications.some(notification => { 233 return notification.id == expectedId; 234 }); 235 }; 236 237 let notifications = await db.queueTask("getall", { 238 origin: persistedNotification.origin, 239 scope: persistedNotification.origin, 240 }); 241 Assert.ok(verifyMemory(notifications, persistedNotification.id)); 242 243 notifications = await memoryDb.queueTask("getall", { 244 origin: persistedNotification.origin, 245 scope: persistedNotification.origin, 246 }); 247 // memoryonly notification must exist in-memory 248 Assert.ok(verifyMemory(notifications, nonPersistedNotification.id)); 249 });