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 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | * http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | const {classes: Cc, interfaces: Ci, results: Cr, utils: Cu, manager: Cm} = Components; |
michael@0 | 5 | |
michael@0 | 6 | Cu.import("resource://gre/modules/Preferences.jsm"); |
michael@0 | 7 | |
michael@0 | 8 | function run_test() { |
michael@0 | 9 | run_next_test(); |
michael@0 | 10 | } |
michael@0 | 11 | |
michael@0 | 12 | add_test(function test_set_get_pref() { |
michael@0 | 13 | Preferences.set("test_set_get_pref.integer", 1); |
michael@0 | 14 | do_check_eq(Preferences.get("test_set_get_pref.integer"), 1); |
michael@0 | 15 | |
michael@0 | 16 | Preferences.set("test_set_get_pref.string", "foo"); |
michael@0 | 17 | do_check_eq(Preferences.get("test_set_get_pref.string"), "foo"); |
michael@0 | 18 | |
michael@0 | 19 | Preferences.set("test_set_get_pref.boolean", true); |
michael@0 | 20 | do_check_eq(Preferences.get("test_set_get_pref.boolean"), true); |
michael@0 | 21 | |
michael@0 | 22 | // Clean up. |
michael@0 | 23 | Preferences.resetBranch("test_set_get_pref."); |
michael@0 | 24 | |
michael@0 | 25 | run_next_test(); |
michael@0 | 26 | }); |
michael@0 | 27 | |
michael@0 | 28 | add_test(function test_set_get_branch_pref() { |
michael@0 | 29 | let prefs = new Preferences("test_set_get_branch_pref."); |
michael@0 | 30 | |
michael@0 | 31 | prefs.set("something", 1); |
michael@0 | 32 | do_check_eq(prefs.get("something"), 1); |
michael@0 | 33 | do_check_false(Preferences.has("something")); |
michael@0 | 34 | |
michael@0 | 35 | // Clean up. |
michael@0 | 36 | prefs.reset("something"); |
michael@0 | 37 | |
michael@0 | 38 | run_next_test(); |
michael@0 | 39 | }); |
michael@0 | 40 | |
michael@0 | 41 | add_test(function test_set_get_multiple_prefs() { |
michael@0 | 42 | Preferences.set({ "test_set_get_multiple_prefs.integer": 1, |
michael@0 | 43 | "test_set_get_multiple_prefs.string": "foo", |
michael@0 | 44 | "test_set_get_multiple_prefs.boolean": true }); |
michael@0 | 45 | |
michael@0 | 46 | let [i, s, b] = Preferences.get(["test_set_get_multiple_prefs.integer", |
michael@0 | 47 | "test_set_get_multiple_prefs.string", |
michael@0 | 48 | "test_set_get_multiple_prefs.boolean"]); |
michael@0 | 49 | |
michael@0 | 50 | do_check_eq(i, 1); |
michael@0 | 51 | do_check_eq(s, "foo"); |
michael@0 | 52 | do_check_eq(b, true); |
michael@0 | 53 | |
michael@0 | 54 | // Clean up. |
michael@0 | 55 | Preferences.resetBranch("test_set_get_multiple_prefs."); |
michael@0 | 56 | |
michael@0 | 57 | run_next_test(); |
michael@0 | 58 | }); |
michael@0 | 59 | |
michael@0 | 60 | add_test(function test_get_multiple_prefs_with_default_value() { |
michael@0 | 61 | Preferences.set({ "test_get_multiple_prefs_with_default_value.a": 1, |
michael@0 | 62 | "test_get_multiple_prefs_with_default_value.b": 2 }); |
michael@0 | 63 | |
michael@0 | 64 | let [a, b, c] = Preferences.get(["test_get_multiple_prefs_with_default_value.a", |
michael@0 | 65 | "test_get_multiple_prefs_with_default_value.b", |
michael@0 | 66 | "test_get_multiple_prefs_with_default_value.c"], |
michael@0 | 67 | 0); |
michael@0 | 68 | |
michael@0 | 69 | do_check_eq(a, 1); |
michael@0 | 70 | do_check_eq(b, 2); |
michael@0 | 71 | do_check_eq(c, 0); |
michael@0 | 72 | |
michael@0 | 73 | // Clean up. |
michael@0 | 74 | Preferences.resetBranch("test_get_multiple_prefs_with_default_value."); |
michael@0 | 75 | |
michael@0 | 76 | run_next_test(); |
michael@0 | 77 | }); |
michael@0 | 78 | |
michael@0 | 79 | add_test(function test_set_get_unicode_pref() { |
michael@0 | 80 | Preferences.set("test_set_get_unicode_pref", String.fromCharCode(960)); |
michael@0 | 81 | do_check_eq(Preferences.get("test_set_get_unicode_pref"), String.fromCharCode(960)); |
michael@0 | 82 | |
michael@0 | 83 | // Clean up. |
michael@0 | 84 | Preferences.reset("test_set_get_unicode_pref"); |
michael@0 | 85 | |
michael@0 | 86 | run_next_test(); |
michael@0 | 87 | }); |
michael@0 | 88 | |
michael@0 | 89 | add_test(function test_set_null_pref() { |
michael@0 | 90 | try { |
michael@0 | 91 | Preferences.set("test_set_null_pref", null); |
michael@0 | 92 | // We expect this to throw, so the test is designed to fail if it doesn't. |
michael@0 | 93 | do_check_true(false); |
michael@0 | 94 | } |
michael@0 | 95 | catch(ex) {} |
michael@0 | 96 | |
michael@0 | 97 | run_next_test(); |
michael@0 | 98 | }); |
michael@0 | 99 | |
michael@0 | 100 | add_test(function test_set_undefined_pref() { |
michael@0 | 101 | try { |
michael@0 | 102 | Preferences.set("test_set_undefined_pref"); |
michael@0 | 103 | // We expect this to throw, so the test is designed to fail if it doesn't. |
michael@0 | 104 | do_check_true(false); |
michael@0 | 105 | } |
michael@0 | 106 | catch(ex) {} |
michael@0 | 107 | |
michael@0 | 108 | run_next_test(); |
michael@0 | 109 | }); |
michael@0 | 110 | |
michael@0 | 111 | add_test(function test_set_unsupported_pref() { |
michael@0 | 112 | try { |
michael@0 | 113 | Preferences.set("test_set_unsupported_pref", new Array()); |
michael@0 | 114 | // We expect this to throw, so the test is designed to fail if it doesn't. |
michael@0 | 115 | do_check_true(false); |
michael@0 | 116 | } |
michael@0 | 117 | catch(ex) {} |
michael@0 | 118 | |
michael@0 | 119 | run_next_test(); |
michael@0 | 120 | }); |
michael@0 | 121 | |
michael@0 | 122 | // Make sure that we can get a string pref that we didn't set ourselves |
michael@0 | 123 | // (i.e. that the way we get a string pref using getComplexValue doesn't |
michael@0 | 124 | // hork us getting a string pref that wasn't set using setComplexValue). |
michael@0 | 125 | add_test(function test_get_string_pref() { |
michael@0 | 126 | let svc = Cc["@mozilla.org/preferences-service;1"]. |
michael@0 | 127 | getService(Ci.nsIPrefService). |
michael@0 | 128 | getBranch(""); |
michael@0 | 129 | svc.setCharPref("test_get_string_pref", "a normal string"); |
michael@0 | 130 | do_check_eq(Preferences.get("test_get_string_pref"), "a normal string"); |
michael@0 | 131 | |
michael@0 | 132 | // Clean up. |
michael@0 | 133 | Preferences.reset("test_get_string_pref"); |
michael@0 | 134 | |
michael@0 | 135 | run_next_test(); |
michael@0 | 136 | }); |
michael@0 | 137 | |
michael@0 | 138 | add_test(function test_set_get_number_pref() { |
michael@0 | 139 | Preferences.set("test_set_get_number_pref", 5); |
michael@0 | 140 | do_check_eq(Preferences.get("test_set_get_number_pref"), 5); |
michael@0 | 141 | |
michael@0 | 142 | // Non-integer values get converted to integers. |
michael@0 | 143 | Preferences.set("test_set_get_number_pref", 3.14159); |
michael@0 | 144 | do_check_eq(Preferences.get("test_set_get_number_pref"), 3); |
michael@0 | 145 | |
michael@0 | 146 | // Values outside the range -(2^31-1) to 2^31-1 overflow. |
michael@0 | 147 | try { |
michael@0 | 148 | Preferences.set("test_set_get_number_pref", Math.pow(2, 31)); |
michael@0 | 149 | // We expect this to throw, so the test is designed to fail if it doesn't. |
michael@0 | 150 | do_check_true(false); |
michael@0 | 151 | } |
michael@0 | 152 | catch(ex) {} |
michael@0 | 153 | |
michael@0 | 154 | // Clean up. |
michael@0 | 155 | Preferences.reset("test_set_get_number_pref"); |
michael@0 | 156 | |
michael@0 | 157 | run_next_test(); |
michael@0 | 158 | }); |
michael@0 | 159 | |
michael@0 | 160 | add_test(function test_reset_pref() { |
michael@0 | 161 | Preferences.set("test_reset_pref", 1); |
michael@0 | 162 | Preferences.reset("test_reset_pref"); |
michael@0 | 163 | do_check_eq(Preferences.get("test_reset_pref"), undefined); |
michael@0 | 164 | |
michael@0 | 165 | run_next_test(); |
michael@0 | 166 | }); |
michael@0 | 167 | |
michael@0 | 168 | add_test(function test_reset_pref_branch() { |
michael@0 | 169 | Preferences.set("test_reset_pref_branch.foo", 1); |
michael@0 | 170 | Preferences.set("test_reset_pref_branch.bar", 2); |
michael@0 | 171 | Preferences.resetBranch("test_reset_pref_branch."); |
michael@0 | 172 | do_check_eq(Preferences.get("test_reset_pref_branch.foo"), undefined); |
michael@0 | 173 | do_check_eq(Preferences.get("test_reset_pref_branch.bar"), undefined); |
michael@0 | 174 | |
michael@0 | 175 | run_next_test(); |
michael@0 | 176 | }); |
michael@0 | 177 | |
michael@0 | 178 | // Make sure the module doesn't throw an exception when asked to reset |
michael@0 | 179 | // a nonexistent pref. |
michael@0 | 180 | add_test(function test_reset_nonexistent_pref() { |
michael@0 | 181 | Preferences.reset("test_reset_nonexistent_pref"); |
michael@0 | 182 | |
michael@0 | 183 | run_next_test(); |
michael@0 | 184 | }); |
michael@0 | 185 | |
michael@0 | 186 | // Make sure the module doesn't throw an exception when asked to reset |
michael@0 | 187 | // a nonexistent pref branch. |
michael@0 | 188 | add_test(function test_reset_nonexistent_pref_branch() { |
michael@0 | 189 | Preferences.resetBranch("test_reset_nonexistent_pref_branch."); |
michael@0 | 190 | |
michael@0 | 191 | run_next_test(); |
michael@0 | 192 | }); |
michael@0 | 193 | |
michael@0 | 194 | add_test(function test_observe_prefs_function() { |
michael@0 | 195 | let observed = false; |
michael@0 | 196 | let observer = function() { observed = !observed }; |
michael@0 | 197 | |
michael@0 | 198 | Preferences.observe("test_observe_prefs_function", observer); |
michael@0 | 199 | Preferences.set("test_observe_prefs_function", "something"); |
michael@0 | 200 | do_check_true(observed); |
michael@0 | 201 | |
michael@0 | 202 | Preferences.ignore("test_observe_prefs_function", observer); |
michael@0 | 203 | Preferences.set("test_observe_prefs_function", "something else"); |
michael@0 | 204 | do_check_true(observed); |
michael@0 | 205 | |
michael@0 | 206 | // Clean up. |
michael@0 | 207 | Preferences.reset("test_observe_prefs_function"); |
michael@0 | 208 | |
michael@0 | 209 | run_next_test(); |
michael@0 | 210 | }); |
michael@0 | 211 | |
michael@0 | 212 | add_test(function test_observe_prefs_object() { |
michael@0 | 213 | let observer = { |
michael@0 | 214 | observed: false, |
michael@0 | 215 | observe: function() { |
michael@0 | 216 | this.observed = !this.observed; |
michael@0 | 217 | } |
michael@0 | 218 | }; |
michael@0 | 219 | |
michael@0 | 220 | Preferences.observe("test_observe_prefs_object", observer.observe, observer); |
michael@0 | 221 | Preferences.set("test_observe_prefs_object", "something"); |
michael@0 | 222 | do_check_true(observer.observed); |
michael@0 | 223 | |
michael@0 | 224 | Preferences.ignore("test_observe_prefs_object", observer.observe, observer); |
michael@0 | 225 | Preferences.set("test_observe_prefs_object", "something else"); |
michael@0 | 226 | do_check_true(observer.observed); |
michael@0 | 227 | |
michael@0 | 228 | // Clean up. |
michael@0 | 229 | Preferences.reset("test_observe_prefs_object"); |
michael@0 | 230 | |
michael@0 | 231 | run_next_test(); |
michael@0 | 232 | }); |
michael@0 | 233 | |
michael@0 | 234 | add_test(function test_observe_prefs_nsIObserver() { |
michael@0 | 235 | let observer = { |
michael@0 | 236 | observed: false, |
michael@0 | 237 | observe: function(subject, topic, data) { |
michael@0 | 238 | this.observed = !this.observed; |
michael@0 | 239 | do_check_true(subject instanceof Ci.nsIPrefBranch); |
michael@0 | 240 | do_check_eq(topic, "nsPref:changed"); |
michael@0 | 241 | do_check_eq(data, "test_observe_prefs_nsIObserver"); |
michael@0 | 242 | } |
michael@0 | 243 | }; |
michael@0 | 244 | |
michael@0 | 245 | Preferences.observe("test_observe_prefs_nsIObserver", observer); |
michael@0 | 246 | Preferences.set("test_observe_prefs_nsIObserver", "something"); |
michael@0 | 247 | do_check_true(observer.observed); |
michael@0 | 248 | |
michael@0 | 249 | Preferences.ignore("test_observe_prefs_nsIObserver", observer); |
michael@0 | 250 | Preferences.set("test_observe_prefs_nsIObserver", "something else"); |
michael@0 | 251 | do_check_true(observer.observed); |
michael@0 | 252 | |
michael@0 | 253 | // Clean up. |
michael@0 | 254 | Preferences.reset("test_observe_prefs_nsIObserver"); |
michael@0 | 255 | |
michael@0 | 256 | run_next_test(); |
michael@0 | 257 | }); |
michael@0 | 258 | |
michael@0 | 259 | /* |
michael@0 | 260 | add_test(function test_observe_exact_pref() { |
michael@0 | 261 | let observed = false; |
michael@0 | 262 | let observer = function() { observed = !observed }; |
michael@0 | 263 | |
michael@0 | 264 | Preferences.observe("test_observe_exact_pref", observer); |
michael@0 | 265 | Preferences.set("test_observe_exact_pref.sub-pref", "something"); |
michael@0 | 266 | do_check_false(observed); |
michael@0 | 267 | |
michael@0 | 268 | // Clean up. |
michael@0 | 269 | Preferences.ignore("test_observe_exact_pref", observer); |
michael@0 | 270 | Preferences.reset("test_observe_exact_pref.sub-pref"); |
michael@0 | 271 | |
michael@0 | 272 | run_next_test(); |
michael@0 | 273 | }); |
michael@0 | 274 | */ |
michael@0 | 275 | |
michael@0 | 276 | add_test(function test_observe_value_of_set_pref() { |
michael@0 | 277 | let observer = function(newVal) { do_check_eq(newVal, "something") }; |
michael@0 | 278 | |
michael@0 | 279 | Preferences.observe("test_observe_value_of_set_pref", observer); |
michael@0 | 280 | Preferences.set("test_observe_value_of_set_pref", "something"); |
michael@0 | 281 | |
michael@0 | 282 | // Clean up. |
michael@0 | 283 | Preferences.ignore("test_observe_value_of_set_pref", observer); |
michael@0 | 284 | Preferences.reset("test_observe_value_of_set_pref"); |
michael@0 | 285 | |
michael@0 | 286 | run_next_test(); |
michael@0 | 287 | }); |
michael@0 | 288 | |
michael@0 | 289 | add_test(function test_observe_value_of_reset_pref() { |
michael@0 | 290 | let observer = function(newVal) { do_check_true(typeof newVal == "undefined") }; |
michael@0 | 291 | |
michael@0 | 292 | Preferences.set("test_observe_value_of_reset_pref", "something"); |
michael@0 | 293 | Preferences.observe("test_observe_value_of_reset_pref", observer); |
michael@0 | 294 | Preferences.reset("test_observe_value_of_reset_pref"); |
michael@0 | 295 | |
michael@0 | 296 | // Clean up. |
michael@0 | 297 | Preferences.ignore("test_observe_value_of_reset_pref", observer); |
michael@0 | 298 | |
michael@0 | 299 | run_next_test(); |
michael@0 | 300 | }); |
michael@0 | 301 | |
michael@0 | 302 | add_test(function test_has_pref() { |
michael@0 | 303 | do_check_false(Preferences.has("test_has_pref")); |
michael@0 | 304 | Preferences.set("test_has_pref", "foo"); |
michael@0 | 305 | do_check_true(Preferences.has("test_has_pref")); |
michael@0 | 306 | |
michael@0 | 307 | Preferences.set("test_has_pref.foo", "foo"); |
michael@0 | 308 | Preferences.set("test_has_pref.bar", "bar"); |
michael@0 | 309 | let [hasFoo, hasBar, hasBaz] = Preferences.has(["test_has_pref.foo", |
michael@0 | 310 | "test_has_pref.bar", |
michael@0 | 311 | "test_has_pref.baz"]); |
michael@0 | 312 | do_check_true(hasFoo); |
michael@0 | 313 | do_check_true(hasBar); |
michael@0 | 314 | do_check_false(hasBaz); |
michael@0 | 315 | |
michael@0 | 316 | // Clean up. |
michael@0 | 317 | Preferences.resetBranch("test_has_pref"); |
michael@0 | 318 | |
michael@0 | 319 | run_next_test(); |
michael@0 | 320 | }); |
michael@0 | 321 | |
michael@0 | 322 | add_test(function test_isSet_pref() { |
michael@0 | 323 | // Use a pref that we know has a default value but no user-set value. |
michael@0 | 324 | // This feels dangerous; perhaps we should create some other default prefs |
michael@0 | 325 | // that we can use for testing. |
michael@0 | 326 | do_check_false(Preferences.isSet("toolkit.defaultChromeURI")); |
michael@0 | 327 | Preferences.set("toolkit.defaultChromeURI", "foo"); |
michael@0 | 328 | do_check_true(Preferences.isSet("toolkit.defaultChromeURI")); |
michael@0 | 329 | |
michael@0 | 330 | // Clean up. |
michael@0 | 331 | Preferences.reset("toolkit.defaultChromeURI"); |
michael@0 | 332 | |
michael@0 | 333 | run_next_test(); |
michael@0 | 334 | }); |
michael@0 | 335 | |
michael@0 | 336 | /* |
michael@0 | 337 | add_test(function test_lock_prefs() { |
michael@0 | 338 | // Use a pref that we know has a default value. |
michael@0 | 339 | // This feels dangerous; perhaps we should create some other default prefs |
michael@0 | 340 | // that we can use for testing. |
michael@0 | 341 | do_check_false(Preferences.locked("toolkit.defaultChromeURI")); |
michael@0 | 342 | Preferences.lock("toolkit.defaultChromeURI"); |
michael@0 | 343 | do_check_true(Preferences.locked("toolkit.defaultChromeURI")); |
michael@0 | 344 | Preferences.unlock("toolkit.defaultChromeURI"); |
michael@0 | 345 | do_check_false(Preferences.locked("toolkit.defaultChromeURI")); |
michael@0 | 346 | |
michael@0 | 347 | let val = Preferences.get("toolkit.defaultChromeURI"); |
michael@0 | 348 | Preferences.set("toolkit.defaultChromeURI", "test_lock_prefs"); |
michael@0 | 349 | do_check_eq(Preferences.get("toolkit.defaultChromeURI"), "test_lock_prefs"); |
michael@0 | 350 | Preferences.lock("toolkit.defaultChromeURI"); |
michael@0 | 351 | do_check_eq(Preferences.get("toolkit.defaultChromeURI"), val); |
michael@0 | 352 | Preferences.unlock("toolkit.defaultChromeURI"); |
michael@0 | 353 | do_check_eq(Preferences.get("toolkit.defaultChromeURI"), "test_lock_prefs"); |
michael@0 | 354 | |
michael@0 | 355 | // Clean up. |
michael@0 | 356 | Preferences.reset("toolkit.defaultChromeURI"); |
michael@0 | 357 | |
michael@0 | 358 | run_next_test(); |
michael@0 | 359 | }); |
michael@0 | 360 | */ |