Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | Cu.import("resource://gre/modules/FileUtils.jsm"); |
michael@0 | 5 | Cu.import("resource://services-sync/util.js"); |
michael@0 | 6 | |
michael@0 | 7 | function run_test() { |
michael@0 | 8 | initTestLogging(); |
michael@0 | 9 | run_next_test(); |
michael@0 | 10 | } |
michael@0 | 11 | |
michael@0 | 12 | add_test(function test_roundtrip() { |
michael@0 | 13 | _("Do a simple write of an array to json and read"); |
michael@0 | 14 | Utils.jsonSave("foo", {}, ["v1", "v2"], ensureThrows(function(error) { |
michael@0 | 15 | do_check_eq(error, null); |
michael@0 | 16 | |
michael@0 | 17 | Utils.jsonLoad("foo", {}, ensureThrows(function(val) { |
michael@0 | 18 | let foo = val; |
michael@0 | 19 | do_check_eq(typeof foo, "object"); |
michael@0 | 20 | do_check_eq(foo.length, 2); |
michael@0 | 21 | do_check_eq(foo[0], "v1"); |
michael@0 | 22 | do_check_eq(foo[1], "v2"); |
michael@0 | 23 | run_next_test(); |
michael@0 | 24 | })); |
michael@0 | 25 | })); |
michael@0 | 26 | }); |
michael@0 | 27 | |
michael@0 | 28 | add_test(function test_string() { |
michael@0 | 29 | _("Try saving simple strings"); |
michael@0 | 30 | Utils.jsonSave("str", {}, "hi", ensureThrows(function(error) { |
michael@0 | 31 | do_check_eq(error, null); |
michael@0 | 32 | |
michael@0 | 33 | Utils.jsonLoad("str", {}, ensureThrows(function(val) { |
michael@0 | 34 | let str = val; |
michael@0 | 35 | do_check_eq(typeof str, "string"); |
michael@0 | 36 | do_check_eq(str.length, 2); |
michael@0 | 37 | do_check_eq(str[0], "h"); |
michael@0 | 38 | do_check_eq(str[1], "i"); |
michael@0 | 39 | run_next_test(); |
michael@0 | 40 | })); |
michael@0 | 41 | })); |
michael@0 | 42 | }); |
michael@0 | 43 | |
michael@0 | 44 | add_test(function test_number() { |
michael@0 | 45 | _("Try saving a number"); |
michael@0 | 46 | Utils.jsonSave("num", {}, 42, ensureThrows(function(error) { |
michael@0 | 47 | do_check_eq(error, null); |
michael@0 | 48 | |
michael@0 | 49 | Utils.jsonLoad("num", {}, ensureThrows(function(val) { |
michael@0 | 50 | let num = val; |
michael@0 | 51 | do_check_eq(typeof num, "number"); |
michael@0 | 52 | do_check_eq(num, 42); |
michael@0 | 53 | run_next_test(); |
michael@0 | 54 | })); |
michael@0 | 55 | })); |
michael@0 | 56 | }); |
michael@0 | 57 | |
michael@0 | 58 | add_test(function test_nonexistent_file() { |
michael@0 | 59 | _("Try loading a non-existent file."); |
michael@0 | 60 | Utils.jsonLoad("non-existent", {}, ensureThrows(function(val) { |
michael@0 | 61 | do_check_eq(val, undefined); |
michael@0 | 62 | run_next_test(); |
michael@0 | 63 | })); |
michael@0 | 64 | }); |
michael@0 | 65 | |
michael@0 | 66 | add_test(function test_save_logging() { |
michael@0 | 67 | _("Verify that writes are logged."); |
michael@0 | 68 | let trace; |
michael@0 | 69 | Utils.jsonSave("log", {_log: {trace: function(msg) { trace = msg; }}}, |
michael@0 | 70 | "hi", ensureThrows(function () { |
michael@0 | 71 | do_check_true(!!trace); |
michael@0 | 72 | run_next_test(); |
michael@0 | 73 | })); |
michael@0 | 74 | }); |
michael@0 | 75 | |
michael@0 | 76 | add_test(function test_load_logging() { |
michael@0 | 77 | _("Verify that reads and read errors are logged."); |
michael@0 | 78 | |
michael@0 | 79 | // Write a file with some invalid JSON |
michael@0 | 80 | let filePath = "weave/log.json"; |
michael@0 | 81 | let file = FileUtils.getFile("ProfD", filePath.split("/"), true); |
michael@0 | 82 | let fos = Cc["@mozilla.org/network/file-output-stream;1"] |
michael@0 | 83 | .createInstance(Ci.nsIFileOutputStream); |
michael@0 | 84 | let flags = FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE |
michael@0 | 85 | | FileUtils.MODE_TRUNCATE; |
michael@0 | 86 | fos.init(file, flags, FileUtils.PERMS_FILE, fos.DEFER_OPEN); |
michael@0 | 87 | let stream = Cc["@mozilla.org/intl/converter-output-stream;1"] |
michael@0 | 88 | .createInstance(Ci.nsIConverterOutputStream); |
michael@0 | 89 | stream.init(fos, "UTF-8", 4096, 0x0000); |
michael@0 | 90 | stream.writeString("invalid json!"); |
michael@0 | 91 | stream.close(); |
michael@0 | 92 | |
michael@0 | 93 | let trace, debug; |
michael@0 | 94 | let obj = { |
michael@0 | 95 | _log: { |
michael@0 | 96 | trace: function(msg) { |
michael@0 | 97 | trace = msg; |
michael@0 | 98 | }, |
michael@0 | 99 | debug: function(msg) { |
michael@0 | 100 | debug = msg; |
michael@0 | 101 | } |
michael@0 | 102 | } |
michael@0 | 103 | }; |
michael@0 | 104 | Utils.jsonLoad("log", obj, ensureThrows(function(val) { |
michael@0 | 105 | do_check_true(!val); |
michael@0 | 106 | do_check_true(!!trace); |
michael@0 | 107 | do_check_true(!!debug); |
michael@0 | 108 | run_next_test(); |
michael@0 | 109 | })); |
michael@0 | 110 | }); |
michael@0 | 111 | |
michael@0 | 112 | add_task(function* test_undefined_callback() { |
michael@0 | 113 | yield Utils.jsonSave("foo", {}, ["v1", "v2"]); |
michael@0 | 114 | }); |