summaryrefslogtreecommitdiffstats
path: root/browser/components/enterprisepolicies/tests/browser/browser_policy_background_app_update.js
blob: a529e79be72be58bf28e2c7e1f8075cb454ceeb5 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";

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

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

async function test_background_update_pref(expectedEnabled, expectedLocked) {
  let actualEnabled = await UpdateUtils.readUpdateConfigSetting(PREF_NAME);
  is(
    actualEnabled,
    expectedEnabled,
    `Actual background update enabled setting should be ${expectedEnabled}`
  );

  let actualLocked = UpdateUtils.appUpdateSettingIsLocked(PREF_NAME);
  is(
    actualLocked,
    expectedLocked,
    `Background update enabled setting ${
      expectedLocked ? "should" : "should not"
    } be locked`
  );

  let setSuccess = true;
  try {
    await UpdateUtils.writeUpdateConfigSetting(PREF_NAME, actualEnabled);
  } catch (error) {
    setSuccess = false;
  }
  is(
    setSuccess,
    !expectedLocked,
    `Setting background update pref ${
      expectedLocked ? "should" : "should not"
    } fail`
  );

  if (AppConstants.MOZ_UPDATE_AGENT) {
    let shouldShowUI =
      !expectedLocked && UpdateUtils.PER_INSTALLATION_PREFS_SUPPORTED;
    await BrowserTestUtils.withNewTab("about:preferences", browser => {
      is(
        browser.contentDocument.getElementById("backgroundUpdate").hidden,
        !shouldShowUI,
        `When background update ${
          expectedLocked ? "is" : "isn't"
        } locked, and per-installation prefs ${
          UpdateUtils.PER_INSTALLATION_PREFS_SUPPORTED ? "are" : "aren't"
        } supported, the corresponding preferences entry ${
          shouldShowUI ? "shouldn't" : "should"
        } be hidden`
      );
    });
  } else {
    // The backgroundUpdate element is #ifdef'ed out if MOZ_UPDATER and
    // MOZ_UPDATE_AGENT are not both defined.
    info(
      "Warning: UI testing skipped because support for background update is " +
        "not present"
    );
  }
}

add_task(async function test_background_app_update_policy() {
  const origBackgroundUpdateVal = await UpdateUtils.readUpdateConfigSetting(
    PREF_NAME
  );
  registerCleanupFunction(async () => {
    await UpdateUtils.writeUpdateConfigSetting(
      PREF_NAME,
      origBackgroundUpdateVal
    );
  });

  await UpdateUtils.writeUpdateConfigSetting(PREF_NAME, true);
  await test_background_update_pref(true, false);

  await setupPolicyEngineWithJson({
    policies: {
      BackgroundAppUpdate: false,
    },
  });
  await test_background_update_pref(false, true);

  await setupPolicyEngineWithJson({});
  await UpdateUtils.writeUpdateConfigSetting(PREF_NAME, false);
  await test_background_update_pref(false, false);

  await setupPolicyEngineWithJson({
    policies: {
      BackgroundAppUpdate: true,
    },
  });
  await test_background_update_pref(true, true);
});