summaryrefslogtreecommitdiffstats
path: root/toolkit/components/search/tests/xpcshell/searchconfigs/test_selector_db_out_of_date.js
blob: 9bd032c3b8ed6f43f2e64b7b4e4b69baeee4cb4f (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

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

do_get_profile();

add_task(async function test_selector_db_out_of_date() {
  let searchConfig = RemoteSettings(SearchUtils.SETTINGS_KEY);

  // Do an initial get to pre-seed the database.
  await searchConfig.get();

  // Now clear the database and re-fill it.
  let db = searchConfig.db;
  await db.clear();
  let databaseEntries = await db.list();
  Assert.equal(databaseEntries.length, 0, "Should have cleared the database.");

  // Add a dummy record with an out-of-date last modified.
  if (SearchUtils.newSearchConfigEnabled) {
    await RemoteSettingsWorker._execute("_test_only_import", [
      "main",
      SearchUtils.SETTINGS_KEY,
      [
        {
          id: "b70edfdd-1c3f-4b7b-ab55-38cb048636c0",
          identifier: "outofdate",
          recordType: "engine",
          base: {},
          variants: [
            {
              environment: {
                allRegionsAndLocales: true,
              },
            },
          ],
          last_modified: 1606227264000,
        },
      ],
      1606227264000,
    ]);
  } else {
    await RemoteSettingsWorker._execute("_test_only_import", [
      "main",
      SearchUtils.SETTINGS_KEY,
      [
        {
          id: "b70edfdd-1c3f-4b7b-ab55-38cb048636c0",
          default: "yes",
          webExtension: { id: "outofdate@search.mozilla.org" },
          appliesTo: [{ included: { everywhere: true } }],
          last_modified: 1606227264000,
        },
      ],
      1606227264000,
    ]);
  }

  // Now load the configuration and check we get what we expect.
  let engineSelector = SearchUtils.newSearchConfigEnabled
    ? new SearchEngineSelector()
    : new SearchEngineSelectorOld();

  let result = await engineSelector.fetchEngineConfiguration({
    // Use the fallback default locale/regions to get a simple list.
    locale: "default",
    region: "default",
  });

  if (SearchUtils.newSearchConfigEnabled) {
    Assert.deepEqual(
      result.engines.map(e => e.identifier),
      ["google", "ddg", "wikipedia"],
      "Should have returned the correct data."
    );
  } else {
    Assert.deepEqual(
      result.engines.map(e => e.webExtension.id),
      [
        "google@search.mozilla.org",
        "wikipedia@search.mozilla.org",
        "ddg@search.mozilla.org",
      ],
      "Should have returned the correct data."
    );
  }
});