diff --git a/testing/mochitest/tests/SimpleTest/specialpowersAPI.js b/testing/mochitest/tests/SimpleTest/specialpowersAPI.js index 77af2d5768d..419d8a9302c 100644 --- a/testing/mochitest/tests/SimpleTest/specialpowersAPI.js +++ b/testing/mochitest/tests/SimpleTest/specialpowersAPI.js @@ -676,5 +676,34 @@ SpecialPowersAPI.prototype = { withCaret ? ctx.DRAWWINDOW_DRAW_CARET : 0); return el; }, + + swapFactoryRegistration: function(cid, contractID, newFactory, oldFactory) { + var componentRegistrar = Components.manager.QueryInterface(Components.interfaces.nsIComponentRegistrar); + + var unregisterFactory = newFactory; + var registerFactory = oldFactory; + + if (cid == null) { + if (contractID != null) { + cid = componentRegistrar.contractIDToCID(contractID); + oldFactory = Components.manager.getClassObject(Components.classes[contractID], + Components.interfaces.nsIFactory); + } else { + return {'error': "trying to register a new contract ID: Missing contractID"}; + } + + unregisterFactory = oldFactory; + registerFactory = newFactory; + } + componentRegistrar.unregisterFactory(cid, + unregisterFactory); + + // Restore the original factory. + componentRegistrar.registerFactory(cid, + "", + contractID, + registerFactory); + return {'cid':cid, 'originalFactory':oldFactory}; + }, }; diff --git a/toolkit/content/tests/browser/common/mockObjects.js b/toolkit/content/tests/browser/common/mockObjects.js index 370158db890..4a65e17f9da 100644 --- a/toolkit/content/tests/browser/common/mockObjects.js +++ b/toolkit/content/tests/browser/common/mockObjects.js @@ -65,7 +65,6 @@ MockObjectRegisterer.prototype = { * to ensure that unregister() is called. */ register: function MOR_register() { - netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); if (this._originalFactory) throw new Exception("Invalid object state when calling register()"); @@ -79,41 +78,24 @@ MockObjectRegisterer.prototype = { } }; - var componentRegistrar = Components.manager.QueryInterface(Components.interfaces.nsIComponentRegistrar); - - // Get the component CID. - this._cid = componentRegistrar.contractIDToCID(this._contractID); - - // ... and make sure we correctly replace the original factory with the mock one. - this._originalFactory = Components.manager.getClassObject(Components.classes[this._contractID], - Components.interfaces.nsIFactory); - - componentRegistrar.unregisterFactory(this._cid, this._originalFactory); - - componentRegistrar.registerFactory(this._cid, - "", - this._contractID, - this._mockFactory); + var retVal = SpecialPowers.swapFactoryRegistration(this._cid, this._contractID, this._mockFactory, this._originalFactory); + if ('error' in retVal) { + throw new Exception("ERROR: " + retVal.error); + } else { + this._cid = retVal.cid; + this._originalFactory = retVal.originalFactory; + } }, /** * Restores the original factory. */ unregister: function MOR_unregister() { - netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); if (!this._originalFactory) throw new Exception("Invalid object state when calling unregister()"); // Free references to the mock factory. - var componentRegistrar = Components.manager.QueryInterface(Components.interfaces.nsIComponentRegistrar); - componentRegistrar.unregisterFactory(this._cid, - this._mockFactory); - - // Restore the original factory. - componentRegistrar.registerFactory(this._cid, - "", - this._contractID, - this._originalFactory); + SpecialPowers.swapFactoryRegistration(this._cid, this._contractID, this._mockFactory, this._originalFactory); // Allow registering a mock factory again later. this._cid = null;