Wed, 31 Dec 2014 06:09:35 +0100
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 | |
michael@0 | 5 | const PREF_INVALID = 0; |
michael@0 | 6 | const PREF_BOOL = 128; |
michael@0 | 7 | const PREF_INT = 64; |
michael@0 | 8 | const PREF_STRING = 32; |
michael@0 | 9 | |
michael@0 | 10 | const MAX_PREF_LENGTH = 1 * 1024 * 1024; |
michael@0 | 11 | |
michael@0 | 12 | function makeList(a) |
michael@0 | 13 | { |
michael@0 | 14 | var o = {}; |
michael@0 | 15 | for(var i=0; i<a.length; i++) |
michael@0 | 16 | { |
michael@0 | 17 | o[a[i]] = ''; |
michael@0 | 18 | } |
michael@0 | 19 | return o; |
michael@0 | 20 | } |
michael@0 | 21 | |
michael@0 | 22 | function run_test() { |
michael@0 | 23 | |
michael@0 | 24 | var ps = Cc["@mozilla.org/preferences-service;1"]. |
michael@0 | 25 | getService(Ci.nsIPrefService); |
michael@0 | 26 | |
michael@0 | 27 | var pb2= Cc["@mozilla.org/preferences-service;1"]. |
michael@0 | 28 | getService(Ci.nsIPrefBranch); |
michael@0 | 29 | |
michael@0 | 30 | var pb = Cc["@mozilla.org/preferences-service;1"]. |
michael@0 | 31 | getService(Ci.nsIPrefBranch); |
michael@0 | 32 | |
michael@0 | 33 | //**************************************************************************// |
michael@0 | 34 | // Nullsafety |
michael@0 | 35 | |
michael@0 | 36 | do_check_throws(function() { |
michael@0 | 37 | pb.getPrefType(null); }, Cr.NS_ERROR_INVALID_ARG); |
michael@0 | 38 | do_check_throws(function() { |
michael@0 | 39 | pb.getBoolPref(null); }, Cr.NS_ERROR_INVALID_ARG); |
michael@0 | 40 | do_check_throws(function() { |
michael@0 | 41 | pb.setBoolPref(null, false); }, Cr.NS_ERROR_INVALID_ARG); |
michael@0 | 42 | do_check_throws(function() { |
michael@0 | 43 | pb.getIntPref(null); }, Cr.NS_ERROR_INVALID_ARG); |
michael@0 | 44 | do_check_throws(function() { |
michael@0 | 45 | pb.setIntPref(null, 0); }, Cr.NS_ERROR_INVALID_ARG); |
michael@0 | 46 | do_check_throws(function() { |
michael@0 | 47 | pb.getCharPref(null); }, Cr.NS_ERROR_INVALID_ARG); |
michael@0 | 48 | do_check_throws(function() { |
michael@0 | 49 | pb.setCharPref(null, null); }, Cr.NS_ERROR_INVALID_ARG); |
michael@0 | 50 | do_check_throws(function() { |
michael@0 | 51 | pb.getComplexValue(null, Components.interfaces.nsISupportsString); }, Cr.NS_ERROR_INVALID_ARG); |
michael@0 | 52 | do_check_throws(function() { |
michael@0 | 53 | pb.setComplexValue(null, Components.interfaces.nsISupportsString, pb); }, Cr.NS_ERROR_INVALID_ARG); |
michael@0 | 54 | do_check_throws(function() { |
michael@0 | 55 | pb.clearUserPref(null); }, Cr.NS_ERROR_INVALID_ARG); |
michael@0 | 56 | do_check_throws(function() { |
michael@0 | 57 | pb.prefHasUserValue(null); }, Cr.NS_ERROR_INVALID_ARG); |
michael@0 | 58 | do_check_throws(function() { |
michael@0 | 59 | pb.lockPref(null); }, Cr.NS_ERROR_INVALID_ARG); |
michael@0 | 60 | do_check_throws(function() { |
michael@0 | 61 | pb.prefIsLocked(null); }, Cr.NS_ERROR_INVALID_ARG); |
michael@0 | 62 | do_check_throws(function() { |
michael@0 | 63 | pb.unlockPref(null); }, Cr.NS_ERROR_INVALID_ARG); |
michael@0 | 64 | do_check_throws(function() { |
michael@0 | 65 | pb.deleteBranch(null); }, Cr.NS_ERROR_INVALID_ARG); |
michael@0 | 66 | do_check_throws(function() { |
michael@0 | 67 | pb.getChildList(null); }, Cr.NS_ERROR_INVALID_ARG); |
michael@0 | 68 | |
michael@0 | 69 | //**************************************************************************// |
michael@0 | 70 | // Nonexisting user preferences |
michael@0 | 71 | |
michael@0 | 72 | do_check_eq(pb.prefHasUserValue("UserPref.nonexistent.hasUserValue"), false); |
michael@0 | 73 | pb.clearUserPref("UserPref.nonexistent.clearUserPref"); // shouldn't throw |
michael@0 | 74 | do_check_eq(pb.getPrefType("UserPref.nonexistent.getPrefType"), PREF_INVALID); |
michael@0 | 75 | do_check_eq(pb.root, ""); |
michael@0 | 76 | |
michael@0 | 77 | // bool... |
michael@0 | 78 | do_check_throws(function() { |
michael@0 | 79 | pb.getBoolPref("UserPref.nonexistent.getBoolPref");}, Cr.NS_ERROR_UNEXPECTED); |
michael@0 | 80 | pb.setBoolPref("UserPref.nonexistent.setBoolPref", false); |
michael@0 | 81 | do_check_eq(pb.getBoolPref("UserPref.nonexistent.setBoolPref"), false); |
michael@0 | 82 | |
michael@0 | 83 | // int... |
michael@0 | 84 | do_check_throws(function() { |
michael@0 | 85 | pb.getIntPref("UserPref.nonexistent.getIntPref");}, Cr.NS_ERROR_UNEXPECTED); |
michael@0 | 86 | pb.setIntPref("UserPref.nonexistent.setIntPref", 5); |
michael@0 | 87 | do_check_eq(pb.getIntPref("UserPref.nonexistent.setIntPref"), 5); |
michael@0 | 88 | |
michael@0 | 89 | // char |
michael@0 | 90 | do_check_throws(function() { |
michael@0 | 91 | pb.getCharPref("UserPref.nonexistent.getCharPref");}, Cr.NS_ERROR_UNEXPECTED); |
michael@0 | 92 | pb.setCharPref("UserPref.nonexistent.setCharPref", "_test"); |
michael@0 | 93 | do_check_eq(pb.getCharPref("UserPref.nonexistent.setCharPref"), "_test"); |
michael@0 | 94 | |
michael@0 | 95 | //**************************************************************************// |
michael@0 | 96 | // Existing user Prefs and data integrity test (round-trip match) |
michael@0 | 97 | |
michael@0 | 98 | pb.setBoolPref("UserPref.existing.bool", true); |
michael@0 | 99 | pb.setIntPref("UserPref.existing.int", 23); |
michael@0 | 100 | pb.setCharPref("UserPref.existing.char", "hey"); |
michael@0 | 101 | |
michael@0 | 102 | // getPref should return the pref value |
michael@0 | 103 | do_check_eq(pb.getBoolPref("UserPref.existing.bool"), true); |
michael@0 | 104 | do_check_eq(pb.getIntPref("UserPref.existing.int"), 23); |
michael@0 | 105 | do_check_eq(pb.getCharPref("UserPref.existing.char"), "hey"); |
michael@0 | 106 | |
michael@0 | 107 | // setPref should not complain and should change the value of the pref |
michael@0 | 108 | pb.setBoolPref("UserPref.existing.bool", false); |
michael@0 | 109 | do_check_eq(pb.getBoolPref("UserPref.existing.bool"), false); |
michael@0 | 110 | pb.setIntPref("UserPref.existing.int", 24); |
michael@0 | 111 | do_check_eq(pb.getIntPref("UserPref.existing.int"), 24); |
michael@0 | 112 | pb.setCharPref("UserPref.existing.char", "hej då!"); |
michael@0 | 113 | do_check_eq(pb.getCharPref("UserPref.existing.char"), "hej då!"); |
michael@0 | 114 | |
michael@0 | 115 | // prefHasUserValue should return true now |
michael@0 | 116 | do_check_true(pb.prefHasUserValue("UserPref.existing.bool")); |
michael@0 | 117 | do_check_true(pb.prefHasUserValue("UserPref.existing.int")); |
michael@0 | 118 | do_check_true(pb.prefHasUserValue("UserPref.existing.char")); |
michael@0 | 119 | |
michael@0 | 120 | // clearUserPref should remove the pref |
michael@0 | 121 | pb.clearUserPref("UserPref.existing.bool"); |
michael@0 | 122 | do_check_false(pb.prefHasUserValue("UserPref.existing.bool")); |
michael@0 | 123 | pb.clearUserPref("UserPref.existing.int"); |
michael@0 | 124 | do_check_false(pb.prefHasUserValue("UserPref.existing.int")); |
michael@0 | 125 | pb.clearUserPref("UserPref.existing.char"); |
michael@0 | 126 | do_check_false(pb.prefHasUserValue("UserPref.existing.char")); |
michael@0 | 127 | |
michael@0 | 128 | //**************************************************************************// |
michael@0 | 129 | // Large value test |
michael@0 | 130 | |
michael@0 | 131 | let largeStr = new Array(MAX_PREF_LENGTH + 1).join('x'); |
michael@0 | 132 | pb.setCharPref("UserPref.large.char", largeStr); |
michael@0 | 133 | largeStr += 'x'; |
michael@0 | 134 | do_check_throws(function() { |
michael@0 | 135 | pb.setCharPref("UserPref.large.char", largeStr); }, Cr.NS_ERROR_ILLEGAL_VALUE); |
michael@0 | 136 | |
michael@0 | 137 | //**************************************************************************// |
michael@0 | 138 | // getPrefType test |
michael@0 | 139 | |
michael@0 | 140 | // bool... |
michael@0 | 141 | pb.setBoolPref("UserPref.getPrefType.bool", true); |
michael@0 | 142 | do_check_eq(pb.getPrefType("UserPref.getPrefType.bool"), PREF_BOOL); |
michael@0 | 143 | |
michael@0 | 144 | // int... |
michael@0 | 145 | pb.setIntPref("UserPref.getPrefType.int", -234); |
michael@0 | 146 | do_check_eq(pb.getPrefType("UserPref.getPrefType.int"), PREF_INT); |
michael@0 | 147 | |
michael@0 | 148 | // char... |
michael@0 | 149 | pb.setCharPref("UserPref.getPrefType.char", "testing1..2"); |
michael@0 | 150 | do_check_eq(pb.getPrefType("UserPref.getPrefType.char"), PREF_STRING); |
michael@0 | 151 | |
michael@0 | 152 | //**************************************************************************// |
michael@0 | 153 | // getBranch tests |
michael@0 | 154 | |
michael@0 | 155 | do_check_eq(ps.root, ""); |
michael@0 | 156 | |
michael@0 | 157 | // bool ... |
michael@0 | 158 | pb.setBoolPref("UserPref.root.boolPref", true); |
michael@0 | 159 | let pb_1 = ps.getBranch("UserPref.root."); |
michael@0 | 160 | do_check_eq(pb_1.getBoolPref("boolPref"), true); |
michael@0 | 161 | let pb_2 = ps.getBranch("UserPref.root.boolPref"); |
michael@0 | 162 | do_check_eq(pb_2.getBoolPref(""), true); |
michael@0 | 163 | pb_2.setBoolPref(".anotherPref", false); |
michael@0 | 164 | let pb_3 = ps.getBranch("UserPref.root.boolPre"); |
michael@0 | 165 | do_check_eq(pb_3.getBoolPref("f.anotherPref"), false); |
michael@0 | 166 | |
michael@0 | 167 | // int ... |
michael@0 | 168 | pb.setIntPref("UserPref.root.intPref", 23); |
michael@0 | 169 | let pb_1 = ps.getBranch("UserPref.root."); |
michael@0 | 170 | do_check_eq(pb_1.getIntPref("intPref"), 23); |
michael@0 | 171 | let pb_2 = ps.getBranch("UserPref.root.intPref"); |
michael@0 | 172 | do_check_eq(pb_2.getIntPref(""), 23); |
michael@0 | 173 | pb_2.setIntPref(".anotherPref", 69); |
michael@0 | 174 | let pb_3 = ps.getBranch("UserPref.root.intPre"); |
michael@0 | 175 | do_check_eq(pb_3.getIntPref("f.anotherPref"), 69); |
michael@0 | 176 | |
michael@0 | 177 | // char... |
michael@0 | 178 | pb.setCharPref("UserPref.root.charPref", "_char"); |
michael@0 | 179 | let pb_1 = ps.getBranch("UserPref.root."); |
michael@0 | 180 | do_check_eq(pb_1.getCharPref("charPref"), "_char"); |
michael@0 | 181 | let pb_2 = ps.getBranch("UserPref.root.charPref"); |
michael@0 | 182 | do_check_eq(pb_2.getCharPref(""), "_char"); |
michael@0 | 183 | pb_2.setCharPref(".anotherPref", "_another"); |
michael@0 | 184 | let pb_3 = ps.getBranch("UserPref.root.charPre"); |
michael@0 | 185 | do_check_eq(pb_3.getCharPref("f.anotherPref"), "_another"); |
michael@0 | 186 | |
michael@0 | 187 | //**************************************************************************// |
michael@0 | 188 | // getChildlist tests |
michael@0 | 189 | |
michael@0 | 190 | // get an already set prefBranch |
michael@0 | 191 | pb1 = ps.getBranch("UserPref.root."); |
michael@0 | 192 | let prefList = pb1.getChildList(""); |
michael@0 | 193 | do_check_eq(prefList.length, 6); |
michael@0 | 194 | |
michael@0 | 195 | // check for specific prefs in the array : the order is not important |
michael@0 | 196 | do_check_true("boolPref" in makeList(prefList)); |
michael@0 | 197 | do_check_true("intPref" in makeList(prefList)); |
michael@0 | 198 | do_check_true("charPref" in makeList(prefList)); |
michael@0 | 199 | do_check_true("boolPref.anotherPref" in makeList(prefList)); |
michael@0 | 200 | do_check_true("intPref.anotherPref" in makeList(prefList)); |
michael@0 | 201 | do_check_true("charPref.anotherPref" in makeList(prefList)); |
michael@0 | 202 | |
michael@0 | 203 | //**************************************************************************// |
michael@0 | 204 | // Default branch tests |
michael@0 | 205 | |
michael@0 | 206 | // bool... |
michael@0 | 207 | pb1 = ps.getDefaultBranch(""); |
michael@0 | 208 | pb1.setBoolPref("DefaultPref.bool", true); |
michael@0 | 209 | do_check_eq(pb1.getBoolPref("DefaultPref.bool"), true); |
michael@0 | 210 | do_check_false(pb1.prefHasUserValue("DefaultPref.bool")); |
michael@0 | 211 | ps.setBoolPref("DefaultPref.bool", false); |
michael@0 | 212 | do_check_true(pb1.prefHasUserValue("DefaultPref.bool")); |
michael@0 | 213 | do_check_eq(ps.getBoolPref("DefaultPref.bool"), false); |
michael@0 | 214 | |
michael@0 | 215 | // int... |
michael@0 | 216 | pb1 = ps.getDefaultBranch(""); |
michael@0 | 217 | pb1.setIntPref("DefaultPref.int", 100); |
michael@0 | 218 | do_check_eq(pb1.getIntPref("DefaultPref.int"), 100); |
michael@0 | 219 | do_check_false(pb1.prefHasUserValue("DefaultPref.int")); |
michael@0 | 220 | ps.setIntPref("DefaultPref.int", 50); |
michael@0 | 221 | do_check_true(pb1.prefHasUserValue("DefaultPref.int")); |
michael@0 | 222 | do_check_eq(ps.getIntPref("DefaultPref.int"), 50); |
michael@0 | 223 | |
michael@0 | 224 | // char... |
michael@0 | 225 | pb1 = ps.getDefaultBranch(""); |
michael@0 | 226 | pb1.setCharPref("DefaultPref.char", "_default"); |
michael@0 | 227 | do_check_eq(pb1.getCharPref("DefaultPref.char"), "_default"); |
michael@0 | 228 | do_check_false(pb1.prefHasUserValue("DefaultPref.char")); |
michael@0 | 229 | ps.setCharPref("DefaultPref.char", "_user"); |
michael@0 | 230 | do_check_true(pb1.prefHasUserValue("DefaultPref.char")); |
michael@0 | 231 | do_check_eq(ps.getCharPref("DefaultPref.char"), "_user"); |
michael@0 | 232 | |
michael@0 | 233 | //**************************************************************************// |
michael@0 | 234 | // pref Locking/Unlocking tests |
michael@0 | 235 | |
michael@0 | 236 | // locking and unlocking a nonexistent pref should throw |
michael@0 | 237 | do_check_throws(function() { |
michael@0 | 238 | ps.lockPref("DefaultPref.nonexistent");}, Cr.NS_ERROR_UNEXPECTED); |
michael@0 | 239 | do_check_throws(function() { |
michael@0 | 240 | ps.unlockPref("DefaultPref.nonexistent");}, Cr.NS_ERROR_UNEXPECTED); |
michael@0 | 241 | |
michael@0 | 242 | // getting a locked pref branch should return the "default" value |
michael@0 | 243 | do_check_false(ps.prefIsLocked("DefaultPref.char")); |
michael@0 | 244 | ps.lockPref("DefaultPref.char"); |
michael@0 | 245 | do_check_eq(ps.getCharPref("DefaultPref.char"), "_default"); |
michael@0 | 246 | do_check_true(ps.prefIsLocked("DefaultPref.char")); |
michael@0 | 247 | |
michael@0 | 248 | // getting an unlocked pref branch should return the "user" value |
michael@0 | 249 | ps.unlockPref("DefaultPref.char"); |
michael@0 | 250 | do_check_eq(ps.getCharPref("DefaultPref.char"), "_user"); |
michael@0 | 251 | do_check_false(ps.prefIsLocked("DefaultPref.char")); |
michael@0 | 252 | |
michael@0 | 253 | // setting the "default" value to a user pref branch should |
michael@0 | 254 | // make prefHasUserValue return false (documented behavior) |
michael@0 | 255 | ps.setCharPref("DefaultPref.char", "_default"); |
michael@0 | 256 | do_check_false(pb1.prefHasUserValue("DefaultPref.char")); |
michael@0 | 257 | |
michael@0 | 258 | // unlocking and locking multiple times shouldn't throw |
michael@0 | 259 | ps.unlockPref("DefaultPref.char"); |
michael@0 | 260 | ps.lockPref("DefaultPref.char"); |
michael@0 | 261 | ps.lockPref("DefaultPref.char"); |
michael@0 | 262 | |
michael@0 | 263 | //**************************************************************************// |
michael@0 | 264 | // resetBranch test |
michael@0 | 265 | |
michael@0 | 266 | // NOT IMPLEMENTED YET in module/libpref. So we're not testing ! |
michael@0 | 267 | // uncomment the following if resetBranch ever gets implemented. |
michael@0 | 268 | /*ps.resetBranch("DefaultPref"); |
michael@0 | 269 | do_check_eq(ps.getBoolPref("DefaultPref.bool"), true); |
michael@0 | 270 | do_check_eq(ps.getIntPref("DefaultPref.int"), 100); |
michael@0 | 271 | do_check_eq(ps.getCharPref("DefaultPref.char"), "_default");*/ |
michael@0 | 272 | |
michael@0 | 273 | //**************************************************************************// |
michael@0 | 274 | // deleteBranch tests |
michael@0 | 275 | |
michael@0 | 276 | // TODO : Really, this should throw!, by documentation. |
michael@0 | 277 | // do_check_throws(function() { |
michael@0 | 278 | // ps.deleteBranch("UserPref.nonexistent.deleteBranch");}, Cr.NS_ERROR_UNEXPECTED); |
michael@0 | 279 | |
michael@0 | 280 | ps.deleteBranch("DefaultPref"); |
michael@0 | 281 | pb = ps.getBranch("DefaultPref"); |
michael@0 | 282 | pb1 = ps.getDefaultBranch("DefaultPref"); |
michael@0 | 283 | |
michael@0 | 284 | // getting prefs on deleted user branches should throw |
michael@0 | 285 | do_check_throws(function() { |
michael@0 | 286 | pb.getBoolPref("DefaultPref.bool");}, Cr.NS_ERROR_UNEXPECTED); |
michael@0 | 287 | do_check_throws(function() { |
michael@0 | 288 | pb.getIntPref("DefaultPref.int");}, Cr.NS_ERROR_UNEXPECTED); |
michael@0 | 289 | do_check_throws(function() { |
michael@0 | 290 | pb.getCharPref("DefaultPref.char");}, Cr.NS_ERROR_UNEXPECTED); |
michael@0 | 291 | |
michael@0 | 292 | // getting prefs on deleted default branches should throw |
michael@0 | 293 | do_check_throws(function() { |
michael@0 | 294 | pb1.getBoolPref("DefaultPref.bool");}, Cr.NS_ERROR_UNEXPECTED); |
michael@0 | 295 | do_check_throws(function() { |
michael@0 | 296 | pb1.getIntPref("DefaultPref.int");}, Cr.NS_ERROR_UNEXPECTED); |
michael@0 | 297 | do_check_throws(function() { |
michael@0 | 298 | pb1.getCharPref("DefaultPref.char");}, Cr.NS_ERROR_UNEXPECTED); |
michael@0 | 299 | |
michael@0 | 300 | //**************************************************************************// |
michael@0 | 301 | // savePrefFile & readPrefFile tests |
michael@0 | 302 | |
michael@0 | 303 | // set some prefs |
michael@0 | 304 | ps.setBoolPref("ReadPref.bool", true); |
michael@0 | 305 | ps.setIntPref("ReadPref.int", 230); |
michael@0 | 306 | ps.setCharPref("ReadPref.char", "hello"); |
michael@0 | 307 | |
michael@0 | 308 | // save those prefs in a file |
michael@0 | 309 | let savePrefFile = do_get_cwd(); |
michael@0 | 310 | savePrefFile.append("data"); |
michael@0 | 311 | savePrefFile.append("savePref.js"); |
michael@0 | 312 | if (savePrefFile.exists()) |
michael@0 | 313 | savePrefFile.remove(false); |
michael@0 | 314 | savePrefFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); |
michael@0 | 315 | ps.savePrefFile(savePrefFile); |
michael@0 | 316 | ps.resetPrefs(); |
michael@0 | 317 | |
michael@0 | 318 | // load a preexisting pref file |
michael@0 | 319 | let prefFile = do_get_file("data/testPref.js"); |
michael@0 | 320 | ps.readUserPrefs(prefFile); |
michael@0 | 321 | |
michael@0 | 322 | // former prefs should have been replaced/lost |
michael@0 | 323 | do_check_throws(function() { |
michael@0 | 324 | do_check_eq(pb.getBoolPref("ReadPref.bool"));}, Cr.NS_ERROR_UNEXPECTED); |
michael@0 | 325 | do_check_throws(function() { |
michael@0 | 326 | do_check_eq(pb.getIntPref("ReadPref.int"));}, Cr.NS_ERROR_UNEXPECTED); |
michael@0 | 327 | do_check_throws(function() { |
michael@0 | 328 | do_check_eq(pb.getCharPref("ReadPref.char"));}, Cr.NS_ERROR_UNEXPECTED); |
michael@0 | 329 | |
michael@0 | 330 | // loaded prefs should read ok. |
michael@0 | 331 | pb = ps.getBranch("testPref."); |
michael@0 | 332 | do_check_eq(pb.getBoolPref("bool1"), true); |
michael@0 | 333 | do_check_eq(pb.getBoolPref("bool2"), false); |
michael@0 | 334 | do_check_eq(pb.getIntPref("int1"), 23); |
michael@0 | 335 | do_check_eq(pb.getIntPref("int2"), -1236); |
michael@0 | 336 | do_check_eq(pb.getCharPref("char1"), "_testPref"); |
michael@0 | 337 | do_check_eq(pb.getCharPref("char2"), "älskar"); |
michael@0 | 338 | |
michael@0 | 339 | // loading our former savePrefFile should allow us to read former prefs |
michael@0 | 340 | ps.readUserPrefs(savePrefFile); |
michael@0 | 341 | // cleanup the file now we don't need it |
michael@0 | 342 | savePrefFile.remove(false); |
michael@0 | 343 | do_check_eq(ps.getBoolPref("ReadPref.bool"), true); |
michael@0 | 344 | do_check_eq(ps.getIntPref("ReadPref.int"), 230); |
michael@0 | 345 | do_check_eq(ps.getCharPref("ReadPref.char"), "hello"); |
michael@0 | 346 | |
michael@0 | 347 | // ... and still be able to access "prior-to-readUserPrefs" preferences |
michael@0 | 348 | do_check_eq(pb.getBoolPref("bool1"), true); |
michael@0 | 349 | do_check_eq(pb.getBoolPref("bool2"), false); |
michael@0 | 350 | do_check_eq(pb.getIntPref("int1"), 23); |
michael@0 | 351 | |
michael@0 | 352 | //**************************************************************************// |
michael@0 | 353 | // preference Observers |
michael@0 | 354 | |
michael@0 | 355 | // an observer... |
michael@0 | 356 | var observer = { |
michael@0 | 357 | QueryInterface: function QueryInterface(aIID) { |
michael@0 | 358 | if (aIID.equals(Ci.nsIObserver) || |
michael@0 | 359 | aIID.equals(Ci.nsISupports)) |
michael@0 | 360 | return this; |
michael@0 | 361 | throw Components.results.NS_NOINTERFACE; |
michael@0 | 362 | }, |
michael@0 | 363 | |
michael@0 | 364 | observe: function observe(aSubject, aTopic, aState) { |
michael@0 | 365 | do_check_eq(aTopic, "nsPref:changed"); |
michael@0 | 366 | do_check_eq(aState, "ReadPref.int"); |
michael@0 | 367 | do_check_eq(ps.getIntPref(aState), 76); |
michael@0 | 368 | ps.removeObserver("ReadPref.int", this); |
michael@0 | 369 | |
michael@0 | 370 | // notification received, we may go on... |
michael@0 | 371 | do_test_finished(); |
michael@0 | 372 | } |
michael@0 | 373 | } |
michael@0 | 374 | |
michael@0 | 375 | pb2.addObserver("ReadPref.int", observer, false); |
michael@0 | 376 | ps.setIntPref("ReadPref.int", 76); |
michael@0 | 377 | |
michael@0 | 378 | // test will continue upon notification... |
michael@0 | 379 | do_test_pending(); |
michael@0 | 380 | |
michael@0 | 381 | // removed observer should not fire |
michael@0 | 382 | pb2.removeObserver("ReadPref.int", observer); |
michael@0 | 383 | ps.setIntPref("ReadPref.int", 32); |
michael@0 | 384 | |
michael@0 | 385 | // let's test observers once more with a non-root prefbranch |
michael@0 | 386 | pb2.getBranch("ReadPref."); |
michael@0 | 387 | pb2.addObserver("int", observer, false); |
michael@0 | 388 | ps.setIntPref("ReadPref.int", 76); |
michael@0 | 389 | |
michael@0 | 390 | // test will complete upon notification... |
michael@0 | 391 | do_test_pending(); |
michael@0 | 392 | } |