summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/browser/browser_helpUrl.js
blob: 5182a8ddb0907973c376cc5b8c13c717737b188b (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
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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// Tests the help/info button that appears for results whose payloads have a
// `helpUrl` property.

"use strict";

const MAX_RESULTS = UrlbarPrefs.get("maxRichResults");
const RESULT_URL = "http://example.com/test";
const RESULT_HELP_URL = "http://example.com/help";

add_setup(async function () {
  // Add enough results to fill up the view.
  await PlacesUtils.history.clear();
  for (let i = 0; i < MAX_RESULTS; i++) {
    await PlacesTestUtils.addVisits("http://example.com/" + i);
  }
  registerCleanupFunction(async () => {
    await PlacesUtils.history.clear();
  });
});

// Sets `helpL10n` on the result payload and makes sure the help button ends
// up with a corresponding l10n attribute.
add_task(async function title_helpL10n() {
  if (UrlbarPrefs.get("resultMenu")) {
    return;
  }
  let provider = registerTestProvider(1);
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    value: "example",
    window,
  });

  await assertIsTestResult(1);

  let result = await UrlbarTestUtils.getDetailsOfResultAt(window, 1);
  let helpButton = result.element.row._buttons.get("help");
  Assert.ok(helpButton, "Sanity check: help button should exist");

  let l10nAttrs = document.l10n.getAttributes(helpButton);
  Assert.deepEqual(
    l10nAttrs,
    { id: "urlbar-tip-help-icon", args: null },
    "The l10n ID attribute was correctly set"
  );

  await UrlbarTestUtils.promisePopupClose(window);
  UrlbarProvidersManager.unregisterProvider(provider);
});

// (SHIFT+)TABs through a result with a help button.  The result is the
// second result and has other results after it.
add_task(async function keyboardSelection_secondResult() {
  let provider = registerTestProvider(1);
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    value: "example",
    window,
  });

  // Sanity-check initial state.
  Assert.equal(
    UrlbarTestUtils.getResultCount(window),
    MAX_RESULTS,
    "There should be MAX_RESULTS results in the view"
  );
  Assert.equal(
    UrlbarTestUtils.getSelectedElementIndex(window),
    0,
    "The heuristic result should be selected"
  );
  await assertIsTestResult(1);

  info("Arrow down to the main part of the result.");
  EventUtils.synthesizeKey("KEY_ArrowDown");
  assertMainPartSelected(1);

  info("TAB to the button.");
  EventUtils.synthesizeKey("KEY_Tab");
  assertButtonSelected(2);

  info("TAB to the next (third) result.");
  EventUtils.synthesizeKey("KEY_Tab");
  assertOtherResultSelected(3, "next result");

  info("SHIFT+TAB to the help button.");
  EventUtils.synthesizeKey("KEY_Tab", { shiftKey: true });
  assertButtonSelected(2);

  info("SHIFT+TAB to the main part of the result.");
  EventUtils.synthesizeKey("KEY_Tab", { shiftKey: true });
  assertMainPartSelected(1);

  info("Arrow up to the previous (first) result.");
  EventUtils.synthesizeKey("KEY_ArrowUp");
  assertOtherResultSelected(0, "previous result");

  await UrlbarTestUtils.promisePopupClose(window);
  UrlbarProvidersManager.unregisterProvider(provider);
});

// (SHIFT+)TABs through a result with a help button.  The result is the
// last result.
add_task(async function keyboardSelection_lastResult() {
  let provider = registerTestProvider(MAX_RESULTS - 1);
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    value: "example",
    window,
  });

  // Sanity-check initial state.
  Assert.equal(
    UrlbarTestUtils.getResultCount(window),
    MAX_RESULTS,
    "There should be MAX_RESULTS results in the view"
  );
  Assert.equal(
    UrlbarTestUtils.getSelectedElementIndex(window),
    0,
    "The heuristic result should be selected"
  );
  await assertIsTestResult(MAX_RESULTS - 1);

  let numSelectable = UrlbarPrefs.get("resultMenu")
    ? MAX_RESULTS * 2 - 2
    : MAX_RESULTS;

  // Arrow down to the main part of the result.
  EventUtils.synthesizeKey("KEY_ArrowDown", { repeat: MAX_RESULTS - 1 });
  assertMainPartSelected(numSelectable - 1);

  // TAB to the help button.
  EventUtils.synthesizeKey("KEY_Tab");
  assertButtonSelected(numSelectable);

  // Arrow down to the first one-off.  If this test is running alone, the
  // one-offs will rebuild themselves when the view is opened above, and they
  // may not be visible yet.  Wait for the first one to become visible before
  // trying to select it.
  await TestUtils.waitForCondition(() => {
    return (
      gURLBar.view.oneOffSearchButtons.buttons.firstElementChild &&
      BrowserTestUtils.is_visible(
        gURLBar.view.oneOffSearchButtons.buttons.firstElementChild
      )
    );
  }, "Waiting for first one-off to become visible.");

  EventUtils.synthesizeKey("KEY_ArrowDown");
  await TestUtils.waitForCondition(() => {
    return gURLBar.view.oneOffSearchButtons.selectedButton;
  }, "Waiting for one-off to become selected.");
  Assert.equal(
    UrlbarTestUtils.getSelectedElementIndex(window),
    -1,
    "No results should be selected."
  );

  // SHIFT+TAB to the help button.
  EventUtils.synthesizeKey("KEY_Tab", { shiftKey: true });
  assertButtonSelected(numSelectable);

  // SHIFT+TAB to the main part of the result.
  EventUtils.synthesizeKey("KEY_Tab", { shiftKey: true });
  assertMainPartSelected(numSelectable - 1);

  // Arrow up to the previous result.
  EventUtils.synthesizeKey("KEY_ArrowUp");
  assertOtherResultSelected(
    numSelectable - (UrlbarPrefs.get("resultMenu") ? 3 : 2),
    "previous result"
  );

  await UrlbarTestUtils.promisePopupClose(window);
  UrlbarProvidersManager.unregisterProvider(provider);
});

// Picks the main part of the test result -- the non-help-button part -- with
// the keyboard.
add_task(async function pick_mainPart_keyboard() {
  await doPickTest({ pickButton: false, useKeyboard: true });
});

// Picks the help button with the keyboard.
add_task(async function pick_helpButton_keyboard() {
  await doPickTest({ pickButton: true, useKeyboard: true });
});

// Picks the main part of the test result -- the non-help-button part -- with
// the mouse.
add_task(async function pick_mainPart_mouse() {
  await doPickTest({ pickButton: false, useKeyboard: false });
});

// Picks the help button with the mouse.
add_task(async function pick_helpButton_mouse() {
  await doPickTest({ pickButton: true, useKeyboard: false });
});

async function doPickTest({ pickButton, useKeyboard }) {
  await BrowserTestUtils.withNewTab("about:blank", async () => {
    let index = 1;
    let provider = registerTestProvider(index);
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      value: "example",
      window,
    });

    // Sanity-check initial state.
    Assert.equal(
      UrlbarTestUtils.getSelectedElementIndex(window),
      0,
      "The heuristic result should be selected"
    );
    await assertIsTestResult(index);

    if (useKeyboard) {
      // Arrow down to the result.
      EventUtils.synthesizeKey("KEY_ArrowDown", { repeat: index });
      assertMainPartSelected(
        UrlbarPrefs.get("resultMenu") ? index * 2 - 1 : index
      );
    }

    // Pick the result.  The appropriate URL should load.
    let loadPromise = pickButton
      ? BrowserTestUtils.waitForNewTab(gBrowser)
      : BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
    await Promise.all([
      loadPromise,
      UrlbarTestUtils.promisePopupClose(window, async () => {
        if (pickButton && UrlbarPrefs.get("resultMenu")) {
          await UrlbarTestUtils.openResultMenuAndPressAccesskey(window, "h", {
            openByMouse: !useKeyboard,
            resultIndex: index,
          });
        } else if (useKeyboard) {
          if (pickButton) {
            // TAB to the button.
            EventUtils.synthesizeKey("KEY_Tab");
            assertButtonSelected(index + 1);
          }
          EventUtils.synthesizeKey("KEY_Enter");
        } else {
          // Get the click target.
          let result = await UrlbarTestUtils.getDetailsOfResultAt(
            window,
            index
          );
          let clickTarget = pickButton
            ? result.element.row._buttons.get("help")
            : result.element.row._content;
          Assert.ok(
            clickTarget,
            "Click target found, pickButton=" + pickButton
          );
          EventUtils.synthesizeMouseAtCenter(clickTarget, {});
        }
      }),
    ]);
    Assert.equal(
      gBrowser.selectedBrowser.currentURI.spec,
      pickButton ? RESULT_HELP_URL : RESULT_URL,
      "Expected URL should have loaded"
    );

    if (pickButton) {
      BrowserTestUtils.removeTab(gBrowser.selectedTab);
    }
    UrlbarProvidersManager.unregisterProvider(provider);

    // Avoid showing adaptive history autofill.
    await PlacesTestUtils.clearInputHistory();
  });
}

/**
 * Registers a provider that creates a result with a help button.
 *
 * @param {number} suggestedIndex
 *   The result's suggestedIndex.
 * @returns {UrlbarProvider}
 *   The new provider.
 */
function registerTestProvider(suggestedIndex) {
  let results = [
    Object.assign(
      new UrlbarResult(
        UrlbarUtils.RESULT_TYPE.URL,
        UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
        {
          url: RESULT_URL,
          helpUrl: RESULT_HELP_URL,
          helpL10n: {
            id: UrlbarPrefs.get("resultMenu")
              ? "urlbar-result-menu-tip-get-help"
              : "urlbar-tip-help-icon",
          },
        }
      ),
      { suggestedIndex }
    ),
  ];
  let provider = new UrlbarTestUtils.TestProvider({ results });
  UrlbarProvidersManager.registerProvider(provider);
  return provider;
}

/**
 * Asserts that the result at the given index is our test result with a help
 * button.
 *
 * @param {number} index
 *   The expected index of the test result.
 */
async function assertIsTestResult(index) {
  let result = await UrlbarTestUtils.getDetailsOfResultAt(window, index);
  Assert.equal(
    result.type,
    UrlbarUtils.RESULT_TYPE.URL,
    "The second result should be a URL"
  );
  Assert.equal(
    result.url,
    RESULT_URL,
    "The result's URL should be the expected URL"
  );

  let { row } = result.element;
  if (UrlbarPrefs.get("resultMenu")) {
    Assert.ok(row._buttons.get("menu"), "The result should have a menu button");
  } else {
    let helpButton = row._buttons.get("help");
    Assert.ok(helpButton, "The result should have a help button");
    Assert.ok(helpButton.id, "Help button has an ID");
  }
  Assert.ok(row._content.id, "Row-inner has an ID");
  Assert.equal(
    row.getAttribute("role"),
    "presentation",
    "Row should have role=presentation"
  );
  Assert.equal(
    row._content.getAttribute("role"),
    "option",
    "Row-inner should have role=option"
  );
}

/**
 * Asserts that a particular element is selected.
 *
 * @param {number} expectedSelectedElementIndex
 *   The expected selected element index.
 * @param {string} expectedClassName
 *   A class name of the expected selected element.
 * @param {string} msg
 *   A string to include in the assertion message.
 */
function assertSelection(expectedSelectedElementIndex, expectedClassName, msg) {
  Assert.equal(
    UrlbarTestUtils.getSelectedElementIndex(window),
    expectedSelectedElementIndex,
    "Expected selected element index: " + msg
  );
  Assert.ok(
    UrlbarTestUtils.getSelectedElement(window).classList.contains(
      expectedClassName
    ),
    `Expected selected element: ${msg} (${
      UrlbarTestUtils.getSelectedElement(window).classList
    } == ${expectedClassName})`
  );
}

/**
 * Asserts that the main part of our test resut -- the non-help-button part --
 * is selected.
 *
 * @param {number} expectedSelectedElementIndex
 *   The expected selected element index.
 */
function assertMainPartSelected(expectedSelectedElementIndex) {
  assertSelection(
    expectedSelectedElementIndex,
    "urlbarView-row-inner",
    "main part of test result"
  );
}

/**
 * Asserts that the help button part of our test result is selected.
 *
 * @param {number} expectedSelectedElementIndex
 *   The expected selected element index.
 */
function assertButtonSelected(expectedSelectedElementIndex) {
  if (UrlbarPrefs.get("resultMenu")) {
    assertSelection(
      expectedSelectedElementIndex,
      "urlbarView-button-menu",
      "menu button"
    );
  } else {
    assertSelection(
      expectedSelectedElementIndex,
      "urlbarView-button-help",
      "help button"
    );
  }
}

/**
 * Asserts that a result other than our test result is selected.
 *
 * @param {number} expectedSelectedElementIndex
 *   The expected selected element index.
 * @param {string} msg
 *   A string to include in the assertion message.
 */
function assertOtherResultSelected(expectedSelectedElementIndex, msg) {
  Assert.equal(
    UrlbarTestUtils.getSelectedElementIndex(window),
    expectedSelectedElementIndex,
    "Expected other selected element index: " + msg
  );
}