tor-browser

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

browser_editor_autocomplete_events.js (4552B)


      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 =
      7  "data:text/html;charset=UTF-8,<html><body><bar></bar>" +
      8  "<div id='baz'></div><body></html>";
      9 
     10 add_task(async function () {
     11  await addTab(TEST_URI);
     12  await runTests();
     13 });
     14 
     15 async function runTests() {
     16  const target = await createAndAttachTargetForTab(gBrowser.selectedTab);
     17  const inspector = await target.getFront("inspector");
     18  const walker = inspector.walker;
     19  const cssPropertiesFront = await target.getFront("cssProperties");
     20  const { ed, win, edWin } = await setup({
     21    autocomplete: true,
     22    mode: Editor.modes.css,
     23    autocompleteOpts: {
     24      walker,
     25      cssProperties: cssPropertiesFront.cssProperties,
     26    },
     27  });
     28  await testMouse(ed, edWin);
     29  await testKeyboard(ed, edWin);
     30  await testKeyboardCycle(ed, edWin);
     31  await testKeyboardCycleForPrefixedString(ed, edWin);
     32  await testKeyboardCSSComma(ed, edWin);
     33  await testCloseOnEscape(ed, edWin);
     34  teardown(ed, win);
     35 }
     36 
     37 async function testKeyboard(ed, win) {
     38  ed.focus();
     39  ed.setText("b");
     40  ed.setCursor({ line: 1, ch: 1 });
     41 
     42  const popupOpened = ed.getAutocompletionPopup().once("popup-opened");
     43 
     44  const autocompleteKey = Editor.keyFor("autocompletion", {
     45    noaccel: true,
     46  }).toUpperCase();
     47  EventUtils.synthesizeKey("VK_" + autocompleteKey, { ctrlKey: true }, win);
     48 
     49  info("Waiting for popup to be opened");
     50  await popupOpened;
     51 
     52  EventUtils.synthesizeKey("VK_RETURN", {}, win);
     53  is(ed.getText(), "bar", "Editor text has been updated");
     54 }
     55 
     56 async function testKeyboardCycle(ed, win) {
     57  ed.focus();
     58  ed.setText("b");
     59  ed.setCursor({ line: 1, ch: 1 });
     60 
     61  const popupOpened = ed.getAutocompletionPopup().once("popup-opened");
     62 
     63  const autocompleteKey = Editor.keyFor("autocompletion", {
     64    noaccel: true,
     65  }).toUpperCase();
     66  EventUtils.synthesizeKey("VK_" + autocompleteKey, { ctrlKey: true }, win);
     67 
     68  info("Waiting for popup to be opened");
     69  await popupOpened;
     70 
     71  EventUtils.synthesizeKey("VK_DOWN", {}, win);
     72  is(ed.getText(), "bar", "Editor text has been updated");
     73 
     74  EventUtils.synthesizeKey("VK_DOWN", {}, win);
     75  is(ed.getText(), "body", "Editor text has been updated");
     76 
     77  EventUtils.synthesizeKey("VK_DOWN", {}, win);
     78  is(ed.getText(), "#baz", "Editor text has been updated");
     79 }
     80 
     81 async function testKeyboardCycleForPrefixedString(ed, win) {
     82  ed.focus();
     83  ed.setText("#b");
     84  ed.setCursor({ line: 1, ch: 2 });
     85 
     86  const popupOpened = ed.getAutocompletionPopup().once("popup-opened");
     87 
     88  const autocompleteKey = Editor.keyFor("autocompletion", {
     89    noaccel: true,
     90  }).toUpperCase();
     91  EventUtils.synthesizeKey("VK_" + autocompleteKey, { ctrlKey: true }, win);
     92 
     93  info("Waiting for popup to be opened");
     94  await popupOpened;
     95 
     96  EventUtils.synthesizeKey("VK_DOWN", {}, win);
     97  is(ed.getText(), "#baz", "Editor text has been updated");
     98 }
     99 
    100 async function testKeyboardCSSComma(ed, win) {
    101  ed.focus();
    102  ed.setText("b");
    103  ed.setCursor({ line: 1, ch: 1 });
    104 
    105  let isPopupOpened = false;
    106  const popupOpened = ed.getAutocompletionPopup().once("popup-opened");
    107  popupOpened.then(() => {
    108    isPopupOpened = true;
    109  });
    110 
    111  EventUtils.synthesizeKey(",", {}, win);
    112 
    113  await wait(500);
    114 
    115  ok(!isPopupOpened, "Autocompletion shouldn't be opened");
    116 }
    117 
    118 async function testMouse(ed, win) {
    119  ed.focus();
    120  ed.setText("b");
    121  ed.setCursor({ line: 1, ch: 1 });
    122 
    123  const popupOpened = ed.getAutocompletionPopup().once("popup-opened");
    124 
    125  const autocompleteKey = Editor.keyFor("autocompletion", {
    126    noaccel: true,
    127  }).toUpperCase();
    128  EventUtils.synthesizeKey("VK_" + autocompleteKey, { ctrlKey: true }, win);
    129 
    130  info("Waiting for popup to be opened");
    131  await popupOpened;
    132  ed.getAutocompletionPopup().list.children[2].click();
    133  is(ed.getText(), "#baz", "Editor text has been updated");
    134 }
    135 
    136 async function testCloseOnEscape(ed, win) {
    137  ed.focus();
    138  ed.setText("b");
    139  ed.setCursor({ line: 1, ch: 1 });
    140 
    141  const popupOpened = ed.getAutocompletionPopup().once("popup-opened");
    142 
    143  const autocompleteKey = Editor.keyFor("autocompletion", {
    144    noaccel: true,
    145  }).toUpperCase();
    146  EventUtils.synthesizeKey("VK_" + autocompleteKey, { ctrlKey: true }, win);
    147 
    148  info("Waiting for popup to be opened");
    149  await popupOpened;
    150 
    151  is(ed.getAutocompletionPopup().isOpen, true, "The popup is open");
    152 
    153  const popupClosed = ed.getAutocompletionPopup().once("popup-closed");
    154  EventUtils.synthesizeKey("VK_ESCAPE", {}, win);
    155 
    156  await popupClosed;
    157  is(ed.getAutocompletionPopup().isOpen, false, "Escape key closed popup");
    158 }