toolkit/modules/tests/xpcshell/test_TelemetryTimestamps.js

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

mercurial