tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_bestMatch.js (5459B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Tests best match rows in the view.
      5 
      6 "use strict";
      7 
      8 // Tests a non-sponsored best match row.
      9 add_task(async function nonsponsored() {
     10  let result = makeBestMatchResult();
     11  await withProvider(result, async () => {
     12    await UrlbarTestUtils.promiseAutocompleteResultPopup({
     13      window,
     14      value: "test",
     15    });
     16    await checkBestMatchRow({ result });
     17    await UrlbarTestUtils.promisePopupClose(window);
     18  });
     19 });
     20 
     21 // Tests a non-sponsored best match row with a help button.
     22 add_task(async function nonsponsoredHelpButton() {
     23  let result = makeBestMatchResult({ helpUrl: "https://example.com/help" });
     24  await withProvider(result, async () => {
     25    await UrlbarTestUtils.promiseAutocompleteResultPopup({
     26      window,
     27      value: "test",
     28    });
     29    await checkBestMatchRow({ result, hasHelpUrl: true });
     30    await UrlbarTestUtils.promisePopupClose(window);
     31  });
     32 });
     33 
     34 // Tests a sponsored best match row.
     35 add_task(async function sponsored() {
     36  let result = makeBestMatchResult({ isSponsored: true });
     37  await withProvider(result, async () => {
     38    await UrlbarTestUtils.promiseAutocompleteResultPopup({
     39      window,
     40      value: "test",
     41    });
     42    await checkBestMatchRow({ result, isSponsored: true });
     43    await UrlbarTestUtils.promisePopupClose(window);
     44  });
     45 });
     46 
     47 // Tests a sponsored best match row with a help button.
     48 add_task(async function sponsoredHelpButton() {
     49  let result = makeBestMatchResult({
     50    isSponsored: true,
     51    helpUrl: "https://example.com/help",
     52  });
     53  await withProvider(result, async () => {
     54    await UrlbarTestUtils.promiseAutocompleteResultPopup({
     55      window,
     56      value: "test",
     57    });
     58    await checkBestMatchRow({ result, isSponsored: true, hasHelpUrl: true });
     59    await UrlbarTestUtils.promisePopupClose(window);
     60  });
     61 });
     62 
     63 // Tests keyboard selection.
     64 add_task(async function keySelection() {
     65  let result = makeBestMatchResult({
     66    isSponsored: true,
     67    helpUrl: "https://example.com/help",
     68  });
     69 
     70  await withProvider(result, async () => {
     71    // Ordered list of class names of the elements that should be selected.
     72    let expectedClassNames = [
     73      "urlbarView-row-inner",
     74      "urlbarView-button-result-menu",
     75    ];
     76 
     77    await UrlbarTestUtils.promiseAutocompleteResultPopup({
     78      window,
     79      value: "test",
     80    });
     81    await checkBestMatchRow({
     82      result,
     83      isSponsored: true,
     84      hasHelpUrl: true,
     85    });
     86 
     87    // Test with the tab key in order vs. reverse order.
     88    for (let reverse of [false, true]) {
     89      info("Doing TAB key selection: " + JSON.stringify({ reverse }));
     90 
     91      let classNames = [...expectedClassNames];
     92      if (reverse) {
     93        classNames.reverse();
     94      }
     95 
     96      let sendKey = () => {
     97        EventUtils.synthesizeKey("KEY_Tab", { shiftKey: reverse });
     98      };
     99 
    100      // Move selection through each expected element.
    101      for (let className of classNames) {
    102        info("Expecting selection: " + className);
    103        sendKey();
    104        Assert.ok(gURLBar.view.isOpen, "View remains open");
    105        let { selectedElement } = gURLBar.view;
    106        Assert.ok(selectedElement, "Selected element exists");
    107        Assert.ok(
    108          selectedElement.classList.contains(className),
    109          "Expected element is selected"
    110        );
    111      }
    112      sendKey();
    113      Assert.ok(
    114        gURLBar.view.isOpen,
    115        "View remains open after keying through best match row"
    116      );
    117    }
    118 
    119    await UrlbarTestUtils.promisePopupClose(window);
    120  });
    121 });
    122 
    123 async function checkBestMatchRow({ result, hasHelpUrl = false }) {
    124  Assert.equal(
    125    UrlbarTestUtils.getResultCount(window),
    126    1,
    127    "One result is present"
    128  );
    129 
    130  let details = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
    131  let { row } = details.element;
    132 
    133  let favicon = row._elements.get("favicon");
    134  Assert.ok(favicon, "Row has a favicon");
    135 
    136  let title = row._elements.get("title");
    137  Assert.ok(title, "Row has a title");
    138  Assert.ok(title.textContent, "Row title has non-empty textContext");
    139  Assert.equal(title.textContent, result.payload.title, "Row title is correct");
    140 
    141  let url = row._elements.get("url");
    142  Assert.ok(url, "Row has a URL");
    143  Assert.ok(url.textContent, "Row URL has non-empty textContext");
    144  Assert.equal(
    145    url.textContent,
    146    result.getDisplayableValueAndHighlights("url", { isURL: true }).value,
    147    "Row URL is correct"
    148  );
    149 
    150  let button = row._buttons.get("result-menu");
    151  Assert.equal(
    152    !!result.payload.helpUrl,
    153    hasHelpUrl,
    154    "Sanity check: Row's expected hasHelpUrl matches result"
    155  );
    156  if (hasHelpUrl) {
    157    Assert.ok(button, "Row with helpUrl has a help or menu button");
    158  } else {
    159    Assert.ok(
    160      !button,
    161      "Row without helpUrl does not have a help or menu button"
    162    );
    163  }
    164 }
    165 
    166 async function withProvider(result, callback) {
    167  let provider = new UrlbarTestUtils.TestProvider({
    168    results: [result],
    169    priority: Infinity,
    170  });
    171  UrlbarProvidersManager.registerProvider(provider);
    172  try {
    173    await callback();
    174  } finally {
    175    UrlbarProvidersManager.unregisterProvider(provider);
    176  }
    177 }
    178 
    179 function makeBestMatchResult(payloadExtra = {}) {
    180  return new UrlbarResult({
    181    type: UrlbarUtils.RESULT_TYPE.URL,
    182    source: UrlbarUtils.RESULT_SOURCE.SEARCH,
    183    isBestMatch: true,
    184    payload: {
    185      title: "Test best match",
    186      url: "https://example.com/best-match",
    187      ...payloadExtra,
    188    },
    189  });
    190 }