summaryrefslogtreecommitdiffstats
path: root/toolkit/components/telemetry/tests/unit/test_ChildScalars.js
blob: 775288fed3a37b88b54f97f67362c6fc504f29fa (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/
*/

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

const MESSAGE_CHILD_TEST_DONE = "ChildTest:Done";

const UINT_SCALAR = "telemetry.test.unsigned_int_kind";
const KEYED_UINT_SCALAR = "telemetry.test.keyed_unsigned_int";
const KEYED_BOOL_SCALAR = "telemetry.test.keyed_boolean_kind";
const CONTENT_ONLY_UINT_SCALAR = "telemetry.test.content_only_uint";
const ALL_PROCESSES_UINT_SCALAR = "telemetry.test.all_processes_uint";
const ALL_CHILD_PROCESSES_STRING_SCALAR =
  "telemetry.test.all_child_processes_string";

function run_child_test() {
  // Attempt to set some scalar values from the "content" process.
  // The next scalars are not allowed to be recorded in the content process.
  Telemetry.scalarSet(UINT_SCALAR, 1);
  Telemetry.keyedScalarSet(KEYED_UINT_SCALAR, "should-not-be-recorded", 1);

  // The next scalars shou be recorded in only the content process.
  Telemetry.scalarSet(CONTENT_ONLY_UINT_SCALAR, 37);
  Telemetry.scalarSet(ALL_CHILD_PROCESSES_STRING_SCALAR, "all-child-processes");

  // The next scalar will be recorded in the parent and content processes.
  Telemetry.keyedScalarSet(KEYED_BOOL_SCALAR, "content-key", true);
  Telemetry.keyedScalarSet(KEYED_BOOL_SCALAR, "content-key2", false);
  Telemetry.scalarSet(ALL_PROCESSES_UINT_SCALAR, 37);
}

function setParentScalars() {
  // The following scalars are not allowed to be recorded in the parent process.
  Telemetry.scalarSet(CONTENT_ONLY_UINT_SCALAR, 15);
  Telemetry.scalarSet(ALL_CHILD_PROCESSES_STRING_SCALAR, "all-child-processes");

  // The next ones will be recorded only in the parent.
  Telemetry.scalarSet(UINT_SCALAR, 15);

  // This last batch will be available both in the parent and child processes.
  Telemetry.keyedScalarSet(KEYED_BOOL_SCALAR, "parent-key", false);
  Telemetry.scalarSet(ALL_PROCESSES_UINT_SCALAR, 37);
}

function checkParentScalars(processData) {
  const scalars = processData.scalars;
  const keyedScalars = processData.keyedScalars;

  // Check the plain scalars, make sure we're only recording what we expect.
  Assert.ok(
    !(CONTENT_ONLY_UINT_SCALAR in scalars),
    "Scalars must not be recorded in other processes unless allowed."
  );
  Assert.ok(
    !(ALL_CHILD_PROCESSES_STRING_SCALAR in scalars),
    "Scalars must not be recorded in other processes unless allowed."
  );
  Assert.ok(
    UINT_SCALAR in scalars,
    `${UINT_SCALAR} must be recorded in the parent process.`
  );
  Assert.equal(
    scalars[UINT_SCALAR],
    15,
    `${UINT_SCALAR} must have the correct value (parent process).`
  );
  Assert.ok(
    ALL_PROCESSES_UINT_SCALAR in scalars,
    `${ALL_PROCESSES_UINT_SCALAR} must be recorded in the parent process.`
  );
  Assert.equal(
    scalars[ALL_PROCESSES_UINT_SCALAR],
    37,
    `${ALL_PROCESSES_UINT_SCALAR} must have the correct value (parent process).`
  );

  // Now check the keyed scalars.
  Assert.ok(
    KEYED_BOOL_SCALAR in keyedScalars,
    `${KEYED_BOOL_SCALAR} must be recorded in the parent process.`
  );
  Assert.ok(
    "parent-key" in keyedScalars[KEYED_BOOL_SCALAR],
    `${KEYED_BOOL_SCALAR} must be recorded in the parent process.`
  );
  Assert.equal(
    Object.keys(keyedScalars[KEYED_BOOL_SCALAR]).length,
    1,
    `${KEYED_BOOL_SCALAR} must only contain the expected key in parent process.`
  );
  Assert.equal(
    keyedScalars[KEYED_BOOL_SCALAR]["parent-key"],
    false,
    `${KEYED_BOOL_SCALAR} must have the correct value (parent process).`
  );
}

function checkContentScalars(processData) {
  const scalars = processData.scalars;
  const keyedScalars = processData.keyedScalars;

  // Check the plain scalars for the content process.
  Assert.ok(
    !(UINT_SCALAR in scalars),
    "Scalars must not be recorded in other processes unless allowed."
  );
  Assert.ok(
    !(KEYED_UINT_SCALAR in keyedScalars),
    "Keyed scalars must not be recorded in other processes unless allowed."
  );
  Assert.ok(
    CONTENT_ONLY_UINT_SCALAR in scalars,
    `${CONTENT_ONLY_UINT_SCALAR} must be recorded in the content process.`
  );
  Assert.equal(
    scalars[CONTENT_ONLY_UINT_SCALAR],
    37,
    `${CONTENT_ONLY_UINT_SCALAR} must have the correct value (content process).`
  );
  Assert.ok(
    ALL_CHILD_PROCESSES_STRING_SCALAR in scalars,
    `${ALL_CHILD_PROCESSES_STRING_SCALAR} must be recorded in the content process.`
  );
  Assert.equal(
    scalars[ALL_CHILD_PROCESSES_STRING_SCALAR],
    "all-child-processes",
    `${ALL_CHILD_PROCESSES_STRING_SCALAR} must have the correct value (content process).`
  );
  Assert.ok(
    ALL_PROCESSES_UINT_SCALAR in scalars,
    `${ALL_PROCESSES_UINT_SCALAR} must be recorded in the content process.`
  );
  Assert.equal(
    scalars[ALL_PROCESSES_UINT_SCALAR],
    37,
    `${ALL_PROCESSES_UINT_SCALAR} must have the correct value (content process).`
  );

  // Check the keyed scalars.
  Assert.ok(
    KEYED_BOOL_SCALAR in keyedScalars,
    `${KEYED_BOOL_SCALAR} must be recorded in the content process.`
  );
  Assert.ok(
    "content-key" in keyedScalars[KEYED_BOOL_SCALAR],
    `${KEYED_BOOL_SCALAR} must be recorded in the content process.`
  );
  Assert.ok(
    "content-key2" in keyedScalars[KEYED_BOOL_SCALAR],
    `${KEYED_BOOL_SCALAR} must be recorded in the content process.`
  );
  Assert.equal(
    keyedScalars[KEYED_BOOL_SCALAR]["content-key"],
    true,
    `${KEYED_BOOL_SCALAR} must have the correct value (content process).`
  );
  Assert.equal(
    keyedScalars[KEYED_BOOL_SCALAR]["content-key2"],
    false,
    `${KEYED_BOOL_SCALAR} must have the correct value (content process).`
  );
  Assert.equal(
    Object.keys(keyedScalars[KEYED_BOOL_SCALAR]).length,
    2,
    `${KEYED_BOOL_SCALAR} must contain the expected keys in content process.`
  );
}

/**
 * This function waits until content scalars are reported into the
 * scalar snapshot.
 */
async function waitForContentScalars() {
  await ContentTaskUtils.waitForCondition(() => {
    const scalars = Telemetry.getSnapshotForScalars("main", false);
    return Object.keys(scalars).includes("content");
  });
}

add_task(async function () {
  if (!runningInParent) {
    TelemetryController.testSetupContent();
    run_child_test();
    do_send_remote_message(MESSAGE_CHILD_TEST_DONE);
    return;
  }

  // Setup.
  do_get_profile(true);
  await loadAddonManager(APP_ID, APP_NAME, APP_VERSION, PLATFORM_VERSION);
  finishAddonManagerStartup();
  fakeIntlReady();
  await TelemetryController.testSetup();
  if (runningInParent) {
    setParentScalars();
    // Make sure we don't generate unexpected pings due to pref changes.
    await setEmptyPrefWatchlist();
  }

  // Run test in child, don't wait for it to finish: just wait for the
  // MESSAGE_CHILD_TEST_DONE.
  run_test_in_child("test_ChildScalars.js");
  await do_await_remote_message(MESSAGE_CHILD_TEST_DONE);

  // Once scalars are set by the content process, they don't immediately get
  // sent to the parent process. Wait for the Telemetry IPC Timer to trigger
  // and batch send the data back to the parent process.
  await waitForContentScalars();

  // Get an "environment-changed" ping rather than a "test-ping", as
  // scalar measurements are only supported in subsession pings.
  const payload = TelemetrySession.getPayload("environment-change");

  // Validate the scalar data.
  Assert.ok("processes" in payload, "Should have processes section");
  Assert.ok(
    "content" in payload.processes,
    "Should have child process section"
  );
  Assert.ok(
    "scalars" in payload.processes.content,
    "Child process section should have scalars."
  );
  Assert.ok(
    "keyedScalars" in payload.processes.content,
    "Child process section should have keyed scalars."
  );
  checkParentScalars(payload.processes.parent);
  checkContentScalars(payload.processes.content);

  do_test_finished();
});