services/sync/tests/unit/test_utils_json.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/services/sync/tests/unit/test_utils_json.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,114 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +Cu.import("resource://gre/modules/FileUtils.jsm");
     1.8 +Cu.import("resource://services-sync/util.js");
     1.9 +
    1.10 +function run_test() {
    1.11 +  initTestLogging();
    1.12 +  run_next_test();
    1.13 +}
    1.14 +
    1.15 +add_test(function test_roundtrip() {
    1.16 +  _("Do a simple write of an array to json and read");
    1.17 +  Utils.jsonSave("foo", {}, ["v1", "v2"], ensureThrows(function(error) {
    1.18 +    do_check_eq(error, null);
    1.19 +
    1.20 +    Utils.jsonLoad("foo", {}, ensureThrows(function(val) {
    1.21 +      let foo = val;
    1.22 +      do_check_eq(typeof foo, "object");
    1.23 +      do_check_eq(foo.length, 2);
    1.24 +      do_check_eq(foo[0], "v1");
    1.25 +      do_check_eq(foo[1], "v2");
    1.26 +      run_next_test();
    1.27 +    }));
    1.28 +  }));
    1.29 +});
    1.30 +
    1.31 +add_test(function test_string() {
    1.32 +  _("Try saving simple strings");
    1.33 +  Utils.jsonSave("str", {}, "hi", ensureThrows(function(error) {
    1.34 +    do_check_eq(error, null);
    1.35 +
    1.36 +    Utils.jsonLoad("str", {}, ensureThrows(function(val) {
    1.37 +      let str = val;
    1.38 +      do_check_eq(typeof str, "string");
    1.39 +      do_check_eq(str.length, 2);
    1.40 +      do_check_eq(str[0], "h");
    1.41 +      do_check_eq(str[1], "i");
    1.42 +      run_next_test();
    1.43 +    }));
    1.44 +  }));
    1.45 +});
    1.46 +
    1.47 +add_test(function test_number() {
    1.48 +  _("Try saving a number");
    1.49 +  Utils.jsonSave("num", {}, 42, ensureThrows(function(error) {
    1.50 +    do_check_eq(error, null);
    1.51 +
    1.52 +    Utils.jsonLoad("num", {}, ensureThrows(function(val) {
    1.53 +      let num = val;
    1.54 +      do_check_eq(typeof num, "number");
    1.55 +      do_check_eq(num, 42);
    1.56 +      run_next_test();
    1.57 +    }));
    1.58 +  }));
    1.59 +});
    1.60 +
    1.61 +add_test(function test_nonexistent_file() {
    1.62 +  _("Try loading a non-existent file.");
    1.63 +  Utils.jsonLoad("non-existent", {}, ensureThrows(function(val) {
    1.64 +    do_check_eq(val, undefined);
    1.65 +    run_next_test();
    1.66 +  }));
    1.67 +});
    1.68 +
    1.69 +add_test(function test_save_logging() {
    1.70 +  _("Verify that writes are logged.");
    1.71 +  let trace;
    1.72 +  Utils.jsonSave("log", {_log: {trace: function(msg) { trace = msg; }}},
    1.73 +                       "hi", ensureThrows(function () {
    1.74 +    do_check_true(!!trace);
    1.75 +    run_next_test();
    1.76 +  }));
    1.77 +});
    1.78 +
    1.79 +add_test(function test_load_logging() {
    1.80 +  _("Verify that reads and read errors are logged.");
    1.81 +
    1.82 +  // Write a file with some invalid JSON
    1.83 +  let filePath = "weave/log.json";
    1.84 +  let file = FileUtils.getFile("ProfD", filePath.split("/"), true);
    1.85 +  let fos = Cc["@mozilla.org/network/file-output-stream;1"]
    1.86 +              .createInstance(Ci.nsIFileOutputStream);
    1.87 +  let flags = FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE
    1.88 +              | FileUtils.MODE_TRUNCATE;
    1.89 +  fos.init(file, flags, FileUtils.PERMS_FILE, fos.DEFER_OPEN);
    1.90 +  let stream = Cc["@mozilla.org/intl/converter-output-stream;1"]
    1.91 +                 .createInstance(Ci.nsIConverterOutputStream);
    1.92 +  stream.init(fos, "UTF-8", 4096, 0x0000);
    1.93 +  stream.writeString("invalid json!");
    1.94 +  stream.close();
    1.95 +
    1.96 +  let trace, debug;
    1.97 +  let obj = {
    1.98 +    _log: {
    1.99 +      trace: function(msg) {
   1.100 +        trace = msg;
   1.101 +      },
   1.102 +      debug: function(msg) {
   1.103 +        debug = msg;
   1.104 +      }
   1.105 +    }
   1.106 +  };
   1.107 +  Utils.jsonLoad("log", obj, ensureThrows(function(val) {
   1.108 +    do_check_true(!val);
   1.109 +    do_check_true(!!trace);
   1.110 +    do_check_true(!!debug);
   1.111 +    run_next_test();
   1.112 +  }));
   1.113 +});
   1.114 +
   1.115 +add_task(function* test_undefined_callback() {
   1.116 +  yield Utils.jsonSave("foo", {}, ["v1", "v2"]);
   1.117 +});

mercurial