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

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

Issue 11193004: Use mock XMLHttpRequest object to speed up tests (Closed)
Patch Set: Created July 25, 2013, 1: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
« no previous file with comments | « no previous file | chrome/content/tests/notification.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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; 6 const MILLIS_IN_SECOND = 1000;
7 const MILLIS_IN_MINUTE = 60 * MILLIS_IN_SECOND; 7 const MILLIS_IN_MINUTE = 60 * MILLIS_IN_SECOND;
8 const MILLIS_IN_HOUR = 60 * MILLIS_IN_MINUTE; 8 const MILLIS_IN_HOUR = 60 * MILLIS_IN_MINUTE;
9 const MILLIS_IN_DAY = 24 * MILLIS_IN_HOUR; 9 const MILLIS_IN_DAY = 24 * MILLIS_IN_HOUR;
10 10
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 { 300 {
301 global.Date.now = this["_origNow" + module]; 301 global.Date.now = this["_origNow" + module];
302 delete this["_origNow" + module]; 302 delete this["_origNow" + module];
303 } 303 }
304 } 304 }
305 305
306 Services.obs.removeObserver(this, "http-on-modify-request", true); 306 Services.obs.removeObserver(this, "http-on-modify-request", true);
307 Services.obs.removeObserver(this, "http-on-examine-response", true); 307 Services.obs.removeObserver(this, "http-on-examine-response", true);
308 } 308 }
309 309
310 function setupVirtualXMLHttp()
311 {
312 let host = "http://example.com";
313 let requestHandlers = {};
314
315 let XMLHttpRequest = function()
316 {
317 this._loadHandlers = [];
318 this._errorHandlers = [];
319 };
320 XMLHttpRequest.prototype = {
321 _path: null,
322 _data: null,
323 _queryString: null,
324 _loadHandlers: null,
325 _errorHandlers: null,
326 status: 0,
327 readyState: 0,
328 responseText: null,
329
330 addEventListener: function(eventName, handler, capture)
331 {
332 let list;
333 if (eventName == "load")
334 list = this._loadHandlers;
335 else if (eventName == "error")
336 list = this._errorHandlers;
337 else
338 throw new Error("Event type " + eventName + " not supported");
339
340 if (list.indexOf(handler) < 0)
341 list.push(handler);
342 },
343
344 removeEventListener: function(eventName, handler, capture)
345 {
346 let list;
347 if (eventName == "load")
348 list = this._loadHandlers;
349 else if (eventName == "error")
350 list = this._errorHandlers;
351 else
352 throw new Error("Event type " + eventName + " not supported");
353
354 let index = list.indexOf(handler);
355 if (index >= 0)
356 list.splice(index, 1);
357 },
358
359 open: function(method, url, async, user, password)
360 {
361 if (method != "GET")
362 throw new Error("Only GET requests are currently supported");
363 if (typeof async != "undefined" && !async)
364 throw new Error("Sync requests are not supported");
365 if (typeof user != "undefined" || typeof password != "undefined")
366 throw new Error("User authentification is not supported");
367
368 let match = /^data:[^,]+,/.exec(url);
369 if (match)
370 {
371 this._data = decodeURIComponent(url.substr(match[0].length));
372 return;
373 }
374
375 if (url.substr(0, host.length) != host)
376 throw new Error("Unexpected URL: " + url + " (URL starting with " + host + "expected)");
377
378 this._path = url.substr(host.length);
379
380 let queryIndex = this._path.indexOf("?");
381 this._queryString = "";
382 if (queryIndex >= 0)
383 {
384 this._queryString = this._path.substr(queryIndex + 1);
385 this._path = this._path.substr(0, queryIndex);
386 }
387 },
388
389 send: function(data)
390 {
391 if (!this._data && !this._path)
392 throw new Error("No request path set");
393 if (typeof data != "undefined" && data)
394 throw new Error("Sending data to server is not supported");
395
396 Utils.runAsync(function()
397 {
398 let result = [Cr.NS_OK, 404, ""];
399 if (this._data)
400 result = [Cr.NS_OK, 0, this._data];
401 else if (this._path in requestHandlers)
402 result = requestHandlers[this._path]({method: "GET", path: this._path, queryString: this._queryString});
403 [this.channel.status, this.channel.responseStatus, this.responseText] = result;
404 this.status = this.channel.responseStatus;
405
406 let eventName = (this.channel.status == Cr.NS_OK ? "load" : "error");
407 let event = {type: eventName};
408 for each (let handler in this["_" + eventName + "Handlers"])
409 handler.call(this, event);
410 }.bind(this));
411 },
412
413 overrideMimeType: function(mime)
414 {
415 },
416
417 channel:
418 {
419 status: -1,
420 responseStatus: 0,
421 loadFlags: 0,
422 INHIBIT_CACHING: 0,
423 VALIDATE_ALWAYS: 0,
424 QueryInterface: function() this
425 }
426 }
427
428 this.registerHandler = function(path, handler) requestHandlers[path] = handler ;
429
430 let modules = Array.prototype.slice.call(arguments, 1);
431 this._virtualXMLHttpModules = modules;
432 for each (let module in this._virtualTimeModules)
433 {
434 let global = getModuleGlobal(module);
435
436 // Replace XMLHttpRequest constructor
437 this["_origXMLHttpRequest" + module] = global.XMLHttpRequest;
438 global.XMLHttpRequest = XMLHttpRequest;
439 }
440 }
441
442 function restoreVirtualXMLHttp()
443 {
444 for each (let module in this._virtualXMLHttpModules)
445 {
446 let global = getModuleGlobal(module);
447
448 // Restore XMLHttpRequest constructor
449 if ("_origXMLHttpRequest" + module in this)
450 {
451 global.XMLHttpRequest = this["_origXMLHttpRequest" + module];
452 delete this["_origXMLHttpRequest" + module];
453 }
454 }
455 }
456
310 function showProfilingData(debuggerService) 457 function showProfilingData(debuggerService)
311 { 458 {
312 let scripts = []; 459 let scripts = [];
313 debuggerService.enumerateScripts({ 460 debuggerService.enumerateScripts({
314 enumerateScript: function(script) 461 enumerateScript: function(script)
315 { 462 {
316 scripts.push(script); 463 scripts.push(script);
317 } 464 }
318 }); 465 });
319 scripts = scripts.filter(function(script) 466 scripts = scripts.filter(function(script)
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
400 return oldFinish.apply(this, arguments); 547 return oldFinish.apply(this, arguments);
401 } 548 }
402 window.addEventListener("unload", function() 549 window.addEventListener("unload", function()
403 { 550 {
404 debuggerService.off(); 551 debuggerService.off();
405 }, true); 552 }, true);
406 debuggerService.on(); 553 debuggerService.on();
407 debuggerService.flags |= debuggerService.COLLECT_PROFILE_DATA; 554 debuggerService.flags |= debuggerService.COLLECT_PROFILE_DATA;
408 debuggerService.clearProfileData(); 555 debuggerService.clearProfileData();
409 } 556 }
OLDNEW
« no previous file with comments | « no previous file | chrome/content/tests/notification.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld