summaryrefslogtreecommitdiffstats
path: root/toolkit/components/search/tests/xpcshell/test_initialization_status_telemetry.js
blob: fc62ded7875c4adb1c4df43821969a8dac320646 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Tests telemetry is captured when search service initialization has failed or
 * succeeded.
 */
const searchService = Services.search.wrappedJSObject;

add_setup(async () => {
  consoleAllowList.push("#init: failure initializing search:");
  await SearchTestUtils.useTestEngines("simple-engines");
  await AddonTestUtils.promiseStartupManager();
  Services.fog.initializeFOG();
});

add_task(async function test_init_success_telemetry() {
  Assert.equal(
    searchService.isInitialized,
    false,
    "Search Service should not be initialized."
  );

  await Services.search.init();

  Assert.equal(
    searchService.hasSuccessfullyInitialized,
    true,
    "Search Service should have initialized successfully."
  );

  Assert.equal(
    1,
    await Glean.searchService.initializationStatus.success.testGetValue(),
    "Should have incremented init success by one."
  );
});

add_task(async function test_init_failure_telemetry() {
  await startInitFailure("Settings");
  Assert.equal(
    1,
    await Glean.searchService.initializationStatus.failedSettings.testGetValue(),
    "Should have incremented get settings failure by one."
  );

  await startInitFailure("FetchEngines");
  Assert.equal(
    1,
    await Glean.searchService.initializationStatus.failedFetchEngines.testGetValue(),
    "Should have incremented fetch engines failure by one."
  );

  await startInitFailure("LoadEngines");
  Assert.equal(
    1,
    await Glean.searchService.initializationStatus.failedLoadEngines.testGetValue(),
    "Should have incremented load engines failure by one."
  );
});

async function startInitFailure(errorType) {
  searchService.reset();
  searchService.errorToThrowInTest = errorType;

  Assert.equal(
    searchService.isInitialized,
    false,
    "Search Service should not be initialized."
  );

  let regex = new RegExp(
    `Fake ${errorType} error during search service initialization.`
  );

  await Assert.rejects(
    Services.search.init(),
    regex,
    "Should have thrown an error on init."
  );

  await Assert.rejects(
    Services.search.promiseInitialized,
    regex,
    "Should have rejected the promise."
  );

  searchService.errorToThrowInTest = null;
}