Bug 1548479 - Rewrite telemetry-related assertions used in WebExtensions tests to make use of TelemetryTestUtils. r=mixedpuppy
authorLuca Greco <lgreco@mozilla.com>
Mon, 12 Aug 2019 16:44:36 +0000
changeset 488214 94ba58e4790d68d948af6a880e908f04ff235991
parent 488213 31e37db859a9add93c95c0be256f799c1e515fc7
child 488215 f69698a5cc46d070a64e8221a68a23da81708d66
push id113904
push userncsoregi@mozilla.com
push dateThu, 15 Aug 2019 19:41:00 +0000
treeherdermozilla-inbound@b283a7ef186c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmixedpuppy
bugs1548479
milestone70.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 1548479 - Rewrite telemetry-related assertions used in WebExtensions tests to make use of TelemetryTestUtils. r=mixedpuppy Depends on D41594 Differential Revision: https://phabricator.services.mozilla.com/D41595
toolkit/components/extensions/test/xpcshell/test_ext_storage_idb_data_migration.js
toolkit/components/extensions/test/xpcshell/test_ext_telemetry.js
--- a/toolkit/components/extensions/test/xpcshell/test_ext_storage_idb_data_migration.js
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_storage_idb_data_migration.js
@@ -1,56 +1,53 @@
 /* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
 /* vim: set sts=2 sw=2 et tw=80: */
 "use strict";
 
 // This test file verifies various scenarios related to the data migration
 // from the JSONFile backend to the IDB backend.
 
+const { getTrimmedString } = ChromeUtils.import(
+  "resource://gre/modules/ExtensionTelemetry.jsm"
+);
+const { ExtensionStorage } = ChromeUtils.import(
+  "resource://gre/modules/ExtensionStorage.jsm"
+);
+const { ExtensionStorageIDB } = ChromeUtils.import(
+  "resource://gre/modules/ExtensionStorageIDB.jsm"
+);
+const { TelemetryController } = ChromeUtils.import(
+  "resource://gre/modules/TelemetryController.jsm"
+);
+const { TelemetryTestUtils } = ChromeUtils.import(
+  "resource://testing-common/TelemetryTestUtils.jsm"
+);
+
+XPCOMUtils.defineLazyModuleGetters(this, {
+  OS: "resource://gre/modules/osfile.jsm",
+});
+
 AddonTestUtils.init(this);
 AddonTestUtils.createAppInfo(
   "xpcshell@tests.mozilla.org",
   "XPCShell",
   "1",
   "42"
 );
 
-const { ExtensionStorage } = ChromeUtils.import(
-  "resource://gre/modules/ExtensionStorage.jsm"
-);
-const { TelemetryController } = ChromeUtils.import(
-  "resource://gre/modules/TelemetryController.jsm"
-);
-const { TelemetryTestUtils } = ChromeUtils.import(
-  "resource://testing-common/TelemetryTestUtils.jsm"
-);
-
-const { ExtensionStorageIDB } = ChromeUtils.import(
-  "resource://gre/modules/ExtensionStorageIDB.jsm"
-);
-
-const { getTrimmedString } = ChromeUtils.import(
-  "resource://gre/modules/ExtensionTelemetry.jsm",
-  null
-);
-
-XPCOMUtils.defineLazyModuleGetters(this, {
-  OS: "resource://gre/modules/osfile.jsm",
-});
-
 const { promiseShutdownManager, promiseStartupManager } = AddonTestUtils;
 
 const {
   IDB_MIGRATED_PREF_BRANCH,
   IDB_MIGRATE_RESULT_HISTOGRAM,
 } = ExtensionStorageIDB;
 const CATEGORIES = ["success", "failure"];
 const EVENT_CATEGORY = "extensions.data";
 const EVENT_OBJECT = "storageLocal";
-const EVENT_METHODS = ["migrateResult"];
+const EVENT_METHOD = "migrateResult";
 const LEAVE_STORAGE_PREF = "extensions.webextensions.keepStorageOnUninstall";
 const LEAVE_UUID_PREF = "extensions.webextensions.keepUuidOnUninstall";
 const TELEMETRY_EVENTS_FILTER = {
   category: "extensions.data",
   method: "migrateResult",
   object: "storageLocal",
 };
 
@@ -87,45 +84,22 @@ function assertMigrationHistogramCount(c
 
   equal(
     histogram.snapshot().values[CATEGORIES.indexOf(category)],
     expectedCount,
     `Got the expected count on category "${category}" for histogram ${IDB_MIGRATE_RESULT_HISTOGRAM}`
   );
 }
 
-function assertTelemetryEvents(extensionId, expectedEvents) {
-  const snapshot = Services.telemetry.snapshotEvents(
-    Ci.nsITelemetry.DATASET_PRERELEASE_CHANNELS,
-    true
-  );
-
-  ok(
-    snapshot.parent && snapshot.parent.length > 0,
-    "Got parent telemetry events in the snapshot"
-  );
-
-  const migrateEvents = snapshot.parent
-    .filter(([timestamp, category, method, object, value]) => {
-      return (
-        category === EVENT_CATEGORY &&
-        EVENT_METHODS.includes(method) &&
-        object === EVENT_OBJECT &&
-        value === extensionId
-      );
-    })
-    .map(event => {
-      return { method: event[2], extra: event[5] };
-    });
-
-  Assert.deepEqual(
-    migrateEvents,
-    expectedEvents,
-    "Got the expected telemetry events"
-  );
+function assertTelemetryEvents(expectedEvents) {
+  TelemetryTestUtils.assertEvents(expectedEvents, {
+    category: EVENT_CATEGORY,
+    method: EVENT_METHOD,
+    object: EVENT_OBJECT,
+  });
 }
 
 add_task(async function setup() {
   Services.prefs.setBoolPref(ExtensionStorageIDB.BACKEND_ENABLED_PREF, true);
 
   await promiseStartupManager();
 
   // Telemetry test setup needed to ensure that the builtin events are defined
@@ -135,16 +109,19 @@ add_task(async function setup() {
   // This is actually only needed on Android, because it does not properly support unified telemetry
   // and so, if not enabled explicitly here, it would make these tests to fail when running on a
   // non-Nightly build.
   const oldCanRecordBase = Services.telemetry.canRecordBase;
   Services.telemetry.canRecordBase = true;
   registerCleanupFunction(() => {
     Services.telemetry.canRecordBase = oldCanRecordBase;
   });
+
+  // Clear any telemetry events collected so far.
+  Services.telemetry.clearEvents();
 });
 
 // Test that for newly installed extension the IDB backend is enabled without
 // any data migration.
 add_task(async function test_no_migration_for_newly_installed_extensions() {
   const EXTENSION_ID = "test-no-data-migration@mochi.test";
 
   await createExtensionJSONFileWithData(EXTENSION_ID, {
@@ -355,19 +332,20 @@ add_task(async function test_storage_loc
     ),
     true,
     `Got the ${IDB_MIGRATED_PREF_BRANCH} preference set to true as expected`
   );
 
   assertMigrationHistogramCount("success", 1);
   assertMigrationHistogramCount("failure", 0);
 
-  assertTelemetryEvents(EXTENSION_ID, [
+  assertTelemetryEvents([
     {
       method: "migrateResult",
+      value: EXTENSION_ID,
       extra: {
         backend: "IndexedDB",
         data_migrated: "y",
         has_jsonfile: "y",
         has_olddata: "y",
       },
     },
   ]);
@@ -463,19 +441,20 @@ add_task(async function test_extensionId
   const expectedTrimmedExtensionId = getTrimmedString(EXTENSION_ID);
 
   equal(
     expectedTrimmedExtensionId.length,
     80,
     "The trimmed version of the extensionId should be 80 chars long"
   );
 
-  assertTelemetryEvents(expectedTrimmedExtensionId, [
+  assertTelemetryEvents([
     {
       method: "migrateResult",
+      value: expectedTrimmedExtensionId,
       extra: {
         backend: "IndexedDB",
         data_migrated: "y",
         has_jsonfile: "y",
         has_olddata: "y",
       },
     },
   ]);
@@ -571,19 +550,20 @@ add_task(async function test_storage_loc
     ),
     true,
     `Got the ${IDB_MIGRATED_PREF_BRANCH} preference set to true as expected`
   );
 
   assertMigrationHistogramCount("success", 1);
   assertMigrationHistogramCount("failure", 0);
 
-  assertTelemetryEvents(EXTENSION_ID, [
+  assertTelemetryEvents([
     {
       method: "migrateResult",
+      value: EXTENSION_ID,
       extra: {
         backend: "IndexedDB",
         data_migrated: "y",
         has_jsonfile: "y",
         has_olddata: "n",
       },
     },
   ]);
@@ -646,19 +626,20 @@ add_task(async function test_storage_loc
   equal(
     await OS.File.exists(oldStorageFilename),
     true,
     "The old json storage should still be available if failed to be read"
   );
 
   await extension.unload();
 
-  assertTelemetryEvents(EXTENSION_ID, [
+  assertTelemetryEvents([
     {
       method: "migrateResult",
+      value: EXTENSION_ID,
       extra: {
         backend: "JSONFile",
         data_migrated: "n",
         error_name: "DataCloneError",
         has_jsonfile: "y",
         has_olddata: "y",
       },
     },
--- a/toolkit/components/extensions/test/xpcshell/test_ext_telemetry.js
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_telemetry.js
@@ -1,12 +1,19 @@
 "use strict";
 
-ChromeUtils.import("resource://gre/modules/TelemetryArchive.jsm", this);
-ChromeUtils.import("resource://gre/modules/TelemetryUtils.jsm", this);
+const { TelemetryArchive } = ChromeUtils.import(
+  "resource://gre/modules/TelemetryArchive.jsm"
+);
+const { TelemetryUtils } = ChromeUtils.import(
+  "resource://gre/modules/TelemetryUtils.jsm"
+);
+const { TelemetryTestUtils } = ChromeUtils.import(
+  "resource://testing-common/TelemetryTestUtils.jsm"
+);
 
 // All tests run privileged unless otherwise specified not to.
 function createExtension(backgroundScript, permissions, isPrivileged = true) {
   let extensionData = {
     background: backgroundScript,
     manifest: { permissions },
     isPrivileged,
   };
@@ -248,22 +255,26 @@ if (AppConstants.MOZ_BUILD_APP === "brow
           "test1",
           "object1"
         );
         browser.test.notifyPass("record_event_ok");
       },
       doneSignal: "record_event_ok",
     });
 
-    let events = Services.telemetry.snapshotEvents(
-      Ci.nsITelemetry.DATASET_PRERELEASE_CHANNELS,
-      true
+    TelemetryTestUtils.assertEvents(
+      [
+        {
+          category: "telemetry.test",
+          method: "test1",
+          object: "object1",
+        },
+      ],
+      { category: "telemetry.test" }
     );
-    equal(events.parent.length, 1);
-    equal(events.parent[0][1], "telemetry.test");
 
     Services.telemetry.setEventRecordingEnabled("telemetry.test", false);
     Services.telemetry.clearEvents();
   });
 
   // Bug 1536877
   add_task(async function test_telemetry_record_event_value_must_be_string() {
     Services.telemetry.clearEvents();
@@ -285,24 +296,27 @@ if (AppConstants.MOZ_BUILD_APP === "brow
           );
           browser.test.notifyPass("record_event_string_value");
           throw ex;
         }
       },
       doneSignal: "record_event_string_value",
     });
 
-    let events = Services.telemetry.snapshotEvents(
-      Ci.nsITelemetry.DATASET_PRERELEASE_CHANNELS,
-      true
+    TelemetryTestUtils.assertEvents(
+      [
+        {
+          category: "telemetry.test",
+          method: "test1",
+          object: "object1",
+          value: "value1",
+        },
+      ],
+      { category: "telemetry.test" }
     );
-    equal(events.parent.length, 1);
-    equal(events.parent[0][1], "telemetry.test");
-    equal(events.parent[0][3], "object1");
-    equal(events.parent[0][4], "value1");
 
     Services.telemetry.setEventRecordingEnabled("telemetry.test", false);
     Services.telemetry.clearEvents();
   });
 
   add_task(async function test_telemetry_register_scalars_string() {
     Services.telemetry.clearScalars();