1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/modules/tests/xpcshell/test_Preferences.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,360 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +const {classes: Cc, interfaces: Ci, results: Cr, utils: Cu, manager: Cm} = Components; 1.8 + 1.9 +Cu.import("resource://gre/modules/Preferences.jsm"); 1.10 + 1.11 +function run_test() { 1.12 + run_next_test(); 1.13 +} 1.14 + 1.15 +add_test(function test_set_get_pref() { 1.16 + Preferences.set("test_set_get_pref.integer", 1); 1.17 + do_check_eq(Preferences.get("test_set_get_pref.integer"), 1); 1.18 + 1.19 + Preferences.set("test_set_get_pref.string", "foo"); 1.20 + do_check_eq(Preferences.get("test_set_get_pref.string"), "foo"); 1.21 + 1.22 + Preferences.set("test_set_get_pref.boolean", true); 1.23 + do_check_eq(Preferences.get("test_set_get_pref.boolean"), true); 1.24 + 1.25 + // Clean up. 1.26 + Preferences.resetBranch("test_set_get_pref."); 1.27 + 1.28 + run_next_test(); 1.29 +}); 1.30 + 1.31 +add_test(function test_set_get_branch_pref() { 1.32 + let prefs = new Preferences("test_set_get_branch_pref."); 1.33 + 1.34 + prefs.set("something", 1); 1.35 + do_check_eq(prefs.get("something"), 1); 1.36 + do_check_false(Preferences.has("something")); 1.37 + 1.38 + // Clean up. 1.39 + prefs.reset("something"); 1.40 + 1.41 + run_next_test(); 1.42 +}); 1.43 + 1.44 +add_test(function test_set_get_multiple_prefs() { 1.45 + Preferences.set({ "test_set_get_multiple_prefs.integer": 1, 1.46 + "test_set_get_multiple_prefs.string": "foo", 1.47 + "test_set_get_multiple_prefs.boolean": true }); 1.48 + 1.49 + let [i, s, b] = Preferences.get(["test_set_get_multiple_prefs.integer", 1.50 + "test_set_get_multiple_prefs.string", 1.51 + "test_set_get_multiple_prefs.boolean"]); 1.52 + 1.53 + do_check_eq(i, 1); 1.54 + do_check_eq(s, "foo"); 1.55 + do_check_eq(b, true); 1.56 + 1.57 + // Clean up. 1.58 + Preferences.resetBranch("test_set_get_multiple_prefs."); 1.59 + 1.60 + run_next_test(); 1.61 +}); 1.62 + 1.63 +add_test(function test_get_multiple_prefs_with_default_value() { 1.64 + Preferences.set({ "test_get_multiple_prefs_with_default_value.a": 1, 1.65 + "test_get_multiple_prefs_with_default_value.b": 2 }); 1.66 + 1.67 + let [a, b, c] = Preferences.get(["test_get_multiple_prefs_with_default_value.a", 1.68 + "test_get_multiple_prefs_with_default_value.b", 1.69 + "test_get_multiple_prefs_with_default_value.c"], 1.70 + 0); 1.71 + 1.72 + do_check_eq(a, 1); 1.73 + do_check_eq(b, 2); 1.74 + do_check_eq(c, 0); 1.75 + 1.76 + // Clean up. 1.77 + Preferences.resetBranch("test_get_multiple_prefs_with_default_value."); 1.78 + 1.79 + run_next_test(); 1.80 +}); 1.81 + 1.82 +add_test(function test_set_get_unicode_pref() { 1.83 + Preferences.set("test_set_get_unicode_pref", String.fromCharCode(960)); 1.84 + do_check_eq(Preferences.get("test_set_get_unicode_pref"), String.fromCharCode(960)); 1.85 + 1.86 + // Clean up. 1.87 + Preferences.reset("test_set_get_unicode_pref"); 1.88 + 1.89 + run_next_test(); 1.90 +}); 1.91 + 1.92 +add_test(function test_set_null_pref() { 1.93 + try { 1.94 + Preferences.set("test_set_null_pref", null); 1.95 + // We expect this to throw, so the test is designed to fail if it doesn't. 1.96 + do_check_true(false); 1.97 + } 1.98 + catch(ex) {} 1.99 + 1.100 + run_next_test(); 1.101 +}); 1.102 + 1.103 +add_test(function test_set_undefined_pref() { 1.104 + try { 1.105 + Preferences.set("test_set_undefined_pref"); 1.106 + // We expect this to throw, so the test is designed to fail if it doesn't. 1.107 + do_check_true(false); 1.108 + } 1.109 + catch(ex) {} 1.110 + 1.111 + run_next_test(); 1.112 +}); 1.113 + 1.114 +add_test(function test_set_unsupported_pref() { 1.115 + try { 1.116 + Preferences.set("test_set_unsupported_pref", new Array()); 1.117 + // We expect this to throw, so the test is designed to fail if it doesn't. 1.118 + do_check_true(false); 1.119 + } 1.120 + catch(ex) {} 1.121 + 1.122 + run_next_test(); 1.123 +}); 1.124 + 1.125 +// Make sure that we can get a string pref that we didn't set ourselves 1.126 +// (i.e. that the way we get a string pref using getComplexValue doesn't 1.127 +// hork us getting a string pref that wasn't set using setComplexValue). 1.128 +add_test(function test_get_string_pref() { 1.129 + let svc = Cc["@mozilla.org/preferences-service;1"]. 1.130 + getService(Ci.nsIPrefService). 1.131 + getBranch(""); 1.132 + svc.setCharPref("test_get_string_pref", "a normal string"); 1.133 + do_check_eq(Preferences.get("test_get_string_pref"), "a normal string"); 1.134 + 1.135 + // Clean up. 1.136 + Preferences.reset("test_get_string_pref"); 1.137 + 1.138 + run_next_test(); 1.139 +}); 1.140 + 1.141 +add_test(function test_set_get_number_pref() { 1.142 + Preferences.set("test_set_get_number_pref", 5); 1.143 + do_check_eq(Preferences.get("test_set_get_number_pref"), 5); 1.144 + 1.145 + // Non-integer values get converted to integers. 1.146 + Preferences.set("test_set_get_number_pref", 3.14159); 1.147 + do_check_eq(Preferences.get("test_set_get_number_pref"), 3); 1.148 + 1.149 + // Values outside the range -(2^31-1) to 2^31-1 overflow. 1.150 + try { 1.151 + Preferences.set("test_set_get_number_pref", Math.pow(2, 31)); 1.152 + // We expect this to throw, so the test is designed to fail if it doesn't. 1.153 + do_check_true(false); 1.154 + } 1.155 + catch(ex) {} 1.156 + 1.157 + // Clean up. 1.158 + Preferences.reset("test_set_get_number_pref"); 1.159 + 1.160 + run_next_test(); 1.161 +}); 1.162 + 1.163 +add_test(function test_reset_pref() { 1.164 + Preferences.set("test_reset_pref", 1); 1.165 + Preferences.reset("test_reset_pref"); 1.166 + do_check_eq(Preferences.get("test_reset_pref"), undefined); 1.167 + 1.168 + run_next_test(); 1.169 +}); 1.170 + 1.171 +add_test(function test_reset_pref_branch() { 1.172 + Preferences.set("test_reset_pref_branch.foo", 1); 1.173 + Preferences.set("test_reset_pref_branch.bar", 2); 1.174 + Preferences.resetBranch("test_reset_pref_branch."); 1.175 + do_check_eq(Preferences.get("test_reset_pref_branch.foo"), undefined); 1.176 + do_check_eq(Preferences.get("test_reset_pref_branch.bar"), undefined); 1.177 + 1.178 + run_next_test(); 1.179 +}); 1.180 + 1.181 +// Make sure the module doesn't throw an exception when asked to reset 1.182 +// a nonexistent pref. 1.183 +add_test(function test_reset_nonexistent_pref() { 1.184 + Preferences.reset("test_reset_nonexistent_pref"); 1.185 + 1.186 + run_next_test(); 1.187 +}); 1.188 + 1.189 +// Make sure the module doesn't throw an exception when asked to reset 1.190 +// a nonexistent pref branch. 1.191 +add_test(function test_reset_nonexistent_pref_branch() { 1.192 + Preferences.resetBranch("test_reset_nonexistent_pref_branch."); 1.193 + 1.194 + run_next_test(); 1.195 +}); 1.196 + 1.197 +add_test(function test_observe_prefs_function() { 1.198 + let observed = false; 1.199 + let observer = function() { observed = !observed }; 1.200 + 1.201 + Preferences.observe("test_observe_prefs_function", observer); 1.202 + Preferences.set("test_observe_prefs_function", "something"); 1.203 + do_check_true(observed); 1.204 + 1.205 + Preferences.ignore("test_observe_prefs_function", observer); 1.206 + Preferences.set("test_observe_prefs_function", "something else"); 1.207 + do_check_true(observed); 1.208 + 1.209 + // Clean up. 1.210 + Preferences.reset("test_observe_prefs_function"); 1.211 + 1.212 + run_next_test(); 1.213 +}); 1.214 + 1.215 +add_test(function test_observe_prefs_object() { 1.216 + let observer = { 1.217 + observed: false, 1.218 + observe: function() { 1.219 + this.observed = !this.observed; 1.220 + } 1.221 + }; 1.222 + 1.223 + Preferences.observe("test_observe_prefs_object", observer.observe, observer); 1.224 + Preferences.set("test_observe_prefs_object", "something"); 1.225 + do_check_true(observer.observed); 1.226 + 1.227 + Preferences.ignore("test_observe_prefs_object", observer.observe, observer); 1.228 + Preferences.set("test_observe_prefs_object", "something else"); 1.229 + do_check_true(observer.observed); 1.230 + 1.231 + // Clean up. 1.232 + Preferences.reset("test_observe_prefs_object"); 1.233 + 1.234 + run_next_test(); 1.235 +}); 1.236 + 1.237 +add_test(function test_observe_prefs_nsIObserver() { 1.238 + let observer = { 1.239 + observed: false, 1.240 + observe: function(subject, topic, data) { 1.241 + this.observed = !this.observed; 1.242 + do_check_true(subject instanceof Ci.nsIPrefBranch); 1.243 + do_check_eq(topic, "nsPref:changed"); 1.244 + do_check_eq(data, "test_observe_prefs_nsIObserver"); 1.245 + } 1.246 + }; 1.247 + 1.248 + Preferences.observe("test_observe_prefs_nsIObserver", observer); 1.249 + Preferences.set("test_observe_prefs_nsIObserver", "something"); 1.250 + do_check_true(observer.observed); 1.251 + 1.252 + Preferences.ignore("test_observe_prefs_nsIObserver", observer); 1.253 + Preferences.set("test_observe_prefs_nsIObserver", "something else"); 1.254 + do_check_true(observer.observed); 1.255 + 1.256 + // Clean up. 1.257 + Preferences.reset("test_observe_prefs_nsIObserver"); 1.258 + 1.259 + run_next_test(); 1.260 +}); 1.261 + 1.262 +/* 1.263 +add_test(function test_observe_exact_pref() { 1.264 + let observed = false; 1.265 + let observer = function() { observed = !observed }; 1.266 + 1.267 + Preferences.observe("test_observe_exact_pref", observer); 1.268 + Preferences.set("test_observe_exact_pref.sub-pref", "something"); 1.269 + do_check_false(observed); 1.270 + 1.271 + // Clean up. 1.272 + Preferences.ignore("test_observe_exact_pref", observer); 1.273 + Preferences.reset("test_observe_exact_pref.sub-pref"); 1.274 + 1.275 + run_next_test(); 1.276 +}); 1.277 +*/ 1.278 + 1.279 +add_test(function test_observe_value_of_set_pref() { 1.280 + let observer = function(newVal) { do_check_eq(newVal, "something") }; 1.281 + 1.282 + Preferences.observe("test_observe_value_of_set_pref", observer); 1.283 + Preferences.set("test_observe_value_of_set_pref", "something"); 1.284 + 1.285 + // Clean up. 1.286 + Preferences.ignore("test_observe_value_of_set_pref", observer); 1.287 + Preferences.reset("test_observe_value_of_set_pref"); 1.288 + 1.289 + run_next_test(); 1.290 +}); 1.291 + 1.292 +add_test(function test_observe_value_of_reset_pref() { 1.293 + let observer = function(newVal) { do_check_true(typeof newVal == "undefined") }; 1.294 + 1.295 + Preferences.set("test_observe_value_of_reset_pref", "something"); 1.296 + Preferences.observe("test_observe_value_of_reset_pref", observer); 1.297 + Preferences.reset("test_observe_value_of_reset_pref"); 1.298 + 1.299 + // Clean up. 1.300 + Preferences.ignore("test_observe_value_of_reset_pref", observer); 1.301 + 1.302 + run_next_test(); 1.303 +}); 1.304 + 1.305 +add_test(function test_has_pref() { 1.306 + do_check_false(Preferences.has("test_has_pref")); 1.307 + Preferences.set("test_has_pref", "foo"); 1.308 + do_check_true(Preferences.has("test_has_pref")); 1.309 + 1.310 + Preferences.set("test_has_pref.foo", "foo"); 1.311 + Preferences.set("test_has_pref.bar", "bar"); 1.312 + let [hasFoo, hasBar, hasBaz] = Preferences.has(["test_has_pref.foo", 1.313 + "test_has_pref.bar", 1.314 + "test_has_pref.baz"]); 1.315 + do_check_true(hasFoo); 1.316 + do_check_true(hasBar); 1.317 + do_check_false(hasBaz); 1.318 + 1.319 + // Clean up. 1.320 + Preferences.resetBranch("test_has_pref"); 1.321 + 1.322 + run_next_test(); 1.323 +}); 1.324 + 1.325 +add_test(function test_isSet_pref() { 1.326 + // Use a pref that we know has a default value but no user-set value. 1.327 + // This feels dangerous; perhaps we should create some other default prefs 1.328 + // that we can use for testing. 1.329 + do_check_false(Preferences.isSet("toolkit.defaultChromeURI")); 1.330 + Preferences.set("toolkit.defaultChromeURI", "foo"); 1.331 + do_check_true(Preferences.isSet("toolkit.defaultChromeURI")); 1.332 + 1.333 + // Clean up. 1.334 + Preferences.reset("toolkit.defaultChromeURI"); 1.335 + 1.336 + run_next_test(); 1.337 +}); 1.338 + 1.339 +/* 1.340 +add_test(function test_lock_prefs() { 1.341 + // Use a pref that we know has a default value. 1.342 + // This feels dangerous; perhaps we should create some other default prefs 1.343 + // that we can use for testing. 1.344 + do_check_false(Preferences.locked("toolkit.defaultChromeURI")); 1.345 + Preferences.lock("toolkit.defaultChromeURI"); 1.346 + do_check_true(Preferences.locked("toolkit.defaultChromeURI")); 1.347 + Preferences.unlock("toolkit.defaultChromeURI"); 1.348 + do_check_false(Preferences.locked("toolkit.defaultChromeURI")); 1.349 + 1.350 + let val = Preferences.get("toolkit.defaultChromeURI"); 1.351 + Preferences.set("toolkit.defaultChromeURI", "test_lock_prefs"); 1.352 + do_check_eq(Preferences.get("toolkit.defaultChromeURI"), "test_lock_prefs"); 1.353 + Preferences.lock("toolkit.defaultChromeURI"); 1.354 + do_check_eq(Preferences.get("toolkit.defaultChromeURI"), val); 1.355 + Preferences.unlock("toolkit.defaultChromeURI"); 1.356 + do_check_eq(Preferences.get("toolkit.defaultChromeURI"), "test_lock_prefs"); 1.357 + 1.358 + // Clean up. 1.359 + Preferences.reset("toolkit.defaultChromeURI"); 1.360 + 1.361 + run_next_test(); 1.362 +}); 1.363 +*/