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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4 "use strict";
michael@0 5
michael@0 6 const prefs = require("sdk/preferences/service");
michael@0 7 const Branch = prefs.Branch;
michael@0 8 const { Cc, Ci, Cu } = require("chrome");
michael@0 9 const BundleService = Cc["@mozilla.org/intl/stringbundle;1"].getService(Ci.nsIStringBundleService);
michael@0 10
michael@0 11 const specialChars = "!@#$%^&*()_-=+[]{}~`\'\"<>,./?;:";
michael@0 12
michael@0 13 exports.testReset = function(assert) {
michael@0 14 prefs.reset("test_reset_pref");
michael@0 15 assert.equal(prefs.has("test_reset_pref"), false);
michael@0 16 assert.equal(prefs.isSet("test_reset_pref"), false);
michael@0 17 prefs.set("test_reset_pref", 5);
michael@0 18 assert.equal(prefs.has("test_reset_pref"), true);
michael@0 19 assert.equal(prefs.isSet("test_reset_pref"), true);
michael@0 20 assert.equal(prefs.keys("test_reset_pref").toString(), "test_reset_pref");
michael@0 21 };
michael@0 22
michael@0 23 exports.testGetAndSet = function(assert) {
michael@0 24 let svc = Cc["@mozilla.org/preferences-service;1"].
michael@0 25 getService(Ci.nsIPrefService).
michael@0 26 getBranch(null);
michael@0 27 svc.setCharPref("test_set_get_pref", "a normal string");
michael@0 28 assert.equal(prefs.get("test_set_get_pref"), "a normal string",
michael@0 29 "preferences-service should read from " +
michael@0 30 "application-wide preferences service");
michael@0 31
michael@0 32 prefs.set("test_set_get_pref.integer", 1);
michael@0 33 assert.equal(prefs.get("test_set_get_pref.integer"), 1,
michael@0 34 "set/get integer preference should work");
michael@0 35
michael@0 36 assert.equal(
michael@0 37 prefs.keys("test_set_get_pref").sort().toString(),
michael@0 38 ["test_set_get_pref.integer","test_set_get_pref"].sort().toString());
michael@0 39
michael@0 40 prefs.set("test_set_get_number_pref", 42);
michael@0 41 assert.throws(
michael@0 42 function() { prefs.set("test_set_get_number_pref", 3.14159); },
michael@0 43 /cannot store non-integer number: 3.14159/,
michael@0 44 "setting a float preference should raise an error"
michael@0 45 );
michael@0 46 assert.equal(prefs.get("test_set_get_number_pref"), 42,
michael@0 47 "bad-type write attempt should not overwrite");
michael@0 48
michael@0 49 // 0x80000000 (no), 0x7fffffff (yes), -0x80000000 (yes), -0x80000001 (no)
michael@0 50 assert.throws(
michael@0 51 function() { prefs.set("test_set_get_number_pref", Math.pow(2, 31)); },
michael@0 52 new RegExp("you cannot set the test_set_get_number_pref pref to the number " +
michael@0 53 "2147483648, as number pref values must be in the signed 32\\-bit " +
michael@0 54 "integer range \\-\\(2\\^31\\) to 2\\^31\\-1. To store numbers outside that " +
michael@0 55 "range, store them as strings."),
michael@0 56 "setting an int pref outside the range -(2^31) to 2^31-1 shouldn't work"
michael@0 57 );
michael@0 58 assert.equal(prefs.get("test_set_get_number_pref"), 42,
michael@0 59 "out-of-range write attempt should not overwrite 1");
michael@0 60 prefs.set("test_set_get_number_pref", Math.pow(2, 31)-1);
michael@0 61 assert.equal(prefs.get("test_set_get_number_pref"), 0x7fffffff,
michael@0 62 "in-range write attempt should work 1");
michael@0 63 prefs.set("test_set_get_number_pref", -Math.pow(2, 31));
michael@0 64 assert.equal(prefs.get("test_set_get_number_pref"), -0x80000000,
michael@0 65 "in-range write attempt should work 2");
michael@0 66 assert.throws(
michael@0 67 function() { prefs.set("test_set_get_number_pref", -0x80000001); },
michael@0 68 new RegExp("you cannot set the test_set_get_number_pref pref to the number " +
michael@0 69 "\\-2147483649, as number pref values must be in the signed 32-bit " +
michael@0 70 "integer range \\-\\(2\\^31\\) to 2\\^31\\-1. To store numbers outside that " +
michael@0 71 "range, store them as strings."),
michael@0 72 "setting an int pref outside the range -(2^31) to 2^31-1 shouldn't work"
michael@0 73 );
michael@0 74 assert.equal(prefs.get("test_set_get_number_pref"), -0x80000000,
michael@0 75 "out-of-range write attempt should not overwrite 2");
michael@0 76
michael@0 77
michael@0 78 prefs.set("test_set_get_pref.string", "foo");
michael@0 79 assert.equal(prefs.get("test_set_get_pref.string"), "foo",
michael@0 80 "set/get string preference should work");
michael@0 81
michael@0 82 prefs.set("test_set_get_pref.boolean", true);
michael@0 83 assert.equal(prefs.get("test_set_get_pref.boolean"), true,
michael@0 84 "set/get boolean preference should work");
michael@0 85
michael@0 86 prefs.set("test_set_get_unicode_pref", String.fromCharCode(960));
michael@0 87 assert.equal(prefs.get("test_set_get_unicode_pref"),
michael@0 88 String.fromCharCode(960),
michael@0 89 "set/get unicode preference should work");
michael@0 90
michael@0 91 var unsupportedValues = [null, [], undefined];
michael@0 92 unsupportedValues.forEach(
michael@0 93 function(value) {
michael@0 94 assert.throws(
michael@0 95 function() { prefs.set("test_set_pref", value); },
michael@0 96 new RegExp("can't set pref test_set_pref to value '" + value + "'; " +
michael@0 97 "it isn't a string, integer, or boolean"),
michael@0 98 "Setting a pref to " + uneval(value) + " should raise error"
michael@0 99 );
michael@0 100 });
michael@0 101 };
michael@0 102
michael@0 103 exports.testPrefClass = function(assert) {
michael@0 104 var branch = Branch("test_foo");
michael@0 105
michael@0 106 assert.equal(branch.test, undefined, "test_foo.test is undefined");
michael@0 107 branch.test = true;
michael@0 108 assert.equal(branch.test, true, "test_foo.test is true");
michael@0 109 delete branch.test;
michael@0 110 assert.equal(branch.test, undefined, "test_foo.test is undefined");
michael@0 111 };
michael@0 112
michael@0 113 exports.testGetSetLocalized = function(assert) {
michael@0 114 let prefName = "general.useragent.locale";
michael@0 115
michael@0 116 // Ensure that "general.useragent.locale" is a 'localized' pref
michael@0 117 let bundleURL = "chrome://global/locale/intl.properties";
michael@0 118 prefs.setLocalized(prefName, bundleURL);
michael@0 119
michael@0 120 // Fetch the expected value directly from the property file
michael@0 121 let expectedValue = BundleService.createBundle(bundleURL).
michael@0 122 GetStringFromName(prefName).
michael@0 123 toLowerCase();
michael@0 124
michael@0 125 assert.equal(prefs.getLocalized(prefName).toLowerCase(),
michael@0 126 expectedValue,
michael@0 127 "get localized preference");
michael@0 128
michael@0 129 // Undo our modification
michael@0 130 prefs.reset(prefName);
michael@0 131 }
michael@0 132
michael@0 133 // TEST: setting and getting preferences with special characters work
michael@0 134 exports.testSpecialChars = function(assert) {
michael@0 135 let chars = specialChars.split('');
michael@0 136 const ROOT = "test.";
michael@0 137
michael@0 138 chars.forEach(function(char) {
michael@0 139 let rand = Math.random() + "";
michael@0 140 prefs.set(ROOT+char, rand);
michael@0 141 assert.equal(prefs.get(ROOT+char), rand, "setting pref with a name that is a special char, " + char + ", worked!");
michael@0 142 });
michael@0 143 };
michael@0 144
michael@0 145 require('sdk/test').run(exports);

mercurial