summaryrefslogtreecommitdiff
path: root/toolkit/components/contentprefs/tests/unit/test_bug679784.js
blob: 97251d87ba9c135955d50a6a56525edec3a06f84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */


var prefObserver = {
    setCalledNum: 0,
    onContentPrefSet: function(aGroup, aName, aValue) {
        this.setCalledNum++;
    },
    removedCalledNum: 0,
    onContentPrefRemoved: function(aGroup, aName) {
        this.removedCalledNum++;
    }
};

function run_test() {
  let loadContext = { get usePrivateBrowsing() { return gInPrivateBrowsing; } };

  var cps = new ContentPrefInstance(loadContext);
  cps.removeGroupedPrefs();

  var uri = ContentPrefTest.getURI("http://www.example.com/");
  var group = cps.grouper.group(uri);

  // first, set a pref in normal mode
  cps.setPref(uri, "value", "foo");
  cps.setPref(null, "value-global", "foo-global");

  var num;
  cps.addObserver("value", prefObserver);
  cps.addObserver("value-global", prefObserver);

  enterPBMode();

  // test setPref
  num = prefObserver.setCalledNum;
  cps.setPref(uri, "value", "foo-private-browsing");
  do_check_eq(cps.hasPref(uri, "value"), true);
  do_check_eq(cps.getPref(uri, "value"), "foo-private-browsing");
  do_check_eq(prefObserver.setCalledNum, num + 1);

  num = prefObserver.setCalledNum;
  cps.setPref(null, "value-global", "foo-private-browsing-global");
  do_check_eq(cps.hasPref(null, "value-global"), true);
  do_check_eq(cps.getPref(null, "value-global"), "foo-private-browsing-global");
  do_check_eq(prefObserver.setCalledNum, num + 1);

  // test removePref
  num = prefObserver.removedCalledNum;
  cps.removePref(uri, "value");
  do_check_eq(cps.hasPref(uri, "value"), true);
  // fallback to non private mode value
  do_check_eq(cps.getPref(uri, "value"), "foo");
  do_check_eq(prefObserver.removedCalledNum, num + 1);

  num = prefObserver.removedCalledNum;
  cps.removePref(null, "value-global");
  do_check_eq(cps.hasPref(null, "value-global"), true);
  // fallback to non private mode value
  do_check_eq(cps.getPref(null, "value-global"), "foo-global") ;
  do_check_eq(prefObserver.removedCalledNum, num + 1);

  // test removeGroupedPrefs
  cps.setPref(uri, "value", "foo-private-browsing");
  cps.removeGroupedPrefs();
  do_check_eq(cps.hasPref(uri, "value"), false);
  do_check_eq(cps.getPref(uri, "value"), undefined);

  cps.setPref(null, "value-global", "foo-private-browsing-global");
  cps.removeGroupedPrefs();
  do_check_eq(cps.hasPref(null, "value-global"), true);
  do_check_eq(cps.getPref(null, "value-global"), "foo-private-browsing-global");

  // test removePrefsByName
  num = prefObserver.removedCalledNum;
  cps.setPref(uri, "value", "foo-private-browsing");
  cps.removePrefsByName("value");
  do_check_eq(cps.hasPref(uri, "value"), false);
  do_check_eq(cps.getPref(uri, "value"), undefined);
  do_check_true(prefObserver.removedCalledNum > num);

  num = prefObserver.removedCalledNum;
  cps.setPref(null, "value-global", "foo-private-browsing");
  cps.removePrefsByName("value-global");
  do_check_eq(cps.hasPref(null, "value-global"), false);
  do_check_eq(cps.getPref(null, "value-global"), undefined);
  do_check_true(prefObserver.removedCalledNum > num);

  // test getPrefs
  cps.setPref(uri, "value", "foo-private-browsing");
  do_check_eq(cps.getPrefs(uri).getProperty("value"), "foo-private-browsing");

  cps.setPref(null, "value-global", "foo-private-browsing-global");
  do_check_eq(cps.getPrefs(null).getProperty("value-global"), "foo-private-browsing-global");

  // test getPrefsByName
  do_check_eq(cps.getPrefsByName("value").getProperty(group), "foo-private-browsing");
  do_check_eq(cps.getPrefsByName("value-global").getProperty(null), "foo-private-browsing-global");

  cps.removeObserver("value", prefObserver);
  cps.removeObserver("value-global", prefObserver);
}