Bug 664932: Make update unit tests not implement nsIXMLHttpRequestEventTarget in script. r=rstrong
authorJonas Sicking <jonas@sicking.cc>
Wed, 22 Jun 2011 15:17:07 -0700
changeset 71821 56854b8cf1a6a146f52bd0f2aed944e08960b7e8
parent 71820 8e30eba8ff6456654f618071faa71b7ca8a7ea82
child 71833 48e72227c2fac1644fec76af2e042c5bb6d61e58
push id209
push userbzbarsky@mozilla.com
push dateTue, 05 Jul 2011 17:42:16 +0000
treeherdermozilla-aurora@cc6e30cce8af [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrstrong
bugs664932
milestone7.0a1
Bug 664932: Make update unit tests not implement nsIXMLHttpRequestEventTarget in script. r=rstrong
toolkit/mozapps/update/nsUpdateService.js
toolkit/mozapps/update/test/unit/head_update.js.in
--- a/toolkit/mozapps/update/nsUpdateService.js
+++ b/toolkit/mozapps/update/nsUpdateService.js
@@ -2133,17 +2133,21 @@ Checker.prototype = {
       return;
 
     // If the user changes the update channel there can be leftover files from
     // a previous download so clean the updates directory for manual checks.
     if (force)
       cleanUpUpdatesDir();
 
     this._request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].
-                    createInstance(Ci.nsIXMLHttpRequest);
+                    createInstance(Ci.nsISupports);
+    // This is here to let unit test code override XHR
+    if (this._request.wrappedJSObject) {
+      this._request = this._request.wrappedJSObject;
+    }
     this._request.open("GET", url, true);
     var allowNonBuiltIn = !getPref("getBoolPref",
                                    PREF_APP_UPDATE_CERT_REQUIREBUILTIN, true);
     this._request.channel.notificationCallbacks = new gCertUtils.BadCertHandler(allowNonBuiltIn);
     this._request.overrideMimeType("text/xml");
     this._request.setRequestHeader("Cache-Control", "no-cache");
 
     var self = this;
--- a/toolkit/mozapps/update/test/unit/head_update.js.in
+++ b/toolkit/mozapps/update/test/unit/head_update.js.in
@@ -965,16 +965,21 @@ function overrideXHR(callback) {
   registrar.registerFactory(gXHR.classID, gXHR.classDescription,
                             gXHR.contractID, gXHR);
 }
 
 /**
  * Bare bones XMLHttpRequest implementation for testing onprogress, onerror,
  * and onload nsIDomEventListener handleEvent.
  */
+function makeHandler(val) {
+  if (typeof val == "function")
+    return ({ handleEvent: val });
+  return val;
+}
 function xhr() {
 }
 xhr.prototype = {
   overrideMimeType: function(mimetype) { },
   setRequestHeader: function(header, value) { },
   status: null,
   channel: { set notificationCallbacks(val) { } },
   _url: null,
@@ -984,50 +989,47 @@ xhr.prototype = {
     gXHR._method = method; gXHR._url = url;
   },
   responseXML: null,
   responseText: null,
   send: function(body) {
     do_execute_soon(gXHRCallback); // Use a timeout so the XHR completes
   },
   _onprogress: null,
-  set onprogress(val) { gXHR._onprogress = val; },
+  set onprogress(val) { gXHR._onprogress = makeHandler(val); },
   get onprogress() { return gXHR._onprogress; },
   _onerror: null,
-  set onerror(val) { gXHR._onerror = val; },
+  set onerror(val) { gXHR._onerror = makeHandler(val); },
   get onerror() { return gXHR._onerror; },
   _onload: null,
-  set onload(val) { gXHR._onload = val; },
+  set onload(val) { gXHR._onload = makeHandler(val); },
   get onload() { return gXHR._onload; },
   flags: AUS_Ci.nsIClassInfo.SINGLETON,
   implementationLanguage: AUS_Ci.nsIProgrammingLanguage.JAVASCRIPT,
   getHelperForLanguage: function(language) null,
   getInterfaces: function(count) {
-    var interfaces = [AUS_Ci.nsIXMLHttpRequest, AUS_Ci.nsIJSXMLHttpRequest,
-                      AUS_Ci.nsIXMLHttpRequestEventTarget];
+    var interfaces = [AUS_Ci.nsISupports];
     count.value = interfaces.length;
     return interfaces;
   },
   classDescription: "XMLHttpRequest",
   contractID: "@mozilla.org/xmlextras/xmlhttprequest;1",
   classID: Components.ID("{c9b37f43-4278-4304-a5e0-600991ab08cb}"),
   createInstance: function (outer, aIID) {
     if (outer == null)
       return gXHR.QueryInterface(aIID);
     throw AUS_Cr.NS_ERROR_NO_AGGREGATION;
   },
   QueryInterface: function(aIID) {
-    if (aIID.equals(AUS_Ci.nsIXMLHttpRequest) ||
-        aIID.equals(AUS_Ci.nsIJSXMLHttpRequest) ||
-        aIID.equals(AUS_Ci.nsIXMLHttpRequestEventTarget) ||
-        aIID.equals(AUS_Ci.nsIClassInfo) ||
+    if (aIID.equals(AUS_Ci.nsIClassInfo) ||
         aIID.equals(AUS_Ci.nsISupports))
       return gXHR;
     throw AUS_Cr.NS_ERROR_NO_INTERFACE;
-  }
+  },
+  get wrappedJSObject() { return this; }
 };
 
 /* Update check listener */
 const updateCheckListener = {
   onProgress: function UCL_onProgress(request, position, totalSize) {
   },
 
   onCheckComplete: function UCL_onCheckComplete(request, updates, updateCount) {