-
Notifications
You must be signed in to change notification settings - Fork 54
/
test_search_engine_alias.js
275 lines (251 loc) · 7.99 KB
/
test_search_engine_alias.js
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
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
const SUGGESTIONS_ENGINE_NAME = "engine-suggestions.xml";
// Basic test that uses two engines, a GET engine and a POST engine, neither
// providing search suggestions.
add_task(async function basicGetAndPost() {
// Note that head_autocomplete.js has already added a MozSearch engine.
// Here we add another engine with a search alias.
await Services.search.addEngineWithDetails("AliasedGETMozSearch", "", "get", "",
"GET", "http://s.example.com/search");
await Services.search.addEngineWithDetails("AliasedPOSTMozSearch", "", "post", "",
"POST", "http://s.example.com/search");
await PlacesTestUtils.addVisits("http://s.example.com/search?q=firefox");
let historyMatch = {
value: "http://s.example.com/search?q=firefox",
comment: "test visit for http://s.example.com/search?q=firefox",
};
for (let alias of ["get", "post"]) {
await check_autocomplete({
search: alias,
searchParam: "enable-actions",
matches: [
makeSearchMatch(`${alias} `, {
engineName: `Aliased${alias.toUpperCase()}MozSearch`,
searchQuery: "",
alias,
heuristic: true,
}),
historyMatch,
],
});
await check_autocomplete({
search: `${alias} `,
searchParam: "enable-actions",
matches: [
makeSearchMatch(`${alias} `, {
engineName: `Aliased${alias.toUpperCase()}MozSearch`,
searchQuery: "",
alias,
heuristic: true,
}),
historyMatch,
],
});
await check_autocomplete({
search: `${alias} fire`,
searchParam: "enable-actions",
matches: [
makeSearchMatch(`${alias} fire`, {
engineName: `Aliased${alias.toUpperCase()}MozSearch`,
searchQuery: "fire",
alias,
heuristic: true,
}),
historyMatch,
],
});
await check_autocomplete({
search: `${alias} mozilla`,
searchParam: "enable-actions",
matches: [
makeSearchMatch(`${alias} mozilla`, {
engineName: `Aliased${alias.toUpperCase()}MozSearch`,
searchQuery: "mozilla",
alias,
heuristic: true,
}),
],
});
await check_autocomplete({
search: `${alias} MoZiLlA`,
searchParam: "enable-actions",
matches: [
makeSearchMatch(`${alias} MoZiLlA`, {
engineName: `Aliased${alias.toUpperCase()}MozSearch`,
searchQuery: "MoZiLlA",
alias,
heuristic: true,
}),
],
});
await check_autocomplete({
search: `${alias} mozzarella mozilla`,
searchParam: "enable-actions",
matches: [
makeSearchMatch(`${alias} mozzarella mozilla`, {
engineName: `Aliased${alias.toUpperCase()}MozSearch`,
searchQuery: "mozzarella mozilla",
alias,
heuristic: true,
}),
],
});
// When a restriction token is used before the alias, the alias should *not*
// be recognized. It should be treated as part of the search string. Try
// all the restriction tokens to test that. We should get a single "search
// with" heuristic result without an alias.
for (let restrictToken in UrlbarTokenizer.RESTRICT) {
let search = `${restrictToken} ${alias} query string`;
await check_autocomplete({
search,
searchParam: "enable-actions",
matches: [
makeSearchMatch(search, {
engineName: "MozSearch",
searchQuery: search,
heuristic: true,
}),
],
});
}
}
await cleanup();
});
// Uses an engine that provides search suggestions.
add_task(async function engineWithSuggestions() {
let engine = await addTestSuggestionsEngine();
// History matches should not appear with @ aliases, so this visit/match
// should not appear when searching with the @ alias below.
let historyTitle = "fire";
await PlacesTestUtils.addVisits({
uri: engine.searchForm,
title: historyTitle,
});
let historyMatch = {
value: "http://localhost:9000/search",
comment: historyTitle,
};
// Search in both a non-private and private context.
for (let private of [false, true]) {
let searchParam = "enable-actions";
if (private) {
searchParam += " private-window";
}
// Use a normal alias and then one with an "@". For the @ alias, the only
// matches should be the search suggestions -- no history matches.
for (let alias of ["moz", "@moz"]) {
engine.alias = alias;
Assert.equal(engine.alias, alias);
// Search for "alias"
let expectedMatches = [
makeSearchMatch(`${alias} `, {
engineName: SUGGESTIONS_ENGINE_NAME,
alias,
searchQuery: "",
heuristic: true,
}),
];
if (alias[0] != "@") {
expectedMatches.push(historyMatch);
}
await check_autocomplete({
search: alias,
searchParam,
matches: expectedMatches,
});
// Search for "alias " (trailing space)
expectedMatches = [
makeSearchMatch(`${alias} `, {
engineName: SUGGESTIONS_ENGINE_NAME,
alias,
searchQuery: "",
heuristic: true,
}),
];
if (alias[0] != "@") {
expectedMatches.push(historyMatch);
}
await check_autocomplete({
search: `${alias} `,
searchParam,
matches: expectedMatches,
});
// Search for "alias historyTitle" -- Include the history title so that
// the history result is eligible to be shown. Whether or not it's
// actually shown depends on the alias: If it's an @ alias, it shouldn't
// be shown.
expectedMatches = [
makeSearchMatch(`${alias} ${historyTitle}`, {
engineName: SUGGESTIONS_ENGINE_NAME,
alias,
searchQuery: historyTitle,
heuristic: true,
}),
];
// Suggestions should be shown in a non-private context but not in a
// private context.
if (!private) {
expectedMatches.push(
makeSearchMatch(`${alias} ${historyTitle} foo`, {
engineName: SUGGESTIONS_ENGINE_NAME,
alias,
searchQuery: historyTitle,
searchSuggestion: `${historyTitle} foo`,
}),
makeSearchMatch(`${alias} ${historyTitle} bar`, {
engineName: SUGGESTIONS_ENGINE_NAME,
alias,
searchQuery: historyTitle,
searchSuggestion: `${historyTitle} bar`,
})
);
}
if (alias[0] != "@") {
expectedMatches.push(historyMatch);
}
await check_autocomplete({
search: `${alias} ${historyTitle}`,
searchParam,
matches: expectedMatches,
});
}
}
engine.alias = "";
await cleanup();
});
// When the search is simply "@", the results should be a list of all the "@"
// alias engines.
add_task(async function tokenAliasEngines() {
let tokenEngines = [];
for (let engine of await Services.search.getEngines()) {
let aliases = [];
if (engine.alias) {
aliases.push(engine.alias);
}
aliases.push(...engine.wrappedJSObject._internalAliases);
let tokenAliases = aliases.filter(a => a.startsWith("@"));
if (tokenAliases.length) {
tokenEngines.push({ engine, tokenAliases });
}
}
if (!tokenEngines.length) {
Assert.ok(true, "No token alias engines, skipping task.");
return;
}
info("Got token alias engines: " +
tokenEngines.map(({ engine }) => engine.name));
await check_autocomplete({
search: "@",
searchParam: "enable-actions",
matches: tokenEngines.map(({ engine, tokenAliases }) => {
let alias = tokenAliases[0];
return makeSearchMatch(alias + " ", {
engineName: engine.name,
alias,
searchQuery: "",
});
}),
});
await cleanup();
});