test_nsPrefOverrideMap.js (2307B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 const BOOL_TEST_PREF = "test.nsPrefOverrideMap.bool"; 6 const INT_TEST_PREF = "test.nsPrefOverrideMap.int"; 7 const STRING_TEST_PREF = "test.nsPrefOverrideMap.string"; 8 9 add_task(async function test_map() { 10 let map = Cc["@mozilla.org/pref-override-map;1"].createInstance( 11 Ci.nsIPrefOverrideMap 12 ); 13 14 // Set types of prefs. Map operations must respect them. 15 Services.prefs.setBoolPref(BOOL_TEST_PREF, false); 16 Services.prefs.setIntPref(INT_TEST_PREF, -1); 17 Services.prefs.setStringPref(STRING_TEST_PREF, "string"); 18 19 Assert.throws( 20 () => map.addEntry("not.a.real.pref", 0), 21 /NS_ERROR_DOM_NOT_FOUND_ERR/ 22 ); 23 Assert.throws( 24 () => map.getEntry("not.a.real.pref"), 25 /NS_ERROR_DOM_NOT_FOUND_ERR/ 26 ); 27 28 // Entry must be added to map before getEntry 29 Assert.throws(() => map.getEntry(BOOL_TEST_PREF), /NS_ERROR_ILLEGAL_VALUE/); 30 31 // Entry value can be changed. 32 map.addEntry(BOOL_TEST_PREF, false); 33 Assert.equal(map.getEntry(BOOL_TEST_PREF), false); 34 map.addEntry(BOOL_TEST_PREF, true); 35 Assert.equal(map.getEntry(BOOL_TEST_PREF), true); 36 37 // Cannot change pref value type 38 Assert.throws( 39 () => map.addEntry(BOOL_TEST_PREF, "wrong type"), 40 /TypeMismatchError/ 41 ); 42 Assert.throws( 43 () => map.addEntry(INT_TEST_PREF, "wrong type"), 44 /TypeMismatchError/ 45 ); 46 Assert.throws( 47 () => map.addEntry(STRING_TEST_PREF, true), 48 /TypeMismatchError/ 49 ); 50 51 // null value is allowed for any type 52 Assert.equal(map.getEntry(BOOL_TEST_PREF), true); 53 map.addEntry(BOOL_TEST_PREF, null); 54 Assert.equal(map.getEntry(BOOL_TEST_PREF), null); 55 map.addEntry(INT_TEST_PREF, null); 56 Assert.equal(map.getEntry(INT_TEST_PREF), null); 57 map.addEntry(STRING_TEST_PREF, null); 58 Assert.equal(map.getEntry(STRING_TEST_PREF), null); 59 60 // Type must be bool, number, string or null 61 Assert.throws(() => map.addEntry(BOOL_TEST_PREF, {}), /TypeMismatchError/); 62 Assert.equal(map.getEntry(BOOL_TEST_PREF), null); 63 64 // Cleanup 65 Services.prefs.clearUserPref(BOOL_TEST_PREF); 66 Services.prefs.clearUserPref(INT_TEST_PREF); 67 Services.prefs.clearUserPref(STRING_TEST_PREF); 68 });