summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/update/tests/unit_background_update/test_backgroundupdate_reason_schedule.js
blob: 277ea993ecb0a896c4e3f6f8f5387ff8ae60207d (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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
 * vim: sw=4 ts=4 sts=4 et
 * 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";

const { BackgroundUpdate } = ChromeUtils.importESModule(
  "resource://gre/modules/BackgroundUpdate.sys.mjs"
);
let reasons = () => BackgroundUpdate._reasonsToNotScheduleUpdates();
let REASON = BackgroundUpdate.REASON;

const { AddonTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/AddonTestUtils.sys.mjs"
);
const { ExtensionTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/ExtensionXPCShellUtils.sys.mjs"
);

setupProfileService();

// Setup that allows to install a langpack.
ExtensionTestUtils.init(this);

AddonTestUtils.init(this);
AddonTestUtils.overrideCertDB();

AddonTestUtils.createAppInfo(
  "xpcshell@tests.mozilla.org",
  "XPCShell",
  "42",
  "42"
);

add_setup(function test_setup() {
  // FOG needs a profile directory to put its data in.
  do_get_profile();

  // We need to initialize it once, otherwise operations will be stuck in the pre-init queue.
  Services.fog.initializeFOG();

  setupProfileService();
});

add_task(
  {
    skip_if: () => !AppConstants.MOZ_BACKGROUNDTASKS,
  },
  async function test_reasons_schedule_langpacks() {
    await AddonTestUtils.promiseStartupManager();

    Services.prefs.setBoolPref("app.update.langpack.enabled", true);

    let result = await reasons();
    Assert.ok(
      !result.includes(REASON.LANGPACK_INSTALLED),
      "Reasons does not include LANGPACK_INSTALLED"
    );

    // Install a langpack.
    let langpack = {
      "manifest.json": {
        name: "test Language Pack",
        version: "1.0",
        manifest_version: 2,
        browser_specific_settings: {
          gecko: {
            id: "@test-langpack",
            strict_min_version: "42.0",
            strict_max_version: "42.0",
          },
        },
        langpack_id: "fr",
        languages: {
          fr: {
            chrome_resources: {
              global: "chrome/fr/locale/fr/global/",
            },
            version: "20171001190118",
          },
        },
        sources: {
          browser: {
            base_path: "browser/",
          },
        },
      },
    };

    await Promise.all([
      TestUtils.topicObserved("webextension-langpack-startup"),
      AddonTestUtils.promiseInstallXPI(langpack),
    ]);

    result = await reasons();
    Assert.ok(
      result.includes(REASON.LANGPACK_INSTALLED),
      "Reasons include LANGPACK_INSTALLED"
    );
    result = await checkGleanPing();
    Assert.ok(
      result.includes(REASON.LANGPACK_INSTALLED),
      "Recognizes a language pack is installed."
    );

    // Now turn off langpack updating.
    Services.prefs.setBoolPref("app.update.langpack.enabled", false);

    result = await reasons();
    Assert.ok(
      !result.includes(REASON.LANGPACK_INSTALLED),
      "Reasons does not include LANGPACK_INSTALLED"
    );
    result = await checkGleanPing();
    Assert.ok(
      !result.includes(REASON.LANGPACK_INSTALLED),
      "No Glean metric when no language pack is installed."
    );
  }
);

add_task(
  {
    skip_if: () => !AppConstants.MOZ_BACKGROUNDTASKS,
  },
  async function test_reasons_schedule_default_profile() {
    // It's difficult to arrange a default profile in a testing environment, so
    // this is not as thorough as we'd like.
    let result = await reasons();

    Assert.ok(result.includes(REASON.NO_DEFAULT_PROFILE_EXISTS));
    Assert.ok(result.includes(REASON.NOT_DEFAULT_PROFILE));
  }
);