Merge services-central into mozilla-central

This commit is contained in:
Gregory Szorc 2012-05-15 09:39:11 -07:00
Родитель 3abd68f5a0 c4ebefe0f4
Коммит 98a675f363
8 изменённых файлов: 142 добавлений и 109 удалений

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

@ -127,6 +127,19 @@ function readBytesFromInputStream(inputStream, count) {
return new BinaryInputStream(inputStream).readBytes(count);
}
/*
* Ensure exceptions from inside callbacks leads to test failures.
*/
function ensureThrows(func) {
return function() {
try {
func.apply(this, arguments);
} catch (ex) {
do_throw(ex);
}
};
}
/**
* Proxy auth helpers.
*/

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

@ -1,16 +1,18 @@
_("Make sure json saves and loads from disk");
Cu.import("resource://services-sync/util.js");
Cu.import("resource://services-sync/constants.js");
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
Cu.import("resource://gre/modules/FileUtils.jsm");
Cu.import("resource://services-common/utils.js");
function run_test() {
initTestLogging();
run_next_test();
}
add_test(function test_roundtrip() {
_("Do a simple write of an array to json and read");
Utils.jsonSave("foo", {}, ["v1", "v2"], ensureThrows(function() {
Utils.jsonLoad("foo", {}, ensureThrows(function(val) {
CommonUtils.jsonSave("foo", {}, ["v1", "v2"], ensureThrows(function() {
CommonUtils.jsonLoad("foo", {}, ensureThrows(function(val) {
let foo = val;
do_check_eq(typeof foo, "object");
do_check_eq(foo.length, 2);
@ -23,8 +25,8 @@ add_test(function test_roundtrip() {
add_test(function test_string() {
_("Try saving simple strings");
Utils.jsonSave("str", {}, "hi", ensureThrows(function() {
Utils.jsonLoad("str", {}, ensureThrows(function(val) {
CommonUtils.jsonSave("str", {}, "hi", ensureThrows(function() {
CommonUtils.jsonLoad("str", {}, ensureThrows(function(val) {
let str = val;
do_check_eq(typeof str, "string");
do_check_eq(str.length, 2);
@ -37,8 +39,8 @@ add_test(function test_string() {
add_test(function test_number() {
_("Try saving a number");
Utils.jsonSave("num", {}, 42, ensureThrows(function() {
Utils.jsonLoad("num", {}, ensureThrows(function(val) {
CommonUtils.jsonSave("num", {}, 42, ensureThrows(function() {
CommonUtils.jsonLoad("num", {}, ensureThrows(function(val) {
let num = val;
do_check_eq(typeof num, "number");
do_check_eq(num, 42);
@ -49,17 +51,17 @@ add_test(function test_number() {
add_test(function test_nonexistent_file() {
_("Try loading a non-existent file.");
Utils.jsonLoad("non-existent", {}, ensureThrows(function(val) {
CommonUtils.jsonLoad("non-existent", {}, ensureThrows(function(val) {
do_check_eq(val, undefined);
run_next_test();
}));
}));
});
add_test(function test_save_logging() {
_("Verify that writes are logged.");
let trace;
Utils.jsonSave("log", {_log: {trace: function(msg) { trace = msg; }}},
"hi", ensureThrows(function () {
CommonUtils.jsonSave("log", {_log: {trace: function(msg) { trace = msg; }}},
"hi", ensureThrows(function () {
do_check_true(!!trace);
run_next_test();
}));
@ -69,7 +71,7 @@ add_test(function test_load_logging() {
_("Verify that reads and read errors are logged.");
// Write a file with some invalid JSON
let filePath = "weave/log.json";
let filePath = "log.json";
let file = FileUtils.getFile("ProfD", filePath.split("/"), true);
let fos = Cc["@mozilla.org/network/file-output-stream;1"]
.createInstance(Ci.nsIFileOutputStream);
@ -83,10 +85,18 @@ add_test(function test_load_logging() {
stream.close();
let trace, debug;
Utils.jsonLoad("log",
{_log: {trace: function(msg) { trace = msg; },
debug: function(msg) { debug = msg; }}},
ensureThrows(function(val) {
let obj = {
_log: {
trace: function(msg) {
trace = msg;
},
debug: function(msg) {
debug = msg;
}
}
};
CommonUtils.jsonLoad("log", obj, ensureThrows(function(val) {
do_check_true(!val);
do_check_true(!!trace);
do_check_true(!!debug);
run_next_test();

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

@ -7,6 +7,7 @@ tail =
[test_utils_atob.js]
[test_utils_encodeBase32.js]
[test_utils_json.js]
[test_utils_makeURI.js]
[test_utils_namedTimer.js]
[test_utils_stackTrace.js]

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

@ -6,6 +6,8 @@ const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
const EXPORTED_SYMBOLS = ["CommonUtils"];
Cu.import("resource://gre/modules/FileUtils.jsm");
Cu.import("resource://gre/modules/NetUtil.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://services-common/log4moz.js");
@ -300,6 +302,87 @@ let CommonUtils = {
let over = len % 4;
return over ? atob(b64.substr(0, len - over)) : atob(b64);
},
/**
* Load a JSON file from disk in the profile directory.
*
* @param filePath
* JSON file path load from profile. Loaded file will be
* <profile>/<filePath>.json. i.e. Do not specify the ".json"
* extension.
* @param that
* Object to use for logging and "this" for callback.
* @param callback
* Function to process json object as its first argument. If the file
* could not be loaded, the first argument will be undefined.
*/
jsonLoad: function jsonLoad(filePath, that, callback) {
let path = filePath + ".json";
if (that._log) {
that._log.trace("Loading json from disk: " + filePath);
}
let file = FileUtils.getFile("ProfD", path.split("/"), true);
if (!file.exists()) {
callback.call(that);
return;
}
let channel = NetUtil.newChannel(file);
channel.contentType = "application/json";
NetUtil.asyncFetch(channel, function (is, result) {
if (!Components.isSuccessCode(result)) {
callback.call(that);
return;
}
let string = NetUtil.readInputStreamToString(is, is.available());
is.close();
let json;
try {
json = JSON.parse(string);
} catch (ex) {
if (that._log) {
that._log.debug("Failed to load json: " +
CommonUtils.exceptionStr(ex));
}
}
callback.call(that, json);
});
},
/**
* Save a json-able object to disk in the profile directory.
*
* @param filePath
* JSON file path save to <filePath>.json
* @param that
* Object to use for logging and "this" for callback
* @param obj
* Function to provide json-able object to save. If this isn't a
* function, it'll be used as the object to make a json string.
* @param callback
* Function called when the write has been performed. Optional.
*/
jsonSave: function jsonSave(filePath, that, obj, callback) {
let path = filePath + ".json";
if (that._log) {
that._log.trace("Saving json to disk: " + path);
}
let file = FileUtils.getFile("ProfD", path.split("/"), true);
let json = typeof obj == "function" ? obj.call(that) : obj;
let out = JSON.stringify(json);
let fos = FileUtils.openSafeFileOutputStream(file);
let is = this._utf8Converter.convertToInputStream(out);
NetUtil.asyncCopy(is, fos, function (result) {
if (typeof callback == "function") {
callback.call(that);
}
});
},
};
XPCOMUtils.defineLazyGetter(CommonUtils, "_utf8Converter", function() {

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

@ -357,77 +357,12 @@ let Utils = {
return Utils.encodeKeyBase32(atob(encodedKey));
},
/**
* Load a json object from disk
*
* @param filePath
* Json file path load from weave/[filePath].json
* @param that
* Object to use for logging and "this" for callback
* @param callback
* Function to process json object as its first parameter
*/
jsonLoad: function Utils_jsonLoad(filePath, that, callback) {
filePath = "weave/" + filePath + ".json";
if (that._log)
that._log.trace("Loading json from disk: " + filePath);
let file = FileUtils.getFile("ProfD", filePath.split("/"), true);
if (!file.exists()) {
callback.call(that);
return;
}
let channel = NetUtil.newChannel(file);
channel.contentType = "application/json";
NetUtil.asyncFetch(channel, function (is, result) {
if (!Components.isSuccessCode(result)) {
callback.call(that);
return;
}
let string = NetUtil.readInputStreamToString(is, is.available());
is.close();
let json;
try {
json = JSON.parse(string);
} catch (ex) {
if (that._log)
that._log.debug("Failed to load json: " + Utils.exceptionStr(ex));
}
callback.call(that, json);
});
jsonLoad: function jsonLoad(path, that, callback) {
CommonUtils.jsonLoad("weave/" + path, that, callback);
},
/**
* Save a json-able object to disk
*
* @param filePath
* Json file path save to weave/[filePath].json
* @param that
* Object to use for logging and "this" for callback
* @param obj
* Function to provide json-able object to save. If this isn't a
* function, it'll be used as the object to make a json string.
* @param callback
* Function called when the write has been performed. Optional.
*/
jsonSave: function Utils_jsonSave(filePath, that, obj, callback) {
filePath = "weave/" + filePath + ".json";
if (that._log)
that._log.trace("Saving json to disk: " + filePath);
let file = FileUtils.getFile("ProfD", filePath.split("/"), true);
let json = typeof obj == "function" ? obj.call(that) : obj;
let out = JSON.stringify(json);
let fos = FileUtils.openSafeFileOutputStream(file);
let is = this._utf8Converter.convertToInputStream(out);
NetUtil.asyncCopy(is, fos, function (result) {
if (typeof callback == "function") {
callback.call(that);
}
});
jsonSave: function jsonSave(path, that, obj, callback) {
CommonUtils.jsonSave("weave/" + path, that, obj, callback);
},
getIcon: function(iconUri, defaultIcon) {

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

@ -251,20 +251,6 @@ function SyncTestingInfrastructure(username, password, syncKey) {
this.fakeCryptoService = new FakeCryptoService();
}
/*
* Ensure exceptions from inside callbacks leads to test failures.
*/
function ensureThrows(func) {
return function() {
try {
func.apply(this, arguments);
} catch (ex) {
do_throw(ex);
}
};
}
_("Setting the identity for passphrase");
Cu.import("resource://services-sync/identity.js");

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

@ -18,7 +18,6 @@ tail =
[test_utils_keyEncoding.js]
[test_utils_getErrorString.js]
[test_utils_getIcon.js]
[test_utils_json.js]
[test_utils_lazyStrings.js]
[test_utils_lock.js]
[test_utils_makeGUID.js]

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

@ -90,6 +90,13 @@ const SYNC_WIPE_SERVER = "wipe-server";
const SYNC_RESET_CLIENT = "reset-client";
const SYNC_START_OVER = "start-over";
const OBSERVER_TOPICS = ["weave:engine:start-tracking",
"weave:engine:stop-tracking",
"weave:service:sync:finish",
"weave:service:sync:error",
"sessionstore-windows-restored",
"private-browsing"];
let TPS =
{
_waitingForSync: false,
@ -184,6 +191,9 @@ let TPS =
},
quit: function () {
OBSERVER_TOPICS.forEach(function(topic) {
Services.obs.removeObserver(this, topic);
}, this);
Logger.close();
this.goQuitApplication();
},
@ -565,13 +575,9 @@ let TPS =
*/
_executeTestPhase: function _executeTestPhase(file, phase, settings) {
try {
// TODO Unregister observers on unload (bug 721283).
Services.obs.addObserver(this, "weave:engine:start-tracking", true);
Services.obs.addObserver(this, "weave:engine:stop-tracking", true);
Services.obs.addObserver(this, "weave:service:sync:finish", true);
Services.obs.addObserver(this, "weave:service:sync:error", true);
Services.obs.addObserver(this, "sessionstore-windows-restored", true);
Services.obs.addObserver(this, "private-browsing", true);
OBSERVER_TOPICS.forEach(function(topic) {
Services.obs.addObserver(this, topic, true);
}, this);
// parse the test file
Services.scriptloader.loadSubScript(file, this);