Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 var prefObserver = {
7 setCalledNum: 0,
8 onContentPrefSet: function(aGroup, aName, aValue) {
9 this.setCalledNum++;
10 },
11 removedCalledNum: 0,
12 onContentPrefRemoved: function(aGroup, aName) {
13 this.removedCalledNum++;
14 }
15 };
17 function run_test() {
18 let loadContext = { get usePrivateBrowsing() { return gInPrivateBrowsing; } };
20 var cps = new ContentPrefInstance(loadContext);
21 cps.removeGroupedPrefs();
23 var uri = ContentPrefTest.getURI("http://www.example.com/");
24 var group = cps.grouper.group(uri);
26 // first, set a pref in normal mode
27 cps.setPref(uri, "value", "foo");
28 cps.setPref(null, "value-global", "foo-global");
30 var num;
31 cps.addObserver("value", prefObserver);
32 cps.addObserver("value-global", prefObserver);
34 enterPBMode();
36 // test setPref
37 num = prefObserver.setCalledNum;
38 cps.setPref(uri, "value", "foo-private-browsing");
39 do_check_eq(cps.hasPref(uri, "value"), true);
40 do_check_eq(cps.getPref(uri, "value"), "foo-private-browsing");
41 do_check_eq(prefObserver.setCalledNum, num + 1);
43 num = prefObserver.setCalledNum;
44 cps.setPref(null, "value-global", "foo-private-browsing-global");
45 do_check_eq(cps.hasPref(null, "value-global"), true);
46 do_check_eq(cps.getPref(null, "value-global"), "foo-private-browsing-global");
47 do_check_eq(prefObserver.setCalledNum, num + 1);
49 // test removePref
50 num = prefObserver.removedCalledNum;
51 cps.removePref(uri, "value");
52 do_check_eq(cps.hasPref(uri, "value"), true);
53 // fallback to non private mode value
54 do_check_eq(cps.getPref(uri, "value"), "foo");
55 do_check_eq(prefObserver.removedCalledNum, num + 1);
57 num = prefObserver.removedCalledNum;
58 cps.removePref(null, "value-global");
59 do_check_eq(cps.hasPref(null, "value-global"), true);
60 // fallback to non private mode value
61 do_check_eq(cps.getPref(null, "value-global"), "foo-global") ;
62 do_check_eq(prefObserver.removedCalledNum, num + 1);
64 // test removeGroupedPrefs
65 cps.setPref(uri, "value", "foo-private-browsing");
66 cps.removeGroupedPrefs();
67 do_check_eq(cps.hasPref(uri, "value"), false);
68 do_check_eq(cps.getPref(uri, "value"), undefined);
70 cps.setPref(null, "value-global", "foo-private-browsing-global");
71 cps.removeGroupedPrefs();
72 do_check_eq(cps.hasPref(null, "value-global"), true);
73 do_check_eq(cps.getPref(null, "value-global"), "foo-private-browsing-global");
75 // test removePrefsByName
76 num = prefObserver.removedCalledNum;
77 cps.setPref(uri, "value", "foo-private-browsing");
78 cps.removePrefsByName("value");
79 do_check_eq(cps.hasPref(uri, "value"), false);
80 do_check_eq(cps.getPref(uri, "value"), undefined);
81 do_check_true(prefObserver.removedCalledNum > num);
83 num = prefObserver.removedCalledNum;
84 cps.setPref(null, "value-global", "foo-private-browsing");
85 cps.removePrefsByName("value-global");
86 do_check_eq(cps.hasPref(null, "value-global"), false);
87 do_check_eq(cps.getPref(null, "value-global"), undefined);
88 do_check_true(prefObserver.removedCalledNum > num);
90 // test getPrefs
91 cps.setPref(uri, "value", "foo-private-browsing");
92 do_check_eq(cps.getPrefs(uri).getProperty("value"), "foo-private-browsing");
94 cps.setPref(null, "value-global", "foo-private-browsing-global");
95 do_check_eq(cps.getPrefs(null).getProperty("value-global"), "foo-private-browsing-global");
97 // test getPrefsByName
98 do_check_eq(cps.getPrefsByName("value").getProperty(group), "foo-private-browsing");
99 do_check_eq(cps.getPrefsByName("value-global").getProperty(null), "foo-private-browsing-global");
101 cps.removeObserver("value", prefObserver);
102 cps.removeObserver("value-global", prefObserver);
103 }