OLD | NEW |
1 (function() | 1 (function() |
2 { | 2 { |
3 let testRunner = null; | 3 let testRunner = null; |
4 let randomResult = 0.5; | 4 let randomResult = 0.5; |
5 | 5 |
6 let originalInfo; | 6 let originalInfo; |
7 let info = require("info"); | 7 let info = require("info"); |
8 | 8 |
| 9 function showNotifications(url) |
| 10 { |
| 11 let shownNotifications = []; |
| 12 function showListener(notification) |
| 13 { |
| 14 shownNotifications.push(notification); |
| 15 Notification.markAsShown(notification.id); |
| 16 } |
| 17 Notification.addShowListener(showListener); |
| 18 Notification.showNext(url); |
| 19 Notification.removeShowListener(showListener); |
| 20 return shownNotifications; |
| 21 } |
| 22 |
9 module("Notification handling", | 23 module("Notification handling", |
10 { | 24 { |
11 setup: function() | 25 setup: function() |
12 { | 26 { |
13 testRunner = this; | 27 testRunner = this; |
14 | 28 |
15 preparePrefs.call(this); | 29 preparePrefs.call(this); |
16 setupVirtualTime.call(this, function(wrapTimer) | 30 setupVirtualTime.call(this, function(wrapTimer) |
17 { | 31 { |
18 let NotificationModule = getModuleGlobal("notification"); | 32 let NotificationModule = getModuleGlobal("notification"); |
(...skipping 14 matching lines...) Expand all Loading... |
33 | 47 |
34 Prefs.notificationurl = "http://example.com/notification.json"; | 48 Prefs.notificationurl = "http://example.com/notification.json"; |
35 Prefs.notificationdata = {}; | 49 Prefs.notificationdata = {}; |
36 Prefs.notifications_ignoredcategories = []; | 50 Prefs.notifications_ignoredcategories = []; |
37 | 51 |
38 // Replace Math.random() function | 52 // Replace Math.random() function |
39 let DownloaderGlobal = Cu.getGlobalForObject(getModuleGlobal("downloader")
); | 53 let DownloaderGlobal = Cu.getGlobalForObject(getModuleGlobal("downloader")
); |
40 this._origRandom = DownloaderGlobal.Math.random; | 54 this._origRandom = DownloaderGlobal.Math.random; |
41 DownloaderGlobal.Math.random = () => randomResult; | 55 DownloaderGlobal.Math.random = () => randomResult; |
42 randomResult = 0.5; | 56 randomResult = 0.5; |
| 57 |
| 58 let NotificationGlobal = getModuleGlobal("notification"); |
| 59 this._origShowListeners = NotificationGlobal.showListeners; |
| 60 NotificationGlobal.showListeners = []; |
43 }, | 61 }, |
44 | 62 |
45 teardown: function() | 63 teardown: function() |
46 { | 64 { |
47 restorePrefs.call(this); | 65 restorePrefs.call(this); |
48 restoreVirtualTime.call(this); | 66 restoreVirtualTime.call(this); |
49 restoreVirtualXMLHttp.call(this); | 67 restoreVirtualXMLHttp.call(this); |
50 | 68 |
51 for (let key in originalInfo) | 69 for (let key in originalInfo) |
52 info[key] = originalInfo[key]; | 70 info[key] = originalInfo[key]; |
53 | 71 |
54 if (this._origRandom) | 72 if (this._origRandom) |
55 { | 73 { |
56 let DownloaderGlobal = Cu.getGlobalForObject(getModuleGlobal("downloader
")); | 74 let DownloaderGlobal = Cu.getGlobalForObject(getModuleGlobal("downloader
")); |
57 DownloaderGlobal.Math.random = this._origRandom; | 75 DownloaderGlobal.Math.random = this._origRandom; |
58 delete this._origRandom; | 76 delete this._origRandom; |
59 } | 77 } |
60 | 78 |
| 79 if (this._origShowListeners) |
| 80 { |
| 81 let NotificationGlobal = getModuleGlobal("notification"); |
| 82 NotificationGlobal.showListeners = this._origShowListeners; |
| 83 delete this._origShowListeners; |
| 84 } |
| 85 |
61 Notification.init(); | 86 Notification.init(); |
62 } | 87 } |
63 }); | 88 }); |
64 | 89 |
65 function registerHandler(notifications, checkCallback) | 90 function registerHandler(notifications, checkCallback) |
66 { | 91 { |
67 testRunner.registerHandler("/notification.json", function(metadata) | 92 testRunner.registerHandler("/notification.json", function(metadata) |
68 { | 93 { |
69 if (checkCallback) | 94 if (checkCallback) |
70 checkCallback(metadata); | 95 checkCallback(metadata); |
(...skipping 11 matching lines...) Expand all Loading... |
82 { | 107 { |
83 // deepEqual() expects that the constructors used in expected objects and | 108 // deepEqual() expects that the constructors used in expected objects and |
84 // the ones in the actual results are the same. That means that we actually | 109 // the ones in the actual results are the same. That means that we actually |
85 // have to construct our objects in the context of the notification module. | 110 // have to construct our objects in the context of the notification module. |
86 let JSON = Cu.getGlobalForObject(Notification).JSON; | 111 let JSON = Cu.getGlobalForObject(Notification).JSON; |
87 return JSON.parse(JSON.stringify(object)); | 112 return JSON.parse(JSON.stringify(object)); |
88 } | 113 } |
89 | 114 |
90 test("No data", function() | 115 test("No data", function() |
91 { | 116 { |
92 equal(Notification.getNextToShow(), null, "null should be returned if there
is no data"); | 117 deepEqual(showNotifications(), [], "No notifications should be returned if t
here is no data"); |
93 }); | 118 }); |
94 | 119 |
95 test("Single notification", function() | 120 test("Single notification", function() |
96 { | 121 { |
97 let information = fixConstructors({ | 122 let information = fixConstructors({ |
98 id: 1, | 123 id: 1, |
99 type: "information", | 124 type: "information", |
100 message: {"en-US": "Information"} | 125 message: {"en-US": "Information"} |
101 }); | 126 }); |
102 | 127 |
103 registerHandler([information]); | 128 registerHandler([information]); |
104 testRunner.runScheduledTasks(1); | 129 testRunner.runScheduledTasks(1); |
105 | 130 |
106 deepEqual(Notification.getNextToShow(), information, "The notification is sh
own"); | 131 deepEqual(showNotifications(), [information], "The notification is shown"); |
107 equal(Notification.getNextToShow(), null, "Informational notifications aren'
t shown more than once"); | 132 deepEqual(showNotifications(), [], "Informational notifications aren't shown
more than once"); |
108 }); | 133 }); |
109 | 134 |
110 test("Information and critical", function() | 135 test("Information and critical", function() |
111 { | 136 { |
112 let information = fixConstructors({ | 137 let information = fixConstructors({ |
113 id: 1, | 138 id: 1, |
114 type: "information", | 139 type: "information", |
115 message: {"en-US": "Information"} | 140 message: {"en-US": "Information"} |
116 }); | 141 }); |
117 let critical = fixConstructors({ | 142 let critical = fixConstructors({ |
118 id: 2, | 143 id: 2, |
119 type: "critical", | 144 type: "critical", |
120 message: {"en-US": "Critical"} | 145 message: {"en-US": "Critical"} |
121 }); | 146 }); |
122 | 147 |
123 registerHandler([information, critical]); | 148 registerHandler([information, critical]); |
124 testRunner.runScheduledTasks(1); | 149 testRunner.runScheduledTasks(1); |
125 | 150 |
126 deepEqual(Notification.getNextToShow(), critical, "The critical notification
is given priority"); | 151 deepEqual(showNotifications(), [critical], "The critical notification is giv
en priority"); |
127 deepEqual(Notification.getNextToShow(), critical, "Critical notifications ca
n be shown multiple times"); | 152 deepEqual(showNotifications(), [critical], "Critical notifications can be sh
own multiple times"); |
128 }); | 153 }); |
129 | 154 |
130 test("No type", function() | 155 test("No type", function() |
131 { | 156 { |
132 let information = fixConstructors({ | 157 let information = fixConstructors({ |
133 id: 1, | 158 id: 1, |
134 message: {"en-US": "Information"} | 159 message: {"en-US": "Information"} |
135 }); | 160 }); |
136 | 161 |
137 registerHandler([information]); | 162 registerHandler([information]); |
138 testRunner.runScheduledTasks(1); | 163 testRunner.runScheduledTasks(1); |
139 | 164 |
140 deepEqual(Notification.getNextToShow(), information, "The notification is sh
own"); | 165 deepEqual(showNotifications(), [information], "The notification is shown"); |
141 equal(Notification.getNextToShow(), null, "Notification is treated as type i
nformation"); | 166 deepEqual(showNotifications(), [], "Notification is treated as type informat
ion"); |
142 }); | 167 }); |
143 | 168 |
144 test("Target selection", function() | 169 test("Target selection", function() |
145 { | 170 { |
146 let targets = [ | 171 let targets = [ |
147 ["extension", "adblockpluschrome", true], | 172 ["extension", "adblockpluschrome", true], |
148 ["extension", "adblockplus", false], | 173 ["extension", "adblockplus", false], |
149 ["extension", "adblockpluschrome2", false], | 174 ["extension", "adblockpluschrome2", false], |
150 ["extensionMinVersion", "1.4", true], | 175 ["extensionMinVersion", "1.4", true], |
151 ["extensionMinVersion", "1.4.1", true], | 176 ["extensionMinVersion", "1.4.1", true], |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 id: 1, | 212 id: 1, |
188 type: "information", | 213 type: "information", |
189 message: {"en-US": "Information"}, | 214 message: {"en-US": "Information"}, |
190 targets: [targetInfo] | 215 targets: [targetInfo] |
191 }); | 216 }); |
192 | 217 |
193 Prefs.notificationdata = {}; | 218 Prefs.notificationdata = {}; |
194 registerHandler([information]); | 219 registerHandler([information]); |
195 testRunner.runScheduledTasks(1); | 220 testRunner.runScheduledTasks(1); |
196 | 221 |
197 let expected = (result ? information : null); | 222 let expected = (result ? [information] : []); |
198 deepEqual(Notification.getNextToShow(), expected, "Selected notification f
or " + JSON.stringify(information.targets)); | 223 deepEqual(showNotifications(), expected, "Selected notification for " + JS
ON.stringify(information.targets)); |
199 deepEqual(Notification.getNextToShow(), null, "No notification on second c
all"); | 224 deepEqual(showNotifications(), [], "No notification on second call"); |
200 } | 225 } |
201 | 226 |
202 function pairs(array) | 227 function pairs(array) |
203 { | 228 { |
204 for (let element1 of array) | 229 for (let element1 of array) |
205 for (let element2 of array) | 230 for (let element2 of array) |
206 yield [element1, element2]; | 231 yield [element1, element2]; |
207 } | 232 } |
208 for (let [[propName1, value1, result1], [propName2, value2, result2]] in pai
rs(targets)) | 233 for (let [[propName1, value1, result1], [propName2, value2, result2]] in pai
rs(targets)) |
209 { | 234 { |
210 let targetInfo1 = {}; | 235 let targetInfo1 = {}; |
211 targetInfo1[propName1] = value1; | 236 targetInfo1[propName1] = value1; |
212 let targetInfo2 = {}; | 237 let targetInfo2 = {}; |
213 targetInfo2[propName2] = value2; | 238 targetInfo2[propName2] = value2; |
214 | 239 |
215 let information = fixConstructors({ | 240 let information = fixConstructors({ |
216 id: 1, | 241 id: 1, |
217 type: "information", | 242 type: "information", |
218 message: {"en-US": "Information"}, | 243 message: {"en-US": "Information"}, |
219 targets: [targetInfo1, targetInfo2] | 244 targets: [targetInfo1, targetInfo2] |
220 }); | 245 }); |
221 | 246 |
222 Prefs.notificationdata = {}; | 247 Prefs.notificationdata = {}; |
223 registerHandler([information]); | 248 registerHandler([information]); |
224 testRunner.runScheduledTasks(1); | 249 testRunner.runScheduledTasks(1); |
225 | 250 |
226 let expected = (result1 || result2 ? information : null) | 251 let expected = (result1 || result2 ? [information] : []) |
227 deepEqual(Notification.getNextToShow(), expected, "Selected notification f
or " + JSON.stringify(information.targets)); | 252 deepEqual(showNotifications(), expected, "Selected notification for " + JS
ON.stringify(information.targets)); |
228 deepEqual(Notification.getNextToShow(), null, "No notification on second c
all"); | 253 deepEqual(showNotifications(), [], "No notification on second call"); |
229 | 254 |
230 information = fixConstructors({ | 255 information = fixConstructors({ |
231 id: 1, | 256 id: 1, |
232 type: "information", | 257 type: "information", |
233 message: {"en-US": "Information"}, | 258 message: {"en-US": "Information"}, |
234 targets: [targetInfo1] | 259 targets: [targetInfo1] |
235 }); | 260 }); |
236 let critical = fixConstructors({ | 261 let critical = fixConstructors({ |
237 id: 2, | 262 id: 2, |
238 type: "critical", | 263 type: "critical", |
239 message: {"en-US": "Critical"}, | 264 message: {"en-US": "Critical"}, |
240 targets: [targetInfo2] | 265 targets: [targetInfo2] |
241 }); | 266 }); |
242 | 267 |
243 Prefs.notificationdata = {}; | 268 Prefs.notificationdata = {}; |
244 registerHandler([information, critical]); | 269 registerHandler([information, critical]); |
245 testRunner.runScheduledTasks(1); | 270 testRunner.runScheduledTasks(1); |
246 | 271 |
247 expected = (result2 ? critical : (result1 ? information : null)); | 272 expected = (result2 ? [critical] : (result1 ? [information] : [])); |
248 deepEqual(Notification.getNextToShow(), expected, "Selected notification f
or information with " + JSON.stringify(information.targets) + " and critical wit
h " + JSON.stringify(critical.targets)); | 273 deepEqual(showNotifications(), expected, "Selected notification for inform
ation with " + JSON.stringify(information.targets) + " and critical with " + JSO
N.stringify(critical.targets)); |
249 } | 274 } |
250 }); | 275 }); |
251 | 276 |
252 test("Parameters sent", function() | 277 test("Parameters sent", function() |
253 { | 278 { |
254 Prefs.notificationdata = { | 279 Prefs.notificationdata = { |
255 data: { | 280 data: { |
256 version: "3" | 281 version: "3" |
257 }, | 282 }, |
258 }; | 283 }; |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 }); | 389 }); |
365 | 390 |
366 registerHandler([ | 391 registerHandler([ |
367 withURLFilterFoo, | 392 withURLFilterFoo, |
368 withoutURLFilter, | 393 withoutURLFilter, |
369 withURLFilterBar, | 394 withURLFilterBar, |
370 subdomainURLFilter | 395 subdomainURLFilter |
371 ]); | 396 ]); |
372 testRunner.runScheduledTasks(1); | 397 testRunner.runScheduledTasks(1); |
373 | 398 |
374 deepEqual(Notification.getNextToShow(), withoutURLFilter, "URL-specific noti
fications are skipped"); | 399 deepEqual(showNotifications(), [withoutURLFilter], "URL-specific notificatio
ns are skipped"); |
375 deepEqual(Notification.getNextToShow("http://foo.com"), withURLFilterFoo, "U
RL-specific notification is retrieved"); | 400 deepEqual(showNotifications("http://foo.com"), [withURLFilterFoo], "URL-spec
ific notification is retrieved"); |
376 deepEqual(Notification.getNextToShow("http://foo.com"), null, "URL-specific
notification is not retrieved"); | 401 deepEqual(showNotifications("http://foo.com"), [], "URL-specific notificatio
n is not retrieved"); |
377 deepEqual(Notification.getNextToShow("http://www.example.com"), subdomainURL
Filter, "URL-specific notification matches subdomain"); | 402 deepEqual(showNotifications("http://www.example.com"), [subdomainURLFilter],
"URL-specific notification matches subdomain"); |
378 }); | 403 }); |
379 | 404 |
380 test("Global opt-out", function() | 405 test("Global opt-out", function() |
381 { | 406 { |
382 Notification.toggleIgnoreCategory("*", true); | 407 Notification.toggleIgnoreCategory("*", true); |
383 ok(Prefs.notifications_ignoredcategories.indexOf("*") != -1, "Force enable g
lobal opt-out"); | 408 ok(Prefs.notifications_ignoredcategories.indexOf("*") != -1, "Force enable g
lobal opt-out"); |
384 Notification.toggleIgnoreCategory("*", true); | 409 Notification.toggleIgnoreCategory("*", true); |
385 ok(Prefs.notifications_ignoredcategories.indexOf("*") != -1, "Force enable g
lobal opt-out (again)"); | 410 ok(Prefs.notifications_ignoredcategories.indexOf("*") != -1, "Force enable g
lobal opt-out (again)"); |
386 Notification.toggleIgnoreCategory("*", false); | 411 Notification.toggleIgnoreCategory("*", false); |
387 ok(Prefs.notifications_ignoredcategories.indexOf("*") == -1, "Force disable
global opt-out"); | 412 ok(Prefs.notifications_ignoredcategories.indexOf("*") == -1, "Force disable
global opt-out"); |
(...skipping 18 matching lines...) Expand all Loading... |
406 }); | 431 }); |
407 let critical = fixConstructors({ | 432 let critical = fixConstructors({ |
408 id: 2, | 433 id: 2, |
409 type: "critical" | 434 type: "critical" |
410 }); | 435 }); |
411 | 436 |
412 Notification.toggleIgnoreCategory("*", true); | 437 Notification.toggleIgnoreCategory("*", true); |
413 registerHandler([information]); | 438 registerHandler([information]); |
414 testRunner.runScheduledTasks(1); | 439 testRunner.runScheduledTasks(1); |
415 | 440 |
416 deepEqual(Notification.getNextToShow(), null, "Information notifications are
ignored after enabling global opt-out"); | 441 deepEqual(showNotifications(), [], "Information notifications are ignored af
ter enabling global opt-out"); |
417 Notification.toggleIgnoreCategory("*", false); | 442 Notification.toggleIgnoreCategory("*", false); |
418 deepEqual(Notification.getNextToShow(), information, "Information notificati
ons are shown after disabling global opt-out"); | 443 deepEqual(showNotifications(), [information], "Information notifications are
shown after disabling global opt-out"); |
419 | 444 |
420 Notification.toggleIgnoreCategory("*", true); | 445 Notification.toggleIgnoreCategory("*", true); |
421 Prefs.notificationdata = {}; | 446 Prefs.notificationdata = {}; |
422 registerHandler([critical]); | 447 registerHandler([critical]); |
423 testRunner.runScheduledTasks(1); | 448 testRunner.runScheduledTasks(1); |
424 | 449 |
425 deepEqual(Notification.getNextToShow(), critical, "Critical notifications ar
e not ignored"); | 450 deepEqual(showNotifications(), [critical], "Critical notifications are not i
gnored"); |
426 }); | 451 }); |
427 | 452 |
428 module("Notification localization"); | 453 module("Notification localization"); |
429 | 454 |
430 test("Message without localization", function() | 455 test("Message without localization", function() |
431 { | 456 { |
432 let notification = {message: "non-localized"}; | 457 let notification = {message: "non-localized"}; |
433 let texts = Notification.getLocalizedTexts(notification, "en-US"); | 458 let texts = Notification.getLocalizedTexts(notification, "en-US"); |
434 equal(texts.message, "non-localized"); | 459 equal(texts.message, "non-localized"); |
435 }); | 460 }); |
(...skipping 16 matching lines...) Expand all Loading... |
452 equal(texts.message, "fr"); | 477 equal(texts.message, "fr"); |
453 }); | 478 }); |
454 | 479 |
455 test("Missing translation", function() | 480 test("Missing translation", function() |
456 { | 481 { |
457 let notification = {message: {"en-US": "en-US"}}; | 482 let notification = {message: {"en-US": "en-US"}}; |
458 let texts = Notification.getLocalizedTexts(notification, "fr"); | 483 let texts = Notification.getLocalizedTexts(notification, "fr"); |
459 equal(texts.message, "en-US"); | 484 equal(texts.message, "en-US"); |
460 }); | 485 }); |
461 })(); | 486 })(); |
OLD | NEW |