test_parsePrefs.js (4221B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/licenses/publicdomain/ */ 3 4 "use strict"; 5 6 // // This undoes some of the configuration from 7 // Services.dirsvc 8 // .QueryInterface(Ci.nsIDirectoryService) 9 // .unregisterProvider(provider); 10 11 class PrefObserver { 12 constructor() { 13 this.events = []; 14 } 15 16 onStringPref(...args) { 17 // // What happens with an exception here? 18 // throw new Error(`foo ${args[1]}`); 19 this.events.push(["string", ...args]); 20 } 21 22 onIntPref(...args) { 23 this.events.push(["int", ...args]); 24 } 25 26 onBoolPref(...args) { 27 this.events.push(["bool", ...args]); 28 } 29 30 onError(message) { 31 this.events.push(["error", message]); 32 } 33 } 34 35 const TESTS = { 36 "data/testPrefSticky.js": [ 37 ["bool", "Default", "testPref.unsticky.bool", true, false, false], 38 ["bool", "Default", "testPref.sticky.bool", false, true, false], 39 ], 40 "data/testPrefStickyUser.js": [ 41 ["bool", "User", "testPref.sticky.bool", false, false, false], 42 ], 43 "data/testPrefLocked.js": [ 44 ["int", "Default", "testPref.unlocked.int", 333, false, false], 45 ["int", "Default", "testPref.locked.int", 444, false, true], 46 ], 47 // data/testPrefLockedUser is ASCII. 48 "data/testPrefLockedUser.js": [ 49 ["int", "User", "testPref.locked.int", 555, false, false], 50 ], 51 // data/testPref is ISO-8859. 52 "data/testPref.js": [ 53 ["bool", "User", "testPref.bool1", true, false, false], 54 ["bool", "User", "testPref.bool2", false, false, false], 55 ["int", "User", "testPref.int1", 23, false, false], 56 ["int", "User", "testPref.int2", -1236, false, false], 57 ["string", "User", "testPref.char1", "_testPref", false, false], 58 ["string", "User", "testPref.char2", "älskar", false, false], 59 ], 60 // data/testParsePrefs is data/testPref.js as UTF-8. 61 "data/testPrefUTF8.js": [ 62 ["bool", "User", "testPref.bool1", true, false, false], 63 ["bool", "User", "testPref.bool2", false, false, false], 64 ["int", "User", "testPref.int1", 23, false, false], 65 ["int", "User", "testPref.int2", -1236, false, false], 66 ["string", "User", "testPref.char1", "_testPref", false, false], 67 // Observe that this is the ISO-8859/Latin-1 encoding of the UTF-8 bytes. 68 // (Note that this source file is encoded as UTF-8.) This appears to just 69 // be how libpref handles this case. This test serves as documentation of 70 // this infelicity. 71 ["string", "User", "testPref.char2", "älskar", false, false], 72 ], 73 }; 74 75 add_task(async function test_success() { 76 for (let [path, expected] of Object.entries(TESTS)) { 77 let prefObserver = new PrefObserver(); 78 79 let prefsFile = do_get_file(path); 80 let data = await IOUtils.read(prefsFile.path); 81 82 Services.prefs.parsePrefsFromBuffer(data, prefObserver, path); 83 Assert.deepEqual( 84 prefObserver.events, 85 expected, 86 `Observations from ${path} are as expected` 87 ); 88 } 89 }); 90 91 add_task(async function test_exceptions() { 92 const { AddonTestUtils } = ChromeUtils.importESModule( 93 "resource://testing-common/AddonTestUtils.sys.mjs" 94 ); 95 96 let s = `user_pref("testPref.bool1", true); 97 user_pref("testPref.bool2", false); 98 user_pref("testPref.int1", 23); 99 user_pref("testPref.int2", -1236); 100 `; 101 102 let onErrorCount = 0; 103 let addPrefCount = 0; 104 105 let marker = "2fc599a1-433b-4de7-bd63-3e69c3bbad5b"; 106 let addPref = (...args) => { 107 addPrefCount += 1; 108 throw new Error(`${marker}${JSON.stringify(args)}${marker}`); 109 }; 110 111 let { messages } = await AddonTestUtils.promiseConsoleOutput(() => { 112 Services.prefs.parsePrefsFromBuffer(new TextEncoder().encode(s), { 113 onStringPref: addPref, 114 onIntPref: addPref, 115 onBoolPref: addPref, 116 onError(message) { 117 onErrorCount += 1; 118 console.error(message); 119 }, 120 }); 121 }); 122 123 Assert.equal(addPrefCount, 4); 124 Assert.equal(onErrorCount, 0); 125 126 // This is fragile but mercifully simple. 127 Assert.deepEqual( 128 messages.map(m => JSON.parse(m.message.split(marker)[1])), 129 [ 130 ["User", "testPref.bool1", true, false, false], 131 ["User", "testPref.bool2", false, false, false], 132 ["User", "testPref.int1", 23, false, false], 133 ["User", "testPref.int2", -1236, false, false], 134 ] 135 ); 136 });