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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const { UrlbarSearchUtils } = ChromeUtils.importESModule(
"resource:///modules/UrlbarSearchUtils.sys.mjs"
);
let baconEngineExtension;
add_task(async function() {
await UrlbarSearchUtils.init();
// Tell the search service we are running in the US. This also has the
// desired side-effect of preventing our geoip lookup.
Services.prefs.setCharPref("browser.search.region", "US");
Services.search.restoreDefaultEngines();
Services.search.resetToAppDefaultEngine();
});
add_task(async function search_engine_match() {
let engine = await Services.search.getDefault();
let domain = engine.getResultDomain();
let token = domain.substr(0, 1);
let matchedEngine = (
await UrlbarSearchUtils.enginesForDomainPrefix(token)
)[0];
Assert.equal(matchedEngine, engine);
});
add_task(async function no_match() {
Assert.equal(
0,
(await UrlbarSearchUtils.enginesForDomainPrefix("test")).length
);
});
add_task(async function hide_search_engine_nomatch() {
let engine = await Services.search.getDefault();
let domain = engine.getResultDomain();
let token = domain.substr(0, 1);
let promiseTopic = promiseSearchTopic("engine-changed");
await Promise.all([Services.search.removeEngine(engine), promiseTopic]);
Assert.ok(engine.hidden);
let matchedEngines = await UrlbarSearchUtils.enginesForDomainPrefix(token);
Assert.ok(
!matchedEngines.length || matchedEngines[0].getResultDomain() != domain
);
engine.hidden = false;
await TestUtils.waitForCondition(
async () => (await UrlbarSearchUtils.enginesForDomainPrefix(token)).length
);
let matchedEngine2 = (
await UrlbarSearchUtils.enginesForDomainPrefix(token)
)[0];
Assert.ok(matchedEngine2);
await Services.search.setDefault(
engine,
Ci.nsISearchService.CHANGE_REASON_UNKNOWN
);
});
add_task(async function onlyEnabled_option_nomatch() {
let engine = await Services.search.getDefault();
let domain = engine.getResultDomain();
let token = domain.substr(0, 1);
Services.prefs.setCharPref("browser.search.hiddenOneOffs", engine.name);
let matchedEngines = await UrlbarSearchUtils.enginesForDomainPrefix(token, {
onlyEnabled: true,
});
Assert.notEqual(matchedEngines[0].getResultDomain(), domain);
Services.prefs.clearUserPref("browser.search.hiddenOneOffs");
matchedEngines = await UrlbarSearchUtils.enginesForDomainPrefix(token, {
onlyEnabled: true,
});
Assert.equal(matchedEngines[0].getResultDomain(), domain);
});
add_task(async function add_search_engine_match() {
Assert.equal(
0,
(await UrlbarSearchUtils.enginesForDomainPrefix("bacon")).length
);
baconEngineExtension = await SearchTestUtils.installSearchExtension(
{
name: "bacon",
keyword: "pork",
search_url: "https://www.bacon.moz/",
},
{ skipUnload: true }
);
let matchedEngine = (
await UrlbarSearchUtils.enginesForDomainPrefix("bacon")
)[0];
Assert.ok(matchedEngine);
Assert.equal(matchedEngine.searchForm, "https://www.bacon.moz");
Assert.equal(matchedEngine.name, "bacon");
Assert.equal(matchedEngine.iconURI, null);
info("also type part of the public suffix");
matchedEngine = (
await UrlbarSearchUtils.enginesForDomainPrefix("bacon.m")
)[0];
Assert.ok(matchedEngine);
Assert.equal(matchedEngine.searchForm, "https://www.bacon.moz");
Assert.equal(matchedEngine.name, "bacon");
Assert.equal(matchedEngine.iconURI, null);
});
add_task(async function match_multiple_search_engines() {
Assert.equal(
0,
(await UrlbarSearchUtils.enginesForDomainPrefix("baseball")).length
);
await SearchTestUtils.installSearchExtension({
name: "baseball",
search_url: "https://www.baseball.moz/",
});
let matchedEngines = await UrlbarSearchUtils.enginesForDomainPrefix("ba");
Assert.equal(
matchedEngines.length,
2,
"enginesForDomainPrefix returned two engines."
);
Assert.equal(matchedEngines[0].searchForm, "https://www.bacon.moz");
Assert.equal(matchedEngines[0].name, "bacon");
Assert.equal(matchedEngines[1].searchForm, "https://www.baseball.moz");
Assert.equal(matchedEngines[1].name, "baseball");
});
add_task(async function test_aliased_search_engine_match() {
Assert.equal(null, await UrlbarSearchUtils.engineForAlias("sober"));
// Lower case
let matchedEngine = await UrlbarSearchUtils.engineForAlias("pork");
Assert.ok(matchedEngine);
Assert.equal(matchedEngine.name, "bacon");
Assert.ok(matchedEngine.aliases.includes("pork"));
Assert.equal(matchedEngine.iconURI, null);
// Upper case
matchedEngine = await UrlbarSearchUtils.engineForAlias("PORK");
Assert.ok(matchedEngine);
Assert.equal(matchedEngine.name, "bacon");
Assert.ok(matchedEngine.aliases.includes("pork"));
Assert.equal(matchedEngine.iconURI, null);
// Cap case
matchedEngine = await UrlbarSearchUtils.engineForAlias("Pork");
Assert.ok(matchedEngine);
Assert.equal(matchedEngine.name, "bacon");
Assert.ok(matchedEngine.aliases.includes("pork"));
Assert.equal(matchedEngine.iconURI, null);
});
add_task(async function test_aliased_search_engine_match_upper_case_alias() {
Assert.equal(
0,
(await UrlbarSearchUtils.enginesForDomainPrefix("patch")).length
);
await SearchTestUtils.installSearchExtension({
name: "patch",
keyword: "PR",
search_url: "https://www.patch.moz/",
});
// lower case
let matchedEngine = await UrlbarSearchUtils.engineForAlias("pr");
Assert.ok(matchedEngine);
Assert.equal(matchedEngine.name, "patch");
Assert.ok(matchedEngine.aliases.includes("PR"));
Assert.equal(matchedEngine.iconURI, null);
// Upper case
matchedEngine = await UrlbarSearchUtils.engineForAlias("PR");
Assert.ok(matchedEngine);
Assert.equal(matchedEngine.name, "patch");
Assert.ok(matchedEngine.aliases.includes("PR"));
Assert.equal(matchedEngine.iconURI, null);
// Cap case
matchedEngine = await UrlbarSearchUtils.engineForAlias("Pr");
Assert.ok(matchedEngine);
Assert.equal(matchedEngine.name, "patch");
Assert.ok(matchedEngine.aliases.includes("PR"));
Assert.equal(matchedEngine.iconURI, null);
});
add_task(async function remove_search_engine_nomatch() {
let promiseTopic = promiseSearchTopic("engine-removed");
await Promise.all([baconEngineExtension.unload(), promiseTopic]);
Assert.equal(
0,
(await UrlbarSearchUtils.enginesForDomainPrefix("bacon")).length
);
});
add_task(async function test_builtin_aliased_search_engine_match() {
let engine = await UrlbarSearchUtils.engineForAlias("@google");
Assert.ok(engine);
Assert.equal(engine.name, "Google");
let promiseTopic = promiseSearchTopic("engine-changed");
await Promise.all([Services.search.removeEngine(engine), promiseTopic]);
let matchedEngine = await UrlbarSearchUtils.engineForAlias("@google");
Assert.ok(!matchedEngine);
engine.hidden = false;
await TestUtils.waitForCondition(() =>
UrlbarSearchUtils.engineForAlias("@google")
);
engine = await UrlbarSearchUtils.engineForAlias("@google");
Assert.ok(engine);
});
add_task(async function test_serps_are_equivalent() {
info("Subset URL has extraneous parameters.");
let url1 = "https://example.com/search?q=test&type=images";
let url2 = "https://example.com/search?q=test";
Assert.ok(!UrlbarSearchUtils.serpsAreEquivalent(url1, url2));
info("Superset URL has extraneous parameters.");
Assert.ok(UrlbarSearchUtils.serpsAreEquivalent(url2, url1));
info("Same keys, different values.");
url1 = "https://example.com/search?q=test&type=images";
url2 = "https://example.com/search?q=test123&type=maps";
Assert.ok(!UrlbarSearchUtils.serpsAreEquivalent(url1, url2));
Assert.ok(!UrlbarSearchUtils.serpsAreEquivalent(url2, url1));
info("Subset matching isn't strict (URL is subset of itself).");
Assert.ok(UrlbarSearchUtils.serpsAreEquivalent(url1, url1));
info("Origin and pathname are ignored.");
url1 = "https://example.com/search?q=test";
url2 = "https://example-1.com/maps?q=test";
Assert.ok(UrlbarSearchUtils.serpsAreEquivalent(url1, url2));
Assert.ok(UrlbarSearchUtils.serpsAreEquivalent(url2, url1));
info("Params can be optionally ignored");
url1 = "https://example.com/search?q=test&abc=123&foo=bar";
url2 = "https://example.com/search?q=test";
Assert.ok(!UrlbarSearchUtils.serpsAreEquivalent(url1, url2));
Assert.ok(UrlbarSearchUtils.serpsAreEquivalent(url1, url2, ["abc", "foo"]));
});
add_task(async function test_get_root_domain_from_engine() {
let extension = await SearchTestUtils.installSearchExtension(
{
name: "TestEngine2",
search_url: "https://example.com/",
},
{ skipUnload: true }
);
let engine = Services.search.getEngineByName("TestEngine2");
Assert.equal(UrlbarSearchUtils.getRootDomainFromEngine(engine), "example");
await extension.unload();
extension = await SearchTestUtils.installSearchExtension(
{
name: "TestEngine",
search_url: "https://www.subdomain.othersubdomain.example.com",
},
{ skipUnload: true }
);
engine = Services.search.getEngineByName("TestEngine");
Assert.equal(UrlbarSearchUtils.getRootDomainFromEngine(engine), "example");
await extension.unload();
// We let engines with URL ending in .test through even though its not a valid
// TLD.
extension = await SearchTestUtils.installSearchExtension(
{
name: "TestMalformed",
search_url: "https://mochi.test/",
search_url_get_params: "search={searchTerms}",
},
{ skipUnload: true }
);
engine = Services.search.getEngineByName("TestMalformed");
Assert.equal(UrlbarSearchUtils.getRootDomainFromEngine(engine), "mochi");
await extension.unload();
// We return the domain for engines with a malformed URL.
extension = await SearchTestUtils.installSearchExtension(
{
name: "TestMalformed",
search_url: "https://subdomain.foobar/",
search_url_get_params: "search={searchTerms}",
},
{ skipUnload: true }
);
engine = Services.search.getEngineByName("TestMalformed");
Assert.equal(
UrlbarSearchUtils.getRootDomainFromEngine(engine),
"subdomain.foobar"
);
await extension.unload();
});
// Tests getSearchTermIfDefaultSerpUri() by using a variety of
// input strings and nsIURI's.
// Should not throw an error if the consumer passes an input
// that when accessed, could cause an error.
add_task(async function get_search_term_if_default_serp_uri() {
let testCases = [
{
url: null,
skipUriTest: true,
},
{
url: "",
skipUriTest: true,
},
{
url: "about:blank",
},
{
url: "about:home",
},
{
url: "about:newtab",
},
{
url: "not://a/supported/protocol",
},
{
url: "view-source:http://www.example.com/",
},
{
// Not a default engine.
url: "http://mochi.test:8888/?q=chocolate&pc=sample_code",
},
{
// Not the correct protocol.
url: "http://example.com/?q=chocolate&pc=sample_code",
},
{
// Not the same query param values.
url: "https://example.com/?q=chocolate&pc=sample_code2",
},
{
// Not the same query param values.
url: "https://example.com/?q=chocolate&pc=sample_code&pc2=sample_code_2",
},
{
url: "https://example.com/?q=chocolate&pc=sample_code",
expectedString: "chocolate",
},
{
url: "https://example.com/?q=chocolate+cakes&pc=sample_code",
expectedString: "chocolate cakes",
},
];
// Create a specific engine so that the tests are matched
// exactly against the query params used.
let extension = await SearchTestUtils.installSearchExtension(
{
name: "TestEngine",
search_url: "https://example.com/",
search_url_get_params: "?q={searchTerms}&pc=sample_code",
},
{ skipUnload: true }
);
let engine = Services.search.getEngineByName("TestEngine");
let originalDefaultEngine = Services.search.defaultEngine;
Services.search.defaultEngine = engine;
for (let testCase of testCases) {
let expectedString = testCase.expectedString ?? "";
Assert.equal(
UrlbarSearchUtils.getSearchTermIfDefaultSerpUri(testCase.url),
expectedString,
`Should return ${
expectedString == "" ? "an empty string" : "a matching search string"
}`
);
// Convert the string into a nsIURI and then
// try the test case with it.
if (!testCase.skipUriTest) {
Assert.equal(
UrlbarSearchUtils.getSearchTermIfDefaultSerpUri(
Services.io.newURI(testCase.url)
),
expectedString,
`Should return ${
expectedString == "" ? "an empty string" : "a matching search string"
}`
);
}
}
Services.search.defaultEngine = originalDefaultEngine;
await extension.unload();
});
add_task(async function matchAllDomainLevels() {
let baseHostname = "matchalldomainlevels";
Assert.equal(
(await UrlbarSearchUtils.enginesForDomainPrefix(baseHostname)).length,
0,
`Sanity check: No engines initially match ${baseHostname}`
);
// Install engines with the following domains. When we match engines below,
// perfectly matching domains should come before partially matching domains.
let baseDomain = `${baseHostname}.com`;
let perfectDomains = [baseDomain, `www.${baseDomain}`];
let partialDomains = [`foo.${baseDomain}`, `foo.bar.${baseDomain}`];
// Install engines with partially matching domains first so that the test
// isn't incidentally passing because engines are installed in the order it
// ultimately expects them in. Wait for each engine to finish installing
// before starting the next one to avoid intermittent out-of-order failures.
let extensions = [];
for (let list of [partialDomains, perfectDomains]) {
for (let domain of list) {
let ext = await SearchTestUtils.installSearchExtension(
{
name: domain,
search_url: `https://${domain}/`,
},
{ skipUnload: true }
);
extensions.push(ext);
}
}
// Perfect matches come before partial matches.
let expectedDomains = [...perfectDomains, ...partialDomains];
// Do searches for the following strings. Each should match all the engines
// installed above.
let searchStrings = [baseHostname, baseHostname + "."];
for (let searchString of searchStrings) {
info(`Searching for "${searchString}"`);
let engines = await UrlbarSearchUtils.enginesForDomainPrefix(searchString, {
matchAllDomainLevels: true,
});
let engineData = engines.map(e => ({
name: e.name,
searchForm: e.searchForm,
}));
info("Matching engines: " + JSON.stringify(engineData));
Assert.equal(
engines.length,
expectedDomains.length,
"Expected number of matching engines"
);
Assert.deepEqual(
engineData.map(d => d.name),
expectedDomains,
"Expected matching engine names/domains in the expected order"
);
}
await Promise.all(extensions.map(e => e.unload()));
});
function promiseSearchTopic(expectedVerb) {
return new Promise(resolve => {
Services.obs.addObserver(function observe(subject, topic, verb) {
info("browser-search-engine-modified: " + verb);
if (verb == expectedVerb) {
Services.obs.removeObserver(observe, "browser-search-engine-modified");
resolve();
}
}, "browser-search-engine-modified");
});
}
|