|
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/. */ |
|
5 |
|
6 Components.utils.import("resource://gre/modules/SharedPreferences.jsm"); |
|
7 Components.utils.import("resource://gre/modules/Promise.jsm"); |
|
8 |
|
9 let _observerId = 0; |
|
10 |
|
11 function makeObserver() { |
|
12 let deferred = Promise.defer(); |
|
13 |
|
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 }; |
|
26 |
|
27 return ret; |
|
28 }; |
|
29 |
|
30 add_task(function test_get_set() { |
|
31 let branch = new SharedPreferences("test"); |
|
32 |
|
33 branch.setBoolPref("boolKey", true); |
|
34 branch.setCharPref("charKey", "string value"); |
|
35 branch.setIntPref("intKey", 1000); |
|
36 |
|
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); |
|
40 |
|
41 branch.setBoolPref("boolKey", false); |
|
42 branch.setCharPref("charKey", "different string value"); |
|
43 branch.setIntPref("intKey", -2000); |
|
44 |
|
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); |
|
48 |
|
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 }); |
|
53 |
|
54 add_task(function test_default() { |
|
55 let branch = new SharedPreferences(); |
|
56 |
|
57 branch.setBoolPref("boolKey", true); |
|
58 branch.setCharPref("charKey", "string value"); |
|
59 branch.setIntPref("intKey", 1000); |
|
60 |
|
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); |
|
64 |
|
65 branch.setBoolPref("boolKey", false); |
|
66 branch.setCharPref("charKey", "different string value"); |
|
67 branch.setIntPref("intKey", -2000); |
|
68 |
|
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); |
|
72 |
|
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 }); |
|
77 |
|
78 add_task(function test_multiple_branches() { |
|
79 let branch1 = new SharedPreferences("test1"); |
|
80 let branch2 = new SharedPreferences("test2"); |
|
81 |
|
82 branch1.setBoolPref("boolKey", true); |
|
83 branch2.setBoolPref("boolKey", false); |
|
84 |
|
85 do_check_eq(branch1.getBoolPref("boolKey"), true); |
|
86 do_check_eq(branch2.getBoolPref("boolKey"), false); |
|
87 |
|
88 branch1.setCharPref("charKey", "a value"); |
|
89 branch2.setCharPref("charKey", "a different value"); |
|
90 |
|
91 do_check_eq(branch1.getCharPref("charKey"), "a value"); |
|
92 do_check_eq(branch2.getCharPref("charKey"), "a different value"); |
|
93 }); |
|
94 |
|
95 add_task(function test_add_remove_observer() { |
|
96 let branch = new SharedPreferences("test"); |
|
97 |
|
98 branch.setBoolPref("boolKey", false); |
|
99 do_check_eq(branch.getBoolPref("boolKey"), false); |
|
100 |
|
101 let obs1 = makeObserver(); |
|
102 branch.addObserver("boolKey", obs1); |
|
103 |
|
104 try { |
|
105 branch.setBoolPref("boolKey", true); |
|
106 do_check_eq(branch.getBoolPref("boolKey"), true); |
|
107 |
|
108 let value1 = yield obs1.promise; |
|
109 do_check_eq(obs1.count, 1); |
|
110 |
|
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 } |
|
118 |
|
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. |
|
124 |
|
125 let obs2 = makeObserver(); |
|
126 branch.addObserver("boolKey", obs2); |
|
127 |
|
128 try { |
|
129 branch.setBoolPref("boolKey", false); |
|
130 do_check_eq(branch.getBoolPref("boolKey"), false); |
|
131 |
|
132 let value2 = yield obs2.promise; |
|
133 do_check_eq(obs2.count, 1); |
|
134 |
|
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); |
|
139 |
|
140 // Original observer count is preserved. |
|
141 do_check_eq(obs1.count, 1); |
|
142 } finally { |
|
143 branch.removeObserver("boolKey", obs2); |
|
144 } |
|
145 }); |
|
146 |
|
147 add_task(function test_observer_ignores() { |
|
148 let branch = new SharedPreferences("test"); |
|
149 |
|
150 branch.setCharPref("charKey", "first value"); |
|
151 do_check_eq(branch.getCharPref("charKey"), "first value"); |
|
152 |
|
153 let obs = makeObserver(); |
|
154 branch.addObserver("charKey", obs); |
|
155 |
|
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); |
|
162 |
|
163 branch.setCharPref("charKey", "a value"); |
|
164 let value = yield obs.promise; |
|
165 |
|
166 // Observer should have been notified exactly once. |
|
167 do_check_eq(obs.count, 1); |
|
168 |
|
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 }); |
|
177 |
|
178 add_task(function test_observer_ignores_branches() { |
|
179 let branch = new SharedPreferences("test"); |
|
180 |
|
181 branch.setCharPref("charKey", "first value"); |
|
182 do_check_eq(branch.getCharPref("charKey"), "first value"); |
|
183 |
|
184 let obs = makeObserver(); |
|
185 branch.addObserver("charKey", obs); |
|
186 |
|
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"); |
|
193 |
|
194 // This should not be ignored. |
|
195 branch.setCharPref("charKey", "a value"); |
|
196 |
|
197 let value = yield obs.promise; |
|
198 |
|
199 // Observer should have been notified exactly once. |
|
200 do_check_eq(obs.count, 1); |
|
201 |
|
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 }); |
|
210 |
|
211 run_next_test(); |