tor-browser

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

browser_jsterm_ctrl_key_nav.js (7502B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Test navigation of webconsole contents via ctrl-a, ctrl-e, ctrl-p, ctrl-n
      5 // see https://bugzilla.mozilla.org/show_bug.cgi?id=804845
      6 //
      7 // The shortcuts tested here have platform limitations:
      8 // - ctrl-e does not work on windows,
      9 // - ctrl-a, ctrl-p and ctrl-n only work on OSX
     10 "use strict";
     11 
     12 const TEST_URI =
     13  "data:text/html;charset=utf-8,<!DOCTYPE html>Web Console test for " +
     14  "bug 804845 and bug 619598";
     15 
     16 add_task(async function () {
     17  const hud = await openNewTabAndConsole(TEST_URI);
     18 
     19  ok(!getInputValue(hud), "input is empty");
     20  checkInputCursorPosition(hud, 0, "Cursor is at the start of the input");
     21 
     22  testSingleLineInputNavNoHistory(hud);
     23  testMultiLineInputNavNoHistory(hud);
     24  await testNavWithHistory(hud);
     25 });
     26 
     27 function testSingleLineInputNavNoHistory(hud) {
     28  const checkInput = (expected, assertionInfo) =>
     29    checkInputValueAndCursorPosition(hud, expected, assertionInfo);
     30 
     31  // Single char input
     32  EventUtils.sendString("1");
     33  checkInput("1|", "caret location after single char input");
     34 
     35  // nav to start/end with ctrl-a and ctrl-e;
     36  synthesizeLineStartKey();
     37  checkInput("|1", "caret location after single char input and ctrl-a");
     38 
     39  synthesizeLineEndKey();
     40  checkInput("1|", "caret location after single char input and ctrl-e");
     41 
     42  // Second char input
     43  EventUtils.sendString("2");
     44  checkInput("12|", "caret location after second char input");
     45 
     46  // nav to start/end with up/down keys; verify behaviour using ctrl-p/ctrl-n
     47  EventUtils.synthesizeKey("KEY_ArrowUp");
     48  checkInput("|12", "caret location after two char input and KEY_ArrowUp");
     49 
     50  EventUtils.synthesizeKey("KEY_ArrowDown");
     51  checkInput("12|", "caret location after two char input and KEY_ArrowDown");
     52 
     53  synthesizeLineStartKey();
     54  checkInput("|12", "move caret to beginning of 2 char input with ctrl-a");
     55 
     56  synthesizeLineStartKey();
     57  checkInput("|12", "no change of caret location on repeat ctrl-a");
     58 
     59  synthesizeLineUpKey();
     60  checkInput(
     61    "|12",
     62    "no change of caret location on ctrl-p from beginning of line"
     63  );
     64 
     65  synthesizeLineEndKey();
     66  checkInput("12|", "move caret to end of 2 char input with ctrl-e");
     67 
     68  synthesizeLineEndKey();
     69  checkInput("12|", "no change of caret location on repeat ctrl-e");
     70 
     71  synthesizeLineDownKey();
     72  checkInput("12|", "no change of caret location on ctrl-n from end of line");
     73 
     74  synthesizeLineUpKey();
     75  checkInput("|12", "ctrl-p moves to start of line");
     76 
     77  synthesizeLineDownKey();
     78  checkInput("12|", "ctrl-n moves to end of line");
     79 }
     80 
     81 function testMultiLineInputNavNoHistory(hud) {
     82  const checkInput = (expected, assertionInfo) =>
     83    checkInputValueAndCursorPosition(hud, expected, assertionInfo);
     84 
     85  const lineValues = ["one", "2", "something longer", "", "", "three!"];
     86  setInputValue(hud, "");
     87  // simulate shift-return
     88  for (const lineValue of lineValues) {
     89    setInputValue(hud, getInputValue(hud) + lineValue);
     90    EventUtils.synthesizeKey("KEY_Enter", { shiftKey: true });
     91  }
     92 
     93  checkInput(
     94    `one
     95 2
     96 something longer
     97 
     98 
     99 three!
    100 |`,
    101    "caret at end of multiline input"
    102  );
    103 
    104  // Ok, test navigating within the multi-line string!
    105  EventUtils.synthesizeKey("KEY_ArrowUp");
    106  checkInput(
    107    `one
    108 2
    109 something longer
    110 
    111 
    112 |three!
    113 `,
    114    "up arrow from end of multiline"
    115  );
    116 
    117  EventUtils.synthesizeKey("KEY_ArrowDown");
    118  checkInput(
    119    `one
    120 2
    121 something longer
    122 
    123 
    124 three!
    125 |`,
    126    "down arrow from within multiline"
    127  );
    128 
    129  // navigate up through input lines
    130  synthesizeLineUpKey();
    131  checkInput(
    132    `one
    133 2
    134 something longer
    135 
    136 
    137 |three!
    138 `,
    139    "ctrl-p from end of multiline"
    140  );
    141 
    142  for (let i = 0; i < 5; i++) {
    143    synthesizeLineUpKey();
    144  }
    145 
    146  checkInput(
    147    `|one
    148 2
    149 something longer
    150 
    151 
    152 three!
    153 `,
    154    "reached start of input"
    155  );
    156 
    157  synthesizeLineUpKey();
    158  checkInput(
    159    `|one
    160 2
    161 something longer
    162 
    163 
    164 three!
    165 `,
    166    "no change to multiline input on ctrl-p from beginning of multiline"
    167  );
    168 
    169  // navigate to end of first line
    170  synthesizeLineEndKey();
    171  checkInput(
    172    `one|
    173 2
    174 something longer
    175 
    176 
    177 three!
    178 `,
    179    "ctrl-e into multiline input"
    180  );
    181 
    182  synthesizeLineEndKey();
    183  checkInput(
    184    `one|
    185 2
    186 something longer
    187 
    188 
    189 three!
    190 `,
    191    "repeat ctrl-e doesn't change caret position in multiline input"
    192  );
    193 
    194  synthesizeLineDownKey();
    195  synthesizeLineStartKey();
    196  checkInput(
    197    `one
    198 |2
    199 something longer
    200 
    201 
    202 three!
    203 `
    204  );
    205 
    206  synthesizeLineEndKey();
    207  synthesizeLineDownKey();
    208  synthesizeLineStartKey();
    209  checkInput(
    210    `one
    211 2
    212 |something longer
    213 
    214 
    215 three!
    216 `
    217  );
    218 }
    219 
    220 async function testNavWithHistory(hud) {
    221  const checkInput = (expected, assertionInfo) =>
    222    checkInputValueAndCursorPosition(hud, expected, assertionInfo);
    223 
    224  // NOTE: Tests does NOT currently define behaviour for ctrl-p/ctrl-n with
    225  // caret placed _within_ single line input
    226  const values = [
    227    "single line input",
    228    "a longer single-line input to check caret repositioning",
    229    "multi-line\ninput\nhere",
    230  ];
    231 
    232  // submit to history
    233  for (const value of values) {
    234    const onResult = waitForMessageByType(hud, "", ".result");
    235    setInputValue(hud, value);
    236    EventUtils.synthesizeKey("KEY_Enter");
    237    await onResult;
    238  }
    239 
    240  checkInput("|", "caret location at start of empty line");
    241 
    242  synthesizeLineUpKey();
    243  checkInput(
    244    "multi-line\ninput\nhere|",
    245    "caret location at end of last history input"
    246  );
    247 
    248  synthesizeLineStartKey();
    249  checkInput(
    250    "multi-line\ninput\n|here",
    251    "caret location at beginning of last line of last history input"
    252  );
    253 
    254  synthesizeLineUpKey();
    255  checkInput(
    256    "multi-line\n|input\nhere",
    257    "caret location at beginning of second line of last history input"
    258  );
    259 
    260  synthesizeLineUpKey();
    261  checkInput(
    262    "|multi-line\ninput\nhere",
    263    "caret location at beginning of first line of last history input"
    264  );
    265 
    266  synthesizeLineUpKey();
    267  checkInput(
    268    "a longer single-line input to check caret repositioning|",
    269    "caret location at the end of second history input"
    270  );
    271 
    272  synthesizeLineUpKey();
    273  checkInput(
    274    "single line input|",
    275    "caret location at the end of first history input"
    276  );
    277 
    278  synthesizeLineUpKey();
    279  checkInput(
    280    "|single line input",
    281    "ctrl-p at beginning of history moves caret location to beginning of line"
    282  );
    283 
    284  synthesizeLineDownKey();
    285  checkInput(
    286    "a longer single-line input to check caret repositioning|",
    287    "caret location at the end of second history input"
    288  );
    289 
    290  synthesizeLineDownKey();
    291  checkInput(
    292    "multi-line\ninput\nhere|",
    293    "caret location at end of last history input"
    294  );
    295 
    296  synthesizeLineDownKey();
    297  checkInput("|", "ctrl-n at end of history updates to empty input");
    298 
    299  // Simulate editing multi-line
    300  const inputValue = "one\nlinebreak";
    301  setInputValue(hud, inputValue);
    302  checkInput("one\nlinebreak|");
    303 
    304  // Attempt nav within input
    305  synthesizeLineUpKey();
    306  checkInput(
    307    "one|\nlinebreak",
    308    "ctrl-p from end of multi-line does not trigger history"
    309  );
    310 
    311  synthesizeLineStartKey();
    312  checkInput("|one\nlinebreak");
    313 
    314  synthesizeLineUpKey();
    315  checkInput(
    316    "multi-line\ninput\nhere|",
    317    "ctrl-p from start of multi-line triggers history"
    318  );
    319 }
    320 
    321 function synthesizeLineStartKey() {
    322  EventUtils.synthesizeKey("a", { ctrlKey: true });
    323 }
    324 
    325 function synthesizeLineEndKey() {
    326  EventUtils.synthesizeKey("e", { ctrlKey: true });
    327 }
    328 
    329 function synthesizeLineUpKey() {
    330  EventUtils.synthesizeKey("p", { ctrlKey: true });
    331 }
    332 
    333 function synthesizeLineDownKey() {
    334  EventUtils.synthesizeKey("n", { ctrlKey: true });
    335 }