1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/tests/testSharedPreferences.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,211 @@ 1.4 +// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +Components.utils.import("resource://gre/modules/SharedPreferences.jsm"); 1.10 +Components.utils.import("resource://gre/modules/Promise.jsm"); 1.11 + 1.12 +let _observerId = 0; 1.13 + 1.14 +function makeObserver() { 1.15 + let deferred = Promise.defer(); 1.16 + 1.17 + let ret = { 1.18 + id: _observerId++, 1.19 + count: 0, 1.20 + promise: deferred.promise, 1.21 + observe: function (subject, topic, data) { 1.22 + ret.count += 1; 1.23 + let msg = { subject: subject, 1.24 + topic: topic, 1.25 + data: data }; 1.26 + deferred.resolve(msg); 1.27 + }, 1.28 + }; 1.29 + 1.30 + return ret; 1.31 +}; 1.32 + 1.33 +add_task(function test_get_set() { 1.34 + let branch = new SharedPreferences("test"); 1.35 + 1.36 + branch.setBoolPref("boolKey", true); 1.37 + branch.setCharPref("charKey", "string value"); 1.38 + branch.setIntPref("intKey", 1000); 1.39 + 1.40 + do_check_eq(branch.getBoolPref("boolKey"), true); 1.41 + do_check_eq(branch.getCharPref("charKey"), "string value"); 1.42 + do_check_eq(branch.getIntPref("intKey"), 1000); 1.43 + 1.44 + branch.setBoolPref("boolKey", false); 1.45 + branch.setCharPref("charKey", "different string value"); 1.46 + branch.setIntPref("intKey", -2000); 1.47 + 1.48 + do_check_eq(branch.getBoolPref("boolKey"), false); 1.49 + do_check_eq(branch.getCharPref("charKey"), "different string value"); 1.50 + do_check_eq(branch.getIntPref("intKey"), -2000); 1.51 + 1.52 + do_check_eq(typeof(branch.getBoolPref("boolKey")), "boolean"); 1.53 + do_check_eq(typeof(branch.getCharPref("charKey")), "string"); 1.54 + do_check_eq(typeof(branch.getIntPref("intKey")), "number"); 1.55 +}); 1.56 + 1.57 +add_task(function test_default() { 1.58 + let branch = new SharedPreferences(); 1.59 + 1.60 + branch.setBoolPref("boolKey", true); 1.61 + branch.setCharPref("charKey", "string value"); 1.62 + branch.setIntPref("intKey", 1000); 1.63 + 1.64 + do_check_eq(branch.getBoolPref("boolKey"), true); 1.65 + do_check_eq(branch.getCharPref("charKey"), "string value"); 1.66 + do_check_eq(branch.getIntPref("intKey"), 1000); 1.67 + 1.68 + branch.setBoolPref("boolKey", false); 1.69 + branch.setCharPref("charKey", "different string value"); 1.70 + branch.setIntPref("intKey", -2000); 1.71 + 1.72 + do_check_eq(branch.getBoolPref("boolKey"), false); 1.73 + do_check_eq(branch.getCharPref("charKey"), "different string value"); 1.74 + do_check_eq(branch.getIntPref("intKey"), -2000); 1.75 + 1.76 + do_check_eq(typeof(branch.getBoolPref("boolKey")), "boolean"); 1.77 + do_check_eq(typeof(branch.getCharPref("charKey")), "string"); 1.78 + do_check_eq(typeof(branch.getIntPref("intKey")), "number"); 1.79 +}); 1.80 + 1.81 +add_task(function test_multiple_branches() { 1.82 + let branch1 = new SharedPreferences("test1"); 1.83 + let branch2 = new SharedPreferences("test2"); 1.84 + 1.85 + branch1.setBoolPref("boolKey", true); 1.86 + branch2.setBoolPref("boolKey", false); 1.87 + 1.88 + do_check_eq(branch1.getBoolPref("boolKey"), true); 1.89 + do_check_eq(branch2.getBoolPref("boolKey"), false); 1.90 + 1.91 + branch1.setCharPref("charKey", "a value"); 1.92 + branch2.setCharPref("charKey", "a different value"); 1.93 + 1.94 + do_check_eq(branch1.getCharPref("charKey"), "a value"); 1.95 + do_check_eq(branch2.getCharPref("charKey"), "a different value"); 1.96 +}); 1.97 + 1.98 +add_task(function test_add_remove_observer() { 1.99 + let branch = new SharedPreferences("test"); 1.100 + 1.101 + branch.setBoolPref("boolKey", false); 1.102 + do_check_eq(branch.getBoolPref("boolKey"), false); 1.103 + 1.104 + let obs1 = makeObserver(); 1.105 + branch.addObserver("boolKey", obs1); 1.106 + 1.107 + try { 1.108 + branch.setBoolPref("boolKey", true); 1.109 + do_check_eq(branch.getBoolPref("boolKey"), true); 1.110 + 1.111 + let value1 = yield obs1.promise; 1.112 + do_check_eq(obs1.count, 1); 1.113 + 1.114 + do_check_eq(value1.subject, obs1); 1.115 + do_check_eq(value1.topic, "boolKey"); 1.116 + do_check_eq(typeof(value1.data), "boolean"); 1.117 + do_check_eq(value1.data, true); 1.118 + } finally { 1.119 + branch.removeObserver("boolKey", obs1); 1.120 + } 1.121 + 1.122 + // Make sure the original observer is really gone, or as close as 1.123 + // we: install a second observer, wait for it to be notified, and 1.124 + // then verify the original observer was *not* notified. This 1.125 + // depends, of course, on the order that observers are notified, but 1.126 + // is better than nothing. 1.127 + 1.128 + let obs2 = makeObserver(); 1.129 + branch.addObserver("boolKey", obs2); 1.130 + 1.131 + try { 1.132 + branch.setBoolPref("boolKey", false); 1.133 + do_check_eq(branch.getBoolPref("boolKey"), false); 1.134 + 1.135 + let value2 = yield obs2.promise; 1.136 + do_check_eq(obs2.count, 1); 1.137 + 1.138 + do_check_eq(value2.subject, obs2); 1.139 + do_check_eq(value2.topic, "boolKey"); 1.140 + do_check_eq(typeof(value2.data), "boolean"); 1.141 + do_check_eq(value2.data, false); 1.142 + 1.143 + // Original observer count is preserved. 1.144 + do_check_eq(obs1.count, 1); 1.145 + } finally { 1.146 + branch.removeObserver("boolKey", obs2); 1.147 + } 1.148 +}); 1.149 + 1.150 +add_task(function test_observer_ignores() { 1.151 + let branch = new SharedPreferences("test"); 1.152 + 1.153 + branch.setCharPref("charKey", "first value"); 1.154 + do_check_eq(branch.getCharPref("charKey"), "first value"); 1.155 + 1.156 + let obs = makeObserver(); 1.157 + branch.addObserver("charKey", obs); 1.158 + 1.159 + try { 1.160 + // These should all be ignored. 1.161 + branch.setBoolPref("boolKey", true); 1.162 + branch.setBoolPref("boolKey", false); 1.163 + branch.setIntPref("intKey", -3000); 1.164 + branch.setIntPref("intKey", 4000); 1.165 + 1.166 + branch.setCharPref("charKey", "a value"); 1.167 + let value = yield obs.promise; 1.168 + 1.169 + // Observer should have been notified exactly once. 1.170 + do_check_eq(obs.count, 1); 1.171 + 1.172 + do_check_eq(value.subject, obs); 1.173 + do_check_eq(value.topic, "charKey"); 1.174 + do_check_eq(typeof(value.data), "string"); 1.175 + do_check_eq(value.data, "a value"); 1.176 + } finally { 1.177 + branch.removeObserver("charKey", obs); 1.178 + } 1.179 +}); 1.180 + 1.181 +add_task(function test_observer_ignores_branches() { 1.182 + let branch = new SharedPreferences("test"); 1.183 + 1.184 + branch.setCharPref("charKey", "first value"); 1.185 + do_check_eq(branch.getCharPref("charKey"), "first value"); 1.186 + 1.187 + let obs = makeObserver(); 1.188 + branch.addObserver("charKey", obs); 1.189 + 1.190 + try { 1.191 + // These should all be ignored. 1.192 + let branch2 = new SharedPreferences("test2"); 1.193 + branch2.setCharPref("charKey", "a wrong value"); 1.194 + let branch3 = new SharedPreferences("test.2"); 1.195 + branch3.setCharPref("charKey", "a different wrong value"); 1.196 + 1.197 + // This should not be ignored. 1.198 + branch.setCharPref("charKey", "a value"); 1.199 + 1.200 + let value = yield obs.promise; 1.201 + 1.202 + // Observer should have been notified exactly once. 1.203 + do_check_eq(obs.count, 1); 1.204 + 1.205 + do_check_eq(value.subject, obs); 1.206 + do_check_eq(value.topic, "charKey"); 1.207 + do_check_eq(typeof(value.data), "string"); 1.208 + do_check_eq(value.data, "a value"); 1.209 + } finally { 1.210 + branch.removeObserver("charKey", obs); 1.211 + } 1.212 +}); 1.213 + 1.214 +run_next_test();