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

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

Issue 11127037: Notifications: implemented better target checks - unit tests (Closed)
Patch Set: Created July 19, 2013, 2:42 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 | « .hgsubstate ('k') | 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 server = null; 4 let server = null;
5 let randomResult = 0.5; 5 let randomResult = 0.5;
6 6
7 let originalApplication; 7 let originalInfo;
8 let originalAddonVersion;
9 let info = require("info"); 8 let info = require("info");
10 9
11 module("Notification", 10 module("Notification",
12 { 11 {
13 setup: function() 12 setup: function()
14 { 13 {
15 testRunner = this; 14 testRunner = this;
16 15
17 preparePrefs.call(this); 16 preparePrefs.call(this);
18 setupVirtualTime.call(this, function(wrapTimer) 17 setupVirtualTime.call(this, function(wrapTimer)
19 { 18 {
20 let NotificationModule = getModuleGlobal("notification"); 19 let NotificationModule = getModuleGlobal("notification");
21 NotificationModule.downloader._timer = wrapTimer(NotificationModule.down loader._timer); 20 NotificationModule.downloader._timer = wrapTimer(NotificationModule.down loader._timer);
22 }, "notification", "downloader"); 21 }, "notification", "downloader");
23 22
24 server = new nsHttpServer(); 23 server = new nsHttpServer();
25 server.start(1234); 24 server.start(1234);
26 25
27 originalApplication = info.application; 26 originalInfo = {};
27 for (let key in info)
28 originalInfo[key] = info[key];
29
30 info.addonName = "adblockpluschrome";
31 info.addonVersion = "1.4.1";
28 info.application = "chrome"; 32 info.application = "chrome";
29 originalAddonVersion = info.addonVersion; 33 info.applicationVersion = "27.0";
30 info.addonVersion = "1.4.1"; 34 info.platform = "chromium";
35 info.platformVersion = "12.0";
31 36
32 Prefs.notificationurl = "http://127.0.0.1:1234/notification.json"; 37 Prefs.notificationurl = "http://127.0.0.1:1234/notification.json";
33 Prefs.notificationdata = {}; 38 Prefs.notificationdata = {};
34 39
35 // Replace Math.random() function 40 // Replace Math.random() function
36 let DownloaderGlobal = Cu.getGlobalForObject(getModuleGlobal("downloader") ); 41 let DownloaderGlobal = Cu.getGlobalForObject(getModuleGlobal("downloader") );
37 this._origRandom = DownloaderGlobal.Math.random; 42 this._origRandom = DownloaderGlobal.Math.random;
38 DownloaderGlobal.Math.random = function() randomResult; 43 DownloaderGlobal.Math.random = function() randomResult;
39 randomResult = 0.5; 44 randomResult = 0.5;
40 }, 45 },
41 46
42 teardown: function() 47 teardown: function()
43 { 48 {
44 restorePrefs.call(this); 49 restorePrefs.call(this);
45 restoreVirtualTime.call(this); 50 restoreVirtualTime.call(this);
46 51
47 stop(); 52 stop();
48 server.stop(function() 53 server.stop(function()
49 { 54 {
50 server = null; 55 server = null;
51 start(); 56 start();
52 }); 57 });
53 58
54 info.application = originalApplication; 59 for (let key in originalInfo)
55 info.addonVersion = originalAddonVersion; 60 info[key] = originalInfo[key];
56 61
57 if (this._origRandom) 62 if (this._origRandom)
58 { 63 {
59 let DownloaderGlobal = Cu.getGlobalForObject(getModuleGlobal("downloader ")); 64 let DownloaderGlobal = Cu.getGlobalForObject(getModuleGlobal("downloader "));
60 DownloaderGlobal.Math.random = this._origRandom; 65 DownloaderGlobal.Math.random = this._origRandom;
61 delete this._origRandom; 66 delete this._origRandom;
62 } 67 }
63 68
64 Notification.init(); 69 Notification.init();
65 } 70 }
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 message: {en: "Information"} 143 message: {en: "Information"}
139 }); 144 });
140 145
141 registerHandler([information]); 146 registerHandler([information]);
142 testRunner.runScheduledTasks(1); 147 testRunner.runScheduledTasks(1);
143 148
144 deepEqual(Notification.getNextToShow(), information, "The notification is sh own"); 149 deepEqual(Notification.getNextToShow(), information, "The notification is sh own");
145 equal(Notification.getNextToShow(), null, "Notification is treated as severi ty information"); 150 equal(Notification.getNextToShow(), null, "Notification is treated as severi ty information");
146 }); 151 });
147 152
148 test("Different platforms", function() 153 test("Target selection", function()
149 { 154 {
150 let information = fixConstructors({ 155 let targets = [
151 id: 1, 156 ["extension", "adblockpluschrome", true],
152 severity: "information", 157 ["extension", "adblockplus", false],
153 message: {en: "Information"}, 158 ["extension", "adblockpluschrome2", false],
154 platforms: ["chrome", "firefox"] 159 ["extensionMinVersion", "1.4", true],
155 }); 160 ["extensionMinVersion", "1.4.1", true],
156 let critical = fixConstructors({ 161 ["extensionMinVersion", "1.5", false],
157 id: 2, 162 ["extensionMaxVersion", "1.5", true],
158 severity: "critical", 163 ["extensionMaxVersion", "1.4.1", true],
159 message: {en: "Critical"}, 164 ["extensionMaxVersion", "1.4.*", true],
160 platforms: ["firefox"] 165 ["extensionMaxVersion", "1.4", false],
161 }); 166 ["application", "chrome", true],
167 ["application", "firefox", false],
168 ["applicationMinVersion", "27.0", true],
169 ["applicationMinVersion", "27", true],
170 ["applicationMinVersion", "26", true],
171 ["applicationMinVersion", "28", false],
172 ["applicationMinVersion", "27.1", false],
173 ["applicationMaxVersion", "27.0", true],
174 ["applicationMaxVersion", "27", true],
175 ["applicationMaxVersion", "28", true],
176 ["applicationMaxVersion", "26", false],
177 ["platform", "chromium", true],
178 ["platform", "gecko", false],
179 ["platformMinVersion", "12.0", true],
180 ["platformMinVersion", "12", true],
181 ["platformMinVersion", "11", true],
182 ["platformMinVersion", "13", false],
183 ["platformMinVersion", "12.1", false],
184 ["platformMaxVersion", "12.0", true],
185 ["platformMaxVersion", "12", true],
186 ["platformMaxVersion", "13", true],
187 ["platformMaxVersion", "11", false],
188 ];
162 189
163 registerHandler([information, critical]); 190 for each (let [propName, value, result] in targets)
164 testRunner.runScheduledTasks(1); 191 {
192 let targetInfo = {};
193 targetInfo[propName] = value;
165 194
166 deepEqual(Notification.getNextToShow(), information, "Critical notification is ignored if platform doesn't match"); 195 let information = fixConstructors({
167 deepEqual(Notification.getNextToShow(), null, "Critical notification still i gnored even if no other notifications available"); 196 id: 1,
168 }); 197 severity: "information",
198 message: {en: "Information"},
199 targets: [targetInfo]
200 });
169 201
170 test("Min version", function() 202 Prefs.notificationdata = {};
171 { 203 registerHandler([information]);
172 let information = fixConstructors({ 204 testRunner.runScheduledTasks(1);
173 id: 1,
174 severity: "information",
175 message: {en: "Information"},
176 minVersion: "1.4"
177 });
178 let critical = fixConstructors({
179 id: 2,
180 severity: "critical",
181 message: {en: "Critical"},
182 minVersion: "1.5"
183 });
184 205
185 registerHandler([information, critical]); 206 let expected = (result ? information : null);
186 testRunner.runScheduledTasks(1); 207 deepEqual(Notification.getNextToShow(), expected, "Selected notification f or " + JSON.stringify(information.targets));
208 deepEqual(Notification.getNextToShow(), null, "No notification on second c all");
209 }
187 210
188 deepEqual(Notification.getNextToShow(), information, "Critical notification is ignored if minVersion doesn't match"); 211 function allPairs(array)
189 deepEqual(Notification.getNextToShow(), null, "Critical notification still i gnored even if no other notifications available"); 212 {
190 }); 213 var pairs = [];
Felix Dahlke 2013/07/25 10:22:04 Why not use let here and below?
Wladimir Palant 2013/07/25 10:58:07 Because that's copy&paste from your code in Chrome
214 for (var i = 0; i < array.length - 1; i++)
215 for (var j = i + 1; j < array.length; j++)
216 pairs.push([array[i], array[j]]);
217 return pairs;
218 }
219 for each (let [[propName1, value1, result1], [propName2, value2, result2]] i n allPairs(targets))
220 {
221 let targetInfo1 = {};
222 targetInfo1[propName1] = value1;
223 let targetInfo2 = {};
224 targetInfo2[propName2] = value2;
191 225
192 test("Max version", function() 226 let information = fixConstructors({
193 { 227 id: 1,
194 let information = fixConstructors({ 228 severity: "information",
195 id: 1, 229 message: {en: "Information"},
196 severity: "information", 230 targets: [targetInfo1, targetInfo2]
197 message: {en: "Information"}, 231 });
198 maxVersion: "1.5"
199 });
200 let critical = fixConstructors({
201 id: 2,
202 severity: "critical",
203 message: {en: "Critical"},
204 maxVersion: "1.4"
205 });
206 232
207 registerHandler([information, critical]); 233 Prefs.notificationdata = {};
208 testRunner.runScheduledTasks(1); 234 registerHandler([information]);
235 testRunner.runScheduledTasks(1);
209 236
210 deepEqual(Notification.getNextToShow(), information, "Critical notification is ignored if maxVersion doesn't match"); 237 let expected = (result1 || result2 ? information : null)
211 deepEqual(Notification.getNextToShow(), null, "Critical notification still i gnored even if no other notifications available"); 238 deepEqual(Notification.getNextToShow(), expected, "Selected notification f or " + JSON.stringify(information.targets));
239 deepEqual(Notification.getNextToShow(), null, "No notification on second c all");
240
241 information = fixConstructors({
242 id: 1,
243 severity: "information",
244 message: {en: "Information"},
245 targets: [targetInfo1]
246 });
247 let critical = fixConstructors({
248 id: 2,
249 severity: "critical",
250 message: {en: "Critical"},
251 targets: [targetInfo2]
252 });
253
254 Prefs.notificationdata = {};
255 registerHandler([information, critical]);
256 testRunner.runScheduledTasks(1);
257
258 expected = (result2 ? critical : (result1 ? information : null));
259 deepEqual(Notification.getNextToShow(), expected, "Selected notification f or information with " + JSON.stringify(information.targets) + " and critical wit h " + JSON.stringify(critical.targets));
260 }
212 }); 261 });
213 })(); 262 })();
OLDNEW
« no previous file with comments | « .hgsubstate ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld