dom/events/test/test_dom_keyboard_event.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/events/test/test_dom_keyboard_event.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,317 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<head>
     1.7 +  <title>Test for DOM KeyboardEvent</title>
     1.8 +  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
     1.9 +  <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
    1.10 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
    1.11 +</head>
    1.12 +<body>
    1.13 +<p id="display"></p>
    1.14 +<div id="content" style="display: none">
    1.15 +  
    1.16 +</div>
    1.17 +<pre id="test">
    1.18 +<script type="application/javascript">
    1.19 +
    1.20 +SimpleTest.waitForExplicitFinish();
    1.21 +SimpleTest.waitForFocus(runTests, window);
    1.22 +
    1.23 +function testInitializingUntrustedEvent()
    1.24 +{
    1.25 +  const kTests = [
    1.26 +    { createEventArg: "KeyboardEvent",
    1.27 +      type: "keydown", bubbles: true, cancelable: true, view: null,
    1.28 +      ctrlKey: false, altKey: false, shiftKey: false, metaKey: false,
    1.29 +      keyCode: 0x00, charCode: 0x00 },
    1.30 +
    1.31 +    { createEventArg: "keyboardevent",
    1.32 +      type: "keyup", bubbles: false, cancelable: true, view: window,
    1.33 +      ctrlKey: true, altKey: false, shiftKey: false, metaKey: false,
    1.34 +      keyCode: 0x10, charCode: 0x00 },
    1.35 +
    1.36 +    { createEventArg: "Keyboardevent",
    1.37 +      type: "keypess", bubbles: true, cancelable: false, view: null,
    1.38 +      ctrlKey: false, altKey: true, shiftKey: false, metaKey: false,
    1.39 +      keyCode: 0x11, charCode: 0x30 },
    1.40 +
    1.41 +    { createEventArg: "keyboardEvent",
    1.42 +      type: "boo", bubbles: false, cancelable: false, view: window,
    1.43 +      ctrlKey: false, altKey: false, shiftKey: true, metaKey: false,
    1.44 +      keyCode: 0x30, charCode: 0x40 },
    1.45 +
    1.46 +    { createEventArg: "KeyEvents",
    1.47 +      type: "foo", bubbles: true, cancelable: true, view: null,
    1.48 +      ctrlKey: false, altKey: false, shiftKey: false, metaKey: true,
    1.49 +      keyCode: 0x00, charCode: 0x50 },
    1.50 +
    1.51 +    { createEventArg: "keyevents",
    1.52 +      type: "bar", bubbles: false, cancelable: true, view: window,
    1.53 +      ctrlKey: true, altKey: true, shiftKey: false, metaKey: false,
    1.54 +      keyCode: 0x00, charCode: 0x60 },
    1.55 +
    1.56 +    { createEventArg: "Keyevents",
    1.57 +      type: "keydown", bubbles: true, cancelable: false, view: null,
    1.58 +      ctrlKey: false, altKey: true, shiftKey: false, metaKey: true,
    1.59 +      keyCode: 0x30, charCode: 0x00 },
    1.60 +
    1.61 +    { createEventArg: "keyEvents",
    1.62 +      type: "keyup", bubbles: false, cancelable: false, view: window,
    1.63 +      ctrlKey: true, altKey: false, shiftKey: true, metaKey: false,
    1.64 +      keyCode: 0x10, charCode: 0x80 },
    1.65 +
    1.66 +    { createEventArg: "KeyboardEvent",
    1.67 +      type: "keypress", bubbles: false, cancelable: false, view: window,
    1.68 +      ctrlKey: true, altKey: false, shiftKey: true, metaKey: true,
    1.69 +      keyCode: 0x10, charCode: 0x80 },
    1.70 +
    1.71 +    { createEventArg: "KeyboardEvent",
    1.72 +      type: "foo", bubbles: false, cancelable: false, view: window,
    1.73 +      ctrlKey: true, altKey: true, shiftKey: true, metaKey: true,
    1.74 +      keyCode: 0x10, charCode: 0x80 },
    1.75 +  ];
    1.76 +
    1.77 +  const kOtherModifierName = [
    1.78 +    "CapsLock", "NumLock", "ScrollLock", "SymbolLock", "Fn", "OS", "AltGraph"
    1.79 +  ];
    1.80 +
    1.81 +  const kInvalidModifierName = [
    1.82 +    "shift", "control", "alt", "meta", "capslock", "numlock", "scrolllock",
    1.83 +    "symbollock", "fn", "os", "altgraph", "Invalid", "Shift Control",
    1.84 +    "Win", "Scroll"
    1.85 +  ];
    1.86 +
    1.87 +  for (var i = 0; i < kTests.length; i++) {
    1.88 +    var description = "testInitializingUntrustedEvent, Index: " + i + ", ";
    1.89 +    const kTest = kTests[i];
    1.90 +    var e = document.createEvent(kTest.createEventArg);
    1.91 +    e.initKeyEvent(kTest.type, kTest.bubbles, kTest.cancelable, kTest.view,
    1.92 +                   kTest.ctrlKey, kTest.altKey, kTest.shiftKey, kTest.metaKey,
    1.93 +                   kTest.keyCode, kTest.charCode);
    1.94 +    is(e.toString(), "[object KeyboardEvent]",
    1.95 +       description + 'class string should be "KeyboardEvent"');
    1.96 +
    1.97 +    for (var attr in kTest) {
    1.98 +      if (attr == "createEventArg") {
    1.99 +        continue;
   1.100 +      }
   1.101 +      if (attr == "keyCode") {
   1.102 +        // If this is keydown, keyup of keypress event, keycod must be correct.
   1.103 +        if (kTest.type == "keydown" || kTest.type == "keyup" || kTest.type == "keypress") {
   1.104 +          is(e[attr], kTest[attr], description + attr + " returns wrong value");
   1.105 +        // Otherwise, should be always zero (why?)
   1.106 +        } else {
   1.107 +          is(e[attr], 0, description + attr + " returns non-zero for invalid event");
   1.108 +        }
   1.109 +      } else if (attr == "charCode") {
   1.110 +        // If this is keydown or keyup event, charCode always 0.
   1.111 +        if (kTest.type == "keydown" || kTest.type == "keyup") {
   1.112 +          is(e[attr], 0, description + attr + " returns non-zero for keydown or keyup event");
   1.113 +        // If this is keypress event, charCode must be correct.
   1.114 +        } else if (kTest.type == "keypress") {
   1.115 +          is(e[attr], kTest[attr], description + attr + " returns wrong value");
   1.116 +        // Otherwise, we have a bug.
   1.117 +        } else {
   1.118 +          if (e[attr] != kTest[attr]) { // avoid random unexpected pass.
   1.119 +            todo_is(e[attr], kTest[attr], description + attr + " returns wrong value");
   1.120 +          }
   1.121 +        }
   1.122 +      } else {
   1.123 +        is(e[attr], kTest[attr], description + attr + " returns wrong value");
   1.124 +      }
   1.125 +    }
   1.126 +    is(e.isTrusted, false, description + "isTrusted returns wrong value");
   1.127 +
   1.128 +    // Currently, there is no way to initialize char and key attribute values.
   1.129 +    ok(e.key === "", description + "key must return empty string - got " + e.key);
   1.130 +
   1.131 +    // getModifierState() tests
   1.132 +    is(e.getModifierState("Shift"), kTest.shiftKey,
   1.133 +       description + "getModifierState(\"Shift\") returns wrong value");
   1.134 +    is(e.getModifierState("Control"), kTest.ctrlKey,
   1.135 +       description + "getModifierState(\"Control\") returns wrong value");
   1.136 +    is(e.getModifierState("Alt"), kTest.altKey,
   1.137 +       description + "getModifierState(\"Alt\") returns wrong value");
   1.138 +    is(e.getModifierState("Meta"), kTest.metaKey,
   1.139 +       description + "getModifierState(\"Meta\") returns wrong value");
   1.140 +
   1.141 +    for (var j = 0; j < kOtherModifierName.length; j++) {
   1.142 +      ok(!e.getModifierState(kOtherModifierName[j]),
   1.143 +         description + "getModifierState(\"" + kOtherModifierName[j] + "\") returns wrong value");
   1.144 +    }
   1.145 +    for (var k = 0; k < kInvalidModifierName.length; k++) {
   1.146 +      ok(!e.getModifierState(kInvalidModifierName[k]),
   1.147 +         description + "getModifierState(\"" + kInvalidModifierName[k] + "\") returns wrong value");
   1.148 +    }
   1.149 +  }
   1.150 +}
   1.151 +
   1.152 +function testSynthesizedKeyLocation()
   1.153 +{
   1.154 +  const kTests = [
   1.155 +    { key: "a", isModifier: false,
   1.156 +      event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false,
   1.157 +               location: KeyboardEvent.DOM_KEY_LOCATION_STANDARD },
   1.158 +    },
   1.159 +    { key: "VK_SHIFT", isModifier: true,
   1.160 +      event: { shiftKey: true, ctrlKey: false, altKey: false, metaKey: false,
   1.161 +               location: KeyboardEvent.DOM_KEY_LOCATION_LEFT },
   1.162 +    },
   1.163 +    { key: "VK_SHIFT", isModifier: true,
   1.164 +      event: { shiftKey: true, ctrlKey: false, altKey: false, metaKey: false,
   1.165 +               location: KeyboardEvent.DOM_KEY_LOCATION_RIGHT },
   1.166 +    },
   1.167 +    { key: "VK_CONTROL", isModifier: true,
   1.168 +      event: { shiftKey: false, ctrlKey: true, altKey: false, metaKey: false,
   1.169 +               location: KeyboardEvent.DOM_KEY_LOCATION_LEFT },
   1.170 +    },
   1.171 +    { key: "VK_CONTROL", isModifier: true,
   1.172 +      event: { shiftKey: false, ctrlKey: true, altKey: false, metaKey: false,
   1.173 +               location: KeyboardEvent.DOM_KEY_LOCATION_RIGHT },
   1.174 +    },
   1.175 +/* XXX Alt key activates menubar even if we consume the key events.
   1.176 +    { key: "VK_ALT", isModifier: true,
   1.177 +      event: { shiftKey: false, ctrlKey: false, altKey: true, metaKey: false,
   1.178 +               location: KeyboardEvent.DOM_KEY_LOCATION_LEFT },
   1.179 +    },
   1.180 +    { key: "VK_ALT", isModifier: true,
   1.181 +      event: { shiftKey: false, ctrlKey: false, altKey: true, metaKey: false,
   1.182 +               location: KeyboardEvent.DOM_KEY_LOCATION_RIGHT },
   1.183 +    },
   1.184 +*/
   1.185 +    { key: "VK_META", isModifier: true,
   1.186 +      event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: true,
   1.187 +               location: KeyboardEvent.DOM_KEY_LOCATION_LEFT },
   1.188 +    },
   1.189 +    { key: "VK_META", isModifier: true,
   1.190 +      event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: true,
   1.191 +               location: KeyboardEvent.DOM_KEY_LOCATION_RIGHT },
   1.192 +    },
   1.193 +    { key: "VK_DOWN", isModifier: false,
   1.194 +      event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false,
   1.195 +               location: KeyboardEvent.DOM_KEY_LOCATION_STANDARD },
   1.196 +    },
   1.197 +    { key: "VK_DOWN", isModifier: false,
   1.198 +      event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false,
   1.199 +               location: KeyboardEvent.DOM_KEY_LOCATION_NUMPAD },
   1.200 +    },
   1.201 +    { key: "VK_DOWN", isModifier: false,
   1.202 +      event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false,
   1.203 +               location: KeyboardEvent.DOM_KEY_LOCATION_JOYSTICK },
   1.204 +    },
   1.205 +    { key: "5", isModifier: false,
   1.206 +      event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false,
   1.207 +               location: KeyboardEvent.DOM_KEY_LOCATION_STANDARD },
   1.208 +    },
   1.209 +    { key: "VK_NUMPAD5", isModifier: false,
   1.210 +      event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false,
   1.211 +               location: KeyboardEvent.DOM_KEY_LOCATION_NUMPAD },
   1.212 +    },
   1.213 +    { key: "5", isModifier: false,
   1.214 +      event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false,
   1.215 +               location: KeyboardEvent.DOM_KEY_LOCATION_MOBILE },
   1.216 +    },
   1.217 +    { key: "VK_NUMPAD5", isModifier: false,
   1.218 +      event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false,
   1.219 +               location: KeyboardEvent.DOM_KEY_LOCATION_MOBILE },
   1.220 +    },
   1.221 +    { key: "+", isModifier: false,
   1.222 +      event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false,
   1.223 +               location: KeyboardEvent.DOM_KEY_LOCATION_STANDARD },
   1.224 +    },
   1.225 +    { key: "VK_ADD", isModifier: false,
   1.226 +      event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false,
   1.227 +               location: KeyboardEvent.DOM_KEY_LOCATION_NUMPAD },
   1.228 +    },
   1.229 +    { key: "VK_RETURN", isModifier: false,
   1.230 +      event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false,
   1.231 +               location: KeyboardEvent.DOM_KEY_LOCATION_STANDARD },
   1.232 +    },
   1.233 +    { key: "VK_RETURN", isModifier: false,
   1.234 +      event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false,
   1.235 +               location: KeyboardEvent.DOM_KEY_LOCATION_NUMPAD },
   1.236 +    },
   1.237 +    { key: "VK_NUM_LOCK", isModifier: true,
   1.238 +      event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false,
   1.239 +               location: KeyboardEvent.DOM_KEY_LOCATION_STANDARD },
   1.240 +    },
   1.241 +    { key: "VK_INSERT", isModifier: false,
   1.242 +      event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false,
   1.243 +               location: KeyboardEvent.DOM_KEY_LOCATION_STANDARD },
   1.244 +    },
   1.245 +    { key: "VK_INSERT", isModifier: false,
   1.246 +      event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false,
   1.247 +               location: KeyboardEvent.DOM_KEY_LOCATION_NUMPAD },
   1.248 +    },
   1.249 +  ];
   1.250 +
   1.251 +  function getLocationName(aLocation)
   1.252 +  {
   1.253 +    switch (aLocation) {
   1.254 +      case KeyboardEvent.DOM_KEY_LOCATION_STANDARD:
   1.255 +        return "DOM_KEY_LOCATION_STANDARD";
   1.256 +      case KeyboardEvent.DOM_KEY_LOCATION_LEFT:
   1.257 +        return "DOM_KEY_LOCATION_LEFT";
   1.258 +      case KeyboardEvent.DOM_KEY_LOCATION_RIGHT:
   1.259 +        return "DOM_KEY_LOCATION_RIGHT";
   1.260 +      case KeyboardEvent.DOM_KEY_LOCATION_MOBILE:
   1.261 +        return "DOM_KEY_LOCATION_MOBILE";
   1.262 +      case KeyboardEvent.DOM_KEY_LOCATION_NUMPAD:
   1.263 +        return "DOM_KEY_LOCATION_NUMPAD";
   1.264 +      case KeyboardEvent.DOM_KEY_LOCATION_JOYSTICK:
   1.265 +        return "DOM_KEY_LOCATION_JOYSTICK";
   1.266 +      default:
   1.267 +        return "Invalid value (" + aLocation + ")";
   1.268 +    }
   1.269 +  }
   1.270 +
   1.271 +  var currentTest, description;
   1.272 +  var events = { keydown: false, keypress: false, keyup: false };
   1.273 +
   1.274 +  function handler(aEvent)
   1.275 +  {
   1.276 +    is(aEvent.location, currentTest.event.location,
   1.277 +       description + "location of " + aEvent.type + " was invalid");
   1.278 +    events[aEvent.type] = true;
   1.279 +    if (aEvent.type != "keydown" ||
   1.280 +        (currentTest.event.isModifier && aEvent.type == "keydown")) {
   1.281 +      aEvent.preventDefault();
   1.282 +    }
   1.283 +  }
   1.284 +
   1.285 +  window.addEventListener("keydown", handler, true);
   1.286 +  window.addEventListener("keypress", handler, true);
   1.287 +  window.addEventListener("keyup", handler, true);
   1.288 +
   1.289 +  for (var i = 0; i < kTests.length; i++) {
   1.290 +    currentTest = kTests[i];
   1.291 +    events = { keydown: false, keypress: false, keyup: false };
   1.292 +    description = "testSynthesizedKeyLocation, " + i + ", key: " +
   1.293 +      currentTest.key + ", location: " +
   1.294 +      getLocationName(currentTest.event.location) + ": ";
   1.295 +    synthesizeKey(currentTest.key, currentTest.event);
   1.296 +    ok(events.keydown, description + "keydown event wasn't fired");
   1.297 +    if (currentTest.isModifier) {
   1.298 +      todo(events.keypress, description + "keypress event was fired for modifier key");
   1.299 +    } else {
   1.300 +      ok(events.keypress, description + "keypress event wasn't fired");
   1.301 +    }
   1.302 +    ok(events.keyup, description + "keyup event wasn't fired");
   1.303 +  }
   1.304 +
   1.305 +  window.removeEventListener("keydown", handler, true);
   1.306 +  window.removeEventListener("keypress", handler, true);
   1.307 +  window.removeEventListener("keyup", handler, true);
   1.308 +}
   1.309 +
   1.310 +function runTests()
   1.311 +{
   1.312 +  testInitializingUntrustedEvent();
   1.313 +  testSynthesizedKeyLocation();
   1.314 +  SimpleTest.finish();
   1.315 +}
   1.316 +
   1.317 +</script>
   1.318 +</pre>
   1.319 +</body>
   1.320 +</html>

mercurial