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
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
*/
ChromeUtils.import("resource://gre/modules/Services.jsm", this);
ChromeUtils.import("resource://gre/modules/TelemetryController.jsm", this);
ChromeUtils.import("resource://gre/modules/TelemetrySession.jsm", this);
ChromeUtils.import("resource://gre/modules/PromiseUtils.jsm", this);
ChromeUtils.import("resource://testing-common/ContentTaskUtils.jsm", this);
const MESSAGE_CHILD_TEST_DONE = "ChildTest:Done";
const PLATFORM_VERSION = "1.9.2";
const APP_VERSION = "1";
const APP_ID = "xpcshell@tests.mozilla.org";
const APP_NAME = "XPCShell";
const RECORDED_CONTENT_EVENTS = [
["telemetry.test", "content_only", "object1"],
["telemetry.test", "main_and_content", "object1"],
["telemetry.test", "content_only", "object1", "some value"],
["telemetry.test", "content_only", "object1", null, { foo: "x", bar: "y" }],
[
"telemetry.test",
"content_only",
"object1",
"some value",
{ foo: "x", bar: "y" },
],
];
const UNRECORDED_CONTENT_EVENTS = [["telemetry.test", "main_only", "object1"]];
const RECORDED_PARENT_EVENTS = [
["telemetry.test", "main_and_content", "object1"],
["telemetry.test", "main_only", "object1"],
];
const UNRECORDED_PARENT_EVENTS = [
["telemetry.test", "content_only", "object1"],
];
const RECORDED_DYNAMIC_EVENTS = [
["telemetry.test.dynamic", "test1", "object1"],
["telemetry.test.dynamic", "test2", "object1"],
];
function run_child_test() {
// Record some events in the "content" process.
RECORDED_CONTENT_EVENTS.forEach(e => Telemetry.recordEvent(...e));
// These events should not be recorded for the content process.
UNRECORDED_CONTENT_EVENTS.forEach(e => Telemetry.recordEvent(...e));
// Record some dynamic events from the content process.
RECORDED_DYNAMIC_EVENTS.forEach(e => Telemetry.recordEvent(...e));
}
/**
* This function waits until content events are reported into the
* events snapshot.
*/
async function waitForContentEvents() {
await ContentTaskUtils.waitForCondition(() => {
const snapshot = Telemetry.snapshotEvents(
Ci.nsITelemetry.DATASET_PRERELEASE_CHANNELS,
false
);
return (
Object.keys(snapshot).includes("content") &&
Object.keys(snapshot).includes("dynamic")
);
});
}
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);
loadAddonManager(APP_ID, APP_NAME, APP_VERSION, PLATFORM_VERSION);
finishAddonManagerStartup();
fakeIntlReady();
await TelemetryController.testSetup();
// Make sure we don't generate unexpected pings due to pref changes.
await setEmptyPrefWatchlist();
// Enable recording for the test event category.
Telemetry.setEventRecordingEnabled("telemetry.test", true);
// Register dynamic test events.
Telemetry.registerEvents("telemetry.test.dynamic", {
// Event with only required fields.
test1: {
methods: ["test1"],
objects: ["object1"],
},
// Event with extra_keys.
test2: {
methods: ["test2", "test2b"],
objects: ["object1"],
extra_keys: ["key1", "key2"],
},
});
// Run test in child, don't wait for it to finish: just wait for the
// MESSAGE_CHILD_TEST_DONE.
const timestampBeforeChildEvents = Telemetry.msSinceProcessStart();
run_test_in_child("test_ChildEvents.js");
await do_await_remote_message(MESSAGE_CHILD_TEST_DONE);
// Once events 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 waitForContentEvents();
const timestampAfterChildEvents = Telemetry.msSinceProcessStart();
// Also record some events in the parent.
RECORDED_PARENT_EVENTS.forEach(e => Telemetry.recordEvent(...e));
UNRECORDED_PARENT_EVENTS.forEach(e => Telemetry.recordEvent(...e));
let snapshot = Telemetry.snapshotEvents(
Ci.nsITelemetry.DATASET_PRERELEASE_CHANNELS,
false
);
Assert.ok("parent" in snapshot, "Should have main process section");
Assert.ok(
!!snapshot.parent.length,
"Main process section should have events."
);
Assert.ok("content" in snapshot, "Should have child process section");
Assert.ok(
!!snapshot.content.length,
"Child process section should have events."
);
Assert.ok("dynamic" in snapshot, "Should have dynamic process section");
Assert.ok(
!!snapshot.dynamic.length,
"Dynamic process section should have events."
);
// Check that the expected events are present from the content process.
let contentEvents = snapshot.content.map(e => e.slice(1));
Assert.equal(
contentEvents.length,
RECORDED_CONTENT_EVENTS.length,
"Should match expected event count."
);
for (let i = 0; i < RECORDED_CONTENT_EVENTS.length; ++i) {
Assert.deepEqual(
contentEvents[i],
RECORDED_CONTENT_EVENTS[i],
"Should have recorded expected event."
);
}
// Check that the expected events are present from the parent process.
let parentEvents = snapshot.parent.map(e => e.slice(1));
Assert.equal(
parentEvents.length,
RECORDED_PARENT_EVENTS.length,
"Should match expected event count."
);
for (let i = 0; i < RECORDED_PARENT_EVENTS.length; ++i) {
Assert.deepEqual(
parentEvents[i],
RECORDED_PARENT_EVENTS[i],
"Should have recorded expected event."
);
}
// Check that the expected dynamic events are present.
let dynamicEvents = snapshot.dynamic.map(e => e.slice(1));
Assert.equal(
dynamicEvents.length,
RECORDED_DYNAMIC_EVENTS.length,
"Should match expected event count."
);
for (let i = 0; i < RECORDED_DYNAMIC_EVENTS.length; ++i) {
Assert.deepEqual(
dynamicEvents[i],
RECORDED_DYNAMIC_EVENTS[i],
"Should have recorded expected event."
);
}
// Check that the event timestamps are in the expected ranges.
let contentTimestamps = snapshot.content.map(e => e[0]);
let parentTimestamps = snapshot.parent.map(e => e[0]);
Assert.ok(
contentTimestamps.every(
ts =>
ts > Math.floor(timestampBeforeChildEvents) &&
ts < timestampAfterChildEvents
),
"All content event timestamps should be in the expected time range."
);
Assert.ok(
parentTimestamps.every(ts => ts >= Math.floor(timestampAfterChildEvents)),
"All parent event timestamps should be in the expected time range."
);
// Make sure all events are cleared from storage properly.
snapshot = Telemetry.snapshotEvents(
Ci.nsITelemetry.DATASET_PRERELEASE_CHANNELS,
true
);
Assert.greaterOrEqual(
Object.keys(snapshot).length,
2,
"Should have events from at least two processes."
);
snapshot = Telemetry.snapshotEvents(
Ci.nsITelemetry.DATASET_PRERELEASE_CHANNELS,
true
);
Assert.equal(
Object.keys(snapshot).length,
0,
"Should have cleared all events from storage."
);
});
|