Skip to content

Commit

Permalink
mocking support for XPCOM only when passed the correct staticArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthropohedron committed Apr 8, 2015
1 parent e6a0bc2 commit 77a2ea3
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*jslint white: true, todo: true */
"use strict";

var staticArgs = require("sdk/system").staticArgs;
// Import Mozilla chrome APIs
var chrome = require("chrome");
var Cc = chrome.Cc;
Expand All @@ -10,25 +11,41 @@ var contractFrags = [
null,
";1"
];
var mocks = {};

exports.createInstance = function createInstance(contractId, iface) {
if (mocks.hasOwnProperty(iface)) { return mocks[iface]; }
function createInstance(contractId, iface) {
contractFrags[1] = contractId;
return Cc[contractFrags.join("")].createInstance(Ci[iface]);
};
}
exports.createInstance = createInstance;

exports.getService = function getService(contractId, service) {
if (mocks.hasOwnProperty(service)) { return mocks[service]; }
function getService(contractId, service) {
contractFrags[1] = contractId;
return Cc[contractFrags.join("")].getService(Ci[service]);
};
}
exports.getService = getService;

exports.setMock = function setMock(key, obj) {
mocks[key] = obj;
};
if (staticArgs.supportMockXPCOM) {
(function() {
var mocks = {};

exports.clearMock = function clearMock(key) {
delete mocks[key];
};
exports.createInstance = function createMockInstance(contractId, iface) {
if (mocks.hasOwnProperty(iface)) { return mocks[iface]; }
return createInstance(contractId, iface);
};

exports.getService = function getMockService(contractId, service) {
if (mocks.hasOwnProperty(service)) { return mocks[service]; }
return createInstance(contractId, service);
};

exports.setMock = function setMock(key, obj) {
mocks[key] = obj;
};

exports.clearMock = function clearMock(key) {
delete mocks[key];
};

}());
}

0 comments on commit 77a2ea3

Please sign in to comment.