Bug 1137252 - Add test coverage for pings archiving. r=vladan

This commit is contained in:
Alessio Placitelli 2015-04-13 19:33:27 +02:00
Родитель 77e10bc782
Коммит 7e010337a6
5 изменённых файлов: 64 добавлений и 3 удалений

Просмотреть файл

@ -141,6 +141,13 @@ function getArchivedPingPath(aPingId, aDate, aType) {
return OS.Path.join(archivedPingDir, fileName);
};
/**
* This is a policy object used to override behavior for testing.
*/
let Policy = {
now: () => new Date(),
}
this.EXPORTED_SYMBOLS = ["TelemetryPing"];
this.TelemetryPing = Object.freeze({
@ -425,7 +432,7 @@ let Impl = {
let pingData = {
type: aType,
id: generateUUID(),
creationDate: (new Date()).toISOString(),
creationDate: (Policy.now()).toISOString(),
version: PING_FORMAT_VERSION,
application: this._getApplicationSection(),
payload: aPayload,

Просмотреть файл

@ -134,6 +134,8 @@ function fakeSchedulerTimer(set, clear) {
// Fake the current date.
function fakeNow(date) {
let ping = Cu.import("resource://gre/modules/TelemetryPing.jsm");
ping.Policy.now = () => date;
let session = Cu.import("resource://gre/modules/TelemetrySession.jsm");
session.Policy.now = () => date;
let environment = Cu.import("resource://gre/modules/TelemetryEnvironment.jsm");

Просмотреть файл

@ -33,6 +33,7 @@ const APP_NAME = "XPCShell";
const PREF_BRANCH = "toolkit.telemetry.";
const PREF_ENABLED = PREF_BRANCH + "enabled";
const PREF_ARCHIVE_ENABLED = PREF_BRANCH + "archive.enabled";
const PREF_FHR_UPLOAD_ENABLED = "datareporting.healthreport.uploadEnabled";
const PREF_FHR_SERVICE_ENABLED = "datareporting.healthreport.service.enabled";
@ -43,6 +44,11 @@ let gServerStarted = false;
let gRequestIterator = null;
let gClientID = null;
function getArchiveFilename(uuid, date, type) {
let ping = Cu.import("resource://gre/modules/TelemetryPing.jsm");
return ping.getArchivedPingPath(uuid, date, type);
}
function sendPing(aSendClientId, aSendEnvironment) {
if (gServerStarted) {
TelemetryPing.setServer("http://localhost:" + gHttpServer.identity.primaryPort);
@ -224,6 +230,51 @@ add_task(function* test_pingHasEnvironmentAndClientId() {
}
});
add_task(function* test_archivePings() {
const ARCHIVE_PATH =
OS.Path.join(OS.Constants.Path.profileDir, "datareporting", "archived");
let now = new Date(2009, 10, 18, 12, 0, 0);
fakeNow(now);
// Disable FHR upload so that pings don't get sent.
Preferences.set(PREF_FHR_UPLOAD_ENABLED, false);
// Register a new Ping Handler that asserts if a ping is received, then send a ping.
registerPingHandler(() => Assert.ok(false, "Telemetry must not send pings if not allowed to."));
let pingId = yield sendPing(true, true);
// Check that the ping was persisted to the pings archive, even with upload disabled.
let pingPath = getArchiveFilename(pingId, now, TEST_PING_TYPE);
Assert.ok((yield OS.File.exists(pingPath)),
"TelemetryPing must archive pings if FHR is enabled.");
// Check that pings don't get archived if not allowed to.
now = new Date(2010, 10, 18, 12, 0, 0);
fakeNow(now);
Preferences.set(PREF_ARCHIVE_ENABLED, false);
pingId = yield sendPing(true, true);
pingPath = getArchiveFilename(pingId, now, TEST_PING_TYPE);
Assert.ok(!(yield OS.File.exists(pingPath)),
"TelemetryPing must not archive pings if the archive pref is disabled.");
// Enable archiving and the upload so that pings get sent and archived again.
Preferences.set(PREF_FHR_UPLOAD_ENABLED, true);
Preferences.set(PREF_ARCHIVE_ENABLED, true);
now = new Date(2014, 06, 18, 22, 0, 0);
fakeNow(now);
// Restore the non asserting ping handler. This is done by the Request() constructor.
gRequestIterator = Iterator(new Request());
pingId = yield sendPing(true, true);
// Check that we archive pings when successfully sending them.
yield gRequestIterator.next();
pingPath = getArchiveFilename(pingId, now, TEST_PING_TYPE);
Assert.ok((yield OS.File.exists(pingPath)),
"TelemetryPing must archive pings if FHR is enabled.");
});
add_task(function* stopServer(){
gHttpServer.stop(do_test_finished);
});

Просмотреть файл

@ -18,6 +18,7 @@
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import("resource://gre/modules/Services.jsm", this);
Cu.import("resource://gre/modules/TelemetryPing.jsm", this);
Cu.import("resource://gre/modules/TelemetrySession.jsm", this);
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
@ -27,7 +28,7 @@ XPCOMUtils.defineLazyGetter(this, "gDatareportingService",
.wrappedJSObject);
// Force the Telemetry enabled preference so that TelemetrySession.reset() doesn't exit early.
Services.prefs.setBoolPref(TelemetrySession.Constants.PREF_ENABLED, true);
Services.prefs.setBoolPref(TelemetryPing.Constants.PREF_ENABLED, true);
// Set up our dummy AppInfo object so we can control the appBuildID.
Cu.import("resource://testing-common/AppInfo.jsm", this);

Просмотреть файл

@ -73,7 +73,7 @@ let createSavedPings = Task.async(function* (aPingInfos) {
let num = aPingInfos[type].num;
let age = now - aPingInfos[type].age;
for (let i = 0; i < num; ++i) {
let pingId = yield TelemetryPing.savePing("test-ping", {}, { overwrite: true });
let pingId = yield TelemetryPing.addPendingPing("test-ping", {}, { overwrite: true });
if (aPingInfos[type].age) {
// savePing writes to the file synchronously, so we're good to
// modify the lastModifedTime now.