summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/xpcshell/rs-blocklist/test_blocklist_telemetry.js
blob: cf1992b121cae0f409cab2f73c03e99b4c6d8331 (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
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

AddonTestUtils.init(this);
AddonTestUtils.createAppInfo(
  "xpcshell@tests.mozilla.org",
  "XPCShell",
  "1",
  "49"
);

const { TelemetryController } = ChromeUtils.importESModule(
  "resource://gre/modules/TelemetryController.sys.mjs"
);
const { TelemetryTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/TelemetryTestUtils.sys.mjs"
);

add_setup({ skip_if: () => IS_ANDROID_BUILD }, function test_setup() {
  // FOG needs a profile directory to put its data in.
  do_get_profile();

  // FOG needs to be initialized in order for data to flow.
  Services.fog.initializeFOG();
});

function assertTelemetryScalars(expectedScalars) {
  if (!IS_ANDROID_BUILD) {
    let scalars = TelemetryTestUtils.getProcessScalars("parent");

    for (const scalarName of Object.keys(expectedScalars || {})) {
      equal(
        scalars[scalarName],
        expectedScalars[scalarName],
        `Got the expected value for ${scalarName} scalar`
      );
    }
  } else {
    info(
      `Skip assertions on collected samples for ${expectedScalars} on android builds`
    );
  }
}

add_task(async function test_setup() {
  // Ensure that the telemetry scalar definitions are loaded and the
  // AddonManager initialized.
  await TelemetryController.testSetup();
  await AddonTestUtils.promiseStartupManager();
});

add_task(async function test_blocklist_lastModified_rs_scalars() {
  resetBlocklistTelemetry();
  const now = Date.now();

  const lastEntryTimes = {
    addons: now - 5000,
    addons_mlbf: now - 4000,
  };

  const lastEntryTimesUTC = {};
  const toUTC = t => new Date(t).toUTCString();
  for (const key of Object.keys(lastEntryTimes)) {
    lastEntryTimesUTC[key] = toUTC(lastEntryTimes[key]);
  }

  const {
    BlocklistPrivate: {
      BlocklistTelemetry,
      ExtensionBlocklistMLBF,
      ExtensionBlocklistRS,
    },
  } = ChromeUtils.importESModule("resource://gre/modules/Blocklist.sys.mjs");

  // Return a promise resolved when the recordRSBlocklistLastModified method
  // has been called (by temporarily replacing the method with a function that
  // calls the real method and then resolve the promise).
  function promiseScalarRecorded() {
    return new Promise(resolve => {
      let origFn = BlocklistTelemetry.recordRSBlocklistLastModified;
      BlocklistTelemetry.recordRSBlocklistLastModified = async (...args) => {
        BlocklistTelemetry.recordRSBlocklistLastModified = origFn;
        let res = await origFn.apply(BlocklistTelemetry, args);
        resolve();
        return res;
      };
    });
  }

  async function fakeRemoteSettingsSync(rsClient, lastModified) {
    await rsClient.db.importChanges({}, lastModified);
    await rsClient.emit("sync");
  }

  assertTelemetryScalars({
    "blocklist.lastModified_rs_addons_mlbf": undefined,
  });
  Assert.equal(
    undefined,
    testGetValue(Glean.blocklist.lastModifiedRsAddonsMblf)
  );

  info("Test RS addon blocklist lastModified scalar");

  await ExtensionBlocklistRS.ensureInitialized();
  await Promise.all([
    promiseScalarRecorded(),
    fakeRemoteSettingsSync(ExtensionBlocklistRS._client, lastEntryTimes.addons),
  ]);

  assertTelemetryScalars({
    "blocklist.lastModified_rs_addons_mlbf": undefined,
  });

  Assert.equal(
    undefined,
    testGetValue(Glean.blocklist.lastModifiedRsAddonsMblf)
  );

  await ExtensionBlocklistMLBF.ensureInitialized();
  await Promise.all([
    promiseScalarRecorded(),
    fakeRemoteSettingsSync(
      ExtensionBlocklistMLBF._client,
      lastEntryTimes.addons_mlbf
    ),
  ]);

  assertTelemetryScalars({
    "blocklist.lastModified_rs_addons_mlbf": lastEntryTimesUTC.addons_mlbf,
  });
  Assert.equal(
    new Date(lastEntryTimesUTC.addons_mlbf).getTime(),
    testGetValue(Glean.blocklist.lastModifiedRsAddonsMblf).getTime()
  );
});