summaryrefslogtreecommitdiffstats
path: root/toolkit/components/search/tests/xpcshell/test_engine_selector.js
blob: 7052a7de76f0382766401274ebe83a23722fa9f0 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* Any copyright is dedicated to the Public Domain.
 *    http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

ChromeUtils.defineESModuleGetters(this, {
  SearchEngineSelector: "resource://gre/modules/SearchEngineSelector.sys.mjs",
});

const TEST_CONFIG = [
  {
    engineName: "aol",
    orderHint: 500,
    webExtension: {
      locales: ["default"],
    },
    appliesTo: [
      {
        included: { everywhere: true },
      },
      {
        included: { regions: ["us"] },
        webExtension: {
          locales: ["baz-$USER_LOCALE"],
        },
        telemetryId: "foo-$USER_LOCALE",
      },
      {
        included: { regions: ["fr"] },
        webExtension: {
          locales: ["region-$USER_REGION"],
        },
        telemetryId: "bar-$USER_REGION",
      },
      {
        included: { regions: ["be"] },
        webExtension: {
          locales: ["$USER_LOCALE"],
        },
        telemetryId: "$USER_LOCALE",
      },
      {
        included: { regions: ["au"] },
        webExtension: {
          locales: ["$USER_REGION"],
        },
        telemetryId: "$USER_REGION",
      },
    ],
  },
  {
    engineName: "lycos",
    orderHint: 1000,
    default: "yes",
    appliesTo: [
      {
        included: { everywhere: true },
        excluded: { locales: { matches: ["zh-CN"] } },
      },
    ],
  },
  {
    engineName: "altavista",
    orderHint: 2000,
    defaultPrivate: "yes",
    appliesTo: [
      {
        included: { locales: { matches: ["en-US"] } },
      },
      {
        included: { regions: ["default"] },
      },
    ],
  },
  {
    engineName: "excite",
    default: "yes-if-no-other",
    appliesTo: [
      {
        included: { everywhere: true },
        excluded: { regions: ["us"] },
      },
      {
        included: { everywhere: true },
        experiment: "acohortid",
      },
    ],
  },
  {
    engineName: "askjeeves",
  },
];

const engineSelector = new SearchEngineSelector();

add_task(async function setup() {
  const settings = await RemoteSettings(SearchUtils.SETTINGS_KEY);
  sinon.stub(settings, "get").returns(TEST_CONFIG);
});

add_task(async function test_engine_selector() {
  let { engines, privateDefault } =
    await engineSelector.fetchEngineConfiguration({
      locale: "en-US",
      region: "us",
    });
  Assert.equal(
    privateDefault.engineName,
    "altavista",
    "Should set altavista as privateDefault"
  );
  let names = engines.map(obj => obj.engineName);
  Assert.deepEqual(names, ["lycos", "altavista", "aol"], "Correct order");
  Assert.equal(
    engines[2].webExtension.locale,
    "baz-en-US",
    "Subsequent matches in applies to can override default"
  );

  ({ engines, privateDefault } = await engineSelector.fetchEngineConfiguration({
    locale: "zh-CN",
    region: "kz",
  }));
  Assert.equal(engines.length, 2, "Correct engines are returns");
  Assert.equal(privateDefault, null, "There should be no privateDefault");
  names = engines.map(obj => obj.engineName);
  Assert.deepEqual(
    names,
    ["excite", "aol"],
    "The engines should be in the correct order"
  );

  ({ engines, privateDefault } = await engineSelector.fetchEngineConfiguration({
    locale: "en-US",
    region: "us",
    experiment: "acohortid",
  }));
  Assert.deepEqual(
    engines.map(obj => obj.engineName),
    ["lycos", "altavista", "aol", "excite"],
    "Engines are in the correct order and include the experiment engine"
  );

  ({ engines, privateDefault } = await engineSelector.fetchEngineConfiguration({
    locale: "en-US",
    region: "default",
    experiment: "acohortid",
  }));
  Assert.deepEqual(
    engines.map(obj => obj.engineName),
    ["lycos", "altavista", "aol", "excite"],
    "The engines should be in the correct order"
  );
  Assert.equal(
    privateDefault.engineName,
    "altavista",
    "Should set altavista as privateDefault"
  );
});

add_task(async function test_locale_region_replacement() {
  let { engines } = await engineSelector.fetchEngineConfiguration({
    locale: "en-US",
    region: "us",
  });
  let engine = engines.find(e => e.engineName == "aol");
  Assert.equal(
    engine.webExtension.locale,
    "baz-en-US",
    "The locale is correctly inserted into the locale field"
  );
  Assert.equal(
    engine.telemetryId,
    "foo-en-US",
    "The locale is correctly inserted into the telemetryId"
  );

  ({ engines } = await engineSelector.fetchEngineConfiguration({
    locale: "it",
    region: "us",
  }));
  engine = engines.find(e => e.engineName == "aol");

  Assert.equal(
    engines.find(e => e.engineName == "aol").webExtension.locale,
    "baz-it",
    "The locale is correctly inserted into the locale field"
  );
  Assert.equal(
    engine.telemetryId,
    "foo-it",
    "The locale is correctly inserted into the telemetryId"
  );

  ({ engines } = await engineSelector.fetchEngineConfiguration({
    locale: "en-CA",
    region: "fr",
  }));
  engine = engines.find(e => e.engineName == "aol");
  Assert.equal(
    engine.webExtension.locale,
    "region-fr",
    "The region is correctly inserted into the locale field"
  );
  Assert.equal(
    engine.telemetryId,
    "bar-fr",
    "The region is correctly inserted into the telemetryId"
  );

  ({ engines } = await engineSelector.fetchEngineConfiguration({
    locale: "fy-NL",
    region: "be",
  }));
  engine = engines.find(e => e.engineName == "aol");
  Assert.equal(
    engine.webExtension.locale,
    "fy-NL",
    "The locale is correctly inserted into the locale field"
  );
  Assert.equal(
    engine.telemetryId,
    "fy-NL",
    "The locale is correctly inserted into the telemetryId"
  );
  ({ engines } = await engineSelector.fetchEngineConfiguration({
    locale: "en-US",
    region: "au",
  }));
  engine = engines.find(e => e.engineName == "aol");
  Assert.equal(
    engine.webExtension.locale,
    "au",
    "The region is correctly inserted into the locale field"
  );
  Assert.equal(
    engine.telemetryId,
    "au",
    "The region is correctly inserted into the telemetryId"
  );
});