summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/update/tests/unit_aus_update/perInstallationPrefs.js
blob: 4a4a8ff2ae141898e382f9df29ad21275b4de708 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* 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/.
 */

let gPolicyFunctionResult;

async function testSetup() {
  // The setup functions we use for update testing typically allow for update.
  // But we are just testing preferences here. We don't want anything to
  // actually attempt to update. Also, because we are messing with the pref
  // system itself in this test, we want to make sure to use a pref outside of
  // that system to disable update.
  Services.prefs.setBoolPref(PREF_APP_UPDATE_DISABLEDFORTESTING, true);

  // We run these tests whether per-installation prefs are supported or not,
  // because this API needs to work in both cases.
  logTestInfo(
    "PER_INSTALLATION_PREFS_SUPPORTED = " +
      UpdateUtils.PER_INSTALLATION_PREFS_SUPPORTED.toString()
  );

  // Save the original state so we can restore it after the test.
  const originalPerInstallationPrefs = UpdateUtils.PER_INSTALLATION_PREFS;
  let configFile = getUpdateDirFile(FILE_UPDATE_CONFIG_JSON);
  try {
    configFile.moveTo(null, FILE_BACKUP_UPDATE_CONFIG_JSON);
  } catch (e) {}

  // Currently, features exist for per-installation prefs that are not used by
  // any actual pref. We added them because we intend to use these features in
  // the future. Thus, we will override the normally defined per-installation
  // prefs and provide our own, for the purposes of testing.
  UpdateUtils.PER_INSTALLATION_PREFS = {
    "test.pref.boolean": {
      type: UpdateUtils.PER_INSTALLATION_PREF_TYPE_BOOL,
      defaultValue: true,
      observerTopic: "test-pref-change-observer-boolean",
    },
    "test.pref.integer": {
      type: UpdateUtils.PER_INSTALLATION_PREF_TYPE_INT,
      defaultValue: 1234,
      observerTopic: "test-pref-change-observer-integer",
    },
    "test.pref.string": {
      type: UpdateUtils.PER_INSTALLATION_PREF_TYPE_ASCII_STRING,
      defaultValue: "<default>",
      observerTopic: "test-pref-change-observer-string",
    },
    "test.pref.policy": {
      type: UpdateUtils.PER_INSTALLATION_PREF_TYPE_INT,
      defaultValue: 1234,
      observerTopic: "test-pref-change-observer-policy",
      policyFn: () => gPolicyFunctionResult,
    },
  };
  // We need to re-initialize the pref system with these new prefs
  UpdateUtils.initPerInstallPrefs();

  registerCleanupFunction(() => {
    if (UpdateUtils.PER_INSTALLATION_PREFS_SUPPORTED) {
      let testConfigFile = getUpdateDirFile(FILE_UPDATE_CONFIG_JSON);
      let backupConfigFile = getUpdateDirFile(FILE_BACKUP_UPDATE_CONFIG_JSON);
      try {
        testConfigFile.remove(false);
        backupConfigFile.moveTo(null, FILE_UPDATE_CONFIG_JSON);
      } catch (ex) {}
    } else {
      for (const prefName in UpdateUtils.PER_INSTALLATION_PREFS) {
        Services.prefs.clearUserPref(prefName);
      }
    }

    UpdateUtils.PER_INSTALLATION_PREFS = originalPerInstallationPrefs;
    UpdateUtils.initPerInstallPrefs();
  });
}

let gObserverSeenCount = 0;
let gExpectedObserverData;
function observerCallback(subject, topic, data) {
  gObserverSeenCount += 1;
  Assert.equal(
    data,
    gExpectedObserverData,
    `Expected observer to have data: "${gExpectedObserverData}". ` +
      `It actually has data: "${data}"`
  );
}

async function changeAndVerifyPref(
  prefName,
  expectedInitialValue,
  newValue,
  setterShouldThrow = false
) {
  let initialValue = await UpdateUtils.readUpdateConfigSetting(prefName);
  Assert.strictEqual(
    initialValue,
    expectedInitialValue,
    `Expected pref '${prefName}' to have an initial value of ` +
      `${JSON.stringify(expectedInitialValue)}. Its actual initial value is ` +
      `${JSON.stringify(initialValue)}`
  );

  let expectedObserverCount = 1;
  if (initialValue == newValue || setterShouldThrow) {
    expectedObserverCount = 0;
  }

  let observerTopic =
    UpdateUtils.PER_INSTALLATION_PREFS[prefName].observerTopic;
  gObserverSeenCount = 0;
  gExpectedObserverData = newValue.toString();
  Services.obs.addObserver(observerCallback, observerTopic);

  let returned;
  let exceptionThrown;
  try {
    returned = await UpdateUtils.writeUpdateConfigSetting(prefName, newValue);
  } catch (e) {
    exceptionThrown = e;
  }
  if (setterShouldThrow) {
    Assert.ok(!!exceptionThrown, "Expected an exception to be thrown");
  } else {
    Assert.ok(
      !exceptionThrown,
      `Unexpected exception thrown by writeUpdateConfigSetting: ` +
        `${exceptionThrown}`
    );
  }

  if (!exceptionThrown) {
    Assert.strictEqual(
      returned,
      newValue,
      `Expected writeUpdateConfigSetting to return ` +
        `${JSON.stringify(newValue)}. It actually returned ` +
        `${JSON.stringify(returned)}`
    );
  }

  let readValue = await UpdateUtils.readUpdateConfigSetting(prefName);
  let expectedReadValue = exceptionThrown ? expectedInitialValue : newValue;
  Assert.strictEqual(
    readValue,
    expectedReadValue,
    `Expected pref '${prefName}' to be ${JSON.stringify(expectedReadValue)}.` +
      ` It was actually ${JSON.stringify(readValue)}.`
  );

  Assert.equal(
    gObserverSeenCount,
    expectedObserverCount,
    `Expected to see observer fire ${expectedObserverCount} times. It ` +
      `actually fired ${gObserverSeenCount} times.`
  );
  Services.obs.removeObserver(observerCallback, observerTopic);
}

async function run_test() {
  setupTestCommon(null);
  standardInit();
  await testSetup();

  logTestInfo("Testing boolean pref and its observer");
  let pref = "test.pref.boolean";
  let defaultValue = UpdateUtils.PER_INSTALLATION_PREFS[pref].defaultValue;
  await changeAndVerifyPref(pref, defaultValue, defaultValue);
  await changeAndVerifyPref(pref, defaultValue, !defaultValue);
  await changeAndVerifyPref(pref, !defaultValue, !defaultValue);
  await changeAndVerifyPref(pref, !defaultValue, defaultValue);
  await changeAndVerifyPref(pref, defaultValue, defaultValue);
  await changeAndVerifyPref(pref, defaultValue, "true", true);
  await changeAndVerifyPref(pref, defaultValue, 1, true);

  logTestInfo("Testing string pref and its observer");
  pref = "test.pref.string";
  defaultValue = UpdateUtils.PER_INSTALLATION_PREFS[pref].defaultValue;
  await changeAndVerifyPref(pref, defaultValue, defaultValue);
  await changeAndVerifyPref(pref, defaultValue, defaultValue + "1");
  await changeAndVerifyPref(pref, defaultValue + "1", "");
  await changeAndVerifyPref(pref, "", 1, true);
  await changeAndVerifyPref(pref, "", true, true);

  logTestInfo("Testing integer pref and its observer");
  pref = "test.pref.integer";
  defaultValue = UpdateUtils.PER_INSTALLATION_PREFS[pref].defaultValue;
  await changeAndVerifyPref(pref, defaultValue, defaultValue);
  await changeAndVerifyPref(pref, defaultValue, defaultValue + 1);
  await changeAndVerifyPref(pref, defaultValue + 1, 0);
  await changeAndVerifyPref(pref, 0, "1", true);
  await changeAndVerifyPref(pref, 0, true, true);

  // Testing that the default pref branch works the same way that the default
  // branch works for our per-profile prefs.
  logTestInfo("Testing default branch behavior");
  pref = "test.pref.integer";
  let originalDefault = UpdateUtils.PER_INSTALLATION_PREFS[pref].defaultValue;
  // Make sure the value is the default value, then change the default value
  // and check that the effective value changes.
  await UpdateUtils.writeUpdateConfigSetting(pref, originalDefault, {
    setDefaultOnly: true,
  });
  await UpdateUtils.writeUpdateConfigSetting(pref, originalDefault);
  await UpdateUtils.writeUpdateConfigSetting(pref, originalDefault + 1, {
    setDefaultOnly: true,
  });
  Assert.strictEqual(
    await UpdateUtils.readUpdateConfigSetting(pref),
    originalDefault + 1,
    `Expected that changing the default of a pref with no user value should ` +
      `change the effective value`
  );
  // Now make the user value different from the default value and ensure that
  // changing the default value does not affect the effective value
  await UpdateUtils.writeUpdateConfigSetting(pref, originalDefault + 10);
  await UpdateUtils.writeUpdateConfigSetting(pref, originalDefault + 20, {
    setDefaultOnly: true,
  });
  Assert.strictEqual(
    await UpdateUtils.readUpdateConfigSetting(pref),
    originalDefault + 10,
    `Expected that changing the default of a pref with a user value should ` +
      `NOT change the effective value`
  );

  logTestInfo("Testing policy behavior");
  pref = "test.pref.policy";
  defaultValue = UpdateUtils.PER_INSTALLATION_PREFS[pref].defaultValue;
  gPolicyFunctionResult = null;
  await changeAndVerifyPref(pref, defaultValue, defaultValue + 1);
  gPolicyFunctionResult = defaultValue + 10;
  await changeAndVerifyPref(pref, gPolicyFunctionResult, 0, true);

  doTestFinish();
}