browser_aboutwelcome_glean.js (4580B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 /** 7 * Tests for the Glean version of onboarding telemetry. 8 */ 9 10 const { AboutWelcomeTelemetry } = ChromeUtils.importESModule( 11 "resource:///modules/aboutwelcome/AboutWelcomeTelemetry.sys.mjs" 12 ); 13 14 const TEST_DEFAULT_CONTENT = [ 15 { 16 id: "AW_STEP1", 17 18 content: { 19 position: "split", 20 title: "Step 1", 21 page: "page 1", 22 source: "test", 23 primary_button: { 24 label: "Next", 25 action: { 26 navigate: true, 27 }, 28 }, 29 secondary_button: { 30 label: "link", 31 }, 32 secondary_button_top: { 33 label: "link top", 34 action: { 35 type: "SHOW_FIREFOX_ACCOUNTS", 36 data: { entrypoint: "test" }, 37 }, 38 }, 39 }, 40 }, 41 { 42 id: "AW_STEP2", 43 content: { 44 position: "center", 45 title: "Step 2", 46 page: "page 1", 47 source: "test", 48 primary_button: { 49 label: "Next", 50 action: { 51 navigate: true, 52 }, 53 }, 54 secondary_button: { 55 label: "link", 56 }, 57 has_noodles: true, 58 }, 59 }, 60 ]; 61 62 const TEST_DEFAULT_JSON = JSON.stringify(TEST_DEFAULT_CONTENT); 63 64 add_task(async function test_welcome_telemetry() { 65 // Have to turn on AS telemetry for anything to be recorded. 66 await SpecialPowers.pushPrefEnv({ 67 set: [["browser.newtabpage.activity-stream.telemetry", true]], 68 }); 69 registerCleanupFunction(async () => { 70 await SpecialPowers.popPrefEnv(); 71 }); 72 73 Services.fog.testResetFOG(); 74 // Let's check that there is nothing in the impression event. 75 // This is useful in mochitests because glean inits fairly late in startup. 76 // We want to make sure we are fully initialized during testing so that 77 // when we call testGetValue() we get predictable behavior. 78 Assert.equal(undefined, Glean.messagingSystem.messageId.testGetValue()); 79 80 // Setup testBeforeNextSubmit. We do this first, progress onboarding, submit 81 // and then check submission. We put the asserts inside testBeforeNextSubmit 82 // because metric lifetimes are 'ping' and are cleared after submission. 83 // See: https://firefox-source-docs.mozilla.org/toolkit/components/glean/user/instrumentation_tests.html#xpcshell-tests 84 let pingSubmitted = false; 85 GleanPings.messagingSystem.testBeforeNextSubmit(() => { 86 pingSubmitted = true; 87 88 const message = Glean.messagingSystem.messageId.testGetValue(); 89 // Because of the asynchronous nature of receiving messages, we cannot 90 // guarantee that we will get the same message first. Instead we check 91 // that the one we get is a valid example of that type. 92 Assert.ok( 93 message.startsWith("MR_WELCOME_DEFAULT"), 94 "Ping is of an expected type" 95 ); 96 Assert.equal( 97 Glean.messagingSystem.unknownKeyCount.testGetValue(), 98 undefined 99 ); 100 }); 101 102 let browser = await openAboutWelcome(TEST_DEFAULT_JSON); 103 // `openAboutWelcome` isn't synchronous wrt the onboarding flow impressing. 104 await TestUtils.waitForCondition( 105 () => pingSubmitted, 106 "Ping was submitted, callback was called." 107 ); 108 109 // Let's reset and assert some values in the next button click. 110 pingSubmitted = false; 111 GleanPings.messagingSystem.testBeforeNextSubmit(() => { 112 pingSubmitted = true; 113 114 // Sometimes the impression for MR_WELCOME_DEFAULT_0_AW_STEP1_SS reaches 115 // the parent process before the button click does. 116 // See https://bugzilla.mozilla.org/show_bug.cgi?id=1834620 117 if (Glean.messagingSystem.event.testGetValue() === "IMPRESSION") { 118 Assert.equal( 119 Glean.messagingSystem.eventPage.testGetValue(), 120 "about:welcome" 121 ); 122 const message = Glean.messagingSystem.messageId.testGetValue(); 123 Assert.ok( 124 message.startsWith("MR_WELCOME_DEFAULT"), 125 "Ping is of an expected type" 126 ); 127 } else { 128 // This is the common and, to my mind, correct case: 129 // the click coming before the next steps' impression. 130 Assert.equal(Glean.messagingSystem.event.testGetValue(), "CLICK_BUTTON"); 131 Assert.equal( 132 Glean.messagingSystem.eventSource.testGetValue(), 133 "primary_button" 134 ); 135 Assert.equal( 136 Glean.messagingSystem.messageId.testGetValue(), 137 "MR_WELCOME_DEFAULT_0_AW_STEP1" 138 ); 139 } 140 Assert.equal( 141 Glean.messagingSystem.unknownKeyCount.testGetValue(), 142 undefined 143 ); 144 }); 145 await onButtonClick(browser, "button.primary"); 146 Assert.ok(pingSubmitted, "Ping was submitted, callback was called."); 147 });