tor-browser

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

browser_jsterm_autocomplete_width.js (2583B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Test that the autocomplete popup is resized when needed.
      7 
      8 const TEST_URI = `data:text/html;charset=utf-8,<!DOCTYPE html>
      9 <head>
     10  <script>
     11    /* Create prototype-less object so popup does not contain native
     12     * Object prototype properties.
     13     */
     14    window.xx = Object.create(null, Object.getOwnPropertyDescriptors({
     15      ["y".repeat(10)]: 1,
     16      ["z".repeat(20)]: 2
     17    }));
     18    window.xxx = 1;
     19  </script>
     20 </head>
     21 <body>Test</body>`;
     22 
     23 add_task(async function () {
     24  const hud = await openNewTabAndConsole(TEST_URI);
     25  const { jsterm } = hud;
     26  const { autocompletePopup: popup } = jsterm;
     27 
     28  info(`wait for completion suggestions for "xx"`);
     29  await setInputValueForAutocompletion(hud, "xx");
     30  ok(popup.isOpen, "popup is open");
     31 
     32  const expectedPopupItems = ["xx", "xxx"];
     33  ok(
     34    hasExactPopupLabels(popup, expectedPopupItems),
     35    "popup has expected items"
     36  );
     37 
     38  const originalWidth = popup.tooltip.container.clientWidth;
     39  Assert.greaterOrEqual(
     40    originalWidth,
     41    getLongestLabelWidth(jsterm),
     42    `popup (${originalWidth}px) is at least wider than the width of the longest list item (${getLongestLabelWidth(
     43      jsterm
     44    )}px)`
     45  );
     46 
     47  info(`wait for completion suggestions for "xx."`);
     48  let onAutocompleteUpdated = jsterm.once("autocomplete-updated");
     49  EventUtils.sendString(".");
     50  await onAutocompleteUpdated;
     51 
     52  ok(
     53    hasExactPopupLabels(popup, ["y".repeat(10), "z".repeat(20)]),
     54    "popup has expected items"
     55  );
     56  const newPopupWidth = popup.tooltip.container.clientWidth;
     57  Assert.greaterOrEqual(
     58    newPopupWidth,
     59    originalWidth,
     60    `The popup width was updated (${originalWidth}px -> ${newPopupWidth}px)`
     61  );
     62  Assert.greaterOrEqual(
     63    newPopupWidth,
     64    getLongestLabelWidth(jsterm),
     65    `popup (${newPopupWidth}px) is at least wider than the width of the longest list item (${getLongestLabelWidth(
     66      jsterm
     67    )}px)`
     68  );
     69 
     70  info(`wait for completion suggestions for "xx"`);
     71  onAutocompleteUpdated = jsterm.once("autocomplete-updated");
     72  EventUtils.synthesizeKey("KEY_Backspace");
     73  await onAutocompleteUpdated;
     74 
     75  is(
     76    popup.tooltip.container.clientWidth,
     77    originalWidth,
     78    "popup is back to its original width"
     79  );
     80 
     81  info("Close autocomplete popup");
     82  await closeAutocompletePopup(hud);
     83 });
     84 
     85 function getLongestLabelWidth(jsterm) {
     86  return (
     87    jsterm._inputCharWidth *
     88    getAutocompletePopupLabels(jsterm.autocompletePopup).sort(
     89      (a, b) => a < b
     90    )[0].length
     91  );
     92 }