summaryrefslogtreecommitdiffstats
path: root/browser/components/enterprisepolicies/tests/browser/browser_policies_setAndLockPref_API.js
blob: 0cad8e5aa3e6ff4cb57797332800dcfe49fc4b1b (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

let { Policies, setAndLockPref, PoliciesUtils } = ChromeUtils.importESModule(
  "resource:///modules/policies/Policies.sys.mjs"
);

add_task(async function test_API_directly() {
  await setupPolicyEngineWithJson("");
  setAndLockPref("policies.test.boolPref", true);
  checkLockedPref("policies.test.boolPref", true);

  // Check that a previously-locked pref can be changed
  // (it will be unlocked first).
  setAndLockPref("policies.test.boolPref", false);
  checkLockedPref("policies.test.boolPref", false);

  setAndLockPref("policies.test.intPref", 2);
  checkLockedPref("policies.test.intPref", 2);

  setAndLockPref("policies.test.stringPref", "policies test");
  checkLockedPref("policies.test.stringPref", "policies test");

  PoliciesUtils.setDefaultPref(
    "policies.test.lockedPref",
    "policies test",
    true
  );
  checkLockedPref("policies.test.lockedPref", "policies test");

  // Test that user values do not override the prefs, and the get*Pref call
  // still return the value set through setAndLockPref
  Services.prefs.setBoolPref("policies.test.boolPref", true);
  checkLockedPref("policies.test.boolPref", false);

  Services.prefs.setIntPref("policies.test.intPref", 10);
  checkLockedPref("policies.test.intPref", 2);

  Services.prefs.setStringPref("policies.test.stringPref", "policies test");
  checkLockedPref("policies.test.stringPref", "policies test");

  try {
    // Test that a non-integer value is correctly rejected, even though
    // typeof(val) == "number"
    setAndLockPref("policies.test.intPref", 1.5);
    ok(false, "Integer value should be rejected");
  } catch (ex) {
    ok(true, "Integer value was rejected");
  }
});

add_task(async function test_API_through_policies() {
  // Ensure that the values received by the policies have the correct
  // type to make sure things are properly working.

  // Implement functions to handle the three simple policies
  // that will be added to the schema.
  Policies.bool_policy = {
    onBeforeUIStartup(manager, param) {
      setAndLockPref("policies.test2.boolPref", param);
    },
  };

  Policies.int_policy = {
    onBeforeUIStartup(manager, param) {
      setAndLockPref("policies.test2.intPref", param);
    },
  };

  Policies.string_policy = {
    onBeforeUIStartup(manager, param) {
      setAndLockPref("policies.test2.stringPref", param);
    },
  };

  await setupPolicyEngineWithJson(
    // policies.json
    {
      policies: {
        bool_policy: true,
        int_policy: 42,
        string_policy: "policies test 2",
      },
    },

    // custom schema
    {
      properties: {
        bool_policy: {
          type: "boolean",
        },

        int_policy: {
          type: "integer",
        },

        string_policy: {
          type: "string",
        },
      },
    }
  );

  is(
    Services.policies.status,
    Ci.nsIEnterprisePolicies.ACTIVE,
    "Engine is active"
  );

  // The expected values come from config_setAndLockPref.json
  checkLockedPref("policies.test2.boolPref", true);
  checkLockedPref("policies.test2.intPref", 42);
  checkLockedPref("policies.test2.stringPref", "policies test 2");

  delete Policies.bool_policy;
  delete Policies.int_policy;
  delete Policies.string_policy;
});

add_task(async function test_pref_tracker() {
  // Tests the test harness functionality that tracks usage of
  // the setAndLockPref and setDefualtPref APIs.

  let defaults = Services.prefs.getDefaultBranch("");

  // Test prefs that had a default value and got changed to another
  defaults.setIntPref("test1.pref1", 10);
  defaults.setStringPref("test1.pref2", "test");

  setAndLockPref("test1.pref1", 20);
  PoliciesUtils.setDefaultPref("test1.pref2", "NEW VALUE");
  setAndLockPref("test1.pref3", "NEW VALUE");
  PoliciesUtils.setDefaultPref("test1.pref4", 20);

  PoliciesPrefTracker.restoreDefaultValues();

  is(
    Services.prefs.getIntPref("test1.pref1"),
    10,
    "Expected value for test1.pref1"
  );
  is(
    Services.prefs.getStringPref("test1.pref2"),
    "test",
    "Expected value for test1.pref2"
  );
  is(
    Services.prefs.prefIsLocked("test1.pref1"),
    false,
    "test1.pref1 got unlocked"
  );
  ok(
    !Services.prefs.getStringPref("test1.pref3", undefined),
    "test1.pref3 should have had its value unset"
  );
  is(
    Services.prefs.getIntPref("test1.pref4", -1),
    -1,
    "test1.pref4 should have had its value unset"
  );

  // Test a pref that had a default value and a user value
  defaults.setIntPref("test2.pref1", 10);
  Services.prefs.setIntPref("test2.pref1", 20);

  setAndLockPref("test2.pref1", 20);

  PoliciesPrefTracker.restoreDefaultValues();

  is(Services.prefs.getIntPref("test2.pref1"), 20, "Correct user value");
  is(defaults.getIntPref("test2.pref1"), 10, "Correct default value");
  is(
    Services.prefs.prefIsLocked("test2.pref1"),
    false,
    "felipe pref is not locked"
  );
});