test_ping_submitted.py (3714B)
1 # This Source Code Form is subject to the terms of the Mozilla Public 2 # License, v. 2.0. If a copy of the MPL was not distributed with this 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 4 5 from marionette_driver import Wait 6 from marionette_harness.marionette_test import MarionetteTestCase 7 8 9 class TestPingSubmitted(MarionetteTestCase): 10 def setUp(self): 11 super().setUp() 12 13 self.marionette.set_context(self.marionette.CONTEXT_CHROME) 14 15 self.marionette.enforce_gecko_prefs({ 16 "datareporting.healthreport.uploadEnabled": True, 17 "telemetry.fog.test.localhost_port": 3000, 18 "browser.search.log": True, 19 }) 20 # The categorization ping is submitted on startup. If anything delays 21 # its initialization, turning the preference on and immediately 22 # attaching a categorization event could result in the ping being 23 # submitted after the test event is reported but before the browser 24 # restarts. 25 script = """ 26 let [outerResolve] = arguments; 27 (async () => { 28 if (!Services.prefs.getBoolPref("browser.search.serpEventTelemetryCategorization.enabled")) { 29 let inited = new Promise(innerResolve => { 30 Services.obs.addObserver(function callback() { 31 Services.obs.removeObserver(callback, "categorization-recorder-init"); 32 innerResolve(); 33 }, "categorization-recorder-init"); 34 }); 35 Services.prefs.setBoolPref("browser.search.serpEventTelemetryCategorization.enabled", true); 36 await inited; 37 } 38 })().then(outerResolve); 39 """ 40 self.marionette.execute_async_script(script) 41 42 def test_ping_submit_on_start(self): 43 # Record an event for the ping to eventually submit. 44 self.marionette.execute_script( 45 """ 46 const { SERPCategorizationRecorder } = ChromeUtils.importESModule("moz-src:///browser/components/search/SERPCategorization.sys.mjs"); 47 SERPCategorizationRecorder.recordCategorizationTelemetry({ 48 organic_category: "3", 49 organic_num_domains: "1", 50 organic_num_inconclusive: "0", 51 organic_num_unknown: "0", 52 sponsored_category: "4", 53 sponsored_num_domains: "2", 54 sponsored_num_inconclusive: "0", 55 sponsored_num_unknown: "0", 56 mappings_version: "1", 57 app_version: "124", 58 channel: "nightly", 59 region: "US", 60 partner_code: "ff", 61 provider: "example", 62 tagged: "true", 63 num_ads_clicked: "0", 64 num_ads_hidden: "0", 65 num_ads_loaded: "2", 66 num_ads_visible: "2", 67 }); 68 """ 69 ) 70 71 Wait(self.marionette, timeout=60).until( 72 lambda _: self.marionette.execute_script( 73 """ 74 return (Glean.serp.categorization.testGetValue()?.length ?? 0) == 1; 75 """ 76 ), 77 message="Should have recorded a SERP categorization event before restart.", 78 ) 79 80 self.marionette.restart(clean=False, in_app=True) 81 82 Wait(self.marionette, timeout=60).until( 83 lambda _: self.marionette.execute_script( 84 """ 85 return (Glean.serp.categorization.testGetValue()?.length ?? 0) == 0; 86 """ 87 ), 88 message="SERP categorization should have been sent some time after restart.", 89 )