michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict"; michael@0: michael@0: const prefs = require("sdk/preferences/service"); michael@0: const Branch = prefs.Branch; michael@0: const { Cc, Ci, Cu } = require("chrome"); michael@0: const BundleService = Cc["@mozilla.org/intl/stringbundle;1"].getService(Ci.nsIStringBundleService); michael@0: michael@0: const specialChars = "!@#$%^&*()_-=+[]{}~`\'\"<>,./?;:"; michael@0: michael@0: exports.testReset = function(assert) { michael@0: prefs.reset("test_reset_pref"); michael@0: assert.equal(prefs.has("test_reset_pref"), false); michael@0: assert.equal(prefs.isSet("test_reset_pref"), false); michael@0: prefs.set("test_reset_pref", 5); michael@0: assert.equal(prefs.has("test_reset_pref"), true); michael@0: assert.equal(prefs.isSet("test_reset_pref"), true); michael@0: assert.equal(prefs.keys("test_reset_pref").toString(), "test_reset_pref"); michael@0: }; michael@0: michael@0: exports.testGetAndSet = function(assert) { michael@0: let svc = Cc["@mozilla.org/preferences-service;1"]. michael@0: getService(Ci.nsIPrefService). michael@0: getBranch(null); michael@0: svc.setCharPref("test_set_get_pref", "a normal string"); michael@0: assert.equal(prefs.get("test_set_get_pref"), "a normal string", michael@0: "preferences-service should read from " + michael@0: "application-wide preferences service"); michael@0: michael@0: prefs.set("test_set_get_pref.integer", 1); michael@0: assert.equal(prefs.get("test_set_get_pref.integer"), 1, michael@0: "set/get integer preference should work"); michael@0: michael@0: assert.equal( michael@0: prefs.keys("test_set_get_pref").sort().toString(), michael@0: ["test_set_get_pref.integer","test_set_get_pref"].sort().toString()); michael@0: michael@0: prefs.set("test_set_get_number_pref", 42); michael@0: assert.throws( michael@0: function() { prefs.set("test_set_get_number_pref", 3.14159); }, michael@0: /cannot store non-integer number: 3.14159/, michael@0: "setting a float preference should raise an error" michael@0: ); michael@0: assert.equal(prefs.get("test_set_get_number_pref"), 42, michael@0: "bad-type write attempt should not overwrite"); michael@0: michael@0: // 0x80000000 (no), 0x7fffffff (yes), -0x80000000 (yes), -0x80000001 (no) michael@0: assert.throws( michael@0: function() { prefs.set("test_set_get_number_pref", Math.pow(2, 31)); }, michael@0: new RegExp("you cannot set the test_set_get_number_pref pref to the number " + michael@0: "2147483648, as number pref values must be in the signed 32\\-bit " + michael@0: "integer range \\-\\(2\\^31\\) to 2\\^31\\-1. To store numbers outside that " + michael@0: "range, store them as strings."), michael@0: "setting an int pref outside the range -(2^31) to 2^31-1 shouldn't work" michael@0: ); michael@0: assert.equal(prefs.get("test_set_get_number_pref"), 42, michael@0: "out-of-range write attempt should not overwrite 1"); michael@0: prefs.set("test_set_get_number_pref", Math.pow(2, 31)-1); michael@0: assert.equal(prefs.get("test_set_get_number_pref"), 0x7fffffff, michael@0: "in-range write attempt should work 1"); michael@0: prefs.set("test_set_get_number_pref", -Math.pow(2, 31)); michael@0: assert.equal(prefs.get("test_set_get_number_pref"), -0x80000000, michael@0: "in-range write attempt should work 2"); michael@0: assert.throws( michael@0: function() { prefs.set("test_set_get_number_pref", -0x80000001); }, michael@0: new RegExp("you cannot set the test_set_get_number_pref pref to the number " + michael@0: "\\-2147483649, as number pref values must be in the signed 32-bit " + michael@0: "integer range \\-\\(2\\^31\\) to 2\\^31\\-1. To store numbers outside that " + michael@0: "range, store them as strings."), michael@0: "setting an int pref outside the range -(2^31) to 2^31-1 shouldn't work" michael@0: ); michael@0: assert.equal(prefs.get("test_set_get_number_pref"), -0x80000000, michael@0: "out-of-range write attempt should not overwrite 2"); michael@0: michael@0: michael@0: prefs.set("test_set_get_pref.string", "foo"); michael@0: assert.equal(prefs.get("test_set_get_pref.string"), "foo", michael@0: "set/get string preference should work"); michael@0: michael@0: prefs.set("test_set_get_pref.boolean", true); michael@0: assert.equal(prefs.get("test_set_get_pref.boolean"), true, michael@0: "set/get boolean preference should work"); michael@0: michael@0: prefs.set("test_set_get_unicode_pref", String.fromCharCode(960)); michael@0: assert.equal(prefs.get("test_set_get_unicode_pref"), michael@0: String.fromCharCode(960), michael@0: "set/get unicode preference should work"); michael@0: michael@0: var unsupportedValues = [null, [], undefined]; michael@0: unsupportedValues.forEach( michael@0: function(value) { michael@0: assert.throws( michael@0: function() { prefs.set("test_set_pref", value); }, michael@0: new RegExp("can't set pref test_set_pref to value '" + value + "'; " + michael@0: "it isn't a string, integer, or boolean"), michael@0: "Setting a pref to " + uneval(value) + " should raise error" michael@0: ); michael@0: }); michael@0: }; michael@0: michael@0: exports.testPrefClass = function(assert) { michael@0: var branch = Branch("test_foo"); michael@0: michael@0: assert.equal(branch.test, undefined, "test_foo.test is undefined"); michael@0: branch.test = true; michael@0: assert.equal(branch.test, true, "test_foo.test is true"); michael@0: delete branch.test; michael@0: assert.equal(branch.test, undefined, "test_foo.test is undefined"); michael@0: }; michael@0: michael@0: exports.testGetSetLocalized = function(assert) { michael@0: let prefName = "general.useragent.locale"; michael@0: michael@0: // Ensure that "general.useragent.locale" is a 'localized' pref michael@0: let bundleURL = "chrome://global/locale/intl.properties"; michael@0: prefs.setLocalized(prefName, bundleURL); michael@0: michael@0: // Fetch the expected value directly from the property file michael@0: let expectedValue = BundleService.createBundle(bundleURL). michael@0: GetStringFromName(prefName). michael@0: toLowerCase(); michael@0: michael@0: assert.equal(prefs.getLocalized(prefName).toLowerCase(), michael@0: expectedValue, michael@0: "get localized preference"); michael@0: michael@0: // Undo our modification michael@0: prefs.reset(prefName); michael@0: } michael@0: michael@0: // TEST: setting and getting preferences with special characters work michael@0: exports.testSpecialChars = function(assert) { michael@0: let chars = specialChars.split(''); michael@0: const ROOT = "test."; michael@0: michael@0: chars.forEach(function(char) { michael@0: let rand = Math.random() + ""; michael@0: prefs.set(ROOT+char, rand); michael@0: assert.equal(prefs.get(ROOT+char), rand, "setting pref with a name that is a special char, " + char + ", worked!"); michael@0: }); michael@0: }; michael@0: michael@0: require('sdk/test').run(exports);