Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
1 // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 Components.utils.import("resource://gre/modules/SharedPreferences.jsm");
7 Components.utils.import("resource://gre/modules/Promise.jsm");
9 let _observerId = 0;
11 function makeObserver() {
12 let deferred = Promise.defer();
14 let ret = {
15 id: _observerId++,
16 count: 0,
17 promise: deferred.promise,
18 observe: function (subject, topic, data) {
19 ret.count += 1;
20 let msg = { subject: subject,
21 topic: topic,
22 data: data };
23 deferred.resolve(msg);
24 },
25 };
27 return ret;
28 };
30 add_task(function test_get_set() {
31 let branch = new SharedPreferences("test");
33 branch.setBoolPref("boolKey", true);
34 branch.setCharPref("charKey", "string value");
35 branch.setIntPref("intKey", 1000);
37 do_check_eq(branch.getBoolPref("boolKey"), true);
38 do_check_eq(branch.getCharPref("charKey"), "string value");
39 do_check_eq(branch.getIntPref("intKey"), 1000);
41 branch.setBoolPref("boolKey", false);
42 branch.setCharPref("charKey", "different string value");
43 branch.setIntPref("intKey", -2000);
45 do_check_eq(branch.getBoolPref("boolKey"), false);
46 do_check_eq(branch.getCharPref("charKey"), "different string value");
47 do_check_eq(branch.getIntPref("intKey"), -2000);
49 do_check_eq(typeof(branch.getBoolPref("boolKey")), "boolean");
50 do_check_eq(typeof(branch.getCharPref("charKey")), "string");
51 do_check_eq(typeof(branch.getIntPref("intKey")), "number");
52 });
54 add_task(function test_default() {
55 let branch = new SharedPreferences();
57 branch.setBoolPref("boolKey", true);
58 branch.setCharPref("charKey", "string value");
59 branch.setIntPref("intKey", 1000);
61 do_check_eq(branch.getBoolPref("boolKey"), true);
62 do_check_eq(branch.getCharPref("charKey"), "string value");
63 do_check_eq(branch.getIntPref("intKey"), 1000);
65 branch.setBoolPref("boolKey", false);
66 branch.setCharPref("charKey", "different string value");
67 branch.setIntPref("intKey", -2000);
69 do_check_eq(branch.getBoolPref("boolKey"), false);
70 do_check_eq(branch.getCharPref("charKey"), "different string value");
71 do_check_eq(branch.getIntPref("intKey"), -2000);
73 do_check_eq(typeof(branch.getBoolPref("boolKey")), "boolean");
74 do_check_eq(typeof(branch.getCharPref("charKey")), "string");
75 do_check_eq(typeof(branch.getIntPref("intKey")), "number");
76 });
78 add_task(function test_multiple_branches() {
79 let branch1 = new SharedPreferences("test1");
80 let branch2 = new SharedPreferences("test2");
82 branch1.setBoolPref("boolKey", true);
83 branch2.setBoolPref("boolKey", false);
85 do_check_eq(branch1.getBoolPref("boolKey"), true);
86 do_check_eq(branch2.getBoolPref("boolKey"), false);
88 branch1.setCharPref("charKey", "a value");
89 branch2.setCharPref("charKey", "a different value");
91 do_check_eq(branch1.getCharPref("charKey"), "a value");
92 do_check_eq(branch2.getCharPref("charKey"), "a different value");
93 });
95 add_task(function test_add_remove_observer() {
96 let branch = new SharedPreferences("test");
98 branch.setBoolPref("boolKey", false);
99 do_check_eq(branch.getBoolPref("boolKey"), false);
101 let obs1 = makeObserver();
102 branch.addObserver("boolKey", obs1);
104 try {
105 branch.setBoolPref("boolKey", true);
106 do_check_eq(branch.getBoolPref("boolKey"), true);
108 let value1 = yield obs1.promise;
109 do_check_eq(obs1.count, 1);
111 do_check_eq(value1.subject, obs1);
112 do_check_eq(value1.topic, "boolKey");
113 do_check_eq(typeof(value1.data), "boolean");
114 do_check_eq(value1.data, true);
115 } finally {
116 branch.removeObserver("boolKey", obs1);
117 }
119 // Make sure the original observer is really gone, or as close as
120 // we: install a second observer, wait for it to be notified, and
121 // then verify the original observer was *not* notified. This
122 // depends, of course, on the order that observers are notified, but
123 // is better than nothing.
125 let obs2 = makeObserver();
126 branch.addObserver("boolKey", obs2);
128 try {
129 branch.setBoolPref("boolKey", false);
130 do_check_eq(branch.getBoolPref("boolKey"), false);
132 let value2 = yield obs2.promise;
133 do_check_eq(obs2.count, 1);
135 do_check_eq(value2.subject, obs2);
136 do_check_eq(value2.topic, "boolKey");
137 do_check_eq(typeof(value2.data), "boolean");
138 do_check_eq(value2.data, false);
140 // Original observer count is preserved.
141 do_check_eq(obs1.count, 1);
142 } finally {
143 branch.removeObserver("boolKey", obs2);
144 }
145 });
147 add_task(function test_observer_ignores() {
148 let branch = new SharedPreferences("test");
150 branch.setCharPref("charKey", "first value");
151 do_check_eq(branch.getCharPref("charKey"), "first value");
153 let obs = makeObserver();
154 branch.addObserver("charKey", obs);
156 try {
157 // These should all be ignored.
158 branch.setBoolPref("boolKey", true);
159 branch.setBoolPref("boolKey", false);
160 branch.setIntPref("intKey", -3000);
161 branch.setIntPref("intKey", 4000);
163 branch.setCharPref("charKey", "a value");
164 let value = yield obs.promise;
166 // Observer should have been notified exactly once.
167 do_check_eq(obs.count, 1);
169 do_check_eq(value.subject, obs);
170 do_check_eq(value.topic, "charKey");
171 do_check_eq(typeof(value.data), "string");
172 do_check_eq(value.data, "a value");
173 } finally {
174 branch.removeObserver("charKey", obs);
175 }
176 });
178 add_task(function test_observer_ignores_branches() {
179 let branch = new SharedPreferences("test");
181 branch.setCharPref("charKey", "first value");
182 do_check_eq(branch.getCharPref("charKey"), "first value");
184 let obs = makeObserver();
185 branch.addObserver("charKey", obs);
187 try {
188 // These should all be ignored.
189 let branch2 = new SharedPreferences("test2");
190 branch2.setCharPref("charKey", "a wrong value");
191 let branch3 = new SharedPreferences("test.2");
192 branch3.setCharPref("charKey", "a different wrong value");
194 // This should not be ignored.
195 branch.setCharPref("charKey", "a value");
197 let value = yield obs.promise;
199 // Observer should have been notified exactly once.
200 do_check_eq(obs.count, 1);
202 do_check_eq(value.subject, obs);
203 do_check_eq(value.topic, "charKey");
204 do_check_eq(typeof(value.data), "string");
205 do_check_eq(value.data, "a value");
206 } finally {
207 branch.removeObserver("charKey", obs);
208 }
209 });
211 run_next_test();