Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: chrome/content/tests/notification.js

Issue 5499340072157184: Issue 2420 - Update notification tests (Closed)
Patch Set: Created June 8, 2015, 11:37 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 }
16 Notification.addShowListener(showListener);
17 Notification.showNext(url);
18 Notification.removeShowListener(showListener);
19 shownNotifications.forEach(function(notification)
20 {
21 Notification.markAsShown(notification.id);
Wladimir Palant 2015/06/08 11:45:21 Shouldn't this be called in showListener above?
Felix Dahlke 2015/06/08 19:36:53 Done.
22 });
23 return shownNotifications;
24 }
25
9 module("Notification handling", 26 module("Notification handling",
10 { 27 {
11 setup: function() 28 setup: function()
12 { 29 {
13 testRunner = this; 30 testRunner = this;
14 31
15 preparePrefs.call(this); 32 preparePrefs.call(this);
16 setupVirtualTime.call(this, function(wrapTimer) 33 setupVirtualTime.call(this, function(wrapTimer)
17 { 34 {
18 let NotificationModule = getModuleGlobal("notification"); 35 let NotificationModule = getModuleGlobal("notification");
(...skipping 14 matching lines...) Expand all
33 50
34 Prefs.notificationurl = "http://example.com/notification.json"; 51 Prefs.notificationurl = "http://example.com/notification.json";
35 Prefs.notificationdata = {}; 52 Prefs.notificationdata = {};
36 Prefs.notifications_ignoredcategories = []; 53 Prefs.notifications_ignoredcategories = [];
37 54
38 // Replace Math.random() function 55 // Replace Math.random() function
39 let DownloaderGlobal = Cu.getGlobalForObject(getModuleGlobal("downloader") ); 56 let DownloaderGlobal = Cu.getGlobalForObject(getModuleGlobal("downloader") );
40 this._origRandom = DownloaderGlobal.Math.random; 57 this._origRandom = DownloaderGlobal.Math.random;
41 DownloaderGlobal.Math.random = () => randomResult; 58 DownloaderGlobal.Math.random = () => randomResult;
42 randomResult = 0.5; 59 randomResult = 0.5;
60
61 Notification.removeAllShowListeners();
Wladimir Palant 2015/06/08 11:45:21 Better save the old listeners and restore them in
Felix Dahlke 2015/06/08 19:36:53 Done.
43 }, 62 },
44 63
45 teardown: function() 64 teardown: function()
46 { 65 {
47 restorePrefs.call(this); 66 restorePrefs.call(this);
48 restoreVirtualTime.call(this); 67 restoreVirtualTime.call(this);
49 restoreVirtualXMLHttp.call(this); 68 restoreVirtualXMLHttp.call(this);
50 69
51 for (let key in originalInfo) 70 for (let key in originalInfo)
52 info[key] = originalInfo[key]; 71 info[key] = originalInfo[key];
(...skipping 29 matching lines...) Expand all
82 { 101 {
83 // deepEqual() expects that the constructors used in expected objects and 102 // 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 103 // 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. 104 // have to construct our objects in the context of the notification module.
86 let JSON = Cu.getGlobalForObject(Notification).JSON; 105 let JSON = Cu.getGlobalForObject(Notification).JSON;
87 return JSON.parse(JSON.stringify(object)); 106 return JSON.parse(JSON.stringify(object));
88 } 107 }
89 108
90 test("No data", function() 109 test("No data", function()
91 { 110 {
92 equal(Notification.getNextToShow(), null, "null should be returned if there is no data"); 111 deepEqual(showNotifications(), [], "No notifications should be returned if t here is no data");
93 }); 112 });
94 113
95 test("Single notification", function() 114 test("Single notification", function()
96 { 115 {
97 let information = fixConstructors({ 116 let information = fixConstructors({
98 id: 1, 117 id: 1,
99 type: "information", 118 type: "information",
100 message: {"en-US": "Information"} 119 message: {"en-US": "Information"}
101 }); 120 });
102 121
103 registerHandler([information]); 122 registerHandler([information]);
104 testRunner.runScheduledTasks(1); 123 testRunner.runScheduledTasks(1);
105 124
106 deepEqual(Notification.getNextToShow(), information, "The notification is sh own"); 125 deepEqual(showNotifications(), [information], "The notification is shown");
107 equal(Notification.getNextToShow(), null, "Informational notifications aren' t shown more than once"); 126 deepEqual(showNotifications(), [], "Informational notifications aren't shown more than once");
108 }); 127 });
109 128
110 test("Information and critical", function() 129 test("Information and critical", function()
111 { 130 {
112 let information = fixConstructors({ 131 let information = fixConstructors({
113 id: 1, 132 id: 1,
114 type: "information", 133 type: "information",
115 message: {"en-US": "Information"} 134 message: {"en-US": "Information"}
116 }); 135 });
117 let critical = fixConstructors({ 136 let critical = fixConstructors({
118 id: 2, 137 id: 2,
119 type: "critical", 138 type: "critical",
120 message: {"en-US": "Critical"} 139 message: {"en-US": "Critical"}
121 }); 140 });
122 141
123 registerHandler([information, critical]); 142 registerHandler([information, critical]);
124 testRunner.runScheduledTasks(1); 143 testRunner.runScheduledTasks(1);
125 144
126 deepEqual(Notification.getNextToShow(), critical, "The critical notification is given priority"); 145 deepEqual(showNotifications(), [critical], "The critical notification is giv en priority");
127 deepEqual(Notification.getNextToShow(), critical, "Critical notifications ca n be shown multiple times"); 146 deepEqual(showNotifications(), [critical], "Critical notifications can be sh own multiple times");
128 }); 147 });
129 148
130 test("No type", function() 149 test("No type", function()
131 { 150 {
132 let information = fixConstructors({ 151 let information = fixConstructors({
133 id: 1, 152 id: 1,
134 message: {"en-US": "Information"} 153 message: {"en-US": "Information"}
135 }); 154 });
136 155
137 registerHandler([information]); 156 registerHandler([information]);
138 testRunner.runScheduledTasks(1); 157 testRunner.runScheduledTasks(1);
139 158
140 deepEqual(Notification.getNextToShow(), information, "The notification is sh own"); 159 deepEqual(showNotifications(), [information], "The notification is shown");
141 equal(Notification.getNextToShow(), null, "Notification is treated as type i nformation"); 160 deepEqual(showNotifications(), [], "Notification is treated as type informat ion");
142 }); 161 });
143 162
144 test("Target selection", function() 163 test("Target selection", function()
145 { 164 {
146 let targets = [ 165 let targets = [
147 ["extension", "adblockpluschrome", true], 166 ["extension", "adblockpluschrome", true],
148 ["extension", "adblockplus", false], 167 ["extension", "adblockplus", false],
149 ["extension", "adblockpluschrome2", false], 168 ["extension", "adblockpluschrome2", false],
150 ["extensionMinVersion", "1.4", true], 169 ["extensionMinVersion", "1.4", true],
151 ["extensionMinVersion", "1.4.1", true], 170 ["extensionMinVersion", "1.4.1", true],
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 id: 1, 206 id: 1,
188 type: "information", 207 type: "information",
189 message: {"en-US": "Information"}, 208 message: {"en-US": "Information"},
190 targets: [targetInfo] 209 targets: [targetInfo]
191 }); 210 });
192 211
193 Prefs.notificationdata = {}; 212 Prefs.notificationdata = {};
194 registerHandler([information]); 213 registerHandler([information]);
195 testRunner.runScheduledTasks(1); 214 testRunner.runScheduledTasks(1);
196 215
197 let expected = (result ? information : null); 216 let expected = (result ? [information] : []);
198 deepEqual(Notification.getNextToShow(), expected, "Selected notification f or " + JSON.stringify(information.targets)); 217 deepEqual(showNotifications(), expected, "Selected notification for " + JS ON.stringify(information.targets));
199 deepEqual(Notification.getNextToShow(), null, "No notification on second c all"); 218 deepEqual(showNotifications(), [], "No notification on second call");
200 } 219 }
201 220
202 function pairs(array) 221 function pairs(array)
203 { 222 {
204 for (let element1 of array) 223 for (let element1 of array)
205 for (let element2 of array) 224 for (let element2 of array)
206 yield [element1, element2]; 225 yield [element1, element2];
207 } 226 }
208 for (let [[propName1, value1, result1], [propName2, value2, result2]] in pai rs(targets)) 227 for (let [[propName1, value1, result1], [propName2, value2, result2]] in pai rs(targets))
209 { 228 {
210 let targetInfo1 = {}; 229 let targetInfo1 = {};
211 targetInfo1[propName1] = value1; 230 targetInfo1[propName1] = value1;
212 let targetInfo2 = {}; 231 let targetInfo2 = {};
213 targetInfo2[propName2] = value2; 232 targetInfo2[propName2] = value2;
214 233
215 let information = fixConstructors({ 234 let information = fixConstructors({
216 id: 1, 235 id: 1,
217 type: "information", 236 type: "information",
218 message: {"en-US": "Information"}, 237 message: {"en-US": "Information"},
219 targets: [targetInfo1, targetInfo2] 238 targets: [targetInfo1, targetInfo2]
220 }); 239 });
221 240
222 Prefs.notificationdata = {}; 241 Prefs.notificationdata = {};
223 registerHandler([information]); 242 registerHandler([information]);
224 testRunner.runScheduledTasks(1); 243 testRunner.runScheduledTasks(1);
225 244
226 let expected = (result1 || result2 ? information : null) 245 let expected = (result1 || result2 ? [information] : [])
227 deepEqual(Notification.getNextToShow(), expected, "Selected notification f or " + JSON.stringify(information.targets)); 246 deepEqual(showNotifications(), expected, "Selected notification for " + JS ON.stringify(information.targets));
228 deepEqual(Notification.getNextToShow(), null, "No notification on second c all"); 247 deepEqual(showNotifications(), [], "No notification on second call");
229 248
230 information = fixConstructors({ 249 information = fixConstructors({
231 id: 1, 250 id: 1,
232 type: "information", 251 type: "information",
233 message: {"en-US": "Information"}, 252 message: {"en-US": "Information"},
234 targets: [targetInfo1] 253 targets: [targetInfo1]
235 }); 254 });
236 let critical = fixConstructors({ 255 let critical = fixConstructors({
237 id: 2, 256 id: 2,
238 type: "critical", 257 type: "critical",
239 message: {"en-US": "Critical"}, 258 message: {"en-US": "Critical"},
240 targets: [targetInfo2] 259 targets: [targetInfo2]
241 }); 260 });
242 261
243 Prefs.notificationdata = {}; 262 Prefs.notificationdata = {};
244 registerHandler([information, critical]); 263 registerHandler([information, critical]);
245 testRunner.runScheduledTasks(1); 264 testRunner.runScheduledTasks(1);
246 265
247 expected = (result2 ? critical : (result1 ? information : null)); 266 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)); 267 deepEqual(showNotifications(), expected, "Selected notification for inform ation with " + JSON.stringify(information.targets) + " and critical with " + JSO N.stringify(critical.targets));
249 } 268 }
250 }); 269 });
251 270
252 test("Parameters sent", function() 271 test("Parameters sent", function()
253 { 272 {
254 Prefs.notificationdata = { 273 Prefs.notificationdata = {
255 data: { 274 data: {
256 version: "3" 275 version: "3"
257 }, 276 },
258 }; 277 };
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 }); 383 });
365 384
366 registerHandler([ 385 registerHandler([
367 withURLFilterFoo, 386 withURLFilterFoo,
368 withoutURLFilter, 387 withoutURLFilter,
369 withURLFilterBar, 388 withURLFilterBar,
370 subdomainURLFilter 389 subdomainURLFilter
371 ]); 390 ]);
372 testRunner.runScheduledTasks(1); 391 testRunner.runScheduledTasks(1);
373 392
374 deepEqual(Notification.getNextToShow(), withoutURLFilter, "URL-specific noti fications are skipped"); 393 deepEqual(showNotifications(), [withoutURLFilter], "URL-specific notificatio ns are skipped");
375 deepEqual(Notification.getNextToShow("http://foo.com"), withURLFilterFoo, "U RL-specific notification is retrieved"); 394 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"); 395 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"); 396 deepEqual(showNotifications("http://www.example.com"), [subdomainURLFilter], "URL-specific notification matches subdomain");
378 }); 397 });
379 398
380 test("Global opt-out", function() 399 test("Global opt-out", function()
381 { 400 {
382 Notification.toggleIgnoreCategory("*", true); 401 Notification.toggleIgnoreCategory("*", true);
383 ok(Prefs.notifications_ignoredcategories.indexOf("*") != -1, "Force enable g lobal opt-out"); 402 ok(Prefs.notifications_ignoredcategories.indexOf("*") != -1, "Force enable g lobal opt-out");
384 Notification.toggleIgnoreCategory("*", true); 403 Notification.toggleIgnoreCategory("*", true);
385 ok(Prefs.notifications_ignoredcategories.indexOf("*") != -1, "Force enable g lobal opt-out (again)"); 404 ok(Prefs.notifications_ignoredcategories.indexOf("*") != -1, "Force enable g lobal opt-out (again)");
386 Notification.toggleIgnoreCategory("*", false); 405 Notification.toggleIgnoreCategory("*", false);
387 ok(Prefs.notifications_ignoredcategories.indexOf("*") == -1, "Force disable global opt-out"); 406 ok(Prefs.notifications_ignoredcategories.indexOf("*") == -1, "Force disable global opt-out");
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
452 equal(texts.message, "fr"); 471 equal(texts.message, "fr");
453 }); 472 });
454 473
455 test("Missing translation", function() 474 test("Missing translation", function()
456 { 475 {
457 let notification = {message: {"en-US": "en-US"}}; 476 let notification = {message: {"en-US": "en-US"}};
458 let texts = Notification.getLocalizedTexts(notification, "fr"); 477 let texts = Notification.getLocalizedTexts(notification, "fr");
459 equal(texts.message, "en-US"); 478 equal(texts.message, "en-US");
460 }); 479 });
461 })(); 480 })();
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld