-
Notifications
You must be signed in to change notification settings - Fork 54
/
test_extension_matches.js
482 lines (392 loc) · 18.1 KB
/
test_extension_matches.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
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
* vim:set ts=2 sw=2 sts=2 et:
* 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 {ExtensionSearchHandler} = ChromeUtils.import("resource://gre/modules/ExtensionSearchHandler.jsm");
let controller = Cc["@mozilla.org/autocomplete/controller;1"].getService(Ci.nsIAutoCompleteController);
add_task(async function test_correct_errors_are_thrown() {
let keyword = "foo";
let anotherKeyword = "bar";
let unregisteredKeyword = "baz";
// Register a keyword.
ExtensionSearchHandler.registerKeyword(keyword, { emit: () => {} });
// Try registering the keyword again.
Assert.throws(() => ExtensionSearchHandler.registerKeyword(keyword, { emit: () => {} }),
/The keyword provided is already registered/);
// Register a different keyword.
ExtensionSearchHandler.registerKeyword(anotherKeyword, { emit: () => {} });
// Try calling handleSearch for an unregistered keyword.
let searchData = {
keyword: unregisteredKeyword,
text: `${unregisteredKeyword} `,
};
Assert.throws(() => ExtensionSearchHandler.handleSearch(searchData, () => {}),
/The keyword provided is not registered/);
// Try calling handleSearch without a callback.
Assert.throws(() => ExtensionSearchHandler.handleSearch(searchData),
/The keyword provided is not registered/);
// Try getting the description for a keyword which isn't registered.
Assert.throws(() => ExtensionSearchHandler.getDescription(unregisteredKeyword),
/The keyword provided is not registered/);
// Try setting the default suggestion for a keyword which isn't registered.
Assert.throws(() => ExtensionSearchHandler.setDefaultSuggestion(unregisteredKeyword, "suggestion"),
/The keyword provided is not registered/);
// Try calling handleInputCancelled when there is no active input session.
Assert.throws(() => ExtensionSearchHandler.handleInputCancelled(),
/There is no active input session/);
// Try calling handleInputEntered when there is no active input session.
Assert.throws(() => ExtensionSearchHandler.handleInputEntered(anotherKeyword, `${anotherKeyword} test`, "tab"),
/There is no active input session/);
// Start a session by calling handleSearch with the registered keyword.
searchData = {
keyword,
text: `${keyword} test`,
};
ExtensionSearchHandler.handleSearch(searchData, () => {});
// Try providing suggestions for an unregistered keyword.
Assert.throws(() => ExtensionSearchHandler.addSuggestions(unregisteredKeyword, 0, []),
/The keyword provided is not registered/);
// Try providing suggestions for an inactive keyword.
Assert.throws(() => ExtensionSearchHandler.addSuggestions(anotherKeyword, 0, []),
/The keyword provided is not apart of an active input session/);
// Try calling handleSearch for an inactive keyword.
searchData = {
keyword: anotherKeyword,
text: `${anotherKeyword} `,
};
Assert.throws(() => ExtensionSearchHandler.handleSearch(searchData, () => {}),
/A different input session is already ongoing/);
// Try calling addSuggestions with an old callback ID.
Assert.throws(() => ExtensionSearchHandler.addSuggestions(keyword, 0, []),
/The callback is no longer active for the keyword provided/);
// Add suggestions with a valid callback ID.
ExtensionSearchHandler.addSuggestions(keyword, 1, []);
// Add suggestions again with a valid callback ID.
ExtensionSearchHandler.addSuggestions(keyword, 1, []);
// Try calling addSuggestions with a future callback ID.
Assert.throws(() => ExtensionSearchHandler.addSuggestions(keyword, 2, []),
/The callback is no longer active for the keyword provided/);
// End the input session by calling handleInputCancelled.
ExtensionSearchHandler.handleInputCancelled();
// Try calling handleInputCancelled after the session has ended.
Assert.throws(() => ExtensionSearchHandler.handleInputCancelled(),
/There is no active input sessio/);
// Try calling handleSearch that doesn't have a space after the keyword.
searchData = {
keyword: anotherKeyword,
text: `${anotherKeyword}`,
};
Assert.throws(() => ExtensionSearchHandler.handleSearch(searchData, () => {}),
/The text provided must start with/);
// Try calling handleSearch with text starting with the wrong keyword.
searchData = {
keyword: anotherKeyword,
text: `${keyword} test`,
};
Assert.throws(() => ExtensionSearchHandler.handleSearch(searchData, () => {}),
/The text provided must start with/);
// Start a new session by calling handleSearch with a different keyword
searchData = {
keyword: anotherKeyword,
text: `${anotherKeyword} test`,
};
ExtensionSearchHandler.handleSearch(searchData, () => {});
// Try adding suggestions again with the same callback ID now that the input session has ended.
Assert.throws(() => ExtensionSearchHandler.addSuggestions(keyword, 1, []),
/The keyword provided is not apart of an active input session/);
// Add suggestions with a valid callback ID.
ExtensionSearchHandler.addSuggestions(anotherKeyword, 2, []);
// Try adding suggestions with a valid callback ID but a different keyword.
Assert.throws(() => ExtensionSearchHandler.addSuggestions(keyword, 2, []),
/The keyword provided is not apart of an active input session/);
// Try adding suggestions with a valid callback ID but an unregistered keyword.
Assert.throws(() => ExtensionSearchHandler.addSuggestions(unregisteredKeyword, 2, []),
/The keyword provided is not registered/);
// Set the default suggestion.
ExtensionSearchHandler.setDefaultSuggestion(anotherKeyword, {description: "test result"});
// Try ending the session using handleInputEntered with a different keyword.
Assert.throws(() => ExtensionSearchHandler.handleInputEntered(keyword, `${keyword} test`, "tab"),
/A different input session is already ongoing/);
// Try calling handleInputEntered with invalid text.
Assert.throws(() => ExtensionSearchHandler.handleInputEntered(anotherKeyword, ` test`, "tab"),
/The text provided must start with/);
// Try calling handleInputEntered with an invalid disposition.
Assert.throws(() => ExtensionSearchHandler.handleInputEntered(anotherKeyword, `${anotherKeyword} test`, "invalid"),
/Invalid "where" argument/);
// End the session by calling handleInputEntered.
ExtensionSearchHandler.handleInputEntered(anotherKeyword, `${anotherKeyword} test`, "tab");
// Try calling handleInputEntered after the session has ended.
Assert.throws(() => ExtensionSearchHandler.handleInputEntered(anotherKeyword, `${anotherKeyword} test`, "tab"),
/There is no active input session/);
// Unregister the keyword.
ExtensionSearchHandler.unregisterKeyword(keyword);
// Try setting the default suggestion for the unregistered keyword.
Assert.throws(() => ExtensionSearchHandler.setDefaultSuggestion(keyword, {description: "test"}),
/The keyword provided is not registered/);
// Try handling a search with the unregistered keyword.
searchData = {
keyword,
text: `${keyword} test`,
};
Assert.throws(() => ExtensionSearchHandler.handleSearch(searchData, () => {}),
/The keyword provided is not registered/);
// Try unregistering the keyword again.
Assert.throws(() => ExtensionSearchHandler.unregisterKeyword(keyword),
/The keyword provided is not registered/);
// Unregister the other keyword.
ExtensionSearchHandler.unregisterKeyword(anotherKeyword);
// Try unregistering the word which was never registered.
Assert.throws(() => ExtensionSearchHandler.unregisterKeyword(unregisteredKeyword),
/The keyword provided is not registered/);
// Try setting the default suggestion for a word that was never registered.
Assert.throws(() => ExtensionSearchHandler.setDefaultSuggestion(unregisteredKeyword, {description: "test"}),
/The keyword provided is not registered/);
await cleanup();
});
add_task(async function test_extension_private_browsing() {
let events = [];
let mockExtension = {
emit: message => events.push(message),
privateBrowsingAllowed: false,
};
let keyword = "foo";
ExtensionSearchHandler.registerKeyword(keyword, mockExtension);
let searchData = {
keyword,
text: `${keyword} test`,
inPrivateWindow: true,
};
let result = await ExtensionSearchHandler.handleSearch(searchData);
Assert.equal(result, false, "unable to handle search for private window");
// Try calling handleInputEntered after the session has ended.
Assert.throws(() => ExtensionSearchHandler.handleInputEntered(keyword, `${keyword} test`, "tab"),
/There is no active input session/);
ExtensionSearchHandler.unregisterKeyword(keyword);
await cleanup();
});
add_task(async function test_correct_events_are_emitted() {
let events = [];
function checkEvents(expectedEvents) {
Assert.equal(events.length, expectedEvents.length, "The correct number of events fired");
expectedEvents.forEach((e, i) => Assert.equal(e, events[i], `Expected "${e}" event to fire`));
events = [];
}
let mockExtension = { emit: message => events.push(message) };
let keyword = "foo";
let anotherKeyword = "bar";
ExtensionSearchHandler.registerKeyword(keyword, mockExtension);
ExtensionSearchHandler.registerKeyword(anotherKeyword, mockExtension);
let searchData = {
keyword,
text: `${keyword} `,
};
ExtensionSearchHandler.handleSearch(searchData, () => {});
checkEvents([ExtensionSearchHandler.MSG_INPUT_STARTED]);
searchData.text = `${keyword} f`;
ExtensionSearchHandler.handleSearch(searchData, () => {});
checkEvents([ExtensionSearchHandler.MSG_INPUT_CHANGED]);
ExtensionSearchHandler.handleInputEntered(keyword, searchData.text, "tab");
checkEvents([ExtensionSearchHandler.MSG_INPUT_ENTERED]);
ExtensionSearchHandler.handleSearch(searchData, () => {});
checkEvents([
ExtensionSearchHandler.MSG_INPUT_STARTED,
ExtensionSearchHandler.MSG_INPUT_CHANGED,
]);
ExtensionSearchHandler.handleInputCancelled();
checkEvents([ExtensionSearchHandler.MSG_INPUT_CANCELLED]);
ExtensionSearchHandler.handleSearch({
keyword: anotherKeyword,
text: `${anotherKeyword} baz`,
}, () => {});
checkEvents([
ExtensionSearchHandler.MSG_INPUT_STARTED,
ExtensionSearchHandler.MSG_INPUT_CHANGED,
]);
ExtensionSearchHandler.handleInputEntered(anotherKeyword, `${anotherKeyword} baz`, "tab");
checkEvents([ExtensionSearchHandler.MSG_INPUT_ENTERED]);
ExtensionSearchHandler.unregisterKeyword(keyword);
});
add_task(async function test_removes_suggestion_if_its_content_is_typed_in() {
let keyword = "test";
let extensionName = "Foo Bar";
let mockExtension = {
name: extensionName,
emit(message, text, id) {
if (message === ExtensionSearchHandler.MSG_INPUT_CHANGED) {
ExtensionSearchHandler.addSuggestions(keyword, id, [
{content: "foo", description: "first suggestion"},
{content: "bar", description: "second suggestion"},
{content: "baz", description: "third suggestion"},
]);
// The API doesn't have a way to notify when addition is complete.
do_timeout(1000, () => {
controller.stopSearch();
});
}
},
};
ExtensionSearchHandler.registerKeyword(keyword, mockExtension);
await check_autocomplete({
search: `${keyword} unmatched`,
searchParam: "enable-actions",
matches: [
makeExtensionMatch({heuristic: true, keyword, description: extensionName, content: `${keyword} unmatched`}),
makeExtensionMatch({keyword, content: `${keyword} foo`, description: "first suggestion"}),
makeExtensionMatch({keyword, content: `${keyword} bar`, description: "second suggestion"}),
makeExtensionMatch({keyword, content: `${keyword} baz`, description: "third suggestion"}),
],
});
await check_autocomplete({
search: `${keyword} foo`,
searchParam: "enable-actions",
matches: [
makeExtensionMatch({heuristic: true, keyword, description: extensionName, content: `${keyword} foo`}),
makeExtensionMatch({keyword, content: `${keyword} bar`, description: "second suggestion"}),
makeExtensionMatch({keyword, content: `${keyword} baz`, description: "third suggestion"}),
],
});
await check_autocomplete({
search: `${keyword} bar`,
searchParam: "enable-actions",
matches: [
makeExtensionMatch({heuristic: true, keyword, description: extensionName, content: `${keyword} bar`}),
makeExtensionMatch({keyword, content: `${keyword} foo`, description: "first suggestion"}),
makeExtensionMatch({keyword, content: `${keyword} baz`, description: "third suggestion"}),
],
});
await check_autocomplete({
search: `${keyword} baz`,
searchParam: "enable-actions",
matches: [
makeExtensionMatch({heuristic: true, keyword, description: extensionName, content: `${keyword} baz`}),
makeExtensionMatch({keyword, content: `${keyword} foo`, description: "first suggestion"}),
makeExtensionMatch({keyword, content: `${keyword} bar`, description: "second suggestion"}),
],
});
ExtensionSearchHandler.unregisterKeyword(keyword);
await cleanup();
});
add_task(async function test_extension_results_should_come_first() {
let keyword = "test";
let extensionName = "Omnibox Example";
let uri = NetUtil.newURI(`http://a.com/b`);
await PlacesTestUtils.addVisits([
{ uri, title: `${keyword} -` },
]);
let mockExtension = {
name: extensionName,
emit(message, text, id) {
if (message === ExtensionSearchHandler.MSG_INPUT_CHANGED) {
ExtensionSearchHandler.addSuggestions(keyword, id, [
{content: "foo", description: "first suggestion"},
{content: "bar", description: "second suggestion"},
{content: "baz", description: "third suggestion"},
]);
}
// Do not stop the search here, of we'd not fetch results after the
// extension ones. The timeout in unifiedComplete will let us proceed
// after a few seconds.
},
};
ExtensionSearchHandler.registerKeyword(keyword, mockExtension);
// Start an input session before testing MSG_INPUT_CHANGED.
ExtensionSearchHandler.handleSearch({keyword, text: `${keyword} `}, () => {});
await check_autocomplete({
search: `${keyword} -`,
searchParam: "enable-actions",
matches: [
makeExtensionMatch({heuristic: true, keyword, description: extensionName, content: `${keyword} -`}),
makeExtensionMatch({keyword, content: `${keyword} foo`, description: "first suggestion"}),
makeExtensionMatch({keyword, content: `${keyword} bar`, description: "second suggestion"}),
makeExtensionMatch({keyword, content: `${keyword} baz`, description: "third suggestion"}),
{ uri, title: `${keyword} -` },
],
});
ExtensionSearchHandler.unregisterKeyword(keyword);
await cleanup();
});
add_task(async function test_setting_the_default_suggestion() {
let keyword = "test";
let extensionName = "Omnibox Example";
let mockExtension = {
name: extensionName,
emit(message, text, id) {
if (message === ExtensionSearchHandler.MSG_INPUT_CHANGED) {
ExtensionSearchHandler.addSuggestions(keyword, id, []);
// The API doesn't have a way to notify when addition is complete.
do_timeout(1000, () => {
controller.stopSearch();
});
}
},
};
ExtensionSearchHandler.registerKeyword(keyword, mockExtension);
ExtensionSearchHandler.setDefaultSuggestion(keyword, {
description: "hello world",
});
let searchString = `${keyword} search query`;
await check_autocomplete({
search: searchString,
searchParam: "enable-actions",
matches: [
makeExtensionMatch({heuristic: true, keyword, description: "hello world", content: searchString}),
],
});
ExtensionSearchHandler.setDefaultSuggestion(keyword, {
description: "foo bar",
});
await check_autocomplete({
search: searchString,
searchParam: "enable-actions",
matches: [
makeExtensionMatch({heuristic: true, keyword, description: "foo bar", content: searchString}),
],
});
ExtensionSearchHandler.unregisterKeyword(keyword);
await cleanup();
});
add_task(async function test_maximum_number_of_suggestions_is_enforced() {
let keyword = "test";
let extensionName = "Omnibox Example";
let mockExtension = {
name: extensionName,
emit(message, text, id) {
if (message === ExtensionSearchHandler.MSG_INPUT_CHANGED) {
ExtensionSearchHandler.addSuggestions(keyword, id, [
{content: "a", description: "first suggestion"},
{content: "b", description: "second suggestion"},
{content: "c", description: "third suggestion"},
{content: "d", description: "fourth suggestion"},
{content: "e", description: "fifth suggestion"},
{content: "f", description: "sixth suggestion"},
{content: "g", description: "seventh suggestion"},
{content: "h", description: "eigth suggestion"},
{content: "i", description: "ninth suggestion"},
{content: "j", description: "tenth suggestion"},
]);
// The API doesn't have a way to notify when addition is complete.
do_timeout(1000, () => {
controller.stopSearch();
});
}
},
};
ExtensionSearchHandler.registerKeyword(keyword, mockExtension);
// Start an input session before testing MSG_INPUT_CHANGED.
ExtensionSearchHandler.handleSearch({keyword, text: `${keyword} `}, () => {});
await check_autocomplete({
search: `${keyword} #`,
searchParam: "enable-actions",
matches: [
makeExtensionMatch({heuristic: true, keyword, description: extensionName, content: `${keyword} #`}),
makeExtensionMatch({keyword, content: `${keyword} a`, description: "first suggestion"}),
makeExtensionMatch({keyword, content: `${keyword} b`, description: "second suggestion"}),
makeExtensionMatch({keyword, content: `${keyword} c`, description: "third suggestion"}),
makeExtensionMatch({keyword, content: `${keyword} d`, description: "fourth suggestion"}),
makeExtensionMatch({keyword, content: `${keyword} e`, description: "fifth suggestion"}),
],
});
ExtensionSearchHandler.unregisterKeyword(keyword);
await cleanup();
});