test_AboutWelcomeTelemetry.js (2708B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ 3 */ 4 5 "use strict"; 6 7 const { AboutWelcomeTelemetry } = ChromeUtils.importESModule( 8 "resource:///modules/aboutwelcome/AboutWelcomeTelemetry.sys.mjs" 9 ); 10 const { AttributionCode } = ChromeUtils.importESModule( 11 "moz-src:///browser/components/attribution/AttributionCode.sys.mjs" 12 ); 13 const { sinon } = ChromeUtils.importESModule( 14 "resource://testing-common/Sinon.sys.mjs" 15 ); 16 const TELEMETRY_PREF = "browser.newtabpage.activity-stream.telemetry"; 17 18 add_setup(function setup() { 19 do_get_profile(); 20 Services.fog.initializeFOG(); 21 }); 22 23 add_task(function test_enabled() { 24 registerCleanupFunction(() => { 25 Services.prefs.clearUserPref(TELEMETRY_PREF); 26 }); 27 Services.prefs.setBoolPref(TELEMETRY_PREF, true); 28 29 const AWTelemetry = new AboutWelcomeTelemetry(); 30 31 equal(AWTelemetry.telemetryEnabled, true, "Telemetry should be on"); 32 33 Services.prefs.setBoolPref(TELEMETRY_PREF, false); 34 35 equal(AWTelemetry.telemetryEnabled, false, "Telemetry should be off"); 36 }); 37 38 add_task(async function test_pingPayload() { 39 registerCleanupFunction(() => { 40 Services.prefs.clearUserPref(TELEMETRY_PREF); 41 }); 42 Services.prefs.setBoolPref(TELEMETRY_PREF, true); 43 const AWTelemetry = new AboutWelcomeTelemetry(); 44 sinon.stub(AWTelemetry, "_createPing").resolves({ event: "MOCHITEST" }); 45 46 let pingSubmitted = false; 47 GleanPings.messagingSystem.testBeforeNextSubmit(() => { 48 pingSubmitted = true; 49 Assert.equal(Glean.messagingSystem.event.testGetValue(), "MOCHITEST"); 50 }); 51 await AWTelemetry.sendTelemetry(); 52 53 ok(pingSubmitted, "Glean ping was submitted"); 54 }); 55 56 add_task(function test_mayAttachAttribution() { 57 const sandbox = sinon.createSandbox(); 58 const AWTelemetry = new AboutWelcomeTelemetry(); 59 60 sandbox.stub(AttributionCode, "getCachedAttributionData").returns(null); 61 62 let ping = AWTelemetry._maybeAttachAttribution({}); 63 64 equal(ping.attribution, undefined, "Should not set attribution if it's null"); 65 66 sandbox.restore(); 67 sandbox.stub(AttributionCode, "getCachedAttributionData").returns({}); 68 ping = AWTelemetry._maybeAttachAttribution({}); 69 70 equal( 71 ping.attribution, 72 undefined, 73 "Should not set attribution if it's empty" 74 ); 75 76 const attr = { 77 source: "google.com", 78 medium: "referral", 79 campaign: "Firefox-Brand-US-Chrome", 80 content: "(not set)", 81 experiment: "(not set)", 82 variation: "(not set)", 83 ua: "chrome", 84 }; 85 sandbox.restore(); 86 sandbox.stub(AttributionCode, "getCachedAttributionData").returns(attr); 87 ping = AWTelemetry._maybeAttachAttribution({}); 88 89 equal(ping.attribution, attr, "Should set attribution if it presents"); 90 });