summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/browser/browser_oneOffs_keyModifiers.js
blob: 30b241b3b3de5ca1a7e00de85ef60f77e4a4d6e7 (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
385
386
387
388
389
390
391
392
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Tests that one-offs behave differently with key modifiers.
 */

"use strict";

const TEST_ENGINE_BASENAME = "searchSuggestionEngine.xml";
const SEARCH_STRING = "foo.bar";

XPCOMUtils.defineLazyGetter(this, "oneOffSearchButtons", () => {
  return UrlbarTestUtils.getOneOffSearchButtons(window);
});

let engine;

async function searchAndOpenPopup(value) {
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value,
    fireInputEvent: true,
  });
  await TestUtils.waitForCondition(
    () => !oneOffSearchButtons._rebuilding,
    "Waiting for one-offs to finish rebuilding"
  );
}

add_setup(async function () {
  // Add a search suggestion engine and move it to the front so that it appears
  // as the first one-off.
  engine = await SearchTestUtils.promiseNewSearchEngine({
    url: getRootDirectory(gTestPath) + TEST_ENGINE_BASENAME,
  });
  await Services.search.moveEngine(engine, 0);

  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.search.separatePrivateDefault.ui.enabled", false],
      ["browser.urlbar.suggest.quickactions", false],
    ],
  });

  registerCleanupFunction(async function () {
    await PlacesUtils.history.clear();
    await UrlbarTestUtils.formHistory.clear();
  });

  // Initialize history with enough visits to fill up the view.
  await PlacesUtils.history.clear();
  await UrlbarTestUtils.formHistory.clear();
  let maxResults = Services.prefs.getIntPref("browser.urlbar.maxRichResults");
  for (let i = 0; i < maxResults; i++) {
    await PlacesTestUtils.addVisits(
      "http://mochi.test:8888/browser_urlbarOneOffs.js/?" + i
    );
  }

  // Add some more visits to the last URL added above so that the top-sites view
  // will be non-empty.
  for (let i = 0; i < 5; i++) {
    await PlacesTestUtils.addVisits(
      "http://mochi.test:8888/browser_urlbarOneOffs.js/?" + (maxResults - 1)
    );
  }
  await updateTopSites(sites => {
    return (
      sites && sites[0] && sites[0].url.startsWith("http://mochi.test:8888/")
    );
  });

  // Move the mouse away from the view so that a result or one-off isn't
  // inadvertently highlighted.  See bug 1659011.
  EventUtils.synthesizeMouse(
    gURLBar.inputField,
    0,
    0,
    { type: "mousemove" },
    window
  );
});

// Shift clicking with no search string should open search mode, like an
// unmodified click.
add_task(async function shift_click_empty() {
  await searchAndOpenPopup("");
  let oneOffs = oneOffSearchButtons.getSelectableButtons(true);
  let searchPromise = UrlbarTestUtils.promiseSearchComplete(window);
  EventUtils.synthesizeMouseAtCenter(oneOffs[0], { shiftKey: true });
  await searchPromise;
  await UrlbarTestUtils.assertSearchMode(window, {
    engineName: oneOffs[0].engine.name,
    entry: "oneoff",
  });
  await UrlbarTestUtils.exitSearchMode(window);
  await UrlbarTestUtils.promisePopupClose(window);
});

// Shift clicking with a search string should perform a search in the current
// tab.
add_task(async function shift_click_search() {
  await searchAndOpenPopup(SEARCH_STRING);
  let resultsPromise = BrowserTestUtils.browserLoaded(
    gBrowser.selectedBrowser,
    false,
    "http://mochi.test:8888/?terms=foo.bar"
  );
  let oneOffs = oneOffSearchButtons.getSelectableButtons(true);
  EventUtils.synthesizeMouseAtCenter(oneOffs[0], { shiftKey: true });
  await resultsPromise;
  await UrlbarTestUtils.assertSearchMode(window, null);
  await UrlbarTestUtils.promisePopupClose(window);
});

// Pressing Shift+Enter on a one-off with no search string should open search
// mode, like an unmodified click.
add_task(async function shift_enter_empty() {
  await searchAndOpenPopup("");
  // Alt+Down to select the first one-off.
  let oneOffs = oneOffSearchButtons.getSelectableButtons(true);
  EventUtils.synthesizeKey("KEY_ArrowDown", { altKey: true });
  let searchPromise = UrlbarTestUtils.promiseSearchComplete(window);
  EventUtils.synthesizeKey("KEY_Enter", { shiftKey: true });
  await searchPromise;
  await UrlbarTestUtils.assertSearchMode(window, {
    engineName: oneOffs[0].engine.name,
    entry: "oneoff",
  });
  await UrlbarTestUtils.exitSearchMode(window);
  await UrlbarTestUtils.promisePopupClose(window);
});

// Pressing Shift+Enter on a one-off with a search string should perform a
// search in the current tab.
add_task(async function shift_enter_search() {
  await searchAndOpenPopup(SEARCH_STRING);
  let resultsPromise = BrowserTestUtils.browserLoaded(
    gBrowser.selectedBrowser,
    false,
    "http://mochi.test:8888/?terms=foo.bar"
  );
  EventUtils.synthesizeKey("KEY_ArrowDown", { altKey: true });
  EventUtils.synthesizeKey("KEY_Enter", { shiftKey: true });
  await resultsPromise;
  await UrlbarTestUtils.assertSearchMode(window, null);
  await UrlbarTestUtils.promisePopupClose(window);
});

// Pressing Alt+Enter on a one-off on an "empty" page (e.g. new tab) should open
// search mode in the current tab.
add_task(async function alt_enter_emptypage() {
  await BrowserTestUtils.withNewTab("about:home", async function (browser) {
    await searchAndOpenPopup(SEARCH_STRING);
    let oneOffs = oneOffSearchButtons.getSelectableButtons(true);
    // Alt+Down to select the first one-off.
    EventUtils.synthesizeKey("KEY_ArrowDown", { altKey: true });
    let searchPromise = UrlbarTestUtils.promiseSearchComplete(window);
    EventUtils.synthesizeKey("KEY_Enter", { altKey: true });
    await searchPromise;
    Assert.equal(
      browser,
      gBrowser.selectedBrowser,
      "The foreground tab hasn't changed."
    );
    await UrlbarTestUtils.assertSearchMode(window, {
      engineName: oneOffs[0].engine.name,
      entry: "oneoff",
    });
    await UrlbarTestUtils.exitSearchMode(window);
    await UrlbarTestUtils.promisePopupClose(window);
  });
});

// Pressing Alt+Enter on a one-off with no search string and on a "non-empty"
// page should open search mode in a new foreground tab.
add_task(async function alt_enter_empty() {
  await BrowserTestUtils.withNewTab("about:robots", async function (browser) {
    await searchAndOpenPopup("");
    let oneOffs = oneOffSearchButtons.getSelectableButtons(true);
    // Alt+Down to select the first one-off.
    EventUtils.synthesizeKey("KEY_ArrowDown", { altKey: true });
    let tabOpenPromise = BrowserTestUtils.waitForEvent(
      gBrowser.tabContainer,
      "TabOpen"
    );
    EventUtils.synthesizeKey("KEY_Enter", { altKey: true });
    await tabOpenPromise;
    Assert.notEqual(
      browser,
      gBrowser.selectedBrowser,
      "The current foreground tab is new."
    );
    await UrlbarTestUtils.assertSearchMode(window, {
      engineName: oneOffs[0].engine.name,
      entry: "oneoff",
    });
    BrowserTestUtils.removeTab(gBrowser.selectedTab);
    Assert.equal(
      browser,
      gBrowser.selectedBrowser,
      "We're back in the original tab."
    );
    await UrlbarTestUtils.assertSearchMode(window, null);
    await UrlbarTestUtils.promisePopupClose(window);
  });
});

// Pressing Alt+Enter on a remote one-off with a search string and on a
// "non-empty" page should perform a search in a new foreground tab.
add_task(async function alt_enter_search_remote() {
  await BrowserTestUtils.withNewTab("about:robots", async function (browser) {
    await searchAndOpenPopup(SEARCH_STRING);
    // Alt+Down to select the first one-off.
    EventUtils.synthesizeKey("KEY_ArrowDown", { altKey: true });
    let tabOpenPromise = BrowserTestUtils.waitForNewTab(
      gBrowser,
      "http://mochi.test:8888/?terms=foo.bar",
      true
    );
    EventUtils.synthesizeKey("KEY_Enter", { altKey: true });
    // This implictly checks the correct page is loaded.
    let newTab = await tabOpenPromise;
    Assert.equal(
      newTab,
      gBrowser.selectedTab,
      "The current foreground tab is new."
    );
    // Check search mode is not activated in the new tab.
    await UrlbarTestUtils.assertSearchMode(window, null);
    BrowserTestUtils.removeTab(newTab);
    Assert.equal(
      browser,
      gBrowser.selectedBrowser,
      "We're back in the original tab."
    );
    await UrlbarTestUtils.assertSearchMode(window, null);
    await UrlbarTestUtils.promisePopupClose(window);
  });
});

// Pressing Alt+Enter on a local one-off with a search string and on a
// "non-empty" page should open search mode in a new foreground tab with the
// search string already populated.
add_task(async function alt_enter_search_local() {
  await BrowserTestUtils.withNewTab("about:robots", async function (browser) {
    await searchAndOpenPopup(SEARCH_STRING);
    // Alt+Down to select the first local one-off.
    EventUtils.synthesizeKey("KEY_ArrowDown", { altKey: true });
    while (
      oneOffSearchButtons.selectedButton.id !=
      "urlbar-engine-one-off-item-bookmarks"
    ) {
      EventUtils.synthesizeKey("KEY_ArrowDown", { altKey: true });
    }
    let tabOpenPromise = BrowserTestUtils.waitForEvent(
      gBrowser.tabContainer,
      "TabOpen"
    );
    EventUtils.synthesizeKey("KEY_Enter", { altKey: true });
    await tabOpenPromise;
    Assert.notEqual(
      browser,
      gBrowser.selectedBrowser,
      "The current foreground tab is new."
    );
    await UrlbarTestUtils.assertSearchMode(window, {
      source: UrlbarUtils.RESULT_SOURCE.BOOKMARKS,
      entry: "oneoff",
    });
    Assert.equal(
      gURLBar.value,
      SEARCH_STRING,
      "The search term was duplicated to the new tab."
    );
    BrowserTestUtils.removeTab(gBrowser.selectedTab);
    Assert.equal(
      browser,
      gBrowser.selectedBrowser,
      "We're back in the original tab."
    );
    await UrlbarTestUtils.assertSearchMode(window, null);
    await UrlbarTestUtils.promisePopupClose(window);
  });
});

// Accel+Clicking a one-off with an empty search string should open search mode
// in a new background tab.
add_task(async function accel_click_empty() {
  await searchAndOpenPopup("");
  let oneOffs = oneOffSearchButtons.getSelectableButtons(true);

  // We have to listen for the new tab using this brute force method.
  // about:newtab is preloaded in the background. When about:newtab is opened,
  // the cached version is shown. Since the page is already loaded,
  // waitForNewTab does not detect it. It also doesn't fire the TabOpen event.
  let tabCount = gBrowser.tabs.length;
  let tabOpenPromise = TestUtils.waitForCondition(
    () =>
      gBrowser.tabs.length == tabCount + 1
        ? gBrowser.tabs[gBrowser.tabs.length - 1]
        : false,
    "Waiting for background about:newtab to open."
  );
  EventUtils.synthesizeMouseAtCenter(oneOffs[0], { accelKey: true });
  let newTab = await tabOpenPromise;
  Assert.notEqual(
    newTab.linkedBrowser,
    gBrowser.selectedBrowser,
    "The foreground tab hasn't changed."
  );
  await UrlbarTestUtils.assertSearchMode(window, null);
  BrowserTestUtils.switchTab(gBrowser, newTab);
  // Check the new background tab is already in search mode.
  await UrlbarTestUtils.assertSearchMode(window, {
    engineName: oneOffs[0].engine.name,
    entry: "oneoff",
  });
  BrowserTestUtils.removeTab(newTab);
  await UrlbarTestUtils.promisePopupClose(window);
});

// Accel+Clicking a remote one-off with a search string should execute a search
// in a new background tab.
add_task(async function accel_click_search_remote() {
  await searchAndOpenPopup(SEARCH_STRING);
  let oneOffs = oneOffSearchButtons.getSelectableButtons(true);
  let tabOpenPromise = BrowserTestUtils.waitForNewTab(
    gBrowser,
    "http://mochi.test:8888/?terms=foo.bar",
    true
  );
  EventUtils.synthesizeMouseAtCenter(oneOffs[0], { accelKey: true });
  // This implictly checks the correct page is loaded.
  let newTab = await tabOpenPromise;
  Assert.notEqual(
    gBrowser.selectedTab,
    newTab,
    "The foreground tab hasn't changed."
  );
  await UrlbarTestUtils.assertSearchMode(window, null);
  // Switch to the background tab, which is the last tab in gBrowser.tabs.
  BrowserTestUtils.switchTab(gBrowser, newTab);
  // Check the new background tab is not search mode.
  await UrlbarTestUtils.assertSearchMode(window, null);
  BrowserTestUtils.removeTab(newTab);
  await UrlbarTestUtils.promisePopupClose(window);
});

// Accel+Clicking a local one-off with a search string should open search mode
// in a new background tab with the search string already populated.
add_task(async function accel_click_search_local() {
  await searchAndOpenPopup(SEARCH_STRING);
  let oneOffs = oneOffSearchButtons.getSelectableButtons(true);
  let oneOff;
  for (oneOff of oneOffs) {
    if (oneOff.id == "urlbar-engine-one-off-item-bookmarks") {
      break;
    }
  }
  let tabCount = gBrowser.tabs.length;
  let tabOpenPromise = TestUtils.waitForCondition(
    () =>
      gBrowser.tabs.length == tabCount + 1
        ? gBrowser.tabs[gBrowser.tabs.length - 1]
        : false,
    "Waiting for background about:newtab to open."
  );
  EventUtils.synthesizeMouseAtCenter(oneOff, { accelKey: true });
  let newTab = await tabOpenPromise;
  Assert.notEqual(
    newTab.linkedBrowser,
    gBrowser.selectedBrowser,
    "The foreground tab hasn't changed."
  );
  await UrlbarTestUtils.assertSearchMode(window, null);
  BrowserTestUtils.switchTab(gBrowser, newTab);
  // Check the new background tab is already in search mode.
  await UrlbarTestUtils.assertSearchMode(window, {
    source: UrlbarUtils.RESULT_SOURCE.BOOKMARKS,
    entry: "oneoff",
  });
  // Check the search string is already populated.
  Assert.equal(
    gURLBar.value,
    SEARCH_STRING,
    "The search term was duplicated to the new tab."
  );
  BrowserTestUtils.removeTab(newTab);
  await UrlbarTestUtils.promisePopupClose(window);
});