summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/xpcshell/head_amremotesettings.js
blob: 36741736fac43362fa6dff4400f42a9e0344acc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

ChromeUtils.defineESModuleGetters(this, {
  RemoteSettings: "resource://services-settings/remote-settings.sys.mjs",
});

async function setAndEmitFakeRemoteSettingsData(
  data,
  expectClientInitialized = true
) {
  const { AMRemoteSettings } = ChromeUtils.importESModule(
    "resource://gre/modules/AddonManager.sys.mjs"
  );
  let client;
  if (expectClientInitialized) {
    ok(AMRemoteSettings.client, "Got a remote settings client");
    ok(AMRemoteSettings.onSync, "Got a remote settings 'sync' event handler");
    client = AMRemoteSettings.client;
  } else {
    // No client is expected to exist, and so we create one to inject the expected data
    // into the RemoteSettings db.
    client = new RemoteSettings(AMRemoteSettings.RS_COLLECTION);
  }

  await client.db.clear();
  if (data.length) {
    await client.db.importChanges({}, Date.now(), data);
  }
  await client.emit("sync", { data: {} });
}