PageRenderTime 33ms CodeModel.GetById 16ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/services/sync/tests/unit/test_Preferences.js

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