Bug 1539830 - Test "optout" ping. r=raphael
authorChris H-C <chutten@mozilla.com>
Mon, 13 May 2019 11:02:45 +0000
changeset 532420 e2b09eeb4cc18a3a4aeb66f26a1f2052aa3d9634
parent 532419 332a8ffe625f851dfa3014c3d52e945790984804
child 532421 7a303f34b6ce2df1f444b031cc947c01117bc0eb
push id11268
push usercsabou@mozilla.com
push dateTue, 14 May 2019 15:24:22 +0000
treeherdermozilla-beta@5fb7fcd568d6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersraphael
bugs1539830
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1539830 - Test "optout" ping. r=raphael This doesn't exactly match the test plan due to issues I experienced trying to set preferences or manually control when the Firefox under test closes and opens. Differential Revision: https://phabricator.services.mozilla.com/D29695
toolkit/components/telemetry/tests/marionette/harness/telemetry_harness/ping_filters.py
toolkit/components/telemetry/tests/marionette/tests/client/manifest.ini
toolkit/components/telemetry/tests/marionette/tests/client/test_optout_ping.py
--- a/toolkit/components/telemetry/tests/marionette/harness/telemetry_harness/ping_filters.py
+++ b/toolkit/components/telemetry/tests/marionette/harness/telemetry_harness/ping_filters.py
@@ -5,16 +5,26 @@
 
 class PingFilter(object):
     """Ping filter that accepts any pings."""
 
     def __call__(self, ping):
         return True
 
 
+class FirstShutdownPingFilter(PingFilter):
+    """Ping filter that accepts first-shutdown pings."""
+
+    def __call__(self, ping):
+        if not super(FirstShutdownPingFilter, self).__call__(ping):
+            return False
+
+        return ping["type"] == "first-shutdown"
+
+
 class MainPingFilter(PingFilter):
     """Ping filter that accepts main pings."""
 
     def __call__(self, ping):
         if not super(MainPingFilter, self).__call__(ping):
             return False
 
         return ping["type"] == "main"
@@ -31,12 +41,24 @@ class MainPingReasonFilter(MainPingFilte
 
     def __call__(self, ping):
         if not super(MainPingReasonFilter, self).__call__(ping):
             return False
 
         return ping["payload"]["info"]["reason"] == self.reason
 
 
+class OptoutPingFilter(PingFilter):
+    """Ping filter that accepts optout pings."""
+
+    def __call__(self, ping):
+        if not super(OptoutPingFilter, self).__call__(ping):
+            return False
+
+        return ping["type"] == "optout"
+
+
 ANY_PING = PingFilter()
+FIRST_SHUTDOWN_PING = FirstShutdownPingFilter()
 MAIN_PING = MainPingFilter()
 MAIN_SHUTDOWN_PING = MainPingReasonFilter("shutdown")
 MAIN_ENVIRONMENT_CHANGE_PING = MainPingReasonFilter("environment-change")
+OPTOUT_PING = OptoutPingFilter()
--- a/toolkit/components/telemetry/tests/marionette/tests/client/manifest.ini
+++ b/toolkit/components/telemetry/tests/marionette/tests/client/manifest.ini
@@ -1,6 +1,7 @@
 [DEFAULT]
 tags = client
 
 [test_main_tab_scalars.py]
+[test_optout_ping.py]
 [test_search_counts_across_sessions.py]
-[test_subsession_management.py]
\ No newline at end of file
+[test_subsession_management.py]
new file mode 100644
--- /dev/null
+++ b/toolkit/components/telemetry/tests/marionette/tests/client/test_optout_ping.py
@@ -0,0 +1,55 @@
+# 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 telemetry_harness.testcase import TelemetryTestCase
+from telemetry_harness.ping_filters import ANY_PING, OPTOUT_PING, FIRST_SHUTDOWN_PING
+
+
+class TestOptoutPing(TelemetryTestCase):
+    """Tests for "optout" ping."""
+
+    def disable_telemetry(self):
+        self.marionette.set_pref("datareporting.healthreport.uploadEnabled", False)
+
+    def enable_telemetry(self):
+        self.marionette.set_pref("datareporting.healthreport.uploadEnabled", True)
+
+    def test_optout_ping(self):
+        """Test for "optout" ping."""
+
+        # Get the client_id.
+        client_id = self.wait_for_ping(self.install_addon, ANY_PING)["clientId"]
+        self.assertIsValidUUID(client_id)
+
+        # Trigger an "optout" ping and check its contents.
+        optout_ping = self.wait_for_ping(self.disable_telemetry, OPTOUT_PING)
+
+        self.assertIsNotNone(optout_ping)
+        self.assertNotIn("clientId", optout_ping)
+        self.assertIn("payload", optout_ping)
+        self.assertNotIn("environment", optout_ping["payload"])
+
+        # Ensure the "optout" ping was the last ping sent
+        self.assertEqual(self.ping_server.pings[-1], optout_ping)
+
+        """ Test what happens when the user opts back in """
+
+        self.enable_telemetry()
+
+        # Restart Firefox so we can capture a "first-shutdown" ping.
+        # We want to check that the client id changed and telemetry.data_upload_optin is set.
+        shutdown_ping = self.wait_for_ping(self.restart_browser, FIRST_SHUTDOWN_PING)
+
+        self.assertIn("clientId", shutdown_ping)
+        self.assertIsValidUUID(shutdown_ping["clientId"])
+        self.assertNotEqual(shutdown_ping["clientId"], client_id)
+
+        self.assertIn("payload", shutdown_ping)
+        self.assertIn("processes", shutdown_ping["payload"])
+        self.assertIn("parent", shutdown_ping["payload"]["processes"])
+        self.assertIn("scalars", shutdown_ping["payload"]["processes"]["parent"])
+        scalars = shutdown_ping["payload"]["processes"]["parent"]["scalars"]
+
+        self.assertIn("telemetry.data_upload_optin", scalars)
+        self.assertTrue(scalars["telemetry.data_upload_optin"])