tor-browser

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

browser_jsterm_autocomplete_arrow_keys.js (6770B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const TEST_URI = `data:text/html;charset=utf-8,<!DOCTYPE html><head><script>
      7    /* Create a prototype-less object so popup does not contain native
      8     * Object prototype properties.
      9     */
     10    window.foo = Object.create(null, Object.getOwnPropertyDescriptors({
     11      aa: "a",
     12      bbb: "b",
     13      bbbb: "b",
     14    }));
     15  </script></head><body>Autocomplete text navigation key usage test</body>`;
     16 
     17 add_task(async function () {
     18  const hud = await openNewTabAndConsole(TEST_URI);
     19  const { jsterm } = hud;
     20  const { autocompletePopup: popup } = jsterm;
     21 
     22  await checkWordNavigation(hud);
     23  await checkArrowLeftDismissPopup(hud);
     24  await checkArrowLeftDismissCompletion(hud);
     25  await checkArrowRightAcceptCompletion(hud);
     26 
     27  info(
     28    "Test that Ctrl/Cmd + Right closes the popup if there's text after cursor"
     29  );
     30  setInputValue(hud, ".");
     31  EventUtils.synthesizeKey("KEY_ArrowLeft");
     32  const onAutocompleteUpdated = jsterm.once("autocomplete-updated");
     33  EventUtils.sendString("win");
     34  await onAutocompleteUpdated;
     35  ok(popup.isOpen, "popup is open");
     36 
     37  const isOSX = Services.appinfo.OS == "Darwin";
     38  const onPopUpClose = popup.once("popup-closed");
     39  EventUtils.synthesizeKey("KEY_ArrowRight", {
     40    [isOSX ? "metaKey" : "ctrlKey"]: true,
     41  });
     42  await onPopUpClose;
     43  is(getInputValue(hud), "win.", "input value wasn't modified");
     44 });
     45 
     46 async function checkArrowLeftDismissPopup(hud) {
     47  const popup = hud.jsterm.autocompletePopup;
     48  let tests;
     49  if (Services.appinfo.OS == "Darwin") {
     50    tests = [
     51      {
     52        keyOption: null,
     53        expectedInput: "window.foo.b|b",
     54      },
     55      {
     56        keyOption: { metaKey: true },
     57        expectedInput: "|window.foo.bb",
     58      },
     59      {
     60        keyOption: { altKey: true },
     61        expectedInput: "window.foo.|bb",
     62      },
     63    ];
     64  } else {
     65    tests = [
     66      {
     67        keyOption: null,
     68        expectedInput: "window.foo.b|b",
     69      },
     70      {
     71        keyOption: { ctrlKey: true },
     72        expectedInput: "window.foo.|bb",
     73      },
     74    ];
     75  }
     76 
     77  for (const test of tests) {
     78    info("Trigger autocomplete popup opening");
     79    const onPopUpOpen = popup.once("popup-opened");
     80    await setInputValueForAutocompletion(hud, "window.foo.bb");
     81    await onPopUpOpen;
     82 
     83    // checkInput is asserting the cursor position with the "|" char.
     84    checkInputValueAndCursorPosition(hud, "window.foo.bb|");
     85    is(popup.isOpen, true, "popup is open");
     86    checkInputCompletionValue(hud, "b", "completeNode has expected value");
     87 
     88    const { keyOption, expectedInput } = test;
     89    info(`Test that arrow left closes the popup and clears complete node`);
     90    const onPopUpClose = popup.once("popup-closed");
     91    EventUtils.synthesizeKey("KEY_ArrowLeft", keyOption);
     92    await onPopUpClose;
     93 
     94    checkInputValueAndCursorPosition(hud, expectedInput);
     95    is(popup.isOpen, false, "popup is closed");
     96    checkInputCompletionValue(hud, "", "completeNode is empty");
     97  }
     98  setInputValue(hud, "");
     99 }
    100 
    101 async function checkArrowLeftDismissCompletion(hud) {
    102  let tests;
    103  if (Services.appinfo.OS == "Darwin") {
    104    tests = [
    105      {
    106        keyOption: null,
    107        expectedInput: "window.foo.|a",
    108      },
    109      {
    110        keyOption: { metaKey: true },
    111        expectedInput: "|window.foo.a",
    112      },
    113      {
    114        keyOption: { altKey: true },
    115        expectedInput: "window.foo.|a",
    116      },
    117    ];
    118  } else {
    119    tests = [
    120      {
    121        keyOption: null,
    122        expectedInput: "window.foo.|a",
    123      },
    124      {
    125        keyOption: { ctrlKey: true },
    126        expectedInput: "window.foo.|a",
    127      },
    128    ];
    129  }
    130 
    131  for (const test of tests) {
    132    await setInputValueForAutocompletion(hud, "window.foo.a");
    133    checkInputCompletionValue(hud, "a", "completeNode has expected value");
    134 
    135    info(`Test that arrow left dismiss the completion text`);
    136    const { keyOption, expectedInput } = test;
    137    EventUtils.synthesizeKey("KEY_ArrowLeft", keyOption);
    138 
    139    checkInputValueAndCursorPosition(hud, expectedInput);
    140    checkInputCompletionValue(hud, "", "completeNode is empty");
    141  }
    142  setInputValue(hud, "");
    143 }
    144 
    145 async function checkArrowRightAcceptCompletion(hud) {
    146  const popup = hud.jsterm.autocompletePopup;
    147  let tests;
    148  if (Services.appinfo.OS == "Darwin") {
    149    tests = [
    150      {
    151        keyOption: null,
    152      },
    153      {
    154        keyOption: { metaKey: true },
    155      },
    156      {
    157        keyOption: { altKey: true },
    158      },
    159    ];
    160  } else {
    161    tests = [
    162      {
    163        keyOption: null,
    164      },
    165      {
    166        keyOption: { ctrlKey: true },
    167      },
    168    ];
    169  }
    170 
    171  for (const test of tests) {
    172    info("Trigger autocomplete popup opening");
    173    const onPopUpOpen = popup.once("popup-opened");
    174    await setInputValueForAutocompletion(hud, `window.foo.bb`);
    175    await onPopUpOpen;
    176 
    177    // checkInput is asserting the cursor position with the "|" char.
    178    checkInputValueAndCursorPosition(hud, `window.foo.bb|`);
    179    is(popup.isOpen, true, "popup is open");
    180    checkInputCompletionValue(hud, "b", "completeNode has expected value");
    181 
    182    const { keyOption } = test;
    183    info(`Test that arrow right closes the popup and accepts the completion`);
    184    const onPopUpClose = popup.once("popup-closed");
    185    EventUtils.synthesizeKey("KEY_ArrowRight", keyOption);
    186    await onPopUpClose;
    187 
    188    checkInputValueAndCursorPosition(hud, "window.foo.bbb|");
    189    is(popup.isOpen, false, "popup is closed");
    190    checkInputCompletionValue(hud, "", "completeNode is empty");
    191  }
    192  setInputValue(hud, "");
    193 }
    194 
    195 async function checkWordNavigation(hud) {
    196  const accelKey = Services.appinfo.OS == "Darwin" ? "altKey" : "ctrlKey";
    197  const goLeft = () =>
    198    EventUtils.synthesizeKey("KEY_ArrowLeft", { [accelKey]: true });
    199  const goRight = () =>
    200    EventUtils.synthesizeKey("KEY_ArrowRight", { [accelKey]: true });
    201 
    202  setInputValue(hud, "aa bb cc dd");
    203  checkInputValueAndCursorPosition(hud, "aa bb cc dd|");
    204 
    205  goRight();
    206  checkInputValueAndCursorPosition(hud, "aa bb cc dd|");
    207 
    208  goLeft();
    209  checkInputValueAndCursorPosition(hud, "aa bb cc |dd");
    210 
    211  goLeft();
    212  checkInputValueAndCursorPosition(hud, "aa bb |cc dd");
    213 
    214  goLeft();
    215  checkInputValueAndCursorPosition(hud, "aa |bb cc dd");
    216 
    217  goLeft();
    218  checkInputValueAndCursorPosition(hud, "|aa bb cc dd");
    219 
    220  goLeft();
    221  checkInputValueAndCursorPosition(hud, "|aa bb cc dd");
    222 
    223  goRight();
    224  // Windows differ from other platforms, going to the start of the next string.
    225  checkInputValueAndCursorPosition(hud, "aa| bb cc dd");
    226 
    227  goRight();
    228  checkInputValueAndCursorPosition(hud, "aa bb| cc dd");
    229 
    230  goRight();
    231  checkInputValueAndCursorPosition(hud, "aa bb cc| dd");
    232 
    233  goRight();
    234  checkInputValueAndCursorPosition(hud, "aa bb cc dd|");
    235 
    236  setInputValue(hud, "");
    237 }