bug 1482466 - Ensure subsession activeTicks are correct in simpleMeasurements r=Dexter
authorChris H-C <chutten@mozilla.com>
Tue, 14 Aug 2018 12:23:49 +0000
changeset 828998 07bf059b54930e579c660cfb7434cb37f3dd3582
parent 828997 0af5942407da25f69e19a5533e921c5f6722fbf8
child 828999 cfadc4196e56392680bf53a81740ecdb74976d30
child 829245 6181783d973c1927d7e63f2ef9a5dbe9d9541b05
push id118741
push userbmo:kshvmdn@gmail.com
push dateTue, 14 Aug 2018 18:31:47 +0000
reviewersDexter
bugs1482466
milestone63.0a1
bug 1482466 - Ensure subsession activeTicks are correct in simpleMeasurements r=Dexter Differential Revision: https://phabricator.services.mozilla.com/D3128
toolkit/components/telemetry/TelemetrySession.jsm
toolkit/components/telemetry/tests/unit/test_TelemetrySession_activeTicks.js
--- a/toolkit/components/telemetry/TelemetrySession.jsm
+++ b/toolkit/components/telemetry/TelemetrySession.jsm
@@ -804,17 +804,17 @@ var Impl = {
       ret[ioCounter] = this._startupIO[ioCounter];
 
     let activeTicks = this._sessionActiveTicks;
     if (isSubsession) {
       activeTicks = this._sessionActiveTicks - this._subsessionStartActiveTicks;
     }
 
     if (clearSubsession) {
-      this._subsessionStartActiveTicks = activeTicks;
+      this._subsessionStartActiveTicks = this._sessionActiveTicks;
     }
 
     ret.activeTicks = activeTicks;
 
     return ret;
   },
 
   /**
--- a/toolkit/components/telemetry/tests/unit/test_TelemetrySession_activeTicks.js
+++ b/toolkit/components/telemetry/tests/unit/test_TelemetrySession_activeTicks.js
@@ -1,15 +1,36 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/
 */
 
 ChromeUtils.import("resource://gre/modules/TelemetryController.jsm", this);
 ChromeUtils.import("resource://gre/modules/TelemetrySession.jsm", this);
 
+function tick(aHowMany) {
+  for (let i = 0; i < aHowMany; i++) {
+    Services.obs.notifyObservers(null, "user-interaction-active");
+  }
+}
+
+function checkSessionTicks(aExpected) {
+  let payload = TelemetrySession.getPayload();
+  Assert.equal(payload.simpleMeasurements.activeTicks, aExpected,
+               "Should record the expected number of active ticks for the session.");
+}
+
+function checkSubsessionTicks(aExpected, aClearSubsession) {
+  let payload = TelemetrySession.getPayload("main", aClearSubsession);
+  Assert.equal(payload.simpleMeasurements.activeTicks, aExpected,
+               "Should record the expected number of active ticks for the subsession.");
+  if (aExpected > 0) {
+    Assert.equal(payload.processes.parent.scalars["browser.engagement.active_ticks"], aExpected,
+                 "Should record the expected number of active ticks for the subsession, in a scalar.");
+  }
+}
 
 add_task(async function test_setup() {
   do_get_profile();
   // Make sure we don't generate unexpected pings due to pref changes.
   await setEmptyPrefWatchlist();
 });
 
 add_task(async function test_record_activeTicks() {
@@ -44,8 +65,38 @@ add_task(async function test_record_acti
   Services.obs.notifyObservers(null, "user-interaction-active");
   checkActiveTicks(4);
 
   Services.obs.notifyObservers(null, "user-interaction-active");
   checkActiveTicks(5);
 
   await TelemetryController.testShutdown();
 });
+
+add_task({
+  skip_if: () => gIsAndroid
+},
+async function test_subsession_activeTicks() {
+  await TelemetryController.testReset();
+  Telemetry.clearScalars();
+
+  tick(5);
+  checkSessionTicks(5);
+  checkSubsessionTicks(5, true);
+
+  // After clearing the subsession, subsession ticks should be 0 but session
+  // ticks should still be 5.
+  checkSubsessionTicks(0);
+  checkSessionTicks(5);
+
+  tick(1);
+  checkSessionTicks(6);
+  checkSubsessionTicks(1, true);
+
+  checkSubsessionTicks(0);
+  checkSessionTicks(6);
+
+  tick(2);
+  checkSessionTicks(8);
+  checkSubsessionTicks(2);
+
+  await TelemetryController.testShutdown();
+});