summaryrefslogtreecommitdiffstats
path: root/toolkit/components/telemetry/tests/unit/test_PrioPing.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
commit43a97878ce14b72f0981164f87f2e35e14151312 (patch)
tree620249daf56c0258faa40cbdcf9cfba06de2a846 /toolkit/components/telemetry/tests/unit/test_PrioPing.js
parentInitial commit. (diff)
downloadfirefox-upstream.tar.xz
firefox-upstream.zip
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/telemetry/tests/unit/test_PrioPing.js')
-rw-r--r--toolkit/components/telemetry/tests/unit/test_PrioPing.js138
1 files changed, 138 insertions, 0 deletions
diff --git a/toolkit/components/telemetry/tests/unit/test_PrioPing.js b/toolkit/components/telemetry/tests/unit/test_PrioPing.js
new file mode 100644
index 0000000000..f9a9fb588b
--- /dev/null
+++ b/toolkit/components/telemetry/tests/unit/test_PrioPing.js
@@ -0,0 +1,138 @@
+/* Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+"use strict";
+
+ChromeUtils.defineESModuleGetters(this, {
+ TelemetryPrioPing: "resource://gre/modules/PrioPing.sys.mjs",
+});
+
+function checkPingStructure(type, payload, options) {
+ Assert.equal(
+ type,
+ TelemetryPrioPing.PRIO_PING_TYPE,
+ "Should be a prio ping."
+ );
+ // Check the payload for required fields.
+ Assert.ok("version" in payload, "Payload must have version.");
+ Assert.ok("reason" in payload, "Payload must have reason.");
+ Assert.ok(
+ Object.values(TelemetryPrioPing.Reason).some(
+ reason => payload.reason === reason
+ ),
+ "Should be a known reason."
+ );
+ Assert.ok(
+ Array.isArray(payload.prioData),
+ "Payload prioData must be present and an array."
+ );
+ payload.prioData.forEach(prioData => {
+ Assert.ok("encoding" in prioData, "All prioData must have encodings.");
+ Assert.ok("prio" in prioData, "All prioData must have prio blocks.");
+ });
+ // Ensure we forbid client id and environment
+ Assert.equal(options.addClientId, false, "Must forbid client Id.");
+ Assert.equal(options.addEnvironment, false, "Must forbid Environment.");
+}
+
+function fakePolicy(set, clear, send, snapshot) {
+ let { Policy } = ChromeUtils.importESModule(
+ "resource://gre/modules/PrioPing.sys.mjs"
+ );
+ Policy.setTimeout = set;
+ Policy.clearTimeout = clear;
+ Policy.sendPing = send;
+ Policy.getEncodedOriginSnapshot = snapshot;
+}
+
+function pass() {
+ /* intentionally empty */
+}
+function fail() {
+ Assert.ok(false, "Not allowed");
+}
+function fakeSnapshot() {
+ return [
+ {
+ encoding: "telemetry.test-1-1",
+ prio: {},
+ },
+ {
+ encoding: "telemetry.test-1-1",
+ prio: {},
+ },
+ ];
+}
+
+add_task(async function setup() {
+ // Trigger a proper telemetry init.
+ do_get_profile(true);
+ // Make sure we don't generate unexpected pings due to pref changes.
+ await setEmptyPrefWatchlist();
+
+ await TelemetryController.testSetup();
+ TelemetryPrioPing.testReset();
+});
+
+// Similarly to test_EventPing tests in this file often follow the form:
+// 1: Fake out timeout, ping submission, and snapshotting
+// 2: Trigger a "prio" ping to happen
+// 3: Inside the fake ping submission, ensure the ping is correctly formed.
+// In sinon this would be replaced with spies and .wasCalledWith().
+
+add_task(async function test_limit_reached() {
+ // Ensure that on being notified of the limit we immediately trigger a ping
+ // with reason "max"
+
+ fakePolicy(
+ pass,
+ pass,
+ (type, payload, options) => {
+ checkPingStructure(type, payload, options);
+ Assert.equal(
+ payload.reason,
+ TelemetryPrioPing.Reason.MAX,
+ "Sent using max reason."
+ );
+ },
+ fakeSnapshot
+ );
+ Services.obs.notifyObservers(null, "origin-telemetry-storage-limit-reached");
+});
+
+add_task(async function test_periodic() {
+ fakePolicy(
+ pass,
+ pass,
+ (type, payload, options) => {
+ checkPingStructure(type, payload, options);
+ Assert.equal(
+ payload.reason,
+ TelemetryPrioPing.Reason.PERIODIC,
+ "Sent with periodic reason."
+ );
+ },
+ fakeSnapshot
+ );
+
+ // This is normally triggered by the scheduler once a day
+ TelemetryPrioPing.periodicPing();
+});
+
+add_task(async function test_shutdown() {
+ fakePolicy(
+ fail,
+ pass,
+ (type, payload, options) => {
+ checkPingStructure(type, payload, options);
+ Assert.equal(
+ payload.reason,
+ TelemetryPrioPing.Reason.SHUTDOWN,
+ "Sent with shutdown reason."
+ );
+ },
+ fakeSnapshot
+ );
+ await TelemetryPrioPing.shutdown();
+});