summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/unifiedcomplete/test_extension_matches.js
blob: 76af20558d23f38587451696aedc3950066cc1f5 (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
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
/* -*- 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/. */

Cu.import("resource://gre/modules/ExtensionSearchHandler.jsm");

let controller = Cc["@mozilla.org/autocomplete/controller;1"].getService(Ci.nsIAutoCompleteController);

add_task(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: () => {} }));

  // Register a different keyword.
  ExtensionSearchHandler.registerKeyword(anotherKeyword, { emit: () => {} });

  // Try calling handleSearch for an unregistered keyword.
  Assert.throws(() => ExtensionSearchHandler.handleSearch(unregisteredKeyword, `${unregisteredKeyword} `, () => {}));

  // Try calling handleSearch without a callback.
  Assert.throws(() => ExtensionSearchHandler.handleSearch(unregisteredKeyword, `${unregisteredKeyword} `));

  // Try getting the description for a keyword which isn't registered.
  Assert.throws(() => ExtensionSearchHandler.getDescription(unregisteredKeyword));

  // Try getting the extension name for a keyword which isn't registered.
  Assert.throws(() => ExtensionSearchHandler.getExtensionName(unregisteredKeyword));

  // Try setting the default suggestion for a keyword which isn't registered.
  Assert.throws(() => ExtensionSearchHandler.setDefaultSuggestion(unregisteredKeyword, "suggestion"));

  // Try calling handleInputCancelled when there is no active input session.
  Assert.throws(() => ExtensionSearchHandler.handleInputCancelled());

  // Try calling handleInputEntered when there is no active input session.
  Assert.throws(() => ExtensionSearchHandler.handleInputEntered(anotherKeyword, `${anotherKeyword} test`, "tab"));

  // Start a session by calling handleSearch with the registered keyword.
  ExtensionSearchHandler.handleSearch(keyword, `${keyword} test`, () => {});

  // Try providing suggestions for an unregistered keyword.
  Assert.throws(() => ExtensionSearchHandler.addSuggestions(unregisteredKeyword, 0, []));

  // Try providing suggestions for an inactive keyword.
  Assert.throws(() => ExtensionSearchHandler.addSuggestions(anotherKeyword, 0, []));

  // Try calling handleSearch for an inactive keyword.
  Assert.throws(() => ExtensionSearchHandler.handleSearch(anotherKeyword, `${anotherKeyword} `, () => {}));

  // Try calling addSuggestions with an old callback ID.
  Assert.throws(() => ExtensionSearchHandler.addSuggestions(keyword, 0, []));

  // 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, []));

  // End the input session by calling handleInputCancelled.
  ExtensionSearchHandler.handleInputCancelled();

  // Try calling handleInputCancelled after the session has ended.
  Assert.throws(() => ExtensionSearchHandler.handleInputCancelled());

  // Try calling handleSearch that doesn't have a space after the keyword.
  Assert.throws(() => ExtensionSearchHandler.handleSearch(anotherKeyword, `${anotherKeyword}`, () => {}));

  // Try calling handleSearch with text starting with the wrong keyword.
  Assert.throws(() => ExtensionSearchHandler.handleSearch(anotherKeyword, `${keyword} test`, () => {}));

  // Start a new session by calling handleSearch with a different keyword
  ExtensionSearchHandler.handleSearch(anotherKeyword, `${anotherKeyword} test`, () => {});

  // Try adding suggestions again with the same callback ID now that the input session has ended.
  Assert.throws(() => ExtensionSearchHandler.addSuggestions(keyword, 1, []));

  // 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, []));

  // Try adding suggestions with a valid callback ID but an unregistered keyword.
  Assert.throws(() => ExtensionSearchHandler.addSuggestions(unregisteredKeyword, 2, []));

  // 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"));

  // Try calling handleInputEntered with invalid text.
  Assert.throws(() => ExtensionSearchHandler.handleInputEntered(anotherKeyword, ` test`, "tab"));

  // Try calling handleInputEntered with an invalid disposition.
  Assert.throws(() => ExtensionSearchHandler.handleInputEntered(anotherKeyword, `${anotherKeyword} test`, "invalid"));

  // 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"));

  // Unregister the keyword.
  ExtensionSearchHandler.unregisterKeyword(keyword);

  // Try setting the default suggestion for the unregistered keyword.
  Assert.throws(() => ExtensionSearchHandler.setDefaultSuggestion(keyword, {description: "test"}));

  // Try handling a search with the unregistered keyword.
  Assert.throws(() => ExtensionSearchHandler.handleSearch(keyword, `${keyword} test`, () => {}));

  // Try unregistering the keyword again.
  Assert.throws(() => ExtensionSearchHandler.unregisterKeyword(keyword));

  // Unregister the other keyword.
  ExtensionSearchHandler.unregisterKeyword(anotherKeyword);

  // Try unregistering the word which was never registered.
  Assert.throws(() => ExtensionSearchHandler.unregisterKeyword(unregisteredKeyword));

  // Try setting the default suggestion for a word that was never registered.
  Assert.throws(() => ExtensionSearchHandler.setDefaultSuggestion(unregisteredKeyword, {description: "test"}));

  yield cleanup();
});

add_task(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);

  ExtensionSearchHandler.handleSearch(keyword, `${keyword} `, () => {});
  checkEvents([ExtensionSearchHandler.MSG_INPUT_STARTED]);

  ExtensionSearchHandler.handleSearch(keyword, `${keyword} f`, () => {});
  checkEvents([ExtensionSearchHandler.MSG_INPUT_CHANGED]);

  ExtensionSearchHandler.handleInputEntered(keyword, `${keyword} f`, "tab");
  checkEvents([ExtensionSearchHandler.MSG_INPUT_ENTERED]);

  ExtensionSearchHandler.handleSearch(keyword, `${keyword} f`, () => {});
  checkEvents([
    ExtensionSearchHandler.MSG_INPUT_STARTED,
    ExtensionSearchHandler.MSG_INPUT_CHANGED
  ]);

  ExtensionSearchHandler.handleInputCancelled();
  checkEvents([ExtensionSearchHandler.MSG_INPUT_CANCELLED]);

  ExtensionSearchHandler.handleSearch(anotherKeyword, `${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(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"},
        ]);
        controller.stopSearch();
      }
    }
  };

  ExtensionSearchHandler.registerKeyword(keyword, mockExtension);

  yield 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"})
    ]
  });

  yield 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"})
    ]
  });

  yield 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"})
    ]
  });

  yield 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);
  yield cleanup();
});

add_task(function* test_extension_results_should_come_first() {
  let keyword = "test";
  let extensionName = "Omnibox Example";

  let uri = NetUtil.newURI(`http://a.com/b`);
  yield 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"},
        ]);
      }
      controller.stopSearch();
    }
  };

  ExtensionSearchHandler.registerKeyword(keyword, mockExtension);

  // Start an input session before testing MSG_INPUT_CHANGED.
  ExtensionSearchHandler.handleSearch(keyword, `${keyword} `, () => {});

  yield 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);
  yield cleanup();
});

add_task(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, []);
      }
      controller.stopSearch();
    }
  };

  ExtensionSearchHandler.registerKeyword(keyword, mockExtension);

  ExtensionSearchHandler.setDefaultSuggestion(keyword, {
    description: "hello world"
  });

  let searchString = `${keyword} search query`;
  yield check_autocomplete({
    search: searchString,
    searchParam: "enable-actions",
    matches: [
      makeExtensionMatch({heuristic: true, keyword, description: "hello world", content: searchString}),
    ]
  });

  ExtensionSearchHandler.setDefaultSuggestion(keyword, {
    description: "foo bar"
  });

  yield check_autocomplete({
    search: searchString,
    searchParam: "enable-actions",
    matches: [
      makeExtensionMatch({heuristic: true, keyword, description: "foo bar", content: searchString}),
    ]
  });

  ExtensionSearchHandler.unregisterKeyword(keyword);
  yield cleanup();
});

add_task(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"},
        ]);
        controller.stopSearch();
      }
    }
  };

  ExtensionSearchHandler.registerKeyword(keyword, mockExtension);

  // Start an input session before testing MSG_INPUT_CHANGED.
  ExtensionSearchHandler.handleSearch(keyword, `${keyword} `, () => {});

  yield 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);
  yield cleanup();
});