summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/update/tests/unit_aus_update/languagePackUpdates.js
blob: 1e27efb75368cf16ad385d166d1a5906f6f7dea7 (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
const { AddonTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/AddonTestUtils.sys.mjs"
);
const { getAppInfo } = ChromeUtils.importESModule(
  "resource://testing-common/AppInfo.sys.mjs"
);
const { XPIExports } = ChromeUtils.importESModule(
  "resource://gre/modules/addons/XPIExports.sys.mjs"
);
const { setTimeout } = ChromeUtils.importESModule(
  "resource://gre/modules/Timer.sys.mjs"
);
const { TelemetryTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/TelemetryTestUtils.sys.mjs"
);

AddonTestUtils.init(this);
setupTestCommon();
AddonTestUtils.appInfo = getAppInfo();
start_httpserver();
setUpdateURL(gURLData + gHTTPHandlerPath);
setUpdateChannel("test_channel");
Services.prefs.setBoolPref(PREF_APP_UPDATE_LANGPACK_ENABLED, true);

/**
 * Checks for updates and waits for the update to download.
 */
async function downloadUpdate() {
  let patches = getRemotePatchString({});
  let updateString = getRemoteUpdateString({}, patches);
  gResponseBody = getRemoteUpdatesXMLString(updateString);

  let { updates } = await waitForUpdateCheck(true);

  initMockIncrementalDownload();
  gIncrementalDownloadErrorType = 3;

  await waitForUpdateDownload(updates, Cr.NS_OK);
}

/**
 * Returns a promise that will resolve when the add-ons manager attempts to
 * stage langpack updates. The returned object contains the appVersion and
 * platformVersion parameters as well as resolve and reject functions to
 * complete the mocked langpack update.
 */
function mockLangpackUpdate() {
  let stagingCall = Promise.withResolvers();
  XPIExports.XPIInstall.stageLangpacksForAppUpdate = (
    appVersion,
    platformVersion
  ) => {
    let result = Promise.withResolvers();
    stagingCall.resolve({
      appVersion,
      platformVersion,
      resolve: result.resolve,
      reject: result.reject,
    });

    return result.promise;
  };

  return stagingCall.promise;
}

add_setup(async function () {
  // Thunderbird doesn't have one or more of the probes used in this test.
  // Ensure the data is collected anyway.
  Services.prefs.setBoolPref(
    "toolkit.telemetry.testing.overrideProductsCheck",
    true
  );

  await AddonTestUtils.promiseStartupManager();
});

add_task(async function testLangpackUpdateSuccess() {
  let histogram = TelemetryTestUtils.getAndClearHistogram(
    "UPDATE_LANGPACK_OVERTIME"
  );

  let updateDownloadNotified = false;
  let notified = waitForEvent("update-downloaded").then(
    () => (updateDownloadNotified = true)
  );

  let stagingCall = mockLangpackUpdate();

  await downloadUpdate();

  // We have to wait for UpdateService's onStopRequest to run far enough that
  // the notification will have been sent if the language pack update completed.
  await TestUtils.waitForCondition(() => readStatusFile() == "pending");

  Assert.ok(
    !updateDownloadNotified,
    "Should not have seen the notification yet."
  );

  let { appVersion, platformVersion, resolve } = await stagingCall;
  Assert.equal(
    appVersion,
    DEFAULT_UPDATE_VERSION,
    "Should see the right app version"
  );
  Assert.equal(
    platformVersion,
    DEFAULT_UPDATE_VERSION,
    "Should see the right platform version"
  );

  resolve();

  await notified;

  // Because we resolved the lang pack call after the download completed a value
  // should have been recorded in telemetry.
  let snapshot = histogram.snapshot();
  Assert.ok(
    !Object.values(snapshot.values).every(val => val == 0),
    "Should have recorded a time"
  );

  // Reload the update manager so that we can download the same update again
  reloadUpdateManagerData(true);
});

add_task(async function testLangpackUpdateFails() {
  let updateDownloadNotified = false;
  let notified = waitForEvent("update-downloaded").then(
    () => (updateDownloadNotified = true)
  );

  let stagingCall = mockLangpackUpdate();

  await downloadUpdate();

  // We have to wait for UpdateService's onStopRequest to run far enough that
  // the notification will have been sent if the language pack update completed.
  await TestUtils.waitForCondition(() => readStatusFile() == "pending");

  Assert.ok(
    !updateDownloadNotified,
    "Should not have seen the notification yet."
  );

  let { appVersion, platformVersion, reject } = await stagingCall;
  Assert.equal(
    appVersion,
    DEFAULT_UPDATE_VERSION,
    "Should see the right app version"
  );
  Assert.equal(
    platformVersion,
    DEFAULT_UPDATE_VERSION,
    "Should see the right platform version"
  );

  reject();

  await notified;

  // Reload the update manager so that we can download the same update again
  reloadUpdateManagerData(true);
});

add_task(async function testLangpackStaged() {
  let updateStagedNotified = false;
  let notified = waitForEvent("update-staged").then(
    () => (updateStagedNotified = true)
  );

  let stagingCall = mockLangpackUpdate();

  Services.prefs.setBoolPref(PREF_APP_UPDATE_STAGING_ENABLED, true);
  copyTestUpdaterToBinDir();

  let greDir = getGREDir();

  if (AppConstants.platform != "macosx") {
    let updateSettingsIni = greDir.clone();
    updateSettingsIni.append(FILE_UPDATE_SETTINGS_INI);
    writeFile(updateSettingsIni, UPDATE_SETTINGS_CONTENTS);
  }

  await downloadUpdate();

  // We have to wait for the update to be applied and then check that the
  // notification hasn't been sent.
  await TestUtils.waitForCondition(() => readStatusFile() == "applied");

  Assert.ok(
    !updateStagedNotified,
    "Should not have seen the notification yet."
  );

  let { appVersion, platformVersion, resolve } = await stagingCall;
  Assert.equal(
    appVersion,
    DEFAULT_UPDATE_VERSION,
    "Should see the right app version"
  );
  Assert.equal(
    platformVersion,
    DEFAULT_UPDATE_VERSION,
    "Should see the right platform version"
  );

  resolve();

  await notified;

  // Reload the update manager so that we can download the same update again
  reloadUpdateManagerData(true);
});

add_task(async function testRedownload() {
  // When the download of a partial mar fails the same downloader is re-used to
  // download the complete mar. We should only call the add-ons manager to stage
  // language packs once in this case.
  Services.prefs.setBoolPref(PREF_APP_UPDATE_STAGING_ENABLED, false);
  let histogram = TelemetryTestUtils.getAndClearHistogram(
    "UPDATE_LANGPACK_OVERTIME"
  );

  let partialPatch = getRemotePatchString({
    type: "partial",
    url: gURLData + "missing.mar",
    size: 28,
  });
  let completePatch = getRemotePatchString({});
  let updateString = getRemoteUpdateString({}, partialPatch + completePatch);
  gResponseBody = getRemoteUpdatesXMLString(updateString);

  let { updates } = await waitForUpdateCheck(true);

  initMockIncrementalDownload();
  gIncrementalDownloadErrorType = 3;

  let stageCount = 0;
  XPIExports.XPIInstall.stageLangpacksForAppUpdate = () => {
    stageCount++;
    return Promise.resolve();
  };

  let downloadCount = 0;
  let listener = {
    onStartRequest: _aRequest => {},
    onProgress: (_aRequest, _aContext, _aProgress, _aMaxProgress) => {},
    onStatus: (_aRequest, _aStatus, _aStatusText) => {},
    onStopRequest: (request, status) => {
      Assert.equal(
        status,
        downloadCount ? 0 : Cr.NS_ERROR_CORRUPTED_CONTENT,
        "Should have seen the right status."
      );
      downloadCount++;

      // Keep the same status.
      gIncrementalDownloadErrorType = 3;
    },
    QueryInterface: ChromeUtils.generateQI([
      "nsIRequestObserver",
      "nsIProgressEventSink",
    ]),
  };
  gAUS.addDownloadListener(listener);

  let bestUpdate = gAUS.selectUpdate(updates);
  await gAUS.downloadUpdate(bestUpdate, false);

  await waitForEvent("update-downloaded");

  gAUS.removeDownloadListener(listener);

  Assert.equal(downloadCount, 2, "Should have seen two downloads");
  Assert.equal(stageCount, 1, "Should have only tried to stage langpacks once");

  // Because we resolved the lang pack call before the download completed a value
  // should not have been recorded in telemetry.
  let snapshot = histogram.snapshot();
  Assert.ok(
    Object.values(snapshot.values).every(val => val == 0),
    "Should have recorded a time"
  );

  // Reload the update manager so that we can download the same update again
  reloadUpdateManagerData(true);
});

add_task(async function finish() {
  stop_httpserver(doTestFinish);
});