michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: const Cu = Components.utils; michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: // The @mozilla/xre/app-info;1 XPCOM object provided by the xpcshell test harness doesn't michael@0: // implement the nsIAppInfo interface, which is needed by Services.jsm and TelemetryPing.jsm. michael@0: // updateAppInfo() creates and registers a minimal mock app-info. michael@0: Cu.import("resource://testing-common/AppInfo.jsm"); michael@0: updateAppInfo(); michael@0: michael@0: function getSimpleMeasurementsFromTelemetryPing() { michael@0: return Cu.import("resource://gre/modules/TelemetryPing.jsm", {}). michael@0: TelemetryPing.getPayload().simpleMeasurements; michael@0: } michael@0: michael@0: function run_test() { michael@0: do_test_pending(); michael@0: const Telemetry = Services.telemetry; michael@0: Telemetry.asyncFetchTelemetryData(function () { michael@0: try { michael@0: actualTest(); michael@0: } michael@0: catch(e) { michael@0: do_throw("Failed: " + e); michael@0: } michael@0: do_test_finished(); michael@0: }); michael@0: } michael@0: michael@0: function actualTest() { michael@0: // Test the module logic michael@0: let tmp = {}; michael@0: Cu.import("resource://gre/modules/TelemetryTimestamps.jsm", tmp); michael@0: let TelemetryTimestamps = tmp.TelemetryTimestamps; michael@0: let now = Date.now(); michael@0: TelemetryTimestamps.add("foo"); michael@0: do_check_true(TelemetryTimestamps.get().foo != null); // foo was added michael@0: do_check_true(TelemetryTimestamps.get().foo >= now); // foo has a reasonable value michael@0: michael@0: // Add timestamp with value michael@0: // Use a value far in the future since TelemetryPing substracts the time of michael@0: // process initialization. michael@0: const YEAR_4000_IN_MS = 64060588800000; michael@0: TelemetryTimestamps.add("bar", YEAR_4000_IN_MS); michael@0: do_check_eq(TelemetryTimestamps.get().bar, YEAR_4000_IN_MS); // bar has the right value michael@0: michael@0: // Can't add the same timestamp twice michael@0: TelemetryTimestamps.add("bar", 2); michael@0: do_check_eq(TelemetryTimestamps.get().bar, YEAR_4000_IN_MS); // bar wasn't overwritten michael@0: michael@0: let threw = false; michael@0: try { michael@0: TelemetryTimestamps.add("baz", "this isn't a number"); michael@0: } catch (ex) { michael@0: threw = true; michael@0: } michael@0: do_check_true(threw); // adding non-number threw michael@0: do_check_null(TelemetryTimestamps.get().baz); // no baz was added michael@0: michael@0: // Test that the data gets added to the telemetry ping properly michael@0: let simpleMeasurements = getSimpleMeasurementsFromTelemetryPing(); michael@0: do_check_true(simpleMeasurements != null); // got simple measurements from ping data michael@0: do_check_true(simpleMeasurements.foo > 1); // foo was included michael@0: do_check_true(simpleMeasurements.bar > 1); // bar was included michael@0: do_check_null(simpleMeasurements.baz); // baz wasn't included since it wasn't added michael@0: }