DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (cf48e59e88de)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
<html>
<head>
  <title>Test for key event handler of text editor</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css"
          href="chrome://mochikit/content/tests/SimpleTest/test.css" />
</head>
<body>
<div id="display">
  <input type="text" id="inputField">
  <input type="password" id="passwordField">
  <textarea id="textarea"></textarea>
</div>
<div id="content" style="display: none">

</div>
<pre id="test">
</pre>

<script class="testbody" type="application/javascript">

SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(runTests, window);

var inputField = document.getElementById("inputField");
var passwordField = document.getElementById("passwordField");
var textarea = document.getElementById("textarea");

const kIsMac = navigator.platform.indexOf("Mac") == 0;
const kIsWin = navigator.platform.indexOf("Win") == 0;
const kIsLinux = navigator.platform.indexOf("Linux") == 0;

function runTests() {
  var fm = SpecialPowers.Services.focus;

  var capturingPhase = { fired: false, prevented: false };
  var bubblingPhase = { fired: false, prevented: false };

  var listener = {
    handleEvent: function _hv(aEvent) {
      is(aEvent.type, "keypress", "unexpected event is handled");
      switch (aEvent.eventPhase) {
      case aEvent.CAPTURING_PHASE:
        capturingPhase.fired = true;
        capturingPhase.prevented = aEvent.defaultPrevented;
        break;
      case aEvent.BUBBLING_PHASE:
        bubblingPhase.fired = true;
        bubblingPhase.prevented = aEvent.defaultPrevented;
        aEvent.preventDefault(); // prevent the browser default behavior
        break;
      default:
        ok(false, "event is handled in unexpected phase");
      }
    },
  };

  function check(aDescription,
                 aFiredOnCapture, aFiredOnBubbling, aPreventedOnBubbling) {
    function getDesciption(aExpected) {
      return aDescription + (aExpected ? " wasn't " : " was ");
    }

    is(capturingPhase.fired, aFiredOnCapture,
       getDesciption(aFiredOnCapture) + "fired on capture phase");
    is(bubblingPhase.fired, aFiredOnBubbling,
       getDesciption(aFiredOnBubbling) + "fired on bubbling phase");

    // If the event is fired on bubbling phase and it was already prevented
    // on capture phase, it must be prevented on bubbling phase too.
    if (capturingPhase.prevented) {
      todo(false, aDescription +
           " was consumed already, so, we cannot test the editor behavior actually");
      aPreventedOnBubbling = true;
    }

    is(bubblingPhase.prevented, aPreventedOnBubbling,
       getDesciption(aPreventedOnBubbling) + "prevented on bubbling phase");
  }

  var parentElement = document.getElementById("display");
  SpecialPowers.addSystemEventListener(parentElement, "keypress", listener,
                                       true);
  SpecialPowers.addSystemEventListener(parentElement, "keypress", listener,
                                       false);

  function doTest(aElement, aDescription, aIsSingleLine, aIsReadonly,
                  aIsTabbable) {
    function reset(aText) {
      capturingPhase.fired = false;
      capturingPhase.prevented = false;
      bubblingPhase.fired = false;
      bubblingPhase.prevented = false;
      aElement.value = aText;
    }

    if (document.activeElement) {
      document.activeElement.blur();
    }

    aDescription += ": ";

    aElement.focus();
    is(SpecialPowers.unwrap(fm.focusedElement), aElement, aDescription + "failed to move focus");

    // Backspace key:
    //   If editor is readonly, it doesn't consume.
    //   If editor is editable, it consumes backspace and shift+backspace.
    //   Otherwise, editor doesn't consume the event but the native key
    //   bindings on nsTextControlFrame may consume it.
    reset("");
    synthesizeKey("KEY_Backspace");
    check(aDescription + "Backspace", true, true, true);

    reset("");
    synthesizeKey("KEY_Backspace", {shiftKey: true});
    check(aDescription + "Shift+Backspace", true, true, true);

    reset("");
    synthesizeKey("KEY_Backspace", {ctrlKey: true});
    // Win: cmd_deleteWordBackward
    check(aDescription + "Ctrl+Backspace",
          true, true, aIsReadonly || kIsWin);

    reset("");
    synthesizeKey("KEY_Backspace", {altKey: true});
    // Win: cmd_undo
    // Mac: cmd_deleteWordBackward
    check(aDescription + "Alt+Backspace",
          true, true, aIsReadonly || kIsWin || kIsMac);

    reset("");
    synthesizeKey("KEY_Backspace", {metaKey: true});
    check(aDescription + "Meta+Backspace", true, true, aIsReadonly);

    reset("");
    synthesizeKey("KEY_Backspace", {osKey: true});
    check(aDescription + "OS+Backspace", true, true, aIsReadonly);

    // Delete key:
    //   If editor is readonly, it doesn't consume.
    //   If editor is editable, delete is consumed.
    //   Otherwise, editor doesn't consume the event but the native key
    //   bindings on nsTextControlFrame may consume it.
    reset("");
    synthesizeKey("KEY_Delete");
    // Linux: native handler
    // Mac: cmd_deleteCharForward
    check(aDescription + "Delete",
          true, true, !aIsReadonly || kIsLinux || kIsMac);

    reset("");
    // Win: cmd_cutOrDelete
    // Linux: cmd_cut
    // Mac: cmd_deleteCharForward
    synthesizeKey("KEY_Delete", {shiftKey: true});
    check(aDescription + "Shift+Delete",
          true, true, true);

    reset("");
    synthesizeKey("KEY_Delete", {ctrlKey: true});
    // Win: cmd_deleteWordForward
    // Linux: cmd_copy
    check(aDescription + "Ctrl+Delete",
          true, true, kIsWin || kIsLinux);

    reset("");
    synthesizeKey("KEY_Delete", {altKey: true});
    // Mac: cmd_deleteWordForward
    check(aDescription + "Alt+Delete",
          true, true, kIsMac);

    reset("");
    synthesizeKey("KEY_Delete", {metaKey: true});
    // Linux: native handler consumed.
    check(aDescription + "Meta+Delete",
          true, true, kIsLinux);

    reset("");
    synthesizeKey("KEY_Delete", {osKey: true});
    check(aDescription + "OS+Delete",
          true, true, false);

    // XXX input.value returns "\n" when it's empty, so, we should use dummy
    // value ("a") for the following tests.

    // Return key:
    //   If editor is readonly, it doesn't consume.
    //   If editor is editable and not single line editor, it consumes Return
    //   and Shift+Return.
    //   Otherwise, editor doesn't consume the event.
    reset("a");
    synthesizeKey("KEY_Enter");
    check(aDescription + "Return",
          true, true, !aIsSingleLine && !aIsReadonly);
    is(aElement.value, !aIsSingleLine && !aIsReadonly ? "a\n" : "a",
       aDescription + "Return");

    reset("a");
    synthesizeKey("KEY_Enter", {shiftKey: true});
    check(aDescription + "Shift+Return",
          true, true, !aIsSingleLine && !aIsReadonly);
    is(aElement.value, !aIsSingleLine && !aIsReadonly ? "a\n" : "a",
       aDescription + "Shift+Return");

    reset("a");
    synthesizeKey("KEY_Enter", {ctrlKey: true});
    check(aDescription + "Ctrl+Return", true, true, false);
    is(aElement.value, "a", aDescription + "Ctrl+Return");

    reset("a");
    synthesizeKey("KEY_Enter", {altKey: true});
    check(aDescription + "Alt+Return", true, true, false);
    is(aElement.value, "a", aDescription + "Alt+Return");

    reset("a");
    synthesizeKey("KEY_Enter", {metaKey: true});
    check(aDescription + "Meta+Return", true, true, false);
    is(aElement.value, "a", aDescription + "Meta+Return");

    reset("a");
    synthesizeKey("KEY_Enter", {osKey: true});
    check(aDescription + "OS+Return", true, true, false);
    is(aElement.value, "a", aDescription + "OS+Return");

    // Tab key:
    //   If editor is tabbable, editor doesn't consume all tab key events.
    //   Otherwise, editor consumes tab key event without any modifier keys.
    reset("a");
    synthesizeKey("KEY_Tab");
    check(aDescription + "Tab",
          true, true, !aIsTabbable && !aIsReadonly);
    is(aElement.value, !aIsTabbable && !aIsReadonly ? "a\t" : "a",
       aDescription + "Tab");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Tab)");

    // If the editor is not tabbable, make sure that it accepts tab characters
    // even if it's empty.
    if (!aIsTabbable && !aIsReadonly) {
      reset("");
      synthesizeKey("KEY_Tab");
      check(aDescription + "Tab on empty textarea",
            true, true, !aIsReadonly);
      is(aElement.value, "\t", aDescription + "Tab on empty textarea");
      is(SpecialPowers.unwrap(fm.focusedElement), aElement,
         aDescription + "focus moved unexpectedly (Tab on empty textarea");
    }

    reset("a");
    synthesizeKey("KEY_Tab", {shiftKey: true});
    check(aDescription + "Shift+Tab", true, true, false);
    is(aElement.value, "a", aDescription + "Shift+Tab");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Shift+Tab)");

    // Ctrl+Tab should be consumed by tabbrowser at keydown, so, keypress
    // event should never be fired.
    reset("a");
    synthesizeKey("KEY_Tab", {ctrlKey: true});
    check(aDescription + "Ctrl+Tab", false, false, false);
    is(aElement.value, "a", aDescription + "Ctrl+Tab");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Ctrl+Tab)");

    reset("a");
    synthesizeKey("KEY_Tab", {altKey: true});
    check(aDescription + "Alt+Tab", true, true, false);
    is(aElement.value, "a", aDescription + "Alt+Tab");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Alt+Tab)");

    reset("a");
    synthesizeKey("KEY_Tab", {metaKey: true});
    check(aDescription + "Meta+Tab", true, true, false);
    is(aElement.value, "a", aDescription + "Meta+Tab");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Meta+Tab)");

    reset("a");
    synthesizeKey("KEY_Tab", {osKey: true});
    check(aDescription + "OS+Tab", true, true, false);
    is(aElement.value, "a", aDescription + "OS+Tab");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (OS+Tab)");

    // Esc key:
    //   In all cases, esc key events are not consumed
    reset("abc");
    synthesizeKey("KEY_Escape");
    check(aDescription + "Esc", true, true, false);

    reset("abc");
    synthesizeKey("KEY_Escape", {shiftKey: true});
    check(aDescription + "Shift+Esc", true, true, false);

    reset("abc");
    synthesizeKey("KEY_Escape", {ctrlKey: true});
    check(aDescription + "Ctrl+Esc", true, true, false);

    reset("abc");
    synthesizeKey("KEY_Escape", {altKey: true});
    check(aDescription + "Alt+Esc", true, true, false);

    reset("abc");
    synthesizeKey("KEY_Escape", {metaKey: true});
    check(aDescription + "Meta+Esc", true, true, false);

    reset("abc");
    synthesizeKey("KEY_Escape", {osKey: true});
    check(aDescription + "OS+Esc", true, true, false);

    // typical typing tests:
    reset("");
    sendString("M");
    check(aDescription + "M", true, true, !aIsReadonly);
    sendString("o");
    check(aDescription + "o", true, true, !aIsReadonly);
    sendString("z");
    check(aDescription + "z", true, true, !aIsReadonly);
    sendString("i");
    check(aDescription + "i", true, true, !aIsReadonly);
    sendString("l");
    check(aDescription + "l", true, true, !aIsReadonly);
    sendString("l");
    check(aDescription + "l", true, true, !aIsReadonly);
    sendString("a");
    check(aDescription + "a", true, true, !aIsReadonly);
    sendString(" ");
    check(aDescription + "' '", true, true, !aIsReadonly);
    is(aElement.value, !aIsReadonly ? "Mozilla " : "",
       aDescription + "typed \"Mozilla \"");
  }

  doTest(inputField, "<input type=\"text\">", true, false, true);

  inputField.setAttribute("readonly", "readonly");
  doTest(inputField, "<input type=\"text\" readonly>", true, true, true);

  doTest(passwordField, "<input type=\"password\">", true, false, true);

  passwordField.setAttribute("readonly", "readonly");
  doTest(passwordField, "<input type=\"password\" readonly>", true, true, true);

  doTest(textarea, "<textarea>", false, false, true);

  textarea.setAttribute("readonly", "readonly");
  doTest(textarea, "<textarea readonly>", false, true, true);

  // make non-tabbable plaintext editor
  textarea.removeAttribute("readonly");
  const nsIEditor = SpecialPowers.Ci.nsIEditor;
  var editor = SpecialPowers.wrap(textarea).editor;
  var flags = editor.flags;
  editor.flags = flags & ~(nsIEditor.eEditorWidgetMask |
                           nsIEditor.eEditorAllowInteraction);
  doTest(textarea, "non-tabbable <textarea>", false, false, false);

  textarea.setAttribute("readonly", "readonly");
  doTest(textarea, "non-tabbable <textarea readonly>", false, true, false);

  editor.flags = flags;

  SpecialPowers.removeSystemEventListener(parentElement, "keypress", listener,
                                          true);
  SpecialPowers.removeSystemEventListener(parentElement, "keypress", listener,
                                          false);

  SimpleTest.finish();
}

</script>
</body>

</html>