modules/libpref/test/unit/test_extprefs.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:2698aff58880
1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/licenses/publicdomain/ */
3
4 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
5 Components.utils.import("resource://gre/modules/Services.jsm");
6
7 // The profile directory is already set up in the head_ files.
8
9 function arrayenumerator(a)
10 {
11 return {
12 i_: 0,
13 QueryInterface: XPCOMUtils.generateQI([Ci.nsISimpleEnumerator]),
14 hasMoreElements: function ae_hasMoreElements() {
15 return this.i_ < a.length;
16 },
17 getNext: function ae_getNext() {
18 return a[this.i_++];
19 }
20 };
21 }
22
23 function run_test() {
24 var ps = Cc["@mozilla.org/preferences-service;1"].
25 getService(Ci.nsIPrefService).QueryInterface(Ci.nsIPrefBranch);
26
27 var extprefs = [do_get_file("extdata")];
28
29 var extProvider = {
30 QueryInterface: XPCOMUtils.generateQI([Ci.nsIDirectoryServiceProvider,
31 Ci.nsIDirectoryServiceProvider2]),
32 getFile: function ep_getFile() {
33 throw Cr.NS_ERROR_FAILURE;
34 },
35
36 getFiles: function ep_getFiles(key) {
37 if (key != "ExtPrefDL")
38 throw Cr.NS_ERROR_FAILURE;
39
40 return arrayenumerator(extprefs);
41 }
42 };
43
44 let prefFile = do_get_file("data/testPref.js");
45
46 do_check_throws(function() {
47 ps.getBoolPref("testExtPref.bool");
48 }, Cr.NS_ERROR_UNEXPECTED);
49 do_check_throws(function() {
50 ps.getBoolPref("testPref.bool1");
51 }, Cr.NS_ERROR_UNEXPECTED);
52
53 ps.readUserPrefs(prefFile);
54
55 do_check_true(ps.getBoolPref("testPref.bool1"));
56 ps.setBoolPref("testPref.bool1", false);
57 do_check_false(ps.getBoolPref("testPref.bool1"));
58
59 dirSvc.registerProvider(extProvider);
60 Services.obs.notifyObservers(null, "load-extension-defaults", null);
61
62 // The extension default should be available.
63 do_check_true(ps.getBoolPref("testExtPref.bool"));
64
65 // The extension default should not override existing user prefs
66 do_check_false(ps.getBoolPref("testPref.bool2"));
67
68 // The extension default should not modify existing set values
69 do_check_false(ps.getBoolPref("testPref.bool1"));
70 }

mercurial