summaryrefslogtreecommitdiffstats
path: root/browser/components/search/test/marionette/telemetry/test_ping_submitted.py
blob: cefe2d72d1af0576e3e70075cbb76b49bb21e241 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- coding: utf-8 -*-
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from marionette_driver import Wait
from marionette_harness.marionette_test import MarionetteTestCase


class TestPingSubmitted(MarionetteTestCase):
    def setUp(self):
        super(TestPingSubmitted, self).setUp()

        self.marionette.set_context(self.marionette.CONTEXT_CHROME)

        self.marionette.enforce_gecko_prefs(
            {
                "datareporting.healthreport.uploadEnabled": True,
                "telemetry.fog.test.localhost_port": 3000,
                "browser.search.log": True,
            }
        )
        # The categorization ping is submitted on startup. If anything delays
        # its initialization, turning the preference on and immediately
        # attaching a categorization event could result in the ping being
        # submitted after the test event is reported but before the browser
        # restarts.
        script = """
            let [outerResolve] = arguments;
            (async () => {
                if (!Services.prefs.getBoolPref("browser.search.serpEventTelemetryCategorization.enabled")) {
                    let inited = new Promise(innerResolve => {
                    Services.obs.addObserver(function callback() {
                        Services.obs.removeObserver(callback, "categorization-recorder-init");
                        innerResolve();
                    }, "categorization-recorder-init");
                    });
                    Services.prefs.setBoolPref("browser.search.serpEventTelemetryCategorization.enabled", true);
                    await inited;
                }
            })().then(outerResolve);
        """
        self.marionette.execute_async_script(script)

    def test_ping_submit_on_start(self):
        # Record an event for the ping to eventually submit.
        self.marionette.execute_script(
            """
        Glean.serp.categorization.record({
            organic_category: "3",
            organic_num_domains: "1",
            organic_num_inconclusive: "0",
            organic_num_unknown: "0",
            sponsored_category: "4",
            sponsored_num_domains: "2",
            sponsored_num_inconclusive: "0",
            sponsored_num_unknown: "0",
            mappings_version: "1",
            app_version: "124",
            channel: "nightly",
            region: "US",
            partner_code: "ff",
            provider: "example",
            tagged: "true",
            num_ads_clicked: "0",
            num_ads_visible: "2",
        });
            """
        )

        Wait(self.marionette, timeout=60).until(
            lambda _: self.marionette.execute_script(
                """
            return (Glean.serp.categorization.testGetValue()?.length ?? 0) == 1;
                """
            ),
            message="Should have recorded a SERP categorization event before restart.",
        )

        self.marionette.restart(clean=False, in_app=True)

        Wait(self.marionette, timeout=60).until(
            lambda _: self.marionette.execute_script(
                """
            return (Glean.serp.categorization.testGetValue()?.length ?? 0) == 0;
                """
            ),
            message="SERP categorization should have been sent some time after restart.",
        )