Bug 588193 - more tests for util.js [r=mconnor]

This commit is contained in:
Eric Lemoine 2010-09-20 18:53:08 +02:00
Родитель cef1e48b12
Коммит 31a753ce81
11 изменённых файлов: 358 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,15 @@
Cu.import("resource://services-sync/util.js");
function run_test() {
let thing = {o: {foo: "foo", bar: ["bar"]}, a: ["foo", {bar: "bar"}]};
let ret = Utils.deepCopy(thing);
do_check_neq(ret, thing)
do_check_neq(ret.o, thing.o);
do_check_neq(ret.o.bar, thing.o.bar);
do_check_neq(ret.a, thing.a);
do_check_neq(ret.a[1], thing.a[1]);
do_check_eq(ret.o.foo, thing.o.foo);
do_check_eq(ret.o.bar[0], thing.o.bar[0]);
do_check_eq(ret.a[0], thing.a[0]);
do_check_eq(ret.a[1].bar, thing.a[1].bar);
}

Просмотреть файл

@ -0,0 +1,35 @@
Cu.import("resource://services-sync/util.js");
function WinMock(href) {
this.location = {href: href};
this._open = true;
};
WinMock.prototype = {
addEventListener: function(type, listener) {
this._listener = listener;
},
close: function() {
if (this._listener) {
this._listener();
}
this._open = false;
}
};
function run_test() {
let w1 = new WinMock("chrome://win/win.xul");
Utils.ensureOneOpen(w1);
do_check_true(w1._open);
let w2 = new WinMock("chrome://win/win.xul");
Utils.ensureOneOpen(w2);
do_check_false(w1._open);
// close w2 and test that ensureOneOpen doesn't
// close it again
w2.close();
w2._open = true;
let w3 = new WinMock("chrome://win/win.xul");
Utils.ensureOneOpen(w3);
do_check_true(w2._open);
}

Просмотреть файл

@ -0,0 +1,134 @@
_("Test file-related utility functions");
Cu.import("resource://services-sync/util.js");
Cu.import("resource://services-sync/constants.js");
function run_test() {
_test_getTmp();
_test_open();
}
function _test_getTmp() {
// as the setup phase remove the tmp directory from the
// filesystem
let svc = Cc["@mozilla.org/file/directory_service;1"]
.getService(Ci.nsIProperties);
let tmp = svc.get("ProfD", Ci.nsIFile);
tmp.QueryInterface(Ci.nsILocalFile);
tmp.append("weave");
tmp.append("tmp");
if (tmp.exists())
tmp.remove(true);
// call getTmp with no argument. A ref to the tmp
// dir is returned
_("getTmp with no argument");
let tmpDir = Utils.getTmp();
do_check_true(tmpDir instanceof Ci.nsILocalFile);
do_check_true(tmpDir.isDirectory());
do_check_true(tmpDir.exists());
do_check_eq(tmpDir.leafName, "tmp");
// call getTmp with a string. A ref to the file
// named with this string and included in the
// tmp dir is returned
_("getTmp with a string");
let tmpFile = Utils.getTmp("name");
do_check_true(tmpFile instanceof Ci.nsILocalFile);
do_check_true(!tmpFile.exists()); // getTmp doesn't create the file!
do_check_eq(tmpFile.leafName, "name");
do_check_true(tmpDir.contains(tmpFile, false));
}
function _test_open() {
// we rely on Utils.getTmp to get a temporary file and
// test Utils.open on that file, that's ok Util.getTmp
// is also tested (test_getTmp)
function createFile() {
let f = Utils.getTmp("_test_");
f.create(f.NORMAL_FILE_TYPE, PERMS_FILE);
return f;
}
// we should probably test more things here, for example
// we should test that we cannot write to a stream that
// is created as a result of opening a file for reading
let s, f;
_("Open for reading, providing a file");
let f1 = createFile();
[s, f] = Utils.open(f1, "<");
do_check_eq(f.path, f1.path);
do_check_true(s instanceof Ci.nsIConverterInputStream);
f1.remove(false);
_("Open for reading, providing a file name");
let f2 = createFile();
let path2 = f2.path;
[s, f] = Utils.open(path2, "<");
do_check_eq(f.path, path2);
do_check_true(s instanceof Ci.nsIConverterInputStream);
f2.remove(false);
_("Open for writing with truncate mode, providing a file");
let f3 = createFile();
[s, f] = Utils.open(f3, ">");
do_check_eq(f.path, f3.path);
do_check_true(s instanceof Ci.nsIConverterOutputStream);
f3.remove(false);
_("Open for writing with truncate mode, providing a file name");
let f4 = createFile();
let path4 = f4.path;
[s, f] = Utils.open(path4, ">");
do_check_eq(f.path, path4);
do_check_true(s instanceof Ci.nsIConverterOutputStream);
f4.remove(false);
_("Open for writing with append mode, providing a file");
let f5 = createFile();
[s, f] = Utils.open(f5, ">>");
do_check_eq(f.path, f5.path);
do_check_true(s instanceof Ci.nsIConverterOutputStream);
f5.remove(false);
_("Open for writing with append mode, providing a file name");
let f6 = createFile();
let path6 = f6.path;
[s, f] = Utils.open(path6, ">>");
do_check_eq(f.path, path6);
do_check_true(s instanceof Ci.nsIConverterOutputStream);
f6.remove(false);
_("Open with illegal mode");
let f7 = createFile();
let except7;
try {
Utils.open(f7, "?!");
} catch(e) {
except7 = e;
}
do_check_true(!!except7);
f7.remove(false);
_("Open non-existing file for reading");
let f8 = createFile();
let path8 = f8.path;
f8.remove(false);
let except8;
try {
Utils.open(path8, "<");
} catch(e) {
except8 = e;
}
do_check_true(!!except8);
_("Open for reading, provide permissions");
let f9 = createFile();
[s, f] = Utils.open(f9, "<", 0644);
do_check_eq(f.path, f9.path);
do_check_true(s instanceof Ci.nsIConverterInputStream);
f9.remove(false);
}

Просмотреть файл

@ -0,0 +1,14 @@
Cu.import("resource://services-sync/util.js");
function run_test() {
let str;
// we just test whether the returned string includes the
// string "unknown", should be good enough
str = Utils.getErrorString("error.login.reason.password");
do_check_true(str.match(/unknown/i) == null);
str = Utils.getErrorString("foobar");
do_check_true(str.match(/unknown/i) != null);
}

Просмотреть файл

@ -0,0 +1,18 @@
Cu.import("resource://services-sync/util.js");
function run_test() {
_("Test with a valid icon URI");
let iconUri = "http://foo.bar/favicon.png";
let icon1 = Utils.getIcon(iconUri);
do_check_true(icon1.indexOf(iconUri) > 0);
_("Test with an invalid icon URI and default icon");
let icon2 = Utils.getIcon("foo", "bar");
do_check_eq(icon2, "bar");
_("Test with an invalid icon URI and no default icon");
let icon3 = Utils.getIcon("foo");
var defaultFavicon = Cc["@mozilla.org/browser/favicon-service;1"]
.getService(Ci.nsIFaviconService).defaultFavicon.spec;
do_check_eq(icon3, defaultFavicon);
}

Просмотреть файл

@ -0,0 +1,14 @@
Cu.import("resource://services-sync/util.js");
let undef;
function run_test() {
do_check_true(Utils.isArray([]));
do_check_true(Utils.isArray([1, 2]));
do_check_false(Utils.isArray({}));
do_check_false(Utils.isArray(1.0));
do_check_false(Utils.isArray("string"));
do_check_false(Utils.isArray(null));
do_check_false(Utils.isArray(undef));
do_check_false(Utils.isArray(new Date()));
}

Просмотреть файл

@ -43,4 +43,18 @@ function run_test() {
});
do_check_eq(typeof num, "number");
do_check_eq(num, 42);
_("Verify that things get logged");
let trace, debug;
Utils.jsonSave("str",
{_log: {trace: function(msg) { trace = msg; }}},
"hi");
do_check_true(!!trace);
trace = undefined;
Utils.jsonLoad("str",
{_log: {trace: function(msg) { trace = msg; },
debug: function(msg) { debug = msg; }}},
function(val) { throw "exception"; });
do_check_true(!!trace);
do_check_true(!!debug);
}

Просмотреть файл

@ -0,0 +1,11 @@
Cu.import("resource://services-sync/util.js");
Cu.import("resource://services-sync/ext/StringBundle.js");
function run_test() {
let fn = Utils.lazyStrings("sync");
do_check_eq(typeof fn, "function");
let bundle = fn();
do_check_true(bundle instanceof StringBundle);
let url = bundle.url;
do_check_eq(url, "chrome://weave/locale/services/sync.properties");
}

Просмотреть файл

@ -1,7 +1,15 @@
_("Make sure uri strings are converted to nsIURIs");
Cu.import("resource://services-sync/util.js");
// both the makeURI and makeURL functions are tested
// in this file
function run_test() {
_test_makeURI();
_test_makeURL();
}
function _test_makeURI() {
_("Check http uris");
let uri1 = "http://mozillalabs.com/";
do_check_eq(Utils.makeURI(uri1).spec, uri1);
@ -57,3 +65,35 @@ function run_test() {
do_check_eq(Utils.makeURI("chrome://badstuff"), undefined);
do_check_eq(Utils.makeURI("this is a test"), undefined);
}
function _test_makeURL() {
_("Check http uri");
let uri = "http://mozillalabs.com/";
do_check_true(Utils.makeURL(uri) instanceof Ci.nsIURL);
_("Check https uri");
let uris = "https://mozillalabs.com/";
do_check_true(Utils.makeURL(uris) instanceof Ci.nsIURL);
let uric = "chrome://browser/content/browser.xul";
do_check_true(Utils.makeURL(uric) instanceof Ci.nsIURL);
_("Check about uri");
let uria = "about:weave";
let except1;
try {
Utils.makeURL(uria);
} catch(e) {
except1 = e;
}
do_check_true(!!except1);
_("Check invalid uri");
let except2;
try {
Utils.makeURL("mozillalabs.com");
} catch(e) {
except2 = e;
}
do_check_true(!!except2);
}

Просмотреть файл

@ -63,6 +63,16 @@ function run_test() {
let r10 = Utils.queryAsync(c("SELECT value, fieldname FROM moz_formhistory"), "fieldname");
do_check_eq(r10.length, 3);
_("Generate an execution error");
let r11, except, query = c("UPDATE moz_formhistory SET value = NULL WHERE fieldname = 'more'");
try {
r11 = Utils.queryAsync(query);
} catch(e) {
except = e;
}
do_check_true(!!except);
do_check_eq(except.result, 19); // constraint violation error
_("Cleaning up");
Utils.queryAsync(c("DELETE FROM moz_formhistory"));

Просмотреть файл

@ -0,0 +1,53 @@
Cu.import("resource://services-sync/util.js");
// both the checkStatus and ensureStatus functions are tested
// here.
function run_test() {
_test_checkStatus();
_test_ensureStatus();
}
function _test_checkStatus() {
let msg = "msg";
_("test with default range");
do_check_true(Utils.checkStatus(200, msg));
do_check_true(Utils.checkStatus(299, msg));
do_check_false(Utils.checkStatus(199, msg));
do_check_false(Utils.checkStatus(300, msg));
_("test with a number");
do_check_true(Utils.checkStatus(100, msg, [100]));
do_check_false(Utils.checkStatus(200, msg, [100]));
_("test with two numbers");
do_check_true(Utils.checkStatus(100, msg, [100, 200]));
do_check_true(Utils.checkStatus(200, msg, [100, 200]));
do_check_false(Utils.checkStatus(50, msg, [100, 200]));
do_check_false(Utils.checkStatus(150, msg, [100, 200]));
do_check_false(Utils.checkStatus(250, msg, [100, 200]));
_("test with a range and a number");
do_check_true(Utils.checkStatus(50, msg, [[50, 100], 100]));
do_check_true(Utils.checkStatus(75, msg, [[50, 100], 100]));
do_check_true(Utils.checkStatus(100, msg, [[50, 100], 100]));
do_check_false(Utils.checkStatus(200, msg, [[50, 100], 100]));
_("test with a number and a range");
do_check_true(Utils.checkStatus(50, msg, [100, [50, 100]]));
do_check_true(Utils.checkStatus(75, msg, [100, [50, 100]]));
do_check_true(Utils.checkStatus(100, msg, [100, [50, 100]]));
do_check_false(Utils.checkStatus(200, msg, [100, [50, 100]]));
}
function _test_ensureStatus() {
_("test that ensureStatus throws exception when it should");
let except;
try {
Utils.ensureStatus(400, "msg", [200]);
} catch(e) {
except = e;
}
do_check_true(!!except);
}