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

Side by Side Diff: chrome/content/common.js

Issue 11144022: Add tests for the Notification module (Closed)
Patch Set: Test downloading (changes by Wladimir) Created July 18, 2013, 5:58 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
OLDNEW
1 const Cc = Components.classes; 1 const Cc = Components.classes;
2 const Ci = Components.interfaces; 2 const Ci = Components.interfaces;
3 const Cr = Components.results; 3 const Cr = Components.results;
4 const Cu = Components.utils; 4 const Cu = Components.utils;
5 5
6 const MILLIS_IN_SECOND = 1000;
7 const MILLIS_IN_MINUTE = 60 * MILLIS_IN_SECOND;
8 const MILLIS_IN_HOUR = 60 * MILLIS_IN_MINUTE;
9 const MILLIS_IN_DAY = 24 * MILLIS_IN_HOUR;
10
6 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 11 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
7 Cu.import("resource://gre/modules/Services.jsm"); 12 Cu.import("resource://gre/modules/Services.jsm");
8 13
9 function require(module) 14 function require(module)
10 { 15 {
11 let result = {}; 16 let result = {};
12 result.wrappedJSObject = result; 17 result.wrappedJSObject = result;
13 Services.obs.notifyObservers(result, "adblockplus-require", module); 18 Services.obs.notifyObservers(result, "adblockplus-require", module);
14 return result.exports; 19 return result.exports;
15 } 20 }
(...skipping 17 matching lines...) Expand all
33 38
34 let {Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter, 39 let {Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter,
35 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter, ElemHideExce ption} = require("filterClasses"); 40 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter, ElemHideExce ption} = require("filterClasses");
36 let {Subscription, SpecialSubscription, RegularSubscription, 41 let {Subscription, SpecialSubscription, RegularSubscription,
37 ExternalSubscription, DownloadableSubscription} = require("subscriptionClas ses"); 42 ExternalSubscription, DownloadableSubscription} = require("subscriptionClas ses");
38 let {defaultMatcher, Matcher, CombinedMatcher} = require("matcher"); 43 let {defaultMatcher, Matcher, CombinedMatcher} = require("matcher");
39 let {FilterListener} = require("filterListener"); 44 let {FilterListener} = require("filterListener");
40 let {FilterNotifier} = require("filterNotifier"); 45 let {FilterNotifier} = require("filterNotifier");
41 let {FilterStorage} = require("filterStorage"); 46 let {FilterStorage} = require("filterStorage");
42 let {ElemHide} = require("elemHide"); 47 let {ElemHide} = require("elemHide");
48 let {Notification} = require("notification");
43 let {Prefs} = require("prefs"); 49 let {Prefs} = require("prefs");
44 let {RequestNotifier} = require("requestNotifier"); 50 let {RequestNotifier} = require("requestNotifier");
45 let {Synchronizer} = require("synchronizer"); 51 let {Synchronizer} = require("synchronizer");
46 let {UI} = require("ui"); 52 let {UI} = require("ui");
47 let {Utils} = require("utils"); 53 let {Utils} = require("utils");
48 54
49 let geckoVersion = Services.appinfo.platformVersion; 55 let geckoVersion = Services.appinfo.platformVersion;
50 function compareGeckoVersion(version) 56 function compareGeckoVersion(version)
51 { 57 {
52 return Services.vc.compare(geckoVersion, version); 58 return Services.vc.compare(geckoVersion, version);
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 } 152 }
147 Prefs.enabled = true; 153 Prefs.enabled = true;
148 } 154 }
149 155
150 function restorePrefs() 156 function restorePrefs()
151 { 157 {
152 for (let pref in this._pbackup) 158 for (let pref in this._pbackup)
153 Prefs[pref] = this._pbackup[pref]; 159 Prefs[pref] = this._pbackup[pref];
154 } 160 }
155 161
162 function setupVirtualTime(processTimers)
163 {
164 let currentTime = 100000 * MILLIS_IN_HOUR;
165 let startTime = currentTime;
166 let scheduledTasks = [];
167
168 let modules = Array.prototype.slice.call(arguments, 1);
169 this._virtualTimeModules = modules;
170
171 for each (let module in this._virtualTimeModules)
172 {
173 let global = Cu.getGlobalForObject(getModuleGlobal(module));
174
175 // Replace Date.now() function
176 this["_origNow" + module] = global.Date.now;
177 global.Date.now = function() currentTime;
178 }
179
180 // Wrap timers
181 if (processTimers)
182 {
183 processTimers(function wrapTimer(timer)
184 {
185 let wrapper = {__proto__: timer};
186 let callback = timer.callback;
187 wrapper.handler = function() callback.notify(wrapper);
188 wrapper.nextExecution = currentTime + timer.delay;
189 scheduledTasks.push(wrapper);
190 timer.cancel();
191 return wrapper;
192 });
193 }
194
195 // Register observer to track outstanding requests
196 this._outstandingRequests = 0;
197 this.observe = function(subject, topic, data)
198 {
199 let orig = this._outstandingRequests;
200 if (topic == "http-on-modify-request")
201 this._outstandingRequests++;
202 else if (topic == "http-on-examine-response")
203 this._outstandingRequests--;
204 };
205 this.QueryInterface = XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWea kReference]);
206 Services.obs.addObserver(this, "http-on-modify-request", true);
207 Services.obs.addObserver(this, "http-on-examine-response", true);
208
209 this.runScheduledTasks = function(maxHours, initial, skip)
210 {
211 if (typeof maxHours != "number")
212 throw new Error("Numerical parameter expected");
213 if (typeof initial != "number")
214 initial = 0;
215 if (typeof skip != "number")
216 skip = 0;
217
218 startTime = currentTime;
219 if (initial >= 0)
220 {
221 this._runScheduledTasks(initial);
222 maxHours -= initial;
223 }
224 if (skip)
225 {
226 this._skipTasks(skip);
227 maxHours -= skip;
228 }
229 this._runScheduledTasks(maxHours);
230 }
231
232 this._runScheduledTasks = function(maxHours)
233 {
234 let endTime = currentTime + maxHours * MILLIS_IN_HOUR;
235 while (true)
236 {
237 let nextTask = null;
238 for each (let task in scheduledTasks)
239 {
240 if (!nextTask || nextTask.nextExecution > task.nextExecution)
241 nextTask = task;
242 }
243 if (!nextTask || nextTask.nextExecution > endTime)
244 break;
245
246 currentTime = nextTask.nextExecution;
247 nextTask.handler();
248
249 // Let all asynchronous actions finish
250 let thread = Services.tm.currentThread;
251 let loopStartTime = Date.now();
252
253 while (this._outstandingRequests > 0 || thread.hasPendingEvents())
254 {
255 thread.processNextEvent(true);
256
257 if (Date.now() - loopStartTime > 5000)
258 throw new Error("Test stuck in a download loop");
259 }
260
261 if (nextTask.type == Components.interfaces.nsITimer.TYPE_ONE_SHOT)
262 scheduledTasks = scheduledTasks.filter(function(task) task != nextTask);
263 else
264 nextTask.nextExecution = currentTime + nextTask.delay;
265 }
266
267 currentTime = endTime;
268 }
269
270 this._skipTasks = function(hours)
271 {
272 let newTasks = [];
273 currentTime += hours * MILLIS_IN_HOUR;
274 for each (let task in scheduledTasks)
275 {
276 if (task.nextExecution >= currentTime)
277 newTasks.push(task);
278 else if (task.type != Components.interfaces.nsITimer.TYPE_ONE_SHOT)
279 {
280 task.nextExecution = currentTime;
281 newTasks.push(task);
282 }
283 }
284 scheduledTasks = newTasks;
285 }
286
287 this.getTimeOffset = function() (currentTime - startTime) / MILLIS_IN_HOUR;
288
289 this.__defineGetter__("currentTime", function() currentTime);
290 }
291
292 function restoreVirtualTime()
293 {
294 for each (let module in this._virtualTimeModules)
295 {
296 let global = Cu.getGlobalForObject(getModuleGlobal(module));
297
298 // Restore Date.now() function
299 if ("_origNow" + module in this)
300 {
301 global.Date.now = this["_origNow" + module];
302 delete this["_origNow" + module];
303 }
304 }
305
306 Services.obs.removeObserver(this, "http-on-modify-request", true);
307 Services.obs.removeObserver(this, "http-on-examine-response", true);
308 }
309
156 function showProfilingData(debuggerService) 310 function showProfilingData(debuggerService)
157 { 311 {
158 let scripts = []; 312 let scripts = [];
159 debuggerService.enumerateScripts({ 313 debuggerService.enumerateScripts({
160 enumerateScript: function(script) 314 enumerateScript: function(script)
161 { 315 {
162 scripts.push(script); 316 scripts.push(script);
163 } 317 }
164 }); 318 });
165 scripts = scripts.filter(function(script) 319 scripts = scripts.filter(function(script)
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 return oldFinish.apply(this, arguments); 400 return oldFinish.apply(this, arguments);
247 } 401 }
248 window.addEventListener("unload", function() 402 window.addEventListener("unload", function()
249 { 403 {
250 debuggerService.off(); 404 debuggerService.off();
251 }, true); 405 }, true);
252 debuggerService.on(); 406 debuggerService.on();
253 debuggerService.flags |= debuggerService.COLLECT_PROFILE_DATA; 407 debuggerService.flags |= debuggerService.COLLECT_PROFILE_DATA;
254 debuggerService.clearProfileData(); 408 debuggerService.clearProfileData();
255 } 409 }
OLDNEW
« no previous file with comments | « no previous file | chrome/content/tests/notification.js » ('j') | chrome/content/tests/notification.js » ('J')

Powered by Google App Engine
This is Rietveld