Moved XPCOM-specific stuff from engines/passwords.js to util.js so they could be easily stubbed-out by unit tests.

This commit is contained in:
Atul Varma 2008-06-18 11:54:24 -07:00
parent 90d243db16
commit 22a628bfea
3 changed files with 21 additions and 15 deletions

View File

@ -1,7 +1,5 @@
const EXPORTED_SYMBOLS = ['PasswordEngine'];
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
Cu.import("resource://weave/util.js");
@ -63,8 +61,7 @@ PasswordSyncCore.prototype = {
__loginManager : null,
get _loginManager() {
if (!this.__loginManager)
this.__loginManager = Cc["@mozilla.org/login-manager;1"].
getService(Ci.nsILoginManager);
this.__loginManager = Utils.getLoginManager();
return this.__loginManager;
},
@ -100,21 +97,17 @@ PasswordStore.prototype = {
__loginManager : null,
get _loginManager() {
if (!this.__loginManager)
this.__loginManager = Cc["@mozilla.org/login-manager;1"].
getService(Ci.nsILoginManager);
this.__loginManager = Utils.getLoginManager();
return this.__loginManager;
},
__nsLoginInfo : null,
get _nsLoginInfo() {
if (!this.__nsLoginInfo)
this.__nsLoginInfo = new Components.Constructor(
"@mozilla.org/login-manager/loginInfo;1",
Ci.nsILoginInfo, "init");
this.__nsLoginInfo = Utils.makeNewLoginInfo();
return this.__nsLoginInfo;
},
_createCommand: function PasswordStore__createCommand(command) {
this._log.info("PasswordStore got createCommand: " + command );

View File

@ -50,6 +50,18 @@ Cu.import("resource://weave/log4moz.js");
*/
let Utils = {
getLoginManager: function getLoginManager() {
return Cc["@mozilla.org/login-manager;1"].
getService(Ci.nsILoginManager);
},
makeNewLoginInfo: function getNewLoginInfo() {
return new Components.Constructor(
"@mozilla.org/login-manager/loginInfo;1",
Ci.nsILoginInfo,
"init"
);
},
findPassword: function findPassword(realm, username) {
// fixme: make a request and get the realm ?

View File

@ -1,3 +1,5 @@
Cu.import("resource://weave/util.js");
function run_test() {
// The JS module we're testing, with all members exposed.
var passwords = loadInSandbox("resource://weave/engines/passwords.js");
@ -13,10 +15,10 @@ function run_test() {
passwordField: "test_password"
};
// Fake nsILoginManager object.
var fakeLoginManager = {
getAllLogins: function() { return [fakeUser]; }
};
Utils.getLoginManager = function fake_getLoginManager() {
// Return a fake nsILoginManager object.
return {getAllLogins: function() { return [fakeUser]; }};
};
// Ensure that _hashLoginInfo() works.
var fakeUserHash = passwords._hashLoginInfo(fakeUser);
@ -25,7 +27,6 @@ function run_test() {
// Ensure that PasswordSyncCore._itemExists() works.
var psc = new passwords.PasswordSyncCore();
psc.__loginManager = fakeLoginManager;
do_check_false(psc._itemExists("invalid guid"));
do_check_true(psc._itemExists(fakeUserHash));
}