toolkit/modules/tests/xpcshell/test_TelemetryTimestamps.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/modules/tests/xpcshell/test_TelemetryTimestamps.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,70 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +const Cu = Components.utils;
     1.8 +const Cc = Components.classes;
     1.9 +const Ci = Components.interfaces;
    1.10 +Cu.import("resource://gre/modules/Services.jsm");
    1.11 +
    1.12 +// The @mozilla/xre/app-info;1 XPCOM object provided by the xpcshell test harness doesn't
    1.13 +// implement the nsIAppInfo interface, which is needed by Services.jsm and TelemetryPing.jsm.
    1.14 +// updateAppInfo() creates and registers a minimal mock app-info.
    1.15 +Cu.import("resource://testing-common/AppInfo.jsm");
    1.16 +updateAppInfo();
    1.17 +
    1.18 +function getSimpleMeasurementsFromTelemetryPing() {
    1.19 +  return Cu.import("resource://gre/modules/TelemetryPing.jsm", {}).
    1.20 +    TelemetryPing.getPayload().simpleMeasurements;
    1.21 +}
    1.22 +
    1.23 +function run_test() {
    1.24 +  do_test_pending();
    1.25 +  const Telemetry = Services.telemetry;
    1.26 +  Telemetry.asyncFetchTelemetryData(function () {
    1.27 +    try {
    1.28 +      actualTest();
    1.29 +    }
    1.30 +    catch(e) {
    1.31 +      do_throw("Failed: " + e);
    1.32 +    }
    1.33 +    do_test_finished();
    1.34 +  });
    1.35 +}
    1.36 +
    1.37 +function actualTest() {
    1.38 +  // Test the module logic
    1.39 +  let tmp = {};
    1.40 +  Cu.import("resource://gre/modules/TelemetryTimestamps.jsm", tmp);
    1.41 +  let TelemetryTimestamps = tmp.TelemetryTimestamps;
    1.42 +  let now = Date.now();
    1.43 +  TelemetryTimestamps.add("foo");
    1.44 +  do_check_true(TelemetryTimestamps.get().foo != null); // foo was added
    1.45 +  do_check_true(TelemetryTimestamps.get().foo >= now); // foo has a reasonable value
    1.46 +
    1.47 +  // Add timestamp with value
    1.48 +  // Use a value far in the future since TelemetryPing substracts the time of
    1.49 +  // process initialization.
    1.50 +  const YEAR_4000_IN_MS = 64060588800000;
    1.51 +  TelemetryTimestamps.add("bar", YEAR_4000_IN_MS);
    1.52 +  do_check_eq(TelemetryTimestamps.get().bar, YEAR_4000_IN_MS); // bar has the right value
    1.53 +
    1.54 +  // Can't add the same timestamp twice
    1.55 +  TelemetryTimestamps.add("bar", 2);
    1.56 +  do_check_eq(TelemetryTimestamps.get().bar, YEAR_4000_IN_MS); // bar wasn't overwritten
    1.57 +
    1.58 +  let threw = false;
    1.59 +  try {
    1.60 +    TelemetryTimestamps.add("baz", "this isn't a number");
    1.61 +  } catch (ex) {
    1.62 +    threw = true;
    1.63 +  }
    1.64 +  do_check_true(threw); // adding non-number threw
    1.65 +  do_check_null(TelemetryTimestamps.get().baz); // no baz was added
    1.66 +
    1.67 +  // Test that the data gets added to the telemetry ping properly
    1.68 +  let simpleMeasurements = getSimpleMeasurementsFromTelemetryPing();
    1.69 +  do_check_true(simpleMeasurements != null); // got simple measurements from ping data
    1.70 +  do_check_true(simpleMeasurements.foo > 1); // foo was included
    1.71 +  do_check_true(simpleMeasurements.bar > 1); // bar was included
    1.72 +  do_check_null(simpleMeasurements.baz); // baz wasn't included since it wasn't added
    1.73 +}

mercurial