summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/browser/browser_groupLabels.js
blob: 2b43990b778baf2260b1af76bd4cb050f7c3fcab (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
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// Tests group labels in the view.

"use strict";

const SUGGESTIONS_FIRST_PREF = "browser.urlbar.showSearchSuggestionsFirst";
const SUGGESTIONS_PREF = "browser.urlbar.suggest.searches";

const TEST_ENGINE_BASENAME = "searchSuggestionEngine.xml";
const TEST_ENGINE_2_BASENAME = "searchSuggestionEngine2.xml";
const MAX_RESULTS = UrlbarPrefs.get("maxRichResults");

const TOP_SITES = [
  "http://example-1.com/",
  "http://example-2.com/",
  "http://example-3.com/",
];

const FIREFOX_SUGGEST_LABEL = "Firefox Suggest";

// %s is replaced with the engine name.
const ENGINE_SUGGESTIONS_LABEL = "%s suggestions";

// Allow more time for Mac machines so they don't time out in verify mode.
if (AppConstants.platform == "macosx") {
  requestLongerTimeout(3);
}

add_setup(async function () {
  Assert.ok(
    UrlbarPrefs.get("showSearchSuggestionsFirst"),
    "Precondition: Search suggestions shown first by default"
  );

  // Add some history.
  await PlacesUtils.history.clear();
  await PlacesUtils.bookmarks.eraseEverything();
  await UrlbarTestUtils.formHistory.clear();
  await addHistory();

  // Make sure we have some top sites.
  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.urlbar.suggest.topsites", true],
      ["browser.newtabpage.activity-stream.default.sites", TOP_SITES.join(",")],
    ],
  });
  // Waiting for all top sites to be added intermittently times out, so just
  // wait for any to be added. We're not testing top sites here; we only need
  // the view to open in top-sites mode.
  await updateTopSites(sites => sites && sites.length);

  // Add a mock engine so we don't hit the network.
  await SearchTestUtils.installSearchExtension({}, { setAsDefault: true });

  registerCleanupFunction(async () => {
    await PlacesUtils.history.clear();
  });
});

// The Firefox Suggest label should not appear when the labels pref is disabled.
add_task(async function prefDisabled() {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.urlbar.groupLabels.enabled", false]],
  });
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "test",
  });
  await checkLabels(MAX_RESULTS, {});
  await UrlbarTestUtils.promisePopupClose(window);
  await SpecialPowers.popPrefEnv();
});

// The Firefox Suggest label should not appear when the view shows top sites.
add_task(async function topSites() {
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "",
  });
  await checkLabels(-1, {});
  await UrlbarTestUtils.promisePopupClose(window);
});

// The Firefox Suggest label should appear when the search string is non-empty
// and there are only general results.
add_task(async function general() {
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "test",
  });
  await checkLabels(MAX_RESULTS, {
    1: FIREFOX_SUGGEST_LABEL,
  });
  await UrlbarTestUtils.promisePopupClose(window);
});

// The Firefox Suggest label should appear when the search string is non-empty
// and there are suggestions followed by general results.
add_task(async function suggestionsBeforeGeneral() {
  await withSuggestions(async () => {
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: "test",
    });
    await checkLabels(MAX_RESULTS, {
      3: FIREFOX_SUGGEST_LABEL,
    });
    await UrlbarTestUtils.promisePopupClose(window);
  });
});

// Both the Firefox Suggest and Suggestions labels should appear when the search
// string is non-empty, general results are shown before suggestions, and there
// are general and suggestion results.
add_task(async function generalBeforeSuggestions() {
  await withSuggestions(async engine => {
    Assert.ok(engine.name, "Engine name is non-empty");
    await SpecialPowers.pushPrefEnv({
      set: [[SUGGESTIONS_FIRST_PREF, false]],
    });
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: "test",
    });
    await checkLabels(MAX_RESULTS, {
      1: FIREFOX_SUGGEST_LABEL,
      [MAX_RESULTS - 2]: engineSuggestionsLabel(engine.name),
    });
    await UrlbarTestUtils.promisePopupClose(window);
  });
});

// Neither the Firefox Suggest nor Suggestions label should appear when the
// search string is non-empty, general results are shown before suggestions, and
// there are only suggestion results.
add_task(async function generalBeforeSuggestions_suggestionsOnly() {
  await PlacesUtils.history.clear();

  await withSuggestions(async engine => {
    await SpecialPowers.pushPrefEnv({
      set: [[SUGGESTIONS_FIRST_PREF, false]],
    });
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: "test",
    });
    await checkLabels(3, {});
    await UrlbarTestUtils.promisePopupClose(window);
  });

  // Add back history so subsequent tasks run with this test's initial state.
  await addHistory();
});

// The Suggestions label should be updated when the default engine changes.
add_task(async function generalBeforeSuggestions_defaultChanged() {
  // Install both test engines, one after the other. Engine 2 will be the final
  // default engine.
  await withSuggestions(async engine1 => {
    await withSuggestions(async engine2 => {
      Assert.ok(engine2.name, "Engine 2 name is non-empty");
      Assert.notEqual(engine1.name, engine2.name, "Engine names are different");
      Assert.equal(
        Services.search.defaultEngine.name,
        engine2.name,
        "Engine 2 is default"
      );
      await SpecialPowers.pushPrefEnv({
        set: [[SUGGESTIONS_FIRST_PREF, false]],
      });
      await UrlbarTestUtils.promiseAutocompleteResultPopup({
        window,
        value: "test",
      });
      await checkLabels(MAX_RESULTS, {
        1: FIREFOX_SUGGEST_LABEL,
        [MAX_RESULTS - 2]: engineSuggestionsLabel(engine2.name),
      });
      await UrlbarTestUtils.promisePopupClose(window);
    }, TEST_ENGINE_2_BASENAME);
  });
});

// The Firefox Suggest label should appear above a suggested-index result when
// the result is the only result with that label.
add_task(async function suggestedIndex_only() {
  // Clear history, add a provider that returns a result with suggestedIndex =
  // -1, set up an engine with suggestions, and start a query. The suggested-
  // index result will be the only result with a label.
  await PlacesUtils.history.clear();

  let index = -1;
  let provider = new SuggestedIndexProvider(index);
  UrlbarProvidersManager.registerProvider(provider);

  await withSuggestions(async engine => {
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: "test",
    });
    let result = await UrlbarTestUtils.getDetailsOfResultAt(window, 3);
    Assert.equal(
      result.element.row.result.suggestedIndex,
      index,
      "Sanity check: Our suggested-index result is present"
    );
    await checkLabels(4, {
      3: FIREFOX_SUGGEST_LABEL,
    });
    await UrlbarTestUtils.promisePopupClose(window);
  });

  UrlbarProvidersManager.unregisterProvider(provider);

  // Add back history so subsequent tasks run with this test's initial state.
  await addHistory();
});

// The Firefox Suggest label should appear above a suggested-index result when
// the result is the first but not the only result with that label.
add_task(async function suggestedIndex_first() {
  let index = 1;
  let provider = new SuggestedIndexProvider(index);
  UrlbarProvidersManager.registerProvider(provider);

  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "test",
  });
  let result = await UrlbarTestUtils.getDetailsOfResultAt(window, index);
  Assert.equal(
    result.element.row.result.suggestedIndex,
    index,
    "Sanity check: Our suggested-index result is present"
  );
  await checkLabels(MAX_RESULTS, {
    [index]: FIREFOX_SUGGEST_LABEL,
  });
  await UrlbarTestUtils.promisePopupClose(window);

  UrlbarProvidersManager.unregisterProvider(provider);
});

// The Firefox Suggest label should not appear above a suggested-index result
// when the result is not the first with that label.
add_task(async function suggestedIndex_notFirst() {
  let index = -1;
  let provider = new SuggestedIndexProvider(index);
  UrlbarProvidersManager.registerProvider(provider);

  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "test",
  });
  let result = await UrlbarTestUtils.getDetailsOfResultAt(
    window,
    MAX_RESULTS + index
  );
  Assert.equal(
    result.element.row.result.suggestedIndex,
    index,
    "Sanity check: Our suggested-index result is present"
  );
  await checkLabels(MAX_RESULTS, {
    1: FIREFOX_SUGGEST_LABEL,
  });
  await UrlbarTestUtils.promisePopupClose(window);

  UrlbarProvidersManager.unregisterProvider(provider);
});

// Labels that appear multiple times but not consecutively should be shown.
add_task(async function repeatLabels() {
  let engineName = Services.search.defaultEngine.name;
  let results = [
    new UrlbarResult(
      UrlbarUtils.RESULT_TYPE.URL,
      UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
      { url: "http://example.com/1" }
    ),
    new UrlbarResult(
      UrlbarUtils.RESULT_TYPE.SEARCH,
      UrlbarUtils.RESULT_SOURCE.SEARCH,
      { suggestion: "test1", engine: engineName }
    ),
    new UrlbarResult(
      UrlbarUtils.RESULT_TYPE.URL,
      UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
      { url: "http://example.com/2" }
    ),
    new UrlbarResult(
      UrlbarUtils.RESULT_TYPE.SEARCH,
      UrlbarUtils.RESULT_SOURCE.SEARCH,
      { suggestion: "test2", engine: engineName }
    ),
  ];

  for (let i = 0; i < results.length; i++) {
    results[i].suggestedIndex = i;
  }

  let provider = new UrlbarTestUtils.TestProvider({
    results,
    priority: Infinity,
  });
  UrlbarProvidersManager.registerProvider(provider);

  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "test",
  });
  await checkLabels(results.length, {
    0: FIREFOX_SUGGEST_LABEL,
    1: engineSuggestionsLabel(engineName),
    2: FIREFOX_SUGGEST_LABEL,
    3: engineSuggestionsLabel(engineName),
  });
  await UrlbarTestUtils.promisePopupClose(window);

  UrlbarProvidersManager.unregisterProvider(provider);
});

// Clicking a row label shouldn't do anything.
add_task(async function clickLabel() {
  await BrowserTestUtils.withNewTab("about:blank", async () => {
    // Do a search. The mock history added in init() should appear with the
    // Firefox Suggest label at index 1.
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: "test",
    });
    await checkLabels(MAX_RESULTS, {
      1: FIREFOX_SUGGEST_LABEL,
    });

    // Check the result at index 2.
    let result2 = await UrlbarTestUtils.getDetailsOfResultAt(window, 2);
    Assert.ok(result2.url, "Result at index 2 has a URL");
    let url2 = result2.url;
    Assert.ok(
      url2.startsWith("http://example.com/"),
      "Result at index 2 is one of our mock history results"
    );

    // Get the row at index 3 and click above it. The click should hit the row
    // at index 2 and load its URL. We do this to make sure our click code
    // here in the test works properly and that performing a similar click
    // relative to index 1 (see below) would hit the row at index 0 if not for
    // the label at index 1.
    let result3 = await UrlbarTestUtils.getDetailsOfResultAt(window, 3);
    let loadPromise = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);

    info("Performing click relative to index 3");
    await UrlbarTestUtils.promisePopupClose(window, () =>
      click(result3.element.row, { y: -2 })
    );
    info("Waiting for load after performing click relative to index 3");
    await loadPromise;
    Assert.equal(gBrowser.currentURI.spec, url2, "Loaded URL at index 2");
    // Now do the search again.
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: "test",
    });

    await checkLabels(MAX_RESULTS, {
      1: FIREFOX_SUGGEST_LABEL,
    });

    // Check the result at index 1, the one with the label.
    let result1 = await UrlbarTestUtils.getDetailsOfResultAt(window, 1);
    Assert.ok(result1.url, "Result at index 1 has a URL");
    let url1 = result1.url;
    Assert.ok(
      url1.startsWith("http://example.com/"),
      "Result at index 1 is one of our mock history results"
    );
    Assert.notEqual(url1, url2, "URLs at indexes 1 and 2 are different");

    // Do a click on the row at index 1 in the same way as before. This time
    // nothing should happen because the click should hit the label, not the
    // row at index 0.
    info("Clicking row label at index 1");
    click(result1.element.row, { y: -2 });
    // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
    await new Promise(r => setTimeout(r, 500));
    Assert.ok(UrlbarTestUtils.isPopupOpen(window), "View remains open");
    Assert.equal(
      gBrowser.currentURI.spec,
      url2,
      "Current URL is still URL from index 2"
    );

    // Now click the main part of the row at index 1. Its URL should load.
    loadPromise = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
    let { height } = result1.element.row.getBoundingClientRect();
    info(`Clicking main part of the row at index 1, height=${height}`);
    await UrlbarTestUtils.promisePopupClose(window, () =>
      click(result1.element.row)
    );
    info("Waiting for load after clicking row at index 1");
    await loadPromise;
    Assert.equal(gBrowser.currentURI.spec, url1, "Loaded URL at index 1");
  });
});

add_task(async function ariaLabel() {
  const helpUrl = "http://example.com/help";
  const results = [
    new UrlbarResult(
      UrlbarUtils.RESULT_TYPE.URL,
      UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
      { url: "http://example.com/1", helpUrl }
    ),
    new UrlbarResult(
      UrlbarUtils.RESULT_TYPE.URL,
      UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
      { url: "http://example.com/2", helpUrl }
    ),
    new UrlbarResult(
      UrlbarUtils.RESULT_TYPE.URL,
      UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
      { url: "http://example.com/3" }
    ),
  ];

  for (let i = 0; i < results.length; i++) {
    results[i].suggestedIndex = i;
  }

  const provider = new UrlbarTestUtils.TestProvider({
    results,
    priority: Infinity,
  });
  UrlbarProvidersManager.registerProvider(provider);

  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "test",
  });
  await checkLabels(results.length, {
    0: FIREFOX_SUGGEST_LABEL,
  });

  const expectedRows = [
    { hasGroupAriaLabel: true, ariaLabel: FIREFOX_SUGGEST_LABEL },
    { hasGroupAriaLabel: false },
    { hasGroupAriaLabel: false },
  ];
  await checkGroupAriaLabels(expectedRows);

  await UrlbarTestUtils.promisePopupClose(window);

  UrlbarProvidersManager.unregisterProvider(provider);
});

/**
 * Provider that returns a suggested-index result.
 */
class SuggestedIndexProvider extends UrlbarTestUtils.TestProvider {
  constructor(suggestedIndex) {
    super({
      results: [
        Object.assign(
          new UrlbarResult(
            UrlbarUtils.RESULT_TYPE.URL,
            UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
            { url: "http://example.com/" }
          ),
          { suggestedIndex }
        ),
      ],
    });
  }
}

async function addHistory() {
  for (let i = 0; i < MAX_RESULTS; i++) {
    await PlacesTestUtils.addVisits("http://example.com/" + i);
  }
}

/**
 * Asserts that each result in the view does or doesn't have a label, depending
 * on `labelsByIndex`.
 *
 * @param {number} resultCount
 *   The expected number of results. Pass -1 to use the max index in
 *   `labelsByIndex` or the actual result count if `labelsByIndex` is empty.
 * @param {object} labelsByIndex
 *   A mapping from indexes to expected labels.
 */
async function checkLabels(resultCount, labelsByIndex) {
  if (resultCount >= 0) {
    Assert.equal(
      UrlbarTestUtils.getResultCount(window),
      resultCount,
      "Expected result count"
    );
  } else {
    // This `else` branch is only necessary because waiting for all top sites to
    // be added intermittently times out. Don't let the test fail for such a
    // dumb reason.
    let indexes = Object.keys(labelsByIndex);
    if (indexes.length) {
      resultCount = indexes.sort((a, b) => b - a)[0] + 1;
    } else {
      resultCount = UrlbarTestUtils.getResultCount(window);
      Assert.greater(resultCount, 0, "Actual result count is > 0");
    }
  }
  for (let i = 0; i < resultCount; i++) {
    let result = await UrlbarTestUtils.getDetailsOfResultAt(window, i);
    let { row } = result.element;
    let before = getComputedStyle(row, "::before");
    if (labelsByIndex.hasOwnProperty(i)) {
      Assert.equal(
        before.content,
        "attr(label)",
        `::before.content is correct at index ${i}`
      );
      Assert.equal(
        row.getAttribute("label"),
        labelsByIndex[i],
        `Row has correct label at index ${i}`
      );
    } else {
      Assert.equal(
        before.content,
        "none",
        `::before.content is 'none' at index ${i}`
      );
      Assert.ok(
        !row.hasAttribute("label"),
        `Row does not have label attribute at index ${i}`
      );
    }
  }
}

/**
 * Asserts that an element for group aria label.
 *
 * @param {Array} expectedRows The expected rows.
 */
async function checkGroupAriaLabels(expectedRows) {
  Assert.equal(
    UrlbarTestUtils.getResultCount(window),
    expectedRows.length,
    "Expected result count"
  );

  for (let i = 0; i < expectedRows.length; i++) {
    const result = await UrlbarTestUtils.getDetailsOfResultAt(window, i);
    const { row } = result.element;
    const groupAriaLabel = row.querySelector(".urlbarView-group-aria-label");

    const expected = expectedRows[i];

    Assert.equal(
      !!groupAriaLabel,
      expected.hasGroupAriaLabel,
      `Group aria label exists as expected in the results[${i}]`
    );

    if (expected.hasGroupAriaLabel) {
      Assert.equal(
        groupAriaLabel.getAttribute("aria-label"),
        expected.ariaLabel,
        `Content of aria-label attribute in the element for group aria label in the results[${i}] is correct`
      );
    }
  }
}

function engineSuggestionsLabel(engineName) {
  return ENGINE_SUGGESTIONS_LABEL.replace("%s", engineName);
}

/**
 * Adds a search engine that provides suggestions, calls your callback, and then
 * remove the engine.
 *
 * @param {Function} callback
 *   Your callback function.
 * @param {string} [engineBasename]
 *   The basename of the engine file.
 */
async function withSuggestions(
  callback,
  engineBasename = TEST_ENGINE_BASENAME
) {
  await SpecialPowers.pushPrefEnv({
    set: [[SUGGESTIONS_PREF, true]],
  });
  let engine = await SearchTestUtils.promiseNewSearchEngine({
    url: getRootDirectory(gTestPath) + engineBasename,
  });
  let oldDefaultEngine = await Services.search.getDefault();
  await Services.search.setDefault(
    engine,
    Ci.nsISearchService.CHANGE_REASON_UNKNOWN
  );
  try {
    await callback(engine);
  } finally {
    await Services.search.setDefault(
      oldDefaultEngine,
      Ci.nsISearchService.CHANGE_REASON_UNKNOWN
    );
    await Services.search.removeEngine(engine);
    await SpecialPowers.popPrefEnv();
  }
}

function click(element, { x = undefined, y = undefined } = {}) {
  let { width, height } = element.getBoundingClientRect();
  if (typeof x != "number") {
    x = width / 2;
  }
  if (typeof y != "number") {
    y = height / 2;
  }
  EventUtils.synthesizeMouse(element, x, y, { type: "mousedown" });
  EventUtils.synthesizeMouse(element, x, y, { type: "mouseup" });
}