toolkit/modules/tests/xpcshell/test_TelemetryTimestamps.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 * http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 const Cu = Components.utils;
michael@0 5 const Cc = Components.classes;
michael@0 6 const Ci = Components.interfaces;
michael@0 7 Cu.import("resource://gre/modules/Services.jsm");
michael@0 8
michael@0 9 // The @mozilla/xre/app-info;1 XPCOM object provided by the xpcshell test harness doesn't
michael@0 10 // implement the nsIAppInfo interface, which is needed by Services.jsm and TelemetryPing.jsm.
michael@0 11 // updateAppInfo() creates and registers a minimal mock app-info.
michael@0 12 Cu.import("resource://testing-common/AppInfo.jsm");
michael@0 13 updateAppInfo();
michael@0 14
michael@0 15 function getSimpleMeasurementsFromTelemetryPing() {
michael@0 16 return Cu.import("resource://gre/modules/TelemetryPing.jsm", {}).
michael@0 17 TelemetryPing.getPayload().simpleMeasurements;
michael@0 18 }
michael@0 19
michael@0 20 function run_test() {
michael@0 21 do_test_pending();
michael@0 22 const Telemetry = Services.telemetry;
michael@0 23 Telemetry.asyncFetchTelemetryData(function () {
michael@0 24 try {
michael@0 25 actualTest();
michael@0 26 }
michael@0 27 catch(e) {
michael@0 28 do_throw("Failed: " + e);
michael@0 29 }
michael@0 30 do_test_finished();
michael@0 31 });
michael@0 32 }
michael@0 33
michael@0 34 function actualTest() {
michael@0 35 // Test the module logic
michael@0 36 let tmp = {};
michael@0 37 Cu.import("resource://gre/modules/TelemetryTimestamps.jsm", tmp);
michael@0 38 let TelemetryTimestamps = tmp.TelemetryTimestamps;
michael@0 39 let now = Date.now();
michael@0 40 TelemetryTimestamps.add("foo");
michael@0 41 do_check_true(TelemetryTimestamps.get().foo != null); // foo was added
michael@0 42 do_check_true(TelemetryTimestamps.get().foo >= now); // foo has a reasonable value
michael@0 43
michael@0 44 // Add timestamp with value
michael@0 45 // Use a value far in the future since TelemetryPing substracts the time of
michael@0 46 // process initialization.
michael@0 47 const YEAR_4000_IN_MS = 64060588800000;
michael@0 48 TelemetryTimestamps.add("bar", YEAR_4000_IN_MS);
michael@0 49 do_check_eq(TelemetryTimestamps.get().bar, YEAR_4000_IN_MS); // bar has the right value
michael@0 50
michael@0 51 // Can't add the same timestamp twice
michael@0 52 TelemetryTimestamps.add("bar", 2);
michael@0 53 do_check_eq(TelemetryTimestamps.get().bar, YEAR_4000_IN_MS); // bar wasn't overwritten
michael@0 54
michael@0 55 let threw = false;
michael@0 56 try {
michael@0 57 TelemetryTimestamps.add("baz", "this isn't a number");
michael@0 58 } catch (ex) {
michael@0 59 threw = true;
michael@0 60 }
michael@0 61 do_check_true(threw); // adding non-number threw
michael@0 62 do_check_null(TelemetryTimestamps.get().baz); // no baz was added
michael@0 63
michael@0 64 // Test that the data gets added to the telemetry ping properly
michael@0 65 let simpleMeasurements = getSimpleMeasurementsFromTelemetryPing();
michael@0 66 do_check_true(simpleMeasurements != null); // got simple measurements from ping data
michael@0 67 do_check_true(simpleMeasurements.foo > 1); // foo was included
michael@0 68 do_check_true(simpleMeasurements.bar > 1); // bar was included
michael@0 69 do_check_null(simpleMeasurements.baz); // baz wasn't included since it wasn't added
michael@0 70 }

mercurial