summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_omnibox.js
blob: 98d3573c55aacfaf8ca86eebab72bcf7fb442e81 (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

function* setup() {
  const SUGGEST_URLBAR_PREF = "browser.urlbar.suggest.searches";
  Services.prefs.setBoolPref(SUGGEST_URLBAR_PREF, false);

  registerCleanupFunction(() => {
    Services.prefs.clearUserPref(SUGGEST_URLBAR_PREF);
  });
}

add_task(function* () {
  let keyword = "test";

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      "omnibox": {
        "keyword": keyword,
      },
    },

    background: function() {
      browser.omnibox.onInputStarted.addListener(() => {
        browser.test.sendMessage("on-input-started-fired");
      });

      let synchronous = true;
      let suggestions = null;
      let suggestCallback = null;

      browser.omnibox.onInputChanged.addListener((text, suggest) => {
        if (synchronous && suggestions) {
          suggest(suggestions);
        } else {
          suggestCallback = suggest;
        }
        browser.test.sendMessage("on-input-changed-fired", {text});
      });

      browser.omnibox.onInputCancelled.addListener(() => {
        browser.test.sendMessage("on-input-cancelled-fired");
      });

      browser.omnibox.onInputEntered.addListener((text, disposition) => {
        browser.test.sendMessage("on-input-entered-fired", {text, disposition});
      });

      browser.test.onMessage.addListener((msg, data) => {
        switch (msg) {
          case "set-suggestions":
            suggestions = data.suggestions;
            browser.test.sendMessage("suggestions-set");
            break;
          case "set-default-suggestion":
            browser.omnibox.setDefaultSuggestion(data.suggestion);
            browser.test.sendMessage("default-suggestion-set");
            break;
          case "set-synchronous":
            synchronous = data.synchronous;
            break;
          case "test-multiple-suggest-calls":
            suggestions.forEach(suggestion => suggestCallback([suggestion]));
            browser.test.sendMessage("test-ready");
            break;
          case "test-suggestions-after-delay":
            Promise.resolve().then(() => {
              suggestCallback(suggestions);
              browser.test.sendMessage("test-ready");
            });
            break;
        }
      });
    },
  });

  function* expectEvent(event, expected = {}) {
    let actual = yield extension.awaitMessage(event);
    if (expected.text) {
      is(actual.text, expected.text,
        `Expected "${event}" to have fired with text: "${expected.text}".`);
    }
    if (expected.disposition) {
      is(actual.disposition, expected.disposition,
        `Expected "${event}" to have fired with disposition: "${expected.disposition}".`);
    }
  }

  function* startInputSession() {
    gURLBar.focus();
    gURLBar.value = keyword;
    EventUtils.synthesizeKey(" ", {});
    yield expectEvent("on-input-started-fired");
    EventUtils.synthesizeKey("t", {});
    yield expectEvent("on-input-changed-fired", {text: "t"});
    return "t";
  }

  function* testInputEvents() {
    gURLBar.focus();

    // Start an input session by typing in <keyword><space>.
    for (let letter of keyword) {
      EventUtils.synthesizeKey(letter, {});
    }
    EventUtils.synthesizeKey(" ", {});
    yield expectEvent("on-input-started-fired");

    // We should expect input changed events now that the keyword is active.
    EventUtils.synthesizeKey("b", {});
    yield expectEvent("on-input-changed-fired", {text: "b"});

    EventUtils.synthesizeKey("c", {});
    yield expectEvent("on-input-changed-fired", {text: "bc"});

    EventUtils.synthesizeKey("VK_BACK_SPACE", {});
    yield expectEvent("on-input-changed-fired", {text: "b"});

    // Even though the input is <keyword><space> We should not expect an
    // input started event to fire since the keyword is active.
    EventUtils.synthesizeKey("VK_BACK_SPACE", {});
    yield expectEvent("on-input-changed-fired", {text: ""});

    // Make the keyword inactive by hitting backspace.
    EventUtils.synthesizeKey("VK_BACK_SPACE", {});
    yield expectEvent("on-input-cancelled-fired");

    // Activate the keyword by typing a space.
    // Expect onInputStarted to fire.
    EventUtils.synthesizeKey(" ", {});
    yield expectEvent("on-input-started-fired");

    // onInputChanged should fire even if a space is entered.
    EventUtils.synthesizeKey(" ", {});
    yield expectEvent("on-input-changed-fired", {text: " "});

    // The active session should cancel if the input blurs.
    gURLBar.blur();
    yield expectEvent("on-input-cancelled-fired");
  }

  function* testHeuristicResult(expectedText, setDefaultSuggestion) {
    if (setDefaultSuggestion) {
      extension.sendMessage("set-default-suggestion", {
        suggestion: {
          description: expectedText,
        },
      });
      yield extension.awaitMessage("default-suggestion-set");
    }

    let text = yield startInputSession();

    let item = gURLBar.popup.richlistbox.children[0];

    is(item.getAttribute("title"), expectedText,
      `Expected heuristic result to have title: "${expectedText}".`);

    is(item.getAttribute("displayurl"), `${keyword} ${text}`,
      `Expected heuristic result to have displayurl: "${keyword} ${text}".`);

    EventUtils.synthesizeMouseAtCenter(item, {});

    yield expectEvent("on-input-entered-fired", {
      text,
      disposition: "currentTab",
    });
  }

  function* testDisposition(suggestionIndex, expectedDisposition, expectedText) {
    yield startInputSession();

    // Select the suggestion.
    for (let i = 0; i < suggestionIndex; i++) {
      EventUtils.synthesizeKey("VK_DOWN", {});
    }

    let item = gURLBar.popup.richlistbox.children[suggestionIndex];
    if (expectedDisposition == "currentTab") {
      EventUtils.synthesizeMouseAtCenter(item, {});
    } else if (expectedDisposition == "newForegroundTab") {
      EventUtils.synthesizeMouseAtCenter(item, {accelKey: true});
    } else if (expectedDisposition == "newBackgroundTab") {
      EventUtils.synthesizeMouseAtCenter(item, {shiftKey: true, accelKey: true});
    }

    yield expectEvent("on-input-entered-fired", {
      text: expectedText,
      disposition: expectedDisposition,
    });
  }

  function* testSuggestions(info) {
    extension.sendMessage("set-synchronous", {synchronous: false});

    function expectSuggestion({content, description}, index) {
      let item = gURLBar.popup.richlistbox.children[index + 1]; // Skip the heuristic result.

      ok(!!item, "Expected item to exist");
      is(item.getAttribute("title"), description,
        `Expected suggestion to have title: "${description}".`);

      is(item.getAttribute("displayurl"), `${keyword} ${content}`,
        `Expected suggestion to have displayurl: "${keyword} ${content}".`);
    }

    let text = yield startInputSession();

    extension.sendMessage(info.test);
    yield extension.awaitMessage("test-ready");

    info.suggestions.forEach(expectSuggestion);

    EventUtils.synthesizeMouseAtCenter(gURLBar.popup.richlistbox.children[0], {});
    yield expectEvent("on-input-entered-fired", {
      text,
      disposition: "currentTab",
    });
  }

  yield setup();
  yield extension.startup();

  yield testInputEvents();

  // Test the heuristic result with default suggestions.
  yield testHeuristicResult("Generated extension", false /* setDefaultSuggestion */);
  yield testHeuristicResult("hello world", true /* setDefaultSuggestion */);
  yield testHeuristicResult("foo bar", true /* setDefaultSuggestion */);

  let suggestions = [
    {content: "a", description: "select a"},
    {content: "b", description: "select b"},
    {content: "c", description: "select c"},
  ];

  extension.sendMessage("set-suggestions", {suggestions});
  yield extension.awaitMessage("suggestions-set");

  // Test each suggestion and search disposition.
  yield testDisposition(1, "currentTab", suggestions[0].content);
  yield testDisposition(2, "newForegroundTab", suggestions[1].content);
  yield testDisposition(3, "newBackgroundTab", suggestions[2].content);

  extension.sendMessage("set-suggestions", {suggestions});
  yield extension.awaitMessage("suggestions-set");

  // Test adding suggestions asynchronously.
  yield testSuggestions({
    test: "test-multiple-suggest-calls",
    skipHeuristic: true,
    suggestions,
  });
  yield testSuggestions({
    test: "test-suggestions-after-delay",
    skipHeuristic: true,
    suggestions,
  });

  // Start monitoring the console.
  SimpleTest.waitForExplicitFinish();
  let waitForConsole = new Promise(resolve => {
    SimpleTest.monitorConsole(resolve, [{
      message: new RegExp(`The keyword provided is already registered: "${keyword}"`),
    }]);
  });

  // Try registering another extension with the same keyword
  let extension2 = ExtensionTestUtils.loadExtension({
    manifest: {
      "omnibox": {
        "keyword": keyword,
      },
    },
  });

  yield extension2.startup();

  // Stop monitoring the console and confirm the correct errors are logged.
  SimpleTest.endMonitorConsole();
  yield waitForConsole;

  yield extension2.unload();
  yield extension.unload();
});