tor-browser

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

test_input.html (6442B)


      1 <html xmlns="http://www.w3.org/1999/xhtml">
      2 <head>
      3 <title>input key handling</title>
      4 
      5 <script type="text/javascript" src="http://mochi.test:8888/tests/SimpleTest/SimpleTest.js"></script>
      6 <script type="text/javascript" src="http://mochi.test:8888/tests/SimpleTest/EventUtils.js"></script>
      7 <script type="text/javascript" src="http://mochi.test:8888/tests/SimpleTest/NativeKeyCodes.js"></script>
      8 <link rel="stylesheet" type="text/css" href="http://mochi.test:8888/tests/SimpleTest/test.css" />
      9 
     10 <script type="text/javascript">
     11 const IS_MAC = navigator.platform.indexOf("Mac") == 0;
     12 const VK = {};
     13 const CHARS = {};
     14 
     15 if (IS_MAC) {
     16  VK.LEFT = MAC_VK_LeftArrow;
     17  CHARS.LEFT = "\uF702";
     18  VK.RIGHT = MAC_VK_RightArrow;
     19  CHARS.RIGHT = "\uF703";
     20  VK.UP = MAC_VK_UpArrow;
     21  CHARS.UP = "\uF700";
     22  VK.DOWN = MAC_VK_DownArrow;
     23  CHARS.DOWN = "\uF701";
     24  VK.X = MAC_VK_ANSI_X;
     25  VK.V = MAC_VK_ANSI_V;
     26  VK.A = MAC_VK_ANSI_A;
     27  VK.F = MAC_VK_ANSI_F;
     28  VK.O = MAC_VK_ANSI_O;
     29  VK.BACKSPACE = MAC_VK_PC_Backspace;
     30  CHARS.BACKSPACE = "\u007F";
     31  VK.Z = MAC_VK_ANSI_Z;
     32  VK.SPACE = MAC_VK_Space;
     33 } else {
     34  VK.LEFT = WIN_VK_LEFT;
     35  CHARS.LEFT = "";
     36  VK.RIGHT = WIN_VK_RIGHT;
     37  CHARS.RIGHT = "";
     38  VK.UP = WIN_VK_UP;
     39  CHARS.UP = "";
     40  VK.DOWN = WIN_VK_DOWN;
     41  CHARS.DOWN = "";
     42  VK.X = WIN_VK_X;
     43  VK.V = WIN_VK_V;
     44  VK.A = WIN_VK_A;
     45  VK.F = WIN_VK_F;
     46  VK.O = WIN_VK_O;
     47  VK.END = WIN_VK_END;
     48  CHARS.END = "";
     49  VK.HOME = WIN_VK_HOME;
     50  CHARS.HOME = "";
     51  VK.BACKSPACE = WIN_VK_BACK;
     52  CHARS.BACKSPACE = "";
     53  VK.Z = WIN_VK_Z;
     54  VK.SPACE = WIN_VK_SPACE;
     55 }
     56 
     57 if (window.arguments && window.arguments[0]) {
     58  ok = window.arguments[0].ok;
     59  is = window.arguments[0].is;
     60 }
     61 
     62 function synthesizeKey(keyCode, modifiers, chars, event = "keyup") {
     63  return new Promise((resolve, reject) => {
     64    window.addEventListener(event, resolve, { once: true });
     65 
     66    if (!synthesizeNativeKey(KEYBOARD_LAYOUT_EN_US, keyCode, modifiers, chars, chars)) {
     67      reject();
     68    }
     69  });
     70 }
     71 
     72 async function checkElement(element, start, end, content = "Test text") {
     73  isReady = () => {
     74    if (start != element.selectionStart) {
     75      return false;
     76    }
     77    if (end != element.selectionEnd) {
     78      return false;
     79    }
     80    if (content != element.value) {
     81      return false;
     82    }
     83    return true;
     84  };
     85 
     86  for (let i = 0; i < 10; i++) {
     87    if (isReady()) {
     88      return;
     89    }
     90 
     91    SimpleTest.requestFlakyTimeout("Polling for changes to apply");
     92    await new Promise(resolve => setTimeout(resolve, 50));
     93  }
     94  ok(false, "Timed out waiting for state");
     95  is(element.selectionStart, start, "Should have the right selectionStart");
     96  is(element.selectionEnd, end, "Should have the right selectionEnd");
     97  is(element.value, content, "Should have the right value");
     98 }
     99 
    100 async function startTest() {
    101  let input = document.getElementById("input");
    102  input.focus();
    103  await checkElement(input, 0, 0);
    104 
    105  info("right");
    106  await synthesizeKey(VK.RIGHT, {}, CHARS.RIGHT);
    107  await checkElement(input, 1, 1);
    108 
    109  info("shift+right");
    110  await synthesizeKey(VK.RIGHT, { shiftKey: true }, CHARS.RIGHT);
    111  await checkElement(input, 1, 2);
    112 
    113  info("shift+right");
    114  await synthesizeKey(VK.RIGHT, { shiftKey: true }, CHARS.RIGHT);
    115  await checkElement(input, 1, 3);
    116 
    117  info("shift+left");
    118  await synthesizeKey(VK.LEFT, { shiftKey: true }, CHARS.LEFT);
    119  await checkElement(input, 1, 2);
    120 
    121  info("shift+left");
    122  await synthesizeKey(VK.LEFT, { shiftKey: true }, CHARS.LEFT);
    123  await checkElement(input, 1, 1);
    124 
    125  info("shift+left");
    126  await synthesizeKey(VK.LEFT, { shiftKey: true }, CHARS.LEFT);
    127  await checkElement(input, 0, 1);
    128 
    129  info("shift+right");
    130  await synthesizeKey(VK.RIGHT, { shiftKey: true }, CHARS.RIGHT);
    131  await checkElement(input, 1, 1);
    132 
    133  info("left");
    134  await synthesizeKey(VK.LEFT, {}, CHARS.LEFT);
    135  await checkElement(input, 0, 0);
    136 
    137  if (IS_MAC) {
    138    info("down");
    139    await synthesizeKey(VK.DOWN, { shiftKey: true }, CHARS.DOWN);
    140  } else {
    141    info("end");
    142    await synthesizeKey(VK.END, { shiftKey: true }, CHARS.END);
    143  }
    144  await checkElement(input, 0, 9);
    145 
    146  info("cut");
    147  await synthesizeKey(VK.X, { accelKey: true }, "x", "input");
    148  await checkElement(input, 0, 0, "");
    149  let text = SpecialPowers.getClipboardData("text/plain");
    150  is(text, "Test text", "Should have cut to the clipboard");
    151  SpecialPowers.clipboardCopyString("New text");
    152 
    153  info("paste");
    154  await synthesizeKey(VK.V, { accelKey: true }, "v", "input");
    155  await checkElement(input, 8, 8, "New text");
    156 
    157  if (IS_MAC) {
    158    info("up");
    159    await synthesizeKey(VK.UP, {}, CHARS.UP);
    160  } else {
    161    info("home");
    162    await synthesizeKey(VK.HOME, {}, CHARS.HOME);
    163  }
    164  await checkElement(input, 0, 0, "New text");
    165 
    166  info("select all");
    167  await synthesizeKey(VK.A, { accelKey: true}, "a", "select");
    168  await checkElement(input, 0, 8, "New text");
    169 
    170  info("right");
    171  await synthesizeKey(VK.RIGHT, {}, CHARS.RIGHT);
    172  await checkElement(input, 8, 8, "New text");
    173 
    174  info("word left");
    175  if (IS_MAC) {
    176    await synthesizeKey(VK.LEFT, { altKey: true }, CHARS.LEFT);
    177  } else {
    178    await synthesizeKey(VK.LEFT, { ctrlKey: true }, CHARS.LEFT);
    179  }
    180  await checkElement(input, 4, 4, "New text");
    181 
    182  info("delete word left");
    183  if (IS_MAC) {
    184    await synthesizeKey(VK.BACKSPACE, { altKey: true }, CHARS.BACKSPACE);
    185  } else {
    186    await synthesizeKey(VK.BACKSPACE, { ctrlKey: true }, CHARS.BACKSPACE);
    187  }
    188  await checkElement(input, 0, 0, "text");
    189 
    190  info("undo");
    191  await synthesizeKey(VK.Z, { accelKey: true }, "", "input");
    192  await checkElement(input, 4, 4, "New text");
    193 
    194  info("redo");
    195  await synthesizeKey(VK.Z, { accelKey: true, shiftKey: true }, "", "input");
    196  await checkElement(input, 0, 0, "text");
    197 
    198  info("typing");
    199  await synthesizeKey(VK.RIGHT, {}, CHARS.RIGHT);
    200  await synthesizeKey(VK.RIGHT, {}, CHARS.RIGHT);
    201  await synthesizeKey(VK.RIGHT, {}, CHARS.RIGHT);
    202  await synthesizeKey(VK.RIGHT, {}, CHARS.RIGHT);
    203  await synthesizeKey(VK.SPACE, {}, " ");
    204  await synthesizeKey(VK.F, {}, "f");
    205  await synthesizeKey(VK.O, {}, "o");
    206  await synthesizeKey(VK.O, {}, "o");
    207  await checkElement(input, 8, 8, "text foo");
    208 }
    209 
    210 async function runTest() {
    211  // When running in windowed mode the caller will start the test once we have
    212  // focus.
    213  if (window.arguments && window.arguments[0]) {
    214    return;
    215  }
    216 
    217  SimpleTest.waitForExplicitFinish();
    218  await startTest();
    219  SimpleTest.finish();
    220 }
    221 </script>
    222 </head>
    223 <body onload="runTest();">
    224 <input id=input value="Test text"/>
    225 </body>
    226 </html>