forked from mirrors/gecko-dev
Bug 1885992 - Test that BackupService.takeMeasurements calls measure on each BackupResource. r=backup-reviewers,fchasen
This also fixes a small typo that prevented us from properly being able to override the built-in default BackupResources while testing. Differential Revision: https://phabricator.services.mozilla.com/D204966
This commit is contained in:
parent
582878ffbb
commit
45ead03a6d
2 changed files with 71 additions and 6 deletions
|
|
@ -2,7 +2,7 @@
|
|||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
import * as BackupResources from "resource:///modules/backup/BackupResources.sys.mjs";
|
||||
import * as DefaultBackupResources from "resource:///modules/backup/BackupResources.sys.mjs";
|
||||
|
||||
const lazy = {};
|
||||
|
||||
|
|
@ -48,7 +48,7 @@ export class BackupService {
|
|||
if (this.#instance) {
|
||||
return this.#instance;
|
||||
}
|
||||
this.#instance = new BackupService(BackupResources);
|
||||
this.#instance = new BackupService(DefaultBackupResources);
|
||||
this.#instance.takeMeasurements();
|
||||
|
||||
return this.#instance;
|
||||
|
|
@ -57,13 +57,13 @@ export class BackupService {
|
|||
/**
|
||||
* Create a BackupService instance.
|
||||
*
|
||||
* @param {object} [backupResources=BackupResources] - Object containing BackupResource classes to associate with this service.
|
||||
* @param {object} [backupResources=DefaultBackupResources] - Object containing BackupResource classes to associate with this service.
|
||||
*/
|
||||
constructor(backupResources = BackupResources) {
|
||||
constructor(backupResources = DefaultBackupResources) {
|
||||
lazy.logConsole.debug("Instantiated");
|
||||
|
||||
for (const resourceName in backupResources) {
|
||||
let resource = BackupResources[resourceName];
|
||||
let resource = backupResources[resourceName];
|
||||
this.#resources.set(resource.key, resource);
|
||||
}
|
||||
}
|
||||
|
|
@ -97,7 +97,14 @@ export class BackupService {
|
|||
|
||||
// Measure the size of each file we are going to backup.
|
||||
for (let resourceClass of this.#resources.values()) {
|
||||
try {
|
||||
await new resourceClass().measure(PathUtils.profileDir);
|
||||
} catch (e) {
|
||||
lazy.logConsole.error(
|
||||
`Failed to measure for resource: ${resourceClass.key}`,
|
||||
e
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,13 +6,23 @@ http://creativecommons.org/publicdomain/zero/1.0/ */
|
|||
const { BackupService } = ChromeUtils.importESModule(
|
||||
"resource:///modules/backup/BackupService.sys.mjs"
|
||||
);
|
||||
|
||||
const { PlacesBackupResource } = ChromeUtils.importESModule(
|
||||
"resource:///modules/backup/PlacesBackupResource.sys.mjs"
|
||||
);
|
||||
|
||||
const { BackupResource } = ChromeUtils.importESModule(
|
||||
"resource:///modules/backup/BackupResource.sys.mjs"
|
||||
);
|
||||
|
||||
const { TelemetryTestUtils } = ChromeUtils.importESModule(
|
||||
"resource://testing-common/TelemetryTestUtils.sys.mjs"
|
||||
);
|
||||
|
||||
const { sinon } = ChromeUtils.importESModule(
|
||||
"resource://testing-common/Sinon.sys.mjs"
|
||||
);
|
||||
|
||||
const EXPECTED_PLACES_DB_SIZE = 5240;
|
||||
const EXPECTED_FAVICONS_DB_SIZE = 5240;
|
||||
|
||||
|
|
@ -23,6 +33,54 @@ add_setup(() => {
|
|||
Services.telemetry.clearScalars();
|
||||
});
|
||||
|
||||
/**
|
||||
* Tests that calling `BackupService.takeMeasurements` will call the measure
|
||||
* method of all registered BackupResource classes.
|
||||
*/
|
||||
add_task(async function test_takeMeasurements() {
|
||||
/**
|
||||
* Some fake backup resource classes to test with.
|
||||
*/
|
||||
class FakeBackupResource1 extends BackupResource {
|
||||
static get key() {
|
||||
return "fake1";
|
||||
}
|
||||
measure() {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Another fake backup resource class to test with.
|
||||
*/
|
||||
class FakeBackupResource2 extends BackupResource {
|
||||
static get key() {
|
||||
return "fake2";
|
||||
}
|
||||
measure() {}
|
||||
}
|
||||
|
||||
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.
|
||||
*/
|
||||
|
|
|
|||
Loading…
Reference in a new issue