Bug 837212: convert browser_TelemetryTimestamps.js to an xpcshell test r=Felipe

--HG--
rename : toolkit/modules/tests/browser_TelemetryTimestamps.js => toolkit/modules/tests/xpcshell/test_TelemetryTimestamps.js
This commit is contained in:
Irving Reid 2013-02-07 16:47:50 -05:00
parent 7b6e49cd35
commit e46e3f8e38
4 changed files with 73 additions and 80 deletions

View File

@ -14,9 +14,3 @@ XPCSHELL_TESTS = xpcshell
include $(topsrcdir)/config/rules.mk
_BROWSER_FILES = \
browser_TelemetryTimestamps.js \
$(NULL)
libs:: $(_BROWSER_FILES)
$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/browser/$(relativesrcdir)

View File

@ -1,74 +0,0 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
function getSimpleMeasurementsFromTelemetryPing() {
const TelemetryPing = Cc["@mozilla.org/base/telemetry-ping;1"].getService(Ci.nsITelemetryPing);
let ping = TelemetryPing.getPayload();
return ping.simpleMeasurements;
}
function test() {
waitForExplicitFinish()
const Telemetry = Services.telemetry;
Telemetry.asyncFetchTelemetryData(function () {
actualTest();
finish();
});
}
function actualTest() {
// Test the module logic
let tmp = {};
Cu.import("resource:///modules/TelemetryTimestamps.jsm", tmp);
let TelemetryTimestamps = tmp.TelemetryTimestamps;
let now = Date.now();
TelemetryTimestamps.add("foo");
ok(TelemetryTimestamps.get().foo, "foo was added");
ok(TelemetryTimestamps.get().foo >= now, "foo has a reasonable value");
// Add timestamp with value
// Use a value far in the future since TelemetryPing substracts the time of
// process initialization.
const YEAR_4000_IN_MS = 64060588800000;
TelemetryTimestamps.add("bar", YEAR_4000_IN_MS);
ok(TelemetryTimestamps.get().bar, "bar was added");
is(TelemetryTimestamps.get().bar, YEAR_4000_IN_MS, "bar has the right value");
// Can't add the same timestamp twice
TelemetryTimestamps.add("bar", 2);
is(TelemetryTimestamps.get().bar, YEAR_4000_IN_MS, "bar wasn't overwritten");
let threw = false;
try {
TelemetryTimestamps.add("baz", "this isn't a number");
} catch (ex) {
threw = true;
}
ok(threw, "adding baz threw");
ok(!TelemetryTimestamps.get().baz, "no baz was added");
// Test that the data gets added to the telemetry ping properly
let simpleMeasurements = getSimpleMeasurementsFromTelemetryPing();
ok(simpleMeasurements, "got simple measurements from ping data");
ok(simpleMeasurements.foo > 1, "foo was included");
ok(simpleMeasurements.bar > 1, "bar was included");
ok(!simpleMeasurements.baz, "baz wasn't included since it wasn't added");
// Check browser timestamps that we add
let props = [
// These can't be reliably tested when the test is run alone
//"delayedStartupStarted",
//"delayedStartupFinished",
"sessionRestoreInitialized",
// This doesn't get hit in the testing profile
//"sessionRestoreRestoring"
];
props.forEach(function (p) {
let value = simpleMeasurements[p];
ok(value, p + " exists");
ok(!isNaN(value), p + " is a number");
ok(value > 0, p + " value is reasonable");
});
}

View File

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

View File

@ -3,3 +3,4 @@ head =
tail =
[test_sqlite.js]
[test_TelemetryTimestamps.js]