summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/update/tests/unit_aus_update/ensureExperimentToRolloutTransitionPerformed.js
blob: 86bc75a7d6765c531d7ce849a82000e71db80eff (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
/* 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/.
 */

ChromeUtils.defineESModuleGetters(this, {
  BackgroundUpdate: "resource://gre/modules/BackgroundUpdate.sys.mjs",
});

const transitionPerformedPref = "app.update.background.rolledout";
const backgroundUpdateEnabledPref = "app.update.background.enabled";
const defaultPrefValue =
  UpdateUtils.PER_INSTALLATION_PREFS[backgroundUpdateEnabledPref].defaultValue;

async function testTransition(options) {
  Services.prefs.clearUserPref(transitionPerformedPref);
  await UpdateUtils.writeUpdateConfigSetting(
    backgroundUpdateEnabledPref,
    options.initialDefaultValue,
    { setDefaultOnly: true }
  );
  await UpdateUtils.writeUpdateConfigSetting(
    backgroundUpdateEnabledPref,
    options.initialUserValue
  );
  BackgroundUpdate.ensureExperimentToRolloutTransitionPerformed();
  Assert.equal(
    await UpdateUtils.readUpdateConfigSetting(backgroundUpdateEnabledPref),
    options.expectedPostTransitionValue,
    "Post transition option value does not match the expected value"
  );

  // Make sure that we only do the transition once.

  // If we change the default value, then change the user value to the same
  // thing, we will end up with only a default value and no saved user value.
  // This allows us to ensure that we read the default value back out, if it is
  // changed.
  await UpdateUtils.writeUpdateConfigSetting(
    backgroundUpdateEnabledPref,
    !defaultPrefValue,
    { setDefaultOnly: true }
  );
  await UpdateUtils.writeUpdateConfigSetting(
    backgroundUpdateEnabledPref,
    !defaultPrefValue
  );
  BackgroundUpdate.ensureExperimentToRolloutTransitionPerformed();
  Assert.equal(
    await UpdateUtils.readUpdateConfigSetting(backgroundUpdateEnabledPref),
    !defaultPrefValue,
    "Transition should not change the pref value if it already ran"
  );
}

async function run_test() {
  setupTestCommon(null);
  standardInit();
  // 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);

  const originalBackgroundUpdateEnabled =
    await UpdateUtils.readUpdateConfigSetting(backgroundUpdateEnabledPref);

  registerCleanupFunction(async () => {
    Services.prefs.clearUserPref(transitionPerformedPref);
    await UpdateUtils.writeUpdateConfigSetting(
      backgroundUpdateEnabledPref,
      originalBackgroundUpdateEnabled
    );
    await UpdateUtils.writeUpdateConfigSetting(
      backgroundUpdateEnabledPref,
      defaultPrefValue,
      { setDefaultOnly: true }
    );
  });

  await testTransition({
    initialDefaultValue: true,
    initialUserValue: true,
    expectedPostTransitionValue: true,
  });

  // Make sure we don't interfere with a user's choice to turn the feature off.
  await testTransition({
    initialDefaultValue: true,
    initialUserValue: false,
    expectedPostTransitionValue: false,
  });

  // In this case, there effectively is no user value since the user value
  // equals the default value. So the effective value should change after
  // the transition switches the default.
  await testTransition({
    initialDefaultValue: false,
    initialUserValue: false,
    expectedPostTransitionValue: true,
  });

  await testTransition({
    initialDefaultValue: false,
    initialUserValue: true,
    expectedPostTransitionValue: true,
  });

  doTestFinish();
}