addon-sdk/source/test/test-preferences-service.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-preferences-service.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,145 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +"use strict";
     1.8 +
     1.9 +const prefs = require("sdk/preferences/service");
    1.10 +const Branch = prefs.Branch;
    1.11 +const { Cc, Ci, Cu } = require("chrome");
    1.12 +const BundleService = Cc["@mozilla.org/intl/stringbundle;1"].getService(Ci.nsIStringBundleService);
    1.13 +
    1.14 +const specialChars = "!@#$%^&*()_-=+[]{}~`\'\"<>,./?;:";
    1.15 +
    1.16 +exports.testReset = function(assert) {
    1.17 +  prefs.reset("test_reset_pref");
    1.18 +  assert.equal(prefs.has("test_reset_pref"), false);
    1.19 +  assert.equal(prefs.isSet("test_reset_pref"), false);
    1.20 +  prefs.set("test_reset_pref", 5);
    1.21 +  assert.equal(prefs.has("test_reset_pref"), true);
    1.22 +  assert.equal(prefs.isSet("test_reset_pref"), true);
    1.23 +  assert.equal(prefs.keys("test_reset_pref").toString(), "test_reset_pref");
    1.24 +};
    1.25 +
    1.26 +exports.testGetAndSet = function(assert) {
    1.27 +  let svc = Cc["@mozilla.org/preferences-service;1"].
    1.28 +            getService(Ci.nsIPrefService).
    1.29 +            getBranch(null);
    1.30 +  svc.setCharPref("test_set_get_pref", "a normal string");
    1.31 +  assert.equal(prefs.get("test_set_get_pref"), "a normal string",
    1.32 +                   "preferences-service should read from " +
    1.33 +                   "application-wide preferences service");
    1.34 +
    1.35 +  prefs.set("test_set_get_pref.integer", 1);
    1.36 +  assert.equal(prefs.get("test_set_get_pref.integer"), 1,
    1.37 +                   "set/get integer preference should work");
    1.38 +
    1.39 +  assert.equal(
    1.40 +      prefs.keys("test_set_get_pref").sort().toString(),
    1.41 +      ["test_set_get_pref.integer","test_set_get_pref"].sort().toString());
    1.42 +
    1.43 +  prefs.set("test_set_get_number_pref", 42);
    1.44 +  assert.throws(
    1.45 +    function() { prefs.set("test_set_get_number_pref", 3.14159); },
    1.46 +    /cannot store non-integer number: 3.14159/,
    1.47 +    "setting a float preference should raise an error"
    1.48 +  );
    1.49 +  assert.equal(prefs.get("test_set_get_number_pref"), 42,
    1.50 +                   "bad-type write attempt should not overwrite");
    1.51 +
    1.52 +  // 0x80000000 (no), 0x7fffffff (yes), -0x80000000 (yes), -0x80000001 (no)
    1.53 +  assert.throws(
    1.54 +    function() { prefs.set("test_set_get_number_pref", Math.pow(2, 31)); },
    1.55 +    new RegExp("you cannot set the test_set_get_number_pref pref to the number " +
    1.56 +     "2147483648, as number pref values must be in the signed 32\\-bit " +
    1.57 +     "integer range \\-\\(2\\^31\\) to 2\\^31\\-1.  To store numbers outside that " +
    1.58 +     "range, store them as strings."),
    1.59 +    "setting an int pref outside the range -(2^31) to 2^31-1 shouldn't work"
    1.60 +  );
    1.61 +  assert.equal(prefs.get("test_set_get_number_pref"), 42,
    1.62 +                   "out-of-range write attempt should not overwrite 1");
    1.63 +  prefs.set("test_set_get_number_pref", Math.pow(2, 31)-1);
    1.64 +  assert.equal(prefs.get("test_set_get_number_pref"), 0x7fffffff,
    1.65 +                   "in-range write attempt should work 1");
    1.66 +  prefs.set("test_set_get_number_pref", -Math.pow(2, 31));
    1.67 +  assert.equal(prefs.get("test_set_get_number_pref"), -0x80000000,
    1.68 +                   "in-range write attempt should work 2");
    1.69 +  assert.throws(
    1.70 +    function() { prefs.set("test_set_get_number_pref", -0x80000001); },
    1.71 +    new RegExp("you cannot set the test_set_get_number_pref pref to the number " +
    1.72 +     "\\-2147483649, as number pref values must be in the signed 32-bit " +
    1.73 +     "integer range \\-\\(2\\^31\\) to 2\\^31\\-1.  To store numbers outside that " +
    1.74 +     "range, store them as strings."),
    1.75 +    "setting an int pref outside the range -(2^31) to 2^31-1 shouldn't work"
    1.76 +  );
    1.77 +  assert.equal(prefs.get("test_set_get_number_pref"), -0x80000000,
    1.78 +                   "out-of-range write attempt should not overwrite 2");
    1.79 +
    1.80 +
    1.81 +  prefs.set("test_set_get_pref.string", "foo");
    1.82 +  assert.equal(prefs.get("test_set_get_pref.string"), "foo",
    1.83 +                   "set/get string preference should work");
    1.84 +
    1.85 +  prefs.set("test_set_get_pref.boolean", true);
    1.86 +  assert.equal(prefs.get("test_set_get_pref.boolean"), true,
    1.87 +                   "set/get boolean preference should work");
    1.88 +
    1.89 +  prefs.set("test_set_get_unicode_pref", String.fromCharCode(960));
    1.90 +  assert.equal(prefs.get("test_set_get_unicode_pref"),
    1.91 +                   String.fromCharCode(960),
    1.92 +                   "set/get unicode preference should work");
    1.93 +
    1.94 +  var unsupportedValues = [null, [], undefined];
    1.95 +  unsupportedValues.forEach(
    1.96 +    function(value) {
    1.97 +      assert.throws(
    1.98 +        function() { prefs.set("test_set_pref", value); },
    1.99 +        new RegExp("can't set pref test_set_pref to value '" + value + "'; " +
   1.100 +         "it isn't a string, integer, or boolean"),
   1.101 +        "Setting a pref to " + uneval(value) + " should raise error"
   1.102 +      );
   1.103 +    });
   1.104 +};
   1.105 +
   1.106 +exports.testPrefClass = function(assert) {
   1.107 +  var branch = Branch("test_foo");
   1.108 +
   1.109 +  assert.equal(branch.test, undefined, "test_foo.test is undefined");
   1.110 +  branch.test = true;
   1.111 +  assert.equal(branch.test, true, "test_foo.test is true");
   1.112 +  delete branch.test;
   1.113 +  assert.equal(branch.test, undefined, "test_foo.test is undefined");
   1.114 +};
   1.115 +
   1.116 +exports.testGetSetLocalized = function(assert) {
   1.117 +  let prefName = "general.useragent.locale";
   1.118 +
   1.119 +  // Ensure that "general.useragent.locale" is a 'localized' pref
   1.120 +  let bundleURL = "chrome://global/locale/intl.properties";
   1.121 +  prefs.setLocalized(prefName, bundleURL);
   1.122 +
   1.123 +  // Fetch the expected value directly from the property file
   1.124 +  let expectedValue = BundleService.createBundle(bundleURL).
   1.125 +    GetStringFromName(prefName).
   1.126 +    toLowerCase();
   1.127 +
   1.128 +  assert.equal(prefs.getLocalized(prefName).toLowerCase(),
   1.129 +                   expectedValue,
   1.130 +                   "get localized preference");
   1.131 +
   1.132 +  // Undo our modification
   1.133 +  prefs.reset(prefName);
   1.134 +}
   1.135 +
   1.136 +// TEST: setting and getting preferences with special characters work
   1.137 +exports.testSpecialChars = function(assert) {
   1.138 +  let chars = specialChars.split('');
   1.139 +  const ROOT = "test.";
   1.140 +
   1.141 +  chars.forEach(function(char) {
   1.142 +    let rand = Math.random() + "";
   1.143 +    prefs.set(ROOT+char, rand);
   1.144 +    assert.equal(prefs.get(ROOT+char), rand, "setting pref with a name that is a special char, " + char + ", worked!");
   1.145 +  });
   1.146 +};
   1.147 +
   1.148 +require('sdk/test').run(exports);

mercurial