From 6bf0a5cb5034a7e684dcc3500e841785237ce2dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 19:32:43 +0200 Subject: Adding upstream version 1:115.7.0. Signed-off-by: Daniel Baumann --- .../test/xpcshell/test_AboutWelcomeTelemetry.js | 101 +++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 browser/components/newtab/test/xpcshell/test_AboutWelcomeTelemetry.js (limited to 'browser/components/newtab/test/xpcshell/test_AboutWelcomeTelemetry.js') diff --git a/browser/components/newtab/test/xpcshell/test_AboutWelcomeTelemetry.js b/browser/components/newtab/test/xpcshell/test_AboutWelcomeTelemetry.js new file mode 100644 index 0000000000..5ecc20f804 --- /dev/null +++ b/browser/components/newtab/test/xpcshell/test_AboutWelcomeTelemetry.js @@ -0,0 +1,101 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +"use strict"; + +const { AboutWelcomeTelemetry } = ChromeUtils.import( + "resource://activity-stream/aboutwelcome/lib/AboutWelcomeTelemetry.jsm" +); +const { AttributionCode } = ChromeUtils.importESModule( + "resource:///modules/AttributionCode.sys.mjs" +); +const { sinon } = ChromeUtils.importESModule( + "resource://testing-common/Sinon.sys.mjs" +); +const TELEMETRY_PREF = "browser.newtabpage.activity-stream.telemetry"; + +add_setup(function setup() { + do_get_profile(); + Services.fog.initializeFOG(); +}); + +add_task(function test_enabled() { + registerCleanupFunction(() => { + Services.prefs.clearUserPref(TELEMETRY_PREF); + }); + Services.prefs.setBoolPref(TELEMETRY_PREF, true); + + const AWTelemetry = new AboutWelcomeTelemetry(); + + equal(AWTelemetry.telemetryEnabled, true, "Telemetry should be on"); + + Services.prefs.setBoolPref(TELEMETRY_PREF, false); + + equal(AWTelemetry.telemetryEnabled, false, "Telemetry should be off"); +}); + +add_task(async function test_pingPayload() { + registerCleanupFunction(() => { + Services.prefs.clearUserPref(TELEMETRY_PREF); + }); + Services.prefs.setBoolPref(TELEMETRY_PREF, true); + const AWTelemetry = new AboutWelcomeTelemetry(); + const stub = sinon.stub( + AWTelemetry.pingCentre, + "sendStructuredIngestionPing" + ); + sinon.stub(AWTelemetry, "_createPing").resolves({ event: "MOCHITEST" }); + + let pingSubmitted = false; + GleanPings.messagingSystem.testBeforeNextSubmit(() => { + pingSubmitted = true; + Assert.equal(Glean.messagingSystem.event.testGetValue(), "MOCHITEST"); + }); + await AWTelemetry.sendTelemetry(); + + equal(stub.callCount, 1, "Call was made"); + // check the endpoint + ok( + stub.firstCall.args[1].includes("/messaging-system/onboarding"), + "Endpoint is correct" + ); + + ok(pingSubmitted, "Glean ping was submitted"); +}); + +add_task(function test_mayAttachAttribution() { + const sandbox = sinon.createSandbox(); + const AWTelemetry = new AboutWelcomeTelemetry(); + + sandbox.stub(AttributionCode, "getCachedAttributionData").returns(null); + + let ping = AWTelemetry._maybeAttachAttribution({}); + + equal(ping.attribution, undefined, "Should not set attribution if it's null"); + + sandbox.restore(); + sandbox.stub(AttributionCode, "getCachedAttributionData").returns({}); + ping = AWTelemetry._maybeAttachAttribution({}); + + equal( + ping.attribution, + undefined, + "Should not set attribution if it's empty" + ); + + const attr = { + source: "google.com", + medium: "referral", + campaign: "Firefox-Brand-US-Chrome", + content: "(not set)", + experiment: "(not set)", + variation: "(not set)", + ua: "chrome", + }; + sandbox.restore(); + sandbox.stub(AttributionCode, "getCachedAttributionData").returns(attr); + ping = AWTelemetry._maybeAttachAttribution({}); + + equal(ping.attribution, attr, "Should set attribution if it presents"); +}); -- cgit v1.2.3