tor-browser

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

browser_jsterm_autocomplete_race_on_enter.js (6010B)


      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 pressing Enter quickly after a letter that makes the input exactly match the
      7 // item in the autocomplete popup does not insert unwanted character. See Bug 1595068.
      8 
      9 const TEST_URI = `data:text/html;charset=utf-8,<!DOCTYPE html><script>
     10  var uvwxyz = "zyxwvu";
     11 </script>Autocomplete race on Enter`;
     12 
     13 add_task(async function () {
     14  const hud = await openNewTabAndConsole(TEST_URI);
     15  const { jsterm } = hud;
     16  const { autocompletePopup } = jsterm;
     17 
     18  info(`Enter "scre" and wait for the autocomplete popup to be displayed`);
     19  let onPopupOpened = autocompletePopup.once("popup-opened");
     20  await setInputValueForAutocompletion(hud, "scre");
     21  await onPopupOpened;
     22  checkInputCompletionValue(hud, "en", "completeNode has expected value");
     23 
     24  info(`Type "n" and quickly after, "Enter"`);
     25  let onPopupClosed = autocompletePopup.once("popup-closed");
     26  EventUtils.synthesizeKey("e");
     27  await waitForTime(50);
     28  EventUtils.synthesizeKey("KEY_Enter");
     29  await onPopupClosed;
     30 
     31  is(getInputValue(hud), "screen", "the input has the expected value");
     32 
     33  setInputValue(hud, "");
     34 
     35  info(
     36    "Check that it works when typed word match exactly the item in the popup"
     37  );
     38  onPopupOpened = autocompletePopup.once("popup-opened");
     39  await setInputValueForAutocompletion(hud, "wind");
     40  await onPopupOpened;
     41  checkInputCompletionValue(hud, "ow", "completeNode has expected value");
     42 
     43  info(`Quickly type "o", "w" and "Enter"`);
     44  onPopupClosed = autocompletePopup.once("popup-closed");
     45  EventUtils.synthesizeKey("o");
     46  await waitForTime(5);
     47  EventUtils.synthesizeKey("w");
     48  await waitForTime(5);
     49  EventUtils.synthesizeKey("KEY_Enter");
     50  await onPopupClosed;
     51 
     52  is(getInputValue(hud), "window", "the input has the expected value");
     53 
     54  setInputValue(hud, "");
     55 
     56  info("Check that it works when there's no autocomplete popup");
     57  let onAutocompleteUpdated = jsterm.once("autocomplete-updated");
     58  await setInputValueForAutocompletion(hud, "uvw");
     59  await onAutocompleteUpdated;
     60  checkInputCompletionValue(hud, "xyz", "completeNode has expected value");
     61 
     62  info(`Quickly type "x" and "Enter"`);
     63  EventUtils.synthesizeKey("x");
     64  await waitForTime(5);
     65  EventUtils.synthesizeKey("KEY_Enter");
     66  await waitFor(
     67    () => getInputValue(hud) === "uvwxyz",
     68    "input has expected 'uvwxyz' value"
     69  );
     70  ok(true, "input has the expected value");
     71 
     72  setInputValue(hud, "");
     73 
     74  info(
     75    "Check that it works when there's no autocomplete popup and the whole word is typed"
     76  );
     77  onAutocompleteUpdated = jsterm.once("autocomplete-updated");
     78  await setInputValueForAutocompletion(hud, "uvw");
     79  await onAutocompleteUpdated;
     80  checkInputCompletionValue(hud, "xyz", "completeNode has expected value");
     81 
     82  info(`Quickly type "x", "y", "z" and "Enter"`);
     83  const onResultMessage = waitForMessageByType(hud, "zyxwvu", ".result");
     84  EventUtils.synthesizeKey("x");
     85  await waitForTime(5);
     86  EventUtils.synthesizeKey("y");
     87  await waitForTime(5);
     88  EventUtils.synthesizeKey("z");
     89  await waitForTime(5);
     90  EventUtils.synthesizeKey("KEY_Enter");
     91  info("wait for result message");
     92  await onResultMessage;
     93  is(getInputValue(hud), "", "Expression was evaluated and input was cleared");
     94 
     95  setInputValue(hud, "");
     96 
     97  info("Check that it works when typed letter match another item in the popup");
     98  onPopupOpened = autocompletePopup.once("popup-opened");
     99  await setInputValueForAutocompletion(hud, "[].so");
    100  await onPopupOpened;
    101  checkInputCompletionValue(hud, "me", "completeNode has expected value");
    102  is(
    103    autocompletePopup.items.map(({ label }) => label).join("|"),
    104    "some|sort",
    105    "autocomplete has expected items"
    106  );
    107 
    108  info(`Quickly type "m" and "Enter"`);
    109  onPopupClosed = autocompletePopup.once("popup-closed");
    110  EventUtils.synthesizeKey("m");
    111  await waitForTime(5);
    112  EventUtils.synthesizeKey("KEY_Enter");
    113  await onPopupClosed;
    114  is(getInputValue(hud), "[].some", "the input has the expected value");
    115 
    116  setInputValue(hud, "");
    117 
    118  info(
    119    "Hitting Enter quickly after a letter that should close the popup evaluates the expression"
    120  );
    121  onPopupOpened = autocompletePopup.once("popup-opened");
    122  await setInputValueForAutocompletion(hud, "doc");
    123  await onPopupOpened;
    124  checkInputCompletionValue(hud, "ument", "completeNode has expected value");
    125 
    126  info(`Quickly type "x" and "Enter"`);
    127  onPopupClosed = autocompletePopup.once("popup-closed");
    128  const onMessage = waitForMessageByType(hud, "docx is not defined", ".error");
    129  EventUtils.synthesizeKey("x");
    130  await waitForTime(5);
    131  EventUtils.synthesizeKey("KEY_Enter");
    132 
    133  await Promise.all([onPopupClosed, onMessage]);
    134  is(
    135    getInputValue(hud),
    136    "",
    137    "the input is empty and the expression was evaluated"
    138  );
    139 
    140  info(
    141    "Hitting Enter quickly after a letter that will make the expression exactly match another item than the selected one"
    142  );
    143  onPopupOpened = autocompletePopup.once("popup-opened");
    144  await setInputValueForAutocompletion(hud, "cons");
    145  await onPopupOpened;
    146  checkInputCompletionValue(hud, "ole", "completeNode has expected value");
    147  info(`Quickly type "t" and "Enter"`);
    148  onPopupClosed = autocompletePopup.once("popup-closed");
    149  EventUtils.synthesizeKey("t");
    150  await waitForTime(5);
    151  EventUtils.synthesizeKey("KEY_Enter");
    152  await onPopupClosed;
    153  is(getInputValue(hud), "const", "the input has the expected item");
    154 
    155  info(
    156    "Hitting Enter quickly after a letter when the expression has text after"
    157  );
    158  await setInputValueForAutocompletion(hud, "f(und");
    159  ok(
    160    hasExactPopupLabels(autocompletePopup, ["undefined"]),
    161    `the popup has the "undefined" item`
    162  );
    163  info(`Quickly type "e" and "Enter"`);
    164  onPopupClosed = autocompletePopup.once("popup-closed");
    165  EventUtils.synthesizeKey("e");
    166  await waitForTime(5);
    167  EventUtils.synthesizeKey("KEY_Enter");
    168  await onPopupClosed;
    169  is(getInputValue(hud), "f(undefined)", "the input has the expected item");
    170 });