2008-04-30 13:01:17 -07:00
|
|
|
version(180);
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
2008-06-16 14:06:05 -07:00
|
|
|
const Cu = Components.utils;
|
2008-04-30 13:01:17 -07:00
|
|
|
|
|
|
|
let ds = Cc["@mozilla.org/file/directory_service;1"]
|
|
|
|
.getService(Ci.nsIProperties);
|
|
|
|
|
|
|
|
let provider = {
|
|
|
|
getFile: function(prop, persistent) {
|
|
|
|
persistent.value = true;
|
|
|
|
if (prop == "ExtPrefDL") {
|
|
|
|
return [ds.get("CurProcD", Ci.nsIFile)];
|
|
|
|
}
|
|
|
|
throw Cr.NS_ERROR_FAILURE;
|
|
|
|
},
|
|
|
|
QueryInterface: function(iid) {
|
|
|
|
if (iid.equals(Ci.nsIDirectoryServiceProvider) ||
|
|
|
|
iid.equals(Ci.nsISupports)) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
throw Cr.NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
ds.QueryInterface(Ci.nsIDirectoryService).registerProvider(provider);
|
|
|
|
|
|
|
|
do_bind_resource(do_get_file("modules"), "weave");
|
2008-06-06 21:40:30 -07:00
|
|
|
|
|
|
|
function loadInSandbox(aUri) {
|
|
|
|
var sandbox = Components.utils.Sandbox(this);
|
|
|
|
var request = Components.
|
|
|
|
classes["@mozilla.org/xmlextras/xmlhttprequest;1"].
|
|
|
|
createInstance();
|
|
|
|
|
|
|
|
request.open("GET", aUri, false);
|
|
|
|
request.send(null);
|
|
|
|
Components.utils.evalInSandbox(request.responseText, sandbox);
|
|
|
|
|
|
|
|
return sandbox;
|
|
|
|
}
|
2008-06-09 20:51:23 -07:00
|
|
|
|
2008-06-16 16:42:32 -07:00
|
|
|
function FakeTimerService() {
|
|
|
|
Cu.import("resource://weave/util.js");
|
|
|
|
|
|
|
|
this.callbackQueue = [];
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.__proto__ = {
|
|
|
|
makeTimerForCall: function FTS_makeTimerForCall(cb) {
|
|
|
|
// Just add the callback to our queue and we'll call it later, so
|
|
|
|
// as to simulate a real nsITimer.
|
|
|
|
self.callbackQueue.push(cb);
|
|
|
|
return "fake nsITimer";
|
|
|
|
},
|
|
|
|
processCallback: function FTS_processCallbacks() {
|
|
|
|
var cb = self.callbackQueue.pop();
|
|
|
|
if (cb) {
|
|
|
|
cb();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Utils.makeTimerForCall = self.makeTimerForCall;
|
|
|
|
};
|
|
|
|
|
2008-06-19 17:04:04 -07:00
|
|
|
function getTestLogger(component) {
|
|
|
|
return Log4Moz.Service.getLogger("Testing");
|
|
|
|
}
|
|
|
|
|
|
|
|
function initTestLogging(level) {
|
2008-06-09 20:51:23 -07:00
|
|
|
Cu.import("resource://weave/log4moz.js");
|
|
|
|
|
2008-06-16 16:22:00 -07:00
|
|
|
function LogStats() {
|
|
|
|
this.errorsLogged = 0;
|
|
|
|
}
|
|
|
|
LogStats.prototype = {
|
2008-06-09 20:51:23 -07:00
|
|
|
format: function BF_format(message) {
|
|
|
|
if (message.level == Log4Moz.Level.Error)
|
2008-06-16 16:22:00 -07:00
|
|
|
this.errorsLogged += 1;
|
2008-06-09 20:51:23 -07:00
|
|
|
return message.loggerName + "\t" + message.levelDesc + "\t" +
|
|
|
|
message.message + "\n";
|
|
|
|
}
|
|
|
|
};
|
2008-06-16 16:22:00 -07:00
|
|
|
LogStats.prototype.__proto__ = new Log4Moz.Formatter();
|
2008-06-09 20:51:23 -07:00
|
|
|
|
|
|
|
var log = Log4Moz.Service.rootLogger;
|
2008-06-16 16:22:00 -07:00
|
|
|
var logStats = new LogStats();
|
|
|
|
var appender = new Log4Moz.DumpAppender(logStats);
|
2008-06-19 17:04:04 -07:00
|
|
|
|
|
|
|
if (typeof(level) == "undefined")
|
|
|
|
level = "Debug";
|
|
|
|
getTestLogger().level = Log4Moz.Level[level];
|
|
|
|
|
|
|
|
log.level = Log4Moz.Level.Trace;
|
|
|
|
appender.level = Log4Moz.Level.Trace;
|
2008-06-09 20:51:23 -07:00
|
|
|
log.addAppender(appender);
|
|
|
|
|
2008-06-16 16:22:00 -07:00
|
|
|
return logStats;
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeAsyncTestRunner(generator) {
|
|
|
|
Cu.import("resource://weave/async.js");
|
|
|
|
|
|
|
|
var logStats = initTestLogging();
|
|
|
|
|
2008-06-09 20:51:23 -07:00
|
|
|
function run_test() {
|
|
|
|
do_test_pending();
|
|
|
|
|
|
|
|
let onComplete = function() {
|
2008-06-16 16:22:00 -07:00
|
|
|
if (logStats.errorsLogged)
|
2008-06-09 20:51:23 -07:00
|
|
|
do_throw("Errors were logged.");
|
|
|
|
else
|
|
|
|
do_test_finished();
|
|
|
|
};
|
|
|
|
|
|
|
|
Async.run({}, generator, onComplete);
|
|
|
|
}
|
|
|
|
|
|
|
|
return run_test;
|
|
|
|
}
|
2008-06-18 16:51:54 -07:00
|
|
|
|
|
|
|
function FakePrefService(contents) {
|
|
|
|
Cu.import("resource://weave/util.js");
|
|
|
|
this.fakeContents = contents;
|
|
|
|
Utils.__prefs = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FakePrefService.prototype = {
|
|
|
|
_getPref: function fake__getPref(pref) {
|
2008-06-19 17:04:04 -07:00
|
|
|
getTestLogger().trace("Getting pref: " + pref);
|
2008-06-18 16:51:54 -07:00
|
|
|
return this.fakeContents[pref];
|
|
|
|
},
|
|
|
|
getCharPref: function fake_getCharPref(pref) {
|
|
|
|
return this._getPref(pref);
|
|
|
|
},
|
|
|
|
getBoolPref: function fake_getBoolPref(pref) {
|
|
|
|
return this._getPref(pref);
|
|
|
|
},
|
|
|
|
getIntPref: function fake_getIntPref(pref) {
|
|
|
|
return this._getPref(pref);
|
|
|
|
},
|
|
|
|
addObserver: function fake_addObserver() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
function makeFakeAsyncFunc(retval) {
|
|
|
|
Cu.import("resource://weave/async.js");
|
|
|
|
Function.prototype.async = Async.sugar;
|
|
|
|
|
|
|
|
function fakeAsyncFunc() {
|
|
|
|
let self = yield;
|
|
|
|
|
|
|
|
Utils.makeTimerForCall(self.cb);
|
|
|
|
yield;
|
|
|
|
|
|
|
|
self.done(retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
return fakeAsyncFunc;
|
|
|
|
}
|
|
|
|
|
|
|
|
function FakeDAVService(contents) {
|
|
|
|
Cu.import("resource://weave/dav.js");
|
|
|
|
|
|
|
|
this.fakeContents = contents;
|
|
|
|
DAV.__proto__ = this;
|
|
|
|
this.checkLogin = makeFakeAsyncFunc(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
FakeDAVService.prototype = {
|
2008-06-18 17:28:28 -07:00
|
|
|
PUT: function fake_PUT(path, data, onComplete) {
|
2008-06-20 15:39:07 -07:00
|
|
|
getTestLogger().info("HTTP PUT to " + path + " with data: " + data);
|
2008-06-18 17:28:28 -07:00
|
|
|
this.fakeContents[path] = data;
|
|
|
|
makeFakeAsyncFunc({status: 200}).async(this, onComplete);
|
|
|
|
},
|
|
|
|
|
2008-06-18 16:51:54 -07:00
|
|
|
GET: function fake_GET(path, onComplete) {
|
|
|
|
var result = {status: 404};
|
|
|
|
if (path in this.fakeContents)
|
|
|
|
result = {status: 200, responseText: this.fakeContents[path]};
|
2008-06-20 15:39:07 -07:00
|
|
|
getTestLogger().info("HTTP GET from " + path + ", returning status " +
|
2008-06-20 12:49:25 -07:00
|
|
|
result.status);
|
2008-06-18 16:51:54 -07:00
|
|
|
return makeFakeAsyncFunc(result).async(this, onComplete);
|
2008-06-18 17:28:28 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
MKCOL: function fake_MKCOL(path, onComplete) {
|
2008-06-20 15:39:07 -07:00
|
|
|
getTestLogger().info("HTTP MKCOL on " + path);
|
2008-06-18 17:28:28 -07:00
|
|
|
makeFakeAsyncFunc(true).async(this, onComplete);
|
2008-06-18 16:51:54 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function FakePasswordService(contents) {
|
|
|
|
Cu.import("resource://weave/util.js");
|
|
|
|
|
|
|
|
this.fakeContents = contents;
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
Utils.findPassword = function fake_findPassword(realm, username) {
|
2008-06-19 17:04:04 -07:00
|
|
|
getTestLogger().trace("Password requested for " +
|
|
|
|
realm + ":" + username);
|
2008-06-18 17:28:28 -07:00
|
|
|
if (realm in self.fakeContents && username in self.fakeContents[realm])
|
|
|
|
return self.fakeContents[realm][username];
|
|
|
|
else
|
|
|
|
return null;
|
2008-06-18 16:51:54 -07:00
|
|
|
};
|
|
|
|
};
|
2008-06-20 17:47:32 -07:00
|
|
|
|
|
|
|
function FakeFilesystemService(contents) {
|
|
|
|
this.fakeContents = contents;
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
Utils.getProfileFile = function fake_getProfileFile(arg) {
|
|
|
|
let fakeNsILocalFile = {
|
|
|
|
exists: function() {
|
|
|
|
return this._fakeFilename in self.fakeContents;
|
|
|
|
},
|
|
|
|
_fakeFilename: (typeof(arg) == "object") ? arg.path : arg
|
|
|
|
};
|
|
|
|
return fakeNsILocalFile;
|
|
|
|
};
|
|
|
|
|
|
|
|
Utils.readStream = function fake_readStream(stream) {
|
|
|
|
getTestLogger().info("Reading from stream.");
|
|
|
|
return stream._fakeData;
|
|
|
|
};
|
|
|
|
|
|
|
|
Utils.open = function fake_open(file, mode) {
|
|
|
|
switch (mode) {
|
|
|
|
case "<":
|
|
|
|
mode = "reading";
|
|
|
|
break;
|
|
|
|
case ">":
|
|
|
|
mode = "writing";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error("Unexpected mode: " + mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
getTestLogger().info("Opening '" + file._fakeFilename + "' for " +
|
|
|
|
mode + ".");
|
|
|
|
var contents = "";
|
|
|
|
if (file._fakeFilename in self.fakeContents && mode == "reading")
|
|
|
|
contents = self.fakeContents[file._fakeFilename];
|
|
|
|
let fakeStream = {
|
|
|
|
writeString: function(data) {
|
|
|
|
contents += data;
|
|
|
|
getTestLogger().info("Writing data to local file '" +
|
|
|
|
file._fakeFilename +"': " + data);
|
|
|
|
},
|
|
|
|
close: function() {
|
|
|
|
self.fakeContents[file._fakeFilename] = contents;
|
|
|
|
},
|
|
|
|
get _fakeData() { return contents; }
|
|
|
|
};
|
|
|
|
return [fakeStream];
|
|
|
|
};
|
|
|
|
};
|
2008-06-23 14:13:46 -07:00
|
|
|
|
|
|
|
function FakeGUIDService() {
|
|
|
|
let latestGUID = 0;
|
|
|
|
|
|
|
|
Utils.makeGUID = function fake_makeGUID() {
|
|
|
|
return "fake-guid-" + latestGUID++;
|
|
|
|
};
|
|
|
|
}
|