Bug 1448945 - Add test coverage for loading dynamic inbuilt events on startup. r=chutten
authorJan-Erik Rediger <jrediger@mozilla.com>
Tue, 10 Apr 2018 13:39:10 +0200
changeset 467822 efb850288b07e0fba98cce31f45bdd20f1ca0124
parent 467821 350a522001fae5494569ea4f8eec6b4556776505
child 467823 4557fca4b7e2b01bb2bec1db2464227b363a9824
push id9165
push userasasaki@mozilla.com
push dateThu, 26 Apr 2018 21:04:54 +0000
treeherdermozilla-beta@064c3804de2e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerschutten
bugs1448945
milestone61.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 1448945 - Add test coverage for loading dynamic inbuilt events on startup. r=chutten MozReview-Commit-ID: Dpl4GUSZePf
toolkit/components/telemetry/tests/unit/test_TelemetryEvents_buildFaster.js
--- a/toolkit/components/telemetry/tests/unit/test_TelemetryEvents_buildFaster.js
+++ b/toolkit/components/telemetry/tests/unit/test_TelemetryEvents_buildFaster.js
@@ -1,19 +1,123 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/
 */
 
+ChromeUtils.import("resource://services-common/utils.js");
+
+/**
+ * Return the path to the definitions file for the events.
+ */
+function getDefinitionsPath() {
+  // Write the event definition to the spec file in the binary directory.
+  let definitionFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
+  definitionFile = Services.dirsvc.get("GreD", Ci.nsIFile);
+  definitionFile.append("EventArtifactDefinitions.json");
+  return definitionFile.path;
+}
+
+add_task(async function test_setup() {
+  do_get_profile();
+});
+
+add_task({
+    // The test needs to write a file, and that fails in tests on Android.
+    // We don't really need the Android coverage, so skip on Android.
+    skip_if: () => AppConstants.platform == "android"
+  }, async function test_invalidJSON() {
+  const INVALID_JSON = "{ invalid,JSON { {1}";
+  const FILE_PATH = getDefinitionsPath();
+
+  // Write a corrupted JSON file.
+  await OS.File.writeAtomic(FILE_PATH, INVALID_JSON, { encoding: "utf-8", noOverwrite: false });
+
+  // Simulate Firefox startup. This should not throw!
+  await TelemetryController.testSetup();
+  await TelemetryController.testPromiseJsProbeRegistration();
+
+  // Cleanup.
+  await TelemetryController.testShutdown();
+  await OS.File.remove(FILE_PATH);
+});
+
+add_task({
+    // The test needs to write a file, and that fails in tests on Android.
+    // We don't really need the Android coverage, so skip on Android.
+    skip_if: () => AppConstants.platform == "android"
+  }, async function test_dynamicBuiltin() {
+  const DYNAMIC_EVENT_SPEC =  {
+    "telemetry.test.builtin": {
+      "test": {
+        "objects": [
+          "object1",
+          "object2"
+        ],
+        "expired": false,
+        "methods": [
+          "test1",
+          "test2"
+        ],
+        "extra_keys": [
+          "key2",
+          "key1"
+        ],
+        "record_on_release": false
+      }
+    }
+  };
+
+  Telemetry.clearEvents();
+
+  // Let's write to the definition file to also cover the file
+  // loading part.
+  const FILE_PATH = getDefinitionsPath();
+  await CommonUtils.writeJSON(DYNAMIC_EVENT_SPEC, FILE_PATH);
+
+  // Start TelemetryController to trigger loading the specs.
+  await TelemetryController.testReset();
+  await TelemetryController.testPromiseJsProbeRegistration();
+
+  // Record the events
+  const TEST_EVENT_NAME = "telemetry.test.builtin";
+  Telemetry.recordEvent(TEST_EVENT_NAME, "test1", "object1");
+  Telemetry.recordEvent(TEST_EVENT_NAME, "test2", "object1", null,
+                        {"key1": "foo", "key2": "bar"});
+  Telemetry.recordEvent(TEST_EVENT_NAME, "test2", "object2", null,
+                        {"key2": "bar"});
+
+  // Check the values we tried to store.
+  const snapshot =
+    Telemetry.snapshotEvents(Ci.nsITelemetry.DATASET_RELEASE_CHANNEL_OPTIN, false);
+  Assert.ok(("parent" in snapshot), "Should have parent events in the snapshot.");
+
+  let expected = [
+    [TEST_EVENT_NAME, "test1", "object1"],
+    [TEST_EVENT_NAME, "test2", "object1", null, {key1: "foo", key2: "bar"}],
+    [TEST_EVENT_NAME, "test2", "object2", null, {key2: "bar"}],
+  ];
+  let events = snapshot.parent;
+  Assert.equal(events.length, expected.length, "Should have recorded the right amount of events.");
+  for (let i = 0; i < expected.length; ++i) {
+    Assert.deepEqual(events[i].slice(1), expected[i],
+                     "Should have recorded the expected event data.");
+  }
+
+  // Clean up.
+  await TelemetryController.testShutdown();
+  await OS.File.remove(FILE_PATH);
+});
+
 add_task(async function test_dynamicBuiltinEvents() {
   Telemetry.clearEvents();
   Telemetry.canRecordExtended = true;
 
   const TEST_EVENT_NAME = "telemetry.test.dynamicbuiltin";
 
-  // Register some dynbamic builtin test events.
+  // Register some dynamic builtin test events.
   Telemetry.registerBuiltinEvents(TEST_EVENT_NAME, {
     // Event with only required fields.
     "test1": {
       methods: ["test1"],
       objects: ["object1"],
     },
     // Event with extra_keys.
     "test2": {