summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/update/tests/browser/browser_aboutPrefs_backgroundUpdateSetting.js
blob: 88afa8fdbe95cfab725bf0c372f957e1de2f811b (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
/* 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/.
 */
"use strict";

/**
 * This file tests the background update UI in about:preferences.
 */

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

const BACKGROUND_UPDATE_PREF = "app.update.background.enabled";

add_task(async function testBackgroundUpdateSettingUI() {
  if (!AppConstants.MOZ_UPDATE_AGENT) {
    // The element that we are testing in about:preferences is #ifdef'ed out of
    // the file if MOZ_UPDATE_AGENT isn't defined. So there is nothing to
    // test in that case.
    logTestInfo(
      `
===============================================================================
WARNING! This test involves background update, but background tasks are
         disabled. This test will unconditionally pass since the feature it
         wants to test isn't available.
===============================================================================
`
    );
    // Some of our testing environments do not consider a test to have passed if
    // it didn't make any assertions.
    ok(true, "Unconditionally passing test");
    return;
  }

  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "about:preferences"
  );

  const originalBackgroundUpdateVal = await UpdateUtils.readUpdateConfigSetting(
    BACKGROUND_UPDATE_PREF
  );
  const originalUpdateAutoVal = await UpdateUtils.getAppUpdateAutoEnabled();
  registerCleanupFunction(async () => {
    await BrowserTestUtils.removeTab(tab);
    await UpdateUtils.writeUpdateConfigSetting(
      BACKGROUND_UPDATE_PREF,
      originalBackgroundUpdateVal
    );
    await UpdateUtils.setAppUpdateAutoEnabled(originalUpdateAutoVal);
  });

  // If auto update is disabled, the control for background update should be
  // disabled, since we cannot update in the background if we can't update
  // automatically.
  await UpdateUtils.setAppUpdateAutoEnabled(false);
  await SpecialPowers.spawn(
    tab.linkedBrowser,
    [UpdateUtils.PER_INSTALLATION_PREFS_SUPPORTED],
    async perInstallationPrefsSupported => {
      let backgroundUpdateCheckbox =
        content.document.getElementById("backgroundUpdate");
      is(
        backgroundUpdateCheckbox.hidden,
        !perInstallationPrefsSupported,
        `The background update UI should ${
          perInstallationPrefsSupported ? "not" : ""
        } be hidden when and perInstallationPrefsSupported is ` +
          `${perInstallationPrefsSupported}`
      );
      if (perInstallationPrefsSupported) {
        is(
          backgroundUpdateCheckbox.disabled,
          true,
          `The background update UI should be disabled when auto update is ` +
            `disabled`
        );
      }
    }
  );

  if (!UpdateUtils.PER_INSTALLATION_PREFS_SUPPORTED) {
    // The remaining tests only make sense on platforms where per-installation
    // prefs are supported and the UI will ever actually be displayed
    return;
  }

  await UpdateUtils.setAppUpdateAutoEnabled(true);
  await UpdateUtils.writeUpdateConfigSetting(BACKGROUND_UPDATE_PREF, true);

  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
    let backgroundUpdateCheckbox =
      content.document.getElementById("backgroundUpdate");
    is(
      backgroundUpdateCheckbox.disabled,
      false,
      `The background update UI should not be disabled when auto update is ` +
        `enabled`
    );

    is(
      backgroundUpdateCheckbox.checked,
      true,
      "After enabling background update, the checkbox should be checked"
    );

    // Note that this action results in asynchronous activity. Normally when
    // we change the update config, we await on the function to wait for the
    // value to be written to the disk. We can't easily await on the UI state
    // though. Luckily, we don't have to because reads/writes of the config file
    // are serialized. So when we verify the written value by awaiting on
    // readUpdateConfigSetting(), that will also wait for the value to be
    // written to disk and for this UI to react to that.
    backgroundUpdateCheckbox.click();
  });

  is(
    await UpdateUtils.readUpdateConfigSetting(BACKGROUND_UPDATE_PREF),
    false,
    "Toggling the checkbox should have changed the setting value to false"
  );

  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
    let backgroundUpdateCheckbox =
      content.document.getElementById("backgroundUpdate");
    is(
      backgroundUpdateCheckbox.checked,
      false,
      "After toggling the checked checkbox, it should be unchecked."
    );

    // Like the last call like this one, this initiates asynchronous behavior.
    backgroundUpdateCheckbox.click();
  });

  is(
    await UpdateUtils.readUpdateConfigSetting(BACKGROUND_UPDATE_PREF),
    true,
    "Toggling the checkbox should have changed the setting value to true"
  );

  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
    is(
      content.document.getElementById("backgroundUpdate").checked,
      true,
      "After toggling the unchecked checkbox, it should be checked"
    );
  });

  // Test that the UI reacts to observed setting changes properly.
  await UpdateUtils.writeUpdateConfigSetting(BACKGROUND_UPDATE_PREF, false);

  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
    is(
      content.document.getElementById("backgroundUpdate").checked,
      false,
      "Externally disabling background update should uncheck the checkbox"
    );
  });

  await UpdateUtils.writeUpdateConfigSetting(BACKGROUND_UPDATE_PREF, true);

  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
    is(
      content.document.getElementById("backgroundUpdate").checked,
      true,
      "Externally enabling background update should check the checkbox"
    );
  });
});