summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/test/unit/test_telemetry_buildconfig.js
blob: aa8d20bbb8474d1b0fccc199514b12075474691d (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// This test is a copy of parts of the following tests:
//
// * toolkit/components/telemetry/tests/unit/test_TelemetryEvents.js
// * toolkit/components/telemetry/tests/unit/test_TelemetryHistograms.js
// * toolkit/components/telemetry/tests/unit/test_TelemetryScalars.js
//
// The probe names have been changed to probes that only exist in a Thunderbird build.
// If this test begins to fail, check for recent changes in toolkit/components/telemetry.

ChromeUtils.defineESModuleGetters(this, {
  TelemetryTestUtils: "resource://testing-common/TelemetryTestUtils.sys.mjs",
});

const Telemetry = Services.telemetry;

const UINT_SCALAR = "tb.test.unsigned_int_kind";
const STRING_SCALAR = "tb.test.string_kind";
const BOOLEAN_SCALAR = "tb.test.boolean_kind";

/**
 * Check that stored events correspond to expectations.
 *
 * @param {Array} summaries - Summary of the expected events.
 * @param {boolean} clearScalars - Whether to clear out data after snapshotting.
 */
function checkEventSummary(summaries, clearScalars) {
  let scalars = Telemetry.getSnapshotForKeyedScalars("main", clearScalars);

  for (let [process, [category, eObject, method], count] of summaries) {
    let uniqueEventName = `${category}#${eObject}#${method}`;
    let summaryCount;
    if (process === "dynamic") {
      summaryCount =
        scalars.dynamic["telemetry.dynamic_event_counts"][uniqueEventName];
    } else {
      summaryCount =
        scalars[process]["telemetry.event_counts"][uniqueEventName];
    }
    Assert.equal(
      summaryCount,
      count,
      `${uniqueEventName} had wrong summary count`
    );
  }
}

/**
 * Test Thunderbird events are included in the build.
 */
add_task(async function test_recording_state() {
  Telemetry.clearEvents();
  Telemetry.clearScalars();

  const events = [["tb.test", "test", "object1"]];

  // Recording off by default.
  events.forEach(e => Telemetry.recordEvent(...e));
  TelemetryTestUtils.assertEvents([]);
  // But still expect a non-zero summary count.
  checkEventSummary(
    events.map(e => ["parent", e, 1]),
    true
  );

  // Once again, with recording on.
  Telemetry.setEventRecordingEnabled("tb.test", true);
  events.forEach(e => Telemetry.recordEvent(...e));
  TelemetryTestUtils.assertEvents(events);
  checkEventSummary(
    events.map(e => ["parent", e, 1]),
    true
  );
});

/**
 * Test Thunderbird histograms are included in the build.
 */
add_task(async function test_categorical_histogram() {
  let h1 = Telemetry.getHistogramById("TELEMETRY_TEST_TB_CATEGORICAL");
  for (let v of ["CommonLabel", "CommonLabel", "Label2", "Label3"]) {
    h1.add(v);
  }
  for (let s of ["", "Label4", "1234"]) {
    // The |add| method should not throw for unexpected values, but rather
    // print an error message in the console.
    h1.add(s);
  }

  let snapshot = h1.snapshot();
  Assert.deepEqual(snapshot.values, { 0: 2, 1: 1, 2: 1, 3: 0 });
  // sum is a little meaningless for categorical histograms, but hey.
  // (CommonLabel is 0, Label2 is 1, Label3 is 2)
  Assert.equal(snapshot.sum, 0 * 2 + 1 * 1 + 2 * 1);
  Assert.deepEqual(snapshot.range, [1, 50]);
});

/**
 * Test Thunderbird scalars are included in the build.
 */
add_task(async function test_serializationFormat() {
  Telemetry.clearScalars();

  // Set the scalars to a known value.
  const expectedUint = 3785;
  const expectedString = "some value";
  Telemetry.scalarSet(UINT_SCALAR, expectedUint);
  Telemetry.scalarSet(STRING_SCALAR, expectedString);
  Telemetry.scalarSet(BOOLEAN_SCALAR, true);

  // Get a snapshot of the scalars for the main process (internally called "default").
  const scalars = TelemetryTestUtils.getProcessScalars("parent");

  // Check that they are serialized to the correct format.
  Assert.equal(
    typeof scalars[UINT_SCALAR],
    "number",
    UINT_SCALAR + " must be serialized to the correct format."
  );
  Assert.ok(
    Number.isInteger(scalars[UINT_SCALAR]),
    UINT_SCALAR + " must be a finite integer."
  );
  Assert.equal(
    scalars[UINT_SCALAR],
    expectedUint,
    UINT_SCALAR + " must have the correct value."
  );
  Assert.equal(
    typeof scalars[STRING_SCALAR],
    "string",
    STRING_SCALAR + " must be serialized to the correct format."
  );
  Assert.equal(
    scalars[STRING_SCALAR],
    expectedString,
    STRING_SCALAR + " must have the correct value."
  );
  Assert.equal(
    typeof scalars[BOOLEAN_SCALAR],
    "boolean",
    BOOLEAN_SCALAR + " must be serialized to the correct format."
  );
  Assert.equal(
    scalars[BOOLEAN_SCALAR],
    true,
    BOOLEAN_SCALAR + " must have the correct value."
  );
});