Bug 1119959: Fix test_TelemetryPing.js for comm-central and simplify it. r=gfritzsche
authorAvi Halachmi <avihpit@yahoo.com>
Sat, 10 Jan 2015 09:52:58 +0200
changeset 249161 2b06fff97d12ed5c456795f3214a807a36e3de7f
parent 249160 12dd1ad43923ac63fa00cdf1484991d5624c4daf
child 249162 58d6a621d9626547407708b7c78cf5dbf1fa935a
push id4489
push userraliiev@mozilla.com
push dateMon, 23 Feb 2015 15:17:55 +0000
treeherdermozilla-beta@fd7c3dc24146 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgfritzsche
bugs1119959
milestone37.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 1119959: Fix test_TelemetryPing.js for comm-central and simplify it. r=gfritzsche
services/datareporting/DataReportingService.js
toolkit/components/telemetry/tests/unit/test_TelemetryPing.js
--- a/services/datareporting/DataReportingService.js
+++ b/services/datareporting/DataReportingService.js
@@ -66,16 +66,20 @@ this.DataReportingService = function () 
                .getService(Ci.nsIObserverService);
 
   this._clientID = null;
   this._loadClientIdTask = null;
   this._saveClientIdTask = null;
 
   this._stateDir = null;
   this._stateFilePath = null;
+
+  // Used for testing only, when true results in getSessionRecorder() returning
+  // undefined. Controlled via simulate* methods.
+  this._simulateNoSessionRecorder = false;
 }
 
 DataReportingService.prototype = Object.freeze({
   classID: Components.ID("{41f6ae36-a79f-4613-9ac3-915e70f83789}"),
 
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
                                          Ci.nsISupportsWeakReference]),
 
@@ -392,17 +396,27 @@ DataReportingService.prototype = Object.
   }),
 
   /**
    * Returns the SessionRecorder instance associated with the data reporting service.
    * Returns an actual object only if FHR is enabled and after initialization,
    * else returns undefined.
    */
   getSessionRecorder: function() {
-    return this.sessionRecorder;
+    return this._simulateNoSessionRecorder ? undefined : this.sessionRecorder;
+  },
+
+  // These two simulate* methods below are only used for testings and control
+  // whether getSessionRecorder() behaves normally or forced to return undefined
+  simulateNoSessionRecorder() {
+    this._simulateNoSessionRecorder = true;
+  },
+
+  simulateRestoreSessionRecorder() {
+    this._simulateNoSessionRecorder = false;
   },
 
   /*
    * Simulate a restart of the service. This is for testing only.
    */
   _reset: Task.async(function* () {
     yield this._loadClientIdTask;
     yield this._saveClientIdTask;
--- a/toolkit/components/telemetry/tests/unit/test_TelemetryPing.js
+++ b/toolkit/components/telemetry/tests/unit/test_TelemetryPing.js
@@ -43,24 +43,25 @@ const NUMBER_OF_THREADS_TO_LAUNCH = 30;
 let gNumberOfThreadsLaunched = 0;
 
 const PREF_BRANCH = "toolkit.telemetry.";
 const PREF_ENABLED = PREF_BRANCH + "enabled";
 const PREF_FHR_UPLOAD_ENABLED = "datareporting.healthreport.uploadEnabled";
 const PREF_FHR_SERVICE_ENABLED = "datareporting.healthreport.service.enabled";
 
 const HAS_DATAREPORTINGSERVICE = "@mozilla.org/datareporting/service;1" in Cc;
+const SESSION_RECORDER_EXPECTED = HAS_DATAREPORTINGSERVICE &&
+                                  Preferences.get(PREF_FHR_SERVICE_ENABLED, true);
 
 const Telemetry = Cc["@mozilla.org/base/telemetry;1"].getService(Ci.nsITelemetry);
 
 let gHttpServer = new HttpServer();
 let gServerStarted = false;
 let gRequestIterator = null;
 let gDataReportingClientID = null;
-let gOrigFhrServiceEnabled = true;
 
 XPCOMUtils.defineLazyGetter(this, "gDatareportingService",
   () => Cc["@mozilla.org/datareporting/service;1"]
           .getService(Ci.nsISupports)
           .wrappedJSObject);
 
 function sendPing () {
   TelemetryPing.gatherStartup();
@@ -220,17 +221,17 @@ function checkPayload(request, reason, s
   do_check_true(payload.simpleMeasurements.uptime >= 0);
   do_check_true(payload.simpleMeasurements.startupInterrupted === 1);
   do_check_eq(payload.simpleMeasurements.shutdownDuration, SHUTDOWN_TIME);
   do_check_eq(payload.simpleMeasurements.savedPings, 1);
   do_check_true("maximalNumberOfConcurrentThreads" in payload.simpleMeasurements);
   do_check_true(payload.simpleMeasurements.maximalNumberOfConcurrentThreads >= gNumberOfThreadsLaunched);
 
   let activeTicks = payload.simpleMeasurements.activeTicks;
-  do_check_true(HAS_DATAREPORTINGSERVICE ? activeTicks >= 0 : activeTicks == -1);
+  do_check_true(SESSION_RECORDER_EXPECTED ? activeTicks >= 0 : activeTicks == -1);
 
   do_check_eq(payload.simpleMeasurements.failedProfileLockCount,
               FAILED_PROFILE_LOCK_ATTEMPTS);
   let profileDirectory = Services.dirsvc.get("ProfD", Ci.nsIFile);
   let failedProfileLocksFile = profileDirectory.clone();
   failedProfileLocksFile.append("Telemetry.FailedProfileLocks.txt");
   do_check_true(!failedProfileLocksFile.exists());
 
@@ -434,37 +435,26 @@ function run_test() {
   try {
     let gfxInfo = Cc["@mozilla.org/gfx/info;1"].getService(Ci.nsIGfxInfoDebug);
     gfxInfo.spoofVendorID("0xabcd");
     gfxInfo.spoofDeviceID("0x1234");
   } catch (x) {
     // If we can't test gfxInfo, that's fine, we'll note it later.
   }
 
-  // make sure getSessionRecorder() can be called before the DRS init.
-  // It's not a requirement that it returns undefined, but that's how it behaves
-  // now - so just let this test fail if this behavior changes.
-  if (HAS_DATAREPORTINGSERVICE) {
-    do_check_true(gDatareportingService.getSessionRecorder() === undefined);
-  }
-
   // Addon manager needs a profile directory
   do_get_profile();
   createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2");
 
   Services.prefs.setBoolPref(PREF_ENABLED, true);
   Services.prefs.setBoolPref(PREF_FHR_UPLOAD_ENABLED, true);
 
   // Send the needed startup notifications to the datareporting service
   // to ensure that it has been initialized.
   if (HAS_DATAREPORTINGSERVICE) {
-    // Initially disable FHR to verify that activeTicks ends up as -1
-    gOrigFhrServiceEnabled = Services.prefs.getBoolPref(PREF_FHR_SERVICE_ENABLED, true);
-    Services.prefs.setBoolPref(PREF_FHR_SERVICE_ENABLED, false);
-
     gDatareportingService.observe(null, "app-startup", null);
     gDatareportingService.observe(null, "profile-after-change", null);
   }
 
   // Make it look like we've previously failed to lock a profile a couple times.
   write_fake_failedprofilelocks_file();
 
   // Make it look like we've shutdown before.
@@ -508,27 +498,27 @@ function actualTest() {
   registerFakePluginHost();
 
   run_next_test();
 }
 
 add_task(function* asyncSetup() {
   yield TelemetryPing.setup();
 
-  // When FHR is disabled or no DRS, the payload's activeTicks should be -1.
-  do_check_true(TelemetryPing.getPayload().simpleMeasurements.activeTicks == -1);
+  if (HAS_DATAREPORTINGSERVICE) {
+    // force getSessionRecorder()==undefined to check the payload's activeTicks
+    gDatareportingService.simulateNoSessionRecorder();
+  }
+
+  // When no DRS or no DRS.getSessionRecorder(), activeTicks should be -1.
+  do_check_eq(TelemetryPing.getPayload().simpleMeasurements.activeTicks, -1);
 
   if (HAS_DATAREPORTINGSERVICE) {
-    // after we got the no-FHR activeTicks, re-enable FHR and re-init the DRS.
-    // Note: this relies on the fact that the data reporting service reinitializes
-    // itself when calling its 'observe' method, without checking if it's already
-    // initialized. If this DRS behavior changes, this test would need to be adapted.
-    Services.prefs.setBoolPref(PREF_FHR_SERVICE_ENABLED, gOrigFhrServiceEnabled);
-    gDatareportingService.observe(null, "app-startup", null);
-    gDatareportingService.observe(null, "profile-after-change", null);
+    // Restore normal behavior for getSessionRecorder()
+    gDatareportingService.simulateRestoreSessionRecorder();
 
     gDataReportingClientID = yield gDatareportingService.getClientID();
 
     // We should have cached the client id now. Lets confirm that by
     // checking the client id before the async ping setup is finished.
     let promisePingSetup = TelemetryPing.reset();
     do_check_eq(TelemetryPing.clientID, gDataReportingClientID);
     yield promisePingSetup;