Source code

Revision control

Copy as Markdown

Other Tools

Test Info: Warnings

/* Any copyright is dedicated to the Public Domain.
"use strict";
add_setup(() => {
// FOG needs to be initialized in order for data to flow.
Services.fog.initializeFOG();
});
/**
* Tests that calling `BackupService.takeMeasurements` will call the measure
* method of all registered BackupResource classes.
*/
add_task(async function test_takeMeasurements() {
let sandbox = sinon.createSandbox();
sandbox.stub(FakeBackupResource1.prototype, "measure").resolves();
sandbox
.stub(FakeBackupResource2.prototype, "measure")
.rejects(new Error("Some failure to measure"));
let bs = new BackupService({ FakeBackupResource1, FakeBackupResource2 });
await bs.takeMeasurements();
for (let backupResourceClass of [FakeBackupResource1, FakeBackupResource2]) {
Assert.ok(
backupResourceClass.prototype.measure.calledOnce,
"Measure was called"
);
Assert.ok(
backupResourceClass.prototype.measure.calledWith(PathUtils.profileDir),
"Measure was called with the profile directory argument"
);
}
sandbox.restore();
});
/**
* Tests that we can measure the disk space available in the profile directory.
*/
add_task(async function test_profDDiskSpace() {
Services.fog.testResetFOG();
let bs = new BackupService();
await bs.takeMeasurements();
let measurement = Glean.browserBackup.profDDiskSpace.testGetValue();
Assert.greater(
measurement,
0,
"Should have collected a measurement for the profile directory storage " +
"device"
);
});
/**
* Tests that we record a scalar if the BackupService is configured to
* initialize on launch.
*/
add_task(async function test_BackupService_enabled_state() {
Services.fog.testResetFOG();
let bs = new BackupService();
await bs.takeMeasurements();
Assert.ok(
Glean.browserBackup.enabled.testGetValue(),
"Should have set the enabled scalar."
);
});
/**
* Tests that we record a scalar if the BackupService is configured to
* initialize on launch.
*/
add_task(async function test_BackupService_scheduler_enabled_state() {
Services.fog.testResetFOG();
const SCHEDULED_BACKUPS_ENABLED_PREF_NAME =
"browser.backup.scheduled.enabled";
Services.prefs.setBoolPref(SCHEDULED_BACKUPS_ENABLED_PREF_NAME, false);
let bs = new BackupService();
await bs.takeMeasurements();
Assert.ok(
!Glean.browserBackup.schedulerEnabled.testGetValue(),
"Scalar for scheduled backups should be false"
);
Services.fog.testResetFOG();
Services.prefs.setBoolPref(SCHEDULED_BACKUPS_ENABLED_PREF_NAME, true);
await bs.takeMeasurements();
Assert.ok(
Glean.browserBackup.schedulerEnabled.testGetValue(),
"Scalar for scheduled backups should be true"
);
// Now reset the scheduling state to the default to not interfere with
// other tests.
Services.prefs.clearUserPref(SCHEDULED_BACKUPS_ENABLED_PREF_NAME);
});
/**
* Tests that we record a scalar if the BackupService is configured to
* encrypt backups.
*/
add_task(async function test_BackupService_pswd_encrypted_state() {
Services.fog.testResetFOG();
let bs = new BackupService();
await bs.takeMeasurements();
Assert.ok(
!Glean.browserBackup.pswdEncrypted.testGetValue(),
"Scalar for encrypted backups should be false"
);
Services.fog.testResetFOG();
const tempDir = await IOUtils.createUniqueDirectory(
PathUtils.tempDir,
"BackupService-takeMeasurements-test"
);
await bs.enableEncryption("some-fake-password", tempDir);
await bs.takeMeasurements();
Assert.ok(
Glean.browserBackup.pswdEncrypted.testGetValue(),
"Scalar for encrypted backups should be true"
);
await maybeRemovePath(tempDir);
});
/**
* Tests that we record a scalar that tells us if backups are configured to
* be written to the default location, or somewhere else entirely.
*/
add_task(async function test_BackupService_location_on_device() {
Services.fog.testResetFOG();
const DEFAULT_LOCATION = 1;
const NON_DEFAULT_LOCATION = 2;
// Don't rely on the test machine actually having a OneDrive or a Documents
// folder - stand one up ourselves so the default location is well-defined.
let fakeDocsDirPath = await IOUtils.createUniqueDirectory(
PathUtils.tempDir,
"BackupService-fake-docs-dir"
);
let fakeDocsDir = await IOUtils.getFile(fakeDocsDirPath);
let sandbox = sinon.createSandbox();
sandbox.stub(BackupService, "oneDriveFolderPath").get(() => null);
sandbox.stub(BackupService, "docsDirFolderPath").get(() => fakeDocsDir);
let bs = new BackupService();
await bs.setParentDirPath(PathUtils.tempDir);
await bs.takeMeasurements();
Assert.equal(
Glean.browserBackup.locationOnDevice.testGetValue(),
NON_DEFAULT_LOCATION,
"Scalar for location on device should indicate the non-default " +
"location"
);
Services.fog.testResetFOG();
await bs.setParentDirPath(BackupService.DEFAULT_PARENT_DIR_PATH);
await bs.takeMeasurements();
Assert.equal(
Glean.browserBackup.locationOnDevice.testGetValue(),
DEFAULT_LOCATION,
"Scalar for location on device should indicate the default location"
);
sandbox.restore();
await maybeRemovePath(fakeDocsDirPath);
});
/**
* Tests that measurements are still taken, and that no scalar is recorded for
* the location on device, when neither a OneDrive folder nor a Documents
* folder can be resolved.
*/
add_task(async function test_BackupService_no_default_location() {
Services.fog.testResetFOG();
let sandbox = sinon.createSandbox();
sandbox.stub(BackupService, "oneDriveFolderPath").get(() => null);
sandbox.stub(BackupService, "docsDirFolderPath").get(() => null);
Assert.equal(
BackupService.DEFAULT_PARENT_DIR_PATH,
"",
"There should be no default parent directory path"
);
let bs = new BackupService();
await bs.takeMeasurements();
Assert.equal(
Glean.browserBackup.locationOnDevice.testGetValue(),
null,
"Scalar for location on device should not have been recorded"
);
Assert.greater(
Glean.browserBackup.profDDiskSpace.testGetValue(),
0,
"Measurements taken after the location on device should still have run"
);
sandbox.restore();
});