tor-browser

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

browser_jsterm_completion.js (3975B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Tests that code completion works properly.
      5 
      6 "use strict";
      7 
      8 const TEST_URI = `data:text/html;charset=utf8,<!DOCTYPE html><p>test code completion
      9  <script>
     10    foobar = true;
     11  </script>`;
     12 
     13 add_task(async function () {
     14  const hud = await openNewTabAndConsole(TEST_URI);
     15  const { jsterm } = hud;
     16  const { autocompletePopup } = jsterm;
     17 
     18  // Test typing 'docu'.
     19  await setInputValueForAutocompletion(hud, "foob");
     20  is(getInputValue(hud), "foob", "'foob' completion (input.value)");
     21  checkInputCompletionValue(hud, "ar", "'foob' completion (completeNode)");
     22  is(autocompletePopup.items.length, 1, "autocomplete popup has 1 item");
     23  is(autocompletePopup.isOpen, false, "autocomplete popup is not open");
     24 
     25  // Test typing 'docu' and press tab.
     26  EventUtils.synthesizeKey("KEY_Tab");
     27  is(getInputValue(hud), "foobar", "'foob' tab completion");
     28 
     29  checkInputCursorPosition(
     30    hud,
     31    "foobar".length,
     32    "cursor is at the end of 'foobar'"
     33  );
     34  is(getInputCompletionValue(hud).replace(/ /g, ""), "", "'foob' completed");
     35 
     36  // Test typing 'window.Ob' and press tab.  Just 'window.O' is
     37  // ambiguous: could be window.Object, window.Option, etc.
     38  await setInputValueForAutocompletion(hud, "window.Ob");
     39  EventUtils.synthesizeKey("KEY_Tab");
     40  is(getInputValue(hud), "window.Object", "'window.Ob' tab completion");
     41 
     42  // Test typing 'document.getElem'.
     43  const onPopupOpened = autocompletePopup.once("popup-opened");
     44  await setInputValueForAutocompletion(hud, "document.getElem");
     45  is(getInputValue(hud), "document.getElem", "'document.getElem' completion");
     46  checkInputCompletionValue(hud, "entById", "'document.getElem' completion");
     47 
     48  // Test pressing key down.
     49  await onPopupOpened;
     50  EventUtils.synthesizeKey("KEY_ArrowDown");
     51  is(getInputValue(hud), "document.getElem", "'document.getElem' completion");
     52  checkInputCompletionValue(
     53    hud,
     54    "entsByClassName",
     55    "'document.getElem' another tab completion"
     56  );
     57 
     58  // Test pressing key up.
     59  EventUtils.synthesizeKey("KEY_ArrowUp");
     60  await waitFor(() => (getInputCompletionValue(hud) || "").includes("entById"));
     61  is(
     62    getInputValue(hud),
     63    "document.getElem",
     64    "'document.getElem' untab completion"
     65  );
     66  checkInputCompletionValue(hud, "entById", "'document.getElem' completion");
     67 
     68  await clearOutput(hud);
     69 
     70  await setInputValueForAutocompletion(hud, "docu");
     71  checkInputCompletionValue(hud, "ment", "'docu' completion");
     72 
     73  let onAutocompletUpdated = jsterm.once("autocomplete-updated");
     74  EventUtils.synthesizeKey("KEY_Enter");
     75  await onAutocompletUpdated;
     76  checkInputCompletionValue(hud, "", "clear completion on execute()");
     77 
     78  // Test multi-line completion works. We can't use setInputValueForAutocompletion because
     79  // it would trigger an evaluation (because of the new line, an Enter keypress is
     80  // simulated).
     81  onAutocompletUpdated = jsterm.once("autocomplete-updated");
     82  setInputValue(hud, "console.log('one');\n");
     83  EventUtils.sendString("consol");
     84  await onAutocompletUpdated;
     85  checkInputCompletionValue(hud, "e", "multi-line completion");
     86 
     87  // Test multi-line completion works even if there is text after the cursor
     88  onAutocompletUpdated = jsterm.once("autocomplete-updated");
     89  setInputValue(hud, "{\n\n}");
     90  EventUtils.synthesizeKey("KEY_ArrowUp");
     91  EventUtils.sendString("console.g");
     92  await onAutocompletUpdated;
     93  checkInputValueAndCursorPosition(hud, "{\nconsole.g|\n}");
     94  checkInputCompletionValue(hud, "roup", "multi-line completion");
     95  is(autocompletePopup.isOpen, true, "popup is opened");
     96 
     97  // Test non-object autocompletion.
     98  await setInputValueForAutocompletion(hud, "Object.name.sl");
     99  checkInputCompletionValue(hud, "ice", "non-object completion");
    100 
    101  // Test string literal autocompletion.
    102  await setInputValueForAutocompletion(hud, "'Asimov'.sl");
    103  checkInputCompletionValue(hud, "ice", "string literal completion");
    104 });