summaryrefslogtreecommitdiffstats
path: root/toolkit/components/search/tests/xpcshell/test_settings_persist.js
blob: e3310a1fa2ce3bc9046837fbaf046a2aa80f84af (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Tests the removal of an engine is persisted in search settings.
 */

"use strict";

const CONFIG_UPDATED = [
  {
    webExtension: {
      id: "plainengine@search.mozilla.org",
      name: "Plain",
      search_url: "https://duckduckgo.com/",
      params: [
        {
          name: "q",
          value: "{searchTerms}",
        },
      ],
    },
    appliesTo: [{ included: { everywhere: true } }],
  },
];

const SEARCH_CONFIG_V2_UPDATED = [
  {
    recordType: "engine",
    identifier: "plainengine",
    base: {
      name: "Plain",
      urls: {
        search: {
          base: "https://duckduckgo.com/",
          searchTermParamName: "q",
        },
      },
    },
    variants: [
      {
        environment: { allRegionsAndLocales: true },
      },
    ],
  },
  {
    recordType: "defaultEngines",
    globalDefault: "plainengine",
    specificDefaults: [],
  },
  {
    recordType: "engineOrders",
    orders: [],
  },
];

async function startup() {
  let settingsFileWritten = promiseAfterSettings();
  let ss = new SearchService();
  await AddonTestUtils.promiseRestartManager();
  await ss.init(false);
  await settingsFileWritten;
  return ss;
}

async function updateConfig(config) {
  const settings = await RemoteSettings(SearchUtils.SETTINGS_KEY);
  settings.get.restore();

  config == "test-extensions"
    ? await SearchTestUtils.useTestEngines("test-extensions")
    : sinon.stub(settings, "get").returns(config);
}

async function visibleEngines(ss) {
  return (await ss.getVisibleEngines()).map(e => e._name);
}

add_setup(async function () {
  await SearchTestUtils.useTestEngines("test-extensions");
  registerCleanupFunction(AddonTestUtils.promiseShutdownManager);
  await AddonTestUtils.promiseStartupManager();
  // This is only needed as otherwise events will not be properly notified
  // due to https://searchfox.org/mozilla-central/rev/5f0a7ca8968ac5cef8846e1d970ef178b8b76dcc/toolkit/components/search/SearchSettings.sys.mjs#41-42
  let settingsFileWritten = promiseAfterSettings();
  await Services.search.init(false);
  Services.search.wrappedJSObject._removeObservers();
  await settingsFileWritten;
});

add_task(async function () {
  let ss = await startup();
  Assert.ok(
    (await visibleEngines(ss)).includes("Special"),
    "Should have both engines on first startup"
  );

  let settingsFileWritten = promiseAfterSettings();
  let engine = await ss.getEngineByName("Special");
  await ss.removeEngine(engine);
  await settingsFileWritten;

  Assert.ok(
    !(await visibleEngines(ss)).includes("Special"),
    "Special has been remove, only Plain should remain"
  );

  ss._removeObservers();
  await updateConfig(
    SearchUtils.newSearchConfigEnabled
      ? SEARCH_CONFIG_V2_UPDATED
      : CONFIG_UPDATED
  );
  ss = await startup();

  Assert.ok(
    !(await visibleEngines(ss)).includes("Special"),
    "Updated to new configuration that doesnt have Special"
  );

  ss._removeObservers();
  await updateConfig("test-extensions");

  ss = await startup();

  Assert.ok(
    !(await visibleEngines(ss)).includes("Special"),
    "Configuration now includes Special but we should remember its removal"
  );
});