gecko-dev/testing/modules/MockRegistrar.jsm

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

137 строки
3.6 KiB
JavaScript
Исходник Обычный вид История

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
var EXPORTED_SYMBOLS = ["MockRegistrar"];
const Cm = Components.manager;
Bug 1514594: Part 3 - Change ChromeUtils.import API. *** Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8 This changes the behavior of ChromeUtils.import() to return an exports object, rather than a module global, in all cases except when `null` is passed as a second argument, and changes the default behavior not to pollute the global scope with the module's exports. Thus, the following code written for the old model: ChromeUtils.import("resource://gre/modules/Services.jsm"); is approximately the same as the following, in the new model: var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm"); Since the two behaviors are mutually incompatible, this patch will land with a scripted rewrite to update all existing callers to use the new model rather than the old. *** Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs This was done using the followng script: https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm *** Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8 Differential Revision: https://phabricator.services.mozilla.com/D16747 *** Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D16748 *** Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D16749 *** Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs *** Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D16750 --HG-- extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895 extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 21:18:31 +03:00
const { Log } = ChromeUtils.import("resource://gre/modules/Log.jsm");
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
var logger = Log.repository.getLogger("MockRegistrar");
XPCOMUtils.defineLazyServiceGetter(
this,
"UUIDGen",
"@mozilla.org/uuid-generator;1",
"nsIUUIDGenerator"
);
var MockRegistrar = Object.freeze({
_registeredComponents: new Map(),
_originalCIDs: new Map(),
get registrar() {
return Cm.QueryInterface(Ci.nsIComponentRegistrar);
},
/**
* Register a mock to override target interfaces.
* The target interface may be accessed through _genuine property of the mock.
* If you register multiple mocks to the same contract ID, you have to call
* unregister in reverse order. Otherwise the previous factory will not be
* restored.
*
* @param contractID The contract ID of the interface which is overridden by
the mock.
* e.g. "@mozilla.org/file/directory_service;1"
* @param mock An object which implements interfaces for the contract ID.
* @param args An array which is passed in the constructor of mock.
*
* @return The CID of the mock.
*/
register(contractID, mock, args) {
let originalCID = this._originalCIDs.get(contractID);
if (!originalCID) {
originalCID = this.registrar.contractIDToCID(contractID);
this._originalCIDs.set(contractID, originalCID);
}
let originalFactory = Cm.getClassObject(originalCID, Ci.nsIFactory);
let cid = UUIDGen.generateUUID();
let factory = {
createInstance(outer, iid) {
if (outer) {
throw Cr.NS_ERROR_NO_AGGREGATION;
}
let wrappedMock;
if (mock.prototype && mock.prototype.constructor) {
wrappedMock = Object.create(mock.prototype);
mock.apply(wrappedMock, args);
} else {
wrappedMock = mock;
}
try {
let genuine = originalFactory.createInstance(outer, iid);
wrappedMock._genuine = genuine;
} catch (ex) {
logger.info("Creating original instance failed", ex);
}
return wrappedMock.QueryInterface(iid);
},
lockFactory(lock) {
throw Cr.NS_ERROR_NOT_IMPLEMENTED;
},
QueryInterface: ChromeUtils.generateQI([Ci.nsIFactory]),
};
this.registrar.registerFactory(
cid,
"A Mock for " + contractID,
contractID,
factory
);
this._registeredComponents.set(cid, {
contractID,
factory,
originalCID,
});
return cid;
},
/**
* Unregister the mock.
*
* @param cid The CID of the mock.
*/
unregister(cid) {
let component = this._registeredComponents.get(cid);
if (!component) {
return;
}
this.registrar.unregisterFactory(cid, component.factory);
if (component.originalCID) {
// Passing `null` for the factory re-maps the contract ID to the
// entry for its original CID.
this.registrar.registerFactory(
component.originalCID,
"",
component.contractID,
null
);
}
this._registeredComponents.delete(cid);
},
/**
* Unregister all registered mocks.
*/
unregisterAll() {
for (let cid of this._registeredComponents.keys()) {
this.unregister(cid);
}
},
});