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

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

Issue 6336691434422272: Added unit tests for URL-specific notifications (Closed)
Patch Set: Created Feb. 19, 2014, 1:01 p.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 module("Notification handling", 9 module("Notification handling",
10 { 10 {
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 88
89 test("No data", function() 89 test("No data", function()
90 { 90 {
91 equal(Notification.getNextToShow(), null, "null should be returned if there is no data"); 91 equal(Notification.getNextToShow(), null, "null should be returned if there is no data");
92 }); 92 });
93 93
94 test("Single notification", function() 94 test("Single notification", function()
95 { 95 {
96 let information = fixConstructors({ 96 let information = fixConstructors({
97 id: 1, 97 id: 1,
98 severity: "information", 98 type: "information",
99 message: {"en-US": "Information"} 99 message: {"en-US": "Information"}
100 }); 100 });
101 101
102 registerHandler([information]); 102 registerHandler([information]);
103 testRunner.runScheduledTasks(1); 103 testRunner.runScheduledTasks(1);
104 104
105 deepEqual(Notification.getNextToShow(), information, "The notification is sh own"); 105 deepEqual(Notification.getNextToShow(), information, "The notification is sh own");
106 equal(Notification.getNextToShow(), null, "Informational notifications aren' t shown more than once"); 106 equal(Notification.getNextToShow(), null, "Informational notifications aren' t shown more than once");
107 }); 107 });
108 108
109 test("Information and critical", function() 109 test("Information and critical", function()
110 { 110 {
111 let information = fixConstructors({ 111 let information = fixConstructors({
112 id: 1, 112 id: 1,
113 severity: "information", 113 type: "information",
114 message: {"en-US": "Information"} 114 message: {"en-US": "Information"}
115 }); 115 });
116 let critical = fixConstructors({ 116 let critical = fixConstructors({
117 id: 2, 117 id: 2,
118 severity: "critical", 118 type: "critical",
119 message: {"en-US": "Critical"} 119 message: {"en-US": "Critical"}
120 }); 120 });
121 121
122 registerHandler([information, critical]); 122 registerHandler([information, critical]);
123 testRunner.runScheduledTasks(1); 123 testRunner.runScheduledTasks(1);
124 124
125 deepEqual(Notification.getNextToShow(), critical, "The critical notification is given priority"); 125 deepEqual(Notification.getNextToShow(), critical, "The critical notification is given priority");
126 deepEqual(Notification.getNextToShow(), critical, "Critical notifications ca n be shown multiple times"); 126 deepEqual(Notification.getNextToShow(), critical, "Critical notifications ca n be shown multiple times");
127 }); 127 });
128 128
129 test("No severity", function() 129 test("No type", function()
130 { 130 {
131 let information = fixConstructors({ 131 let information = fixConstructors({
132 id: 1, 132 id: 1,
133 message: {"en-US": "Information"} 133 message: {"en-US": "Information"}
134 }); 134 });
135 135
136 registerHandler([information]); 136 registerHandler([information]);
137 testRunner.runScheduledTasks(1); 137 testRunner.runScheduledTasks(1);
138 138
139 deepEqual(Notification.getNextToShow(), information, "The notification is sh own"); 139 deepEqual(Notification.getNextToShow(), information, "The notification is sh own");
140 equal(Notification.getNextToShow(), null, "Notification is treated as severi ty information"); 140 equal(Notification.getNextToShow(), null, "Notification is treated as type i nformation");
141 }); 141 });
142 142
143 test("Target selection", function() 143 test("Target selection", function()
144 { 144 {
145 let targets = [ 145 let targets = [
146 ["extension", "adblockpluschrome", true], 146 ["extension", "adblockpluschrome", true],
147 ["extension", "adblockplus", false], 147 ["extension", "adblockplus", false],
148 ["extension", "adblockpluschrome2", false], 148 ["extension", "adblockpluschrome2", false],
149 ["extensionMinVersion", "1.4", true], 149 ["extensionMinVersion", "1.4", true],
150 ["extensionMinVersion", "1.4.1", true], 150 ["extensionMinVersion", "1.4.1", true],
(...skipping 26 matching lines...) Expand all
177 ["platformMaxVersion", "11", false], 177 ["platformMaxVersion", "11", false],
178 ]; 178 ];
179 179
180 for each (let [propName, value, result] in targets) 180 for each (let [propName, value, result] in targets)
181 { 181 {
182 let targetInfo = {}; 182 let targetInfo = {};
183 targetInfo[propName] = value; 183 targetInfo[propName] = value;
184 184
185 let information = fixConstructors({ 185 let information = fixConstructors({
186 id: 1, 186 id: 1,
187 severity: "information", 187 type: "information",
188 message: {"en-US": "Information"}, 188 message: {"en-US": "Information"},
189 targets: [targetInfo] 189 targets: [targetInfo]
190 }); 190 });
191 191
192 Prefs.notificationdata = {}; 192 Prefs.notificationdata = {};
193 registerHandler([information]); 193 registerHandler([information]);
194 testRunner.runScheduledTasks(1); 194 testRunner.runScheduledTasks(1);
195 195
196 let expected = (result ? information : null); 196 let expected = (result ? information : null);
197 deepEqual(Notification.getNextToShow(), expected, "Selected notification f or " + JSON.stringify(information.targets)); 197 deepEqual(Notification.getNextToShow(), expected, "Selected notification f or " + JSON.stringify(information.targets));
198 deepEqual(Notification.getNextToShow(), null, "No notification on second c all"); 198 deepEqual(Notification.getNextToShow(), null, "No notification on second c all");
199 } 199 }
200 200
201 function pairs(array) 201 function pairs(array)
202 { 202 {
203 for each (let element1 in array) 203 for each (let element1 in array)
204 for each (let element2 in array) 204 for each (let element2 in array)
205 yield [element1, element2]; 205 yield [element1, element2];
206 } 206 }
207 for (let [[propName1, value1, result1], [propName2, value2, result2]] in pai rs(targets)) 207 for (let [[propName1, value1, result1], [propName2, value2, result2]] in pai rs(targets))
208 { 208 {
209 let targetInfo1 = {}; 209 let targetInfo1 = {};
210 targetInfo1[propName1] = value1; 210 targetInfo1[propName1] = value1;
211 let targetInfo2 = {}; 211 let targetInfo2 = {};
212 targetInfo2[propName2] = value2; 212 targetInfo2[propName2] = value2;
213 213
214 let information = fixConstructors({ 214 let information = fixConstructors({
215 id: 1, 215 id: 1,
216 severity: "information", 216 type: "information",
217 message: {"en-US": "Information"}, 217 message: {"en-US": "Information"},
218 targets: [targetInfo1, targetInfo2] 218 targets: [targetInfo1, targetInfo2]
219 }); 219 });
220 220
221 Prefs.notificationdata = {}; 221 Prefs.notificationdata = {};
222 registerHandler([information]); 222 registerHandler([information]);
223 testRunner.runScheduledTasks(1); 223 testRunner.runScheduledTasks(1);
224 224
225 let expected = (result1 || result2 ? information : null) 225 let expected = (result1 || result2 ? information : null)
226 deepEqual(Notification.getNextToShow(), expected, "Selected notification f or " + JSON.stringify(information.targets)); 226 deepEqual(Notification.getNextToShow(), expected, "Selected notification f or " + JSON.stringify(information.targets));
227 deepEqual(Notification.getNextToShow(), null, "No notification on second c all"); 227 deepEqual(Notification.getNextToShow(), null, "No notification on second c all");
228 228
229 information = fixConstructors({ 229 information = fixConstructors({
230 id: 1, 230 id: 1,
231 severity: "information", 231 type: "information",
232 message: {"en-US": "Information"}, 232 message: {"en-US": "Information"},
233 targets: [targetInfo1] 233 targets: [targetInfo1]
234 }); 234 });
235 let critical = fixConstructors({ 235 let critical = fixConstructors({
236 id: 2, 236 id: 2,
237 severity: "critical", 237 type: "critical",
238 message: {"en-US": "Critical"}, 238 message: {"en-US": "Critical"},
239 targets: [targetInfo2] 239 targets: [targetInfo2]
240 }); 240 });
241 241
242 Prefs.notificationdata = {}; 242 Prefs.notificationdata = {};
243 registerHandler([information, critical]); 243 registerHandler([information, critical]);
244 testRunner.runScheduledTasks(1); 244 testRunner.runScheduledTasks(1);
245 245
246 expected = (result2 ? critical : (result1 ? information : null)); 246 expected = (result2 ? critical : (result1 ? information : null));
247 deepEqual(Notification.getNextToShow(), expected, "Selected notification f or information with " + JSON.stringify(information.targets) + " and critical wit h " + JSON.stringify(critical.targets)); 247 deepEqual(Notification.getNextToShow(), expected, "Selected notification f or information with " + JSON.stringify(information.targets) + " and critical wit h " + JSON.stringify(critical.targets));
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 randomResult = test.randomResult; 310 randomResult = test.randomResult;
311 311
312 let maxHours = Math.round(Math.max.apply(null, test.requests)) + 1; 312 let maxHours = Math.round(Math.max.apply(null, test.requests)) + 1;
313 testRunner.runScheduledTasks(maxHours, test.skipAfter, test.skip); 313 testRunner.runScheduledTasks(maxHours, test.skipAfter, test.skip);
314 314
315 let skipAddendum = (typeof test.skip != "number" ? "" : " skipping " + tes t.skip + " hours after " + test.skipAfter + " hours"); 315 let skipAddendum = (typeof test.skip != "number" ? "" : " skipping " + tes t.skip + " hours after " + test.skipAfter + " hours");
316 deepEqual(requests, test.requests, "Requests with Math.random() returning " + randomResult + skipAddendum); 316 deepEqual(requests, test.requests, "Requests with Math.random() returning " + randomResult + skipAddendum);
317 } 317 }
318 }); 318 });
319 319
320 test("Uses severity instead of type", 3, function()
321 {
322 let severityNotification = {
323 id: 1,
324 severity: "information",
325 message: {"en-US": "Information"}
326 };
327
328 function listener(name)
329 {
330 if (name !== "notificationdata")
331 return;
332
333 Prefs.removeListener(listener);
334 let notification = Prefs.notificationdata.data.notifications[0];
335 ok(!("severity" in notification), "Severity property was removed");
336 ok("type" in notification, "Type property was added");
337 equal(notification.type, severityNotification.severity, "Type property has correct value");
338 }
339 Prefs.addListener(listener);
340
341 let responseText = JSON.stringify({
342 notifications: [severityNotification]
343 });
344 Notification._onDownloadSuccess(null, responseText, function() {}, function( ) {});
345 });
346
347 test("URL-specific notification", function()
348 {
349 let withURLFilterFoo = fixConstructors({
350 id: 1,
351 urlFilters: ["foo.com"]
352 });
353 let withoutURLFilter = fixConstructors({
354 id: 2
355 });
356 let withURLFilterBar = fixConstructors({
357 id: 3,
358 urlFilters: ["bar.com"]
359 });
360 let subdomainURLFilter = fixConstructors({
361 id: 4,
362 urlFilters: ["||example.com"]
363 });
364
365 registerHandler([
366 withURLFilterFoo,
367 withoutURLFilter,
368 withURLFilterBar,
369 subdomainURLFilter
370 ]);
371 testRunner.runScheduledTasks(1);
372
373 deepEqual(Notification.getNextToShow(), withoutURLFilter, "URL-specific noti fications are skipped");
374 deepEqual(Notification.getNextToShow("http://foo.com"), withURLFilterFoo, "U RL-specific notification is retrieved");
375 deepEqual(Notification.getNextToShow("http://foo.com"), null, "URL-specific notification is not retrieved");
376 deepEqual(Notification.getNextToShow("http://www.example.com"), subdomainURL Filter, "URL-specific notification matches subdomain");
377 });
378
320 module("Notification localization"); 379 module("Notification localization");
321 380
381 test("Message without localization", function()
382 {
383 let notification = {message: "non-localized"};
384 let texts = Notification.getLocalizedTexts(notification, "en-US");
385 equal(texts.message, "non-localized");
386 });
387
322 test("Language only", function() 388 test("Language only", function()
323 { 389 {
324 let notification = {message: {fr: "fr"}}; 390 let notification = {message: {fr: "fr"}};
325 let texts = Notification.getLocalizedTexts(notification, "fr"); 391 let texts = Notification.getLocalizedTexts(notification, "fr");
326 equal(texts.message, "fr"); 392 equal(texts.message, "fr");
327 texts = Notification.getLocalizedTexts(notification, "fr-CA"); 393 texts = Notification.getLocalizedTexts(notification, "fr-CA");
328 equal(texts.message, "fr"); 394 equal(texts.message, "fr");
329 }); 395 });
330 396
331 test("Language and country", function() 397 test("Language and country", function()
332 { 398 {
333 let notification = {message: {fr: "fr", "fr-CA": "fr-CA"}}; 399 let notification = {message: {fr: "fr", "fr-CA": "fr-CA"}};
334 let texts = Notification.getLocalizedTexts(notification, "fr-CA"); 400 let texts = Notification.getLocalizedTexts(notification, "fr-CA");
335 equal(texts.message, "fr-CA"); 401 equal(texts.message, "fr-CA");
336 texts = Notification.getLocalizedTexts(notification, "fr"); 402 texts = Notification.getLocalizedTexts(notification, "fr");
337 equal(texts.message, "fr"); 403 equal(texts.message, "fr");
338 }); 404 });
339 405
340 test("Missing translation", function() 406 test("Missing translation", function()
341 { 407 {
342 let notification = {message: {"en-US": "en-US"}}; 408 let notification = {message: {"en-US": "en-US"}};
343 let texts = Notification.getLocalizedTexts(notification, "fr"); 409 let texts = Notification.getLocalizedTexts(notification, "fr");
344 equal(texts.message, "en-US"); 410 equal(texts.message, "en-US");
345 }); 411 });
346 })(); 412 })();
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