1
_("Make sure json saves and loads from disk");
2
Cu.import("resource://services-sync/util.js");
3
Cu.import("resource://services-sync/constants.js");
7
Utils.asyncChain(function (next) {
9
_("Do a simple write of an array to json and read");
10
Utils.jsonSave("foo", {}, ["v1", "v2"], ensureThrows(function() {
11
Utils.jsonLoad("foo", {}, ensureThrows(function(val) {
13
do_check_eq(typeof foo, "object");
14
do_check_eq(foo.length, 2);
15
do_check_eq(foo[0], "v1");
16
do_check_eq(foo[1], "v2");
23
_("Try saving simple strings");
24
Utils.jsonSave("str", {}, "hi", ensureThrows(function() {
25
Utils.jsonLoad("str", {}, ensureThrows(function(val) {
27
do_check_eq(typeof str, "string");
28
do_check_eq(str.length, 2);
29
do_check_eq(str[0], "h");
30
do_check_eq(str[1], "i");
37
_("Try saving a number");
38
Utils.jsonSave("num", {}, 42, ensureThrows(function() {
39
Utils.jsonLoad("num", {}, ensureThrows(function(val) {
41
do_check_eq(typeof num, "number");
49
_("Try loading a non-existent file.");
50
Utils.jsonLoad("non-existent", {}, ensureThrows(function(val) {
51
do_check_eq(val, undefined);
57
_("Verify that writes are logged.");
59
Utils.jsonSave("log", {_log: {trace: function(msg) { trace = msg; }}},
60
"hi", ensureThrows(function () {
61
do_check_true(!!trace);
67
_("Verify that reads and read errors are logged.");
69
// Write a file with some invalid JSON
70
let file = Utils.getProfileFile({ autoCreate: true,
71
path: "weave/log.json" });
72
let fos = Cc["@mozilla.org/network/file-output-stream;1"]
73
.createInstance(Ci.nsIFileOutputStream);
74
fos.init(file, MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE, PERMS_FILE,
76
let stream = Cc["@mozilla.org/intl/converter-output-stream;1"]
77
.createInstance(Ci.nsIConverterOutputStream);
78
stream.init(fos, "UTF-8", 4096, 0x0000);
79
stream.writeString("invalid json!");
84
{_log: {trace: function(msg) { trace = msg; },
85
debug: function(msg) { debug = msg; }}},
86
ensureThrows(function(val) {
87
do_check_true(!!trace);
88
do_check_true(!!debug);
92
}, do_test_finished)();