diff --git a/services/settings/RemoteSettingsWorker.js b/services/settings/RemoteSettingsWorker.js index 41f191297b86..d7ab0ca56e4b 100644 --- a/services/settings/RemoteSettingsWorker.js +++ b/services/settings/RemoteSettingsWorker.js @@ -65,9 +65,7 @@ const Agent = { */ async importJSONDump(bucket, collection) { const { data: records } = await loadJSONDump(bucket, collection); - if (records.length > 0) { - await importDumpIDB(bucket, collection, records); - } + await importDumpIDB(bucket, collection, records); return records.length; }, }; @@ -140,8 +138,8 @@ async function importDumpIDB(bucket, collection, records) { } }); - // Store the highest timestamp as the collection timestamp. - const timestamp = Math.max(...records.map(record => record.last_modified)); + // Store the highest timestamp as the collection timestamp (or zero if dump is empty). + const timestamp = records.length === 0 ? 0 : Math.max(...records.map(record => record.last_modified)); await executeIDB(db, IDB_TIMESTAMPS_STORE, store => store.put({ cid, value: timestamp })); } diff --git a/services/settings/test/unit/test_remote_settings.js b/services/settings/test/unit/test_remote_settings.js index 4a15ef6bd9c0..9a5d16d81cb9 100644 --- a/services/settings/test/unit/test_remote_settings.js +++ b/services/settings/test/unit/test_remote_settings.js @@ -8,6 +8,7 @@ const {AppConstants} = ChromeUtils.import("resource://gre/modules/AppConstants.j const IS_ANDROID = AppConstants.platform == "android"; const { RemoteSettings } = ChromeUtils.import("resource://services-settings/remote-settings.js"); +const { Utils } = ChromeUtils.import("resource://services-settings/Utils.jsm"); const { UptakeTelemetry } = ChromeUtils.import("resource://services-common/uptake-telemetry.js"); const BinaryInputStream = CC("@mozilla.org/binaryinputstream;1", @@ -133,6 +134,21 @@ add_task(async function test_get_loads_default_records_from_a_local_dump_when_da }); add_task(clear_state); +add_task(async function test_get_does_not_sync_if_empty_dump_is_provided() { + if (IS_ANDROID) { + // Skip test: we don't ship remote settings dumps on Android (see package-manifest). + return; + } + + const clientWithEmptyDump = RemoteSettings("example"); + Assert.ok(!(await Utils.hasLocalData(clientWithEmptyDump))); + + const data = await clientWithEmptyDump.get(); + + equal(data.length, 0); + Assert.ok(await Utils.hasLocalData(clientWithEmptyDump)); +}); + add_task(async function test_get_triggers_synchronization_when_database_is_empty() { // The "password-fields" collection has no local dump, and no local data. // Therefore a synchronization will happen.