Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | <!DOCTYPE HTML> |
michael@0 | 2 | <html> |
michael@0 | 3 | <head> |
michael@0 | 4 | <title>Test for DOM KeyboardEvent</title> |
michael@0 | 5 | <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
michael@0 | 6 | <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script> |
michael@0 | 7 | <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> |
michael@0 | 8 | </head> |
michael@0 | 9 | <body> |
michael@0 | 10 | <p id="display"></p> |
michael@0 | 11 | <div id="content" style="display: none"> |
michael@0 | 12 | |
michael@0 | 13 | </div> |
michael@0 | 14 | <pre id="test"> |
michael@0 | 15 | <script type="application/javascript"> |
michael@0 | 16 | |
michael@0 | 17 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 18 | SimpleTest.waitForFocus(runTests, window); |
michael@0 | 19 | |
michael@0 | 20 | function testInitializingUntrustedEvent() |
michael@0 | 21 | { |
michael@0 | 22 | const kTests = [ |
michael@0 | 23 | { createEventArg: "KeyboardEvent", |
michael@0 | 24 | type: "keydown", bubbles: true, cancelable: true, view: null, |
michael@0 | 25 | ctrlKey: false, altKey: false, shiftKey: false, metaKey: false, |
michael@0 | 26 | keyCode: 0x00, charCode: 0x00 }, |
michael@0 | 27 | |
michael@0 | 28 | { createEventArg: "keyboardevent", |
michael@0 | 29 | type: "keyup", bubbles: false, cancelable: true, view: window, |
michael@0 | 30 | ctrlKey: true, altKey: false, shiftKey: false, metaKey: false, |
michael@0 | 31 | keyCode: 0x10, charCode: 0x00 }, |
michael@0 | 32 | |
michael@0 | 33 | { createEventArg: "Keyboardevent", |
michael@0 | 34 | type: "keypess", bubbles: true, cancelable: false, view: null, |
michael@0 | 35 | ctrlKey: false, altKey: true, shiftKey: false, metaKey: false, |
michael@0 | 36 | keyCode: 0x11, charCode: 0x30 }, |
michael@0 | 37 | |
michael@0 | 38 | { createEventArg: "keyboardEvent", |
michael@0 | 39 | type: "boo", bubbles: false, cancelable: false, view: window, |
michael@0 | 40 | ctrlKey: false, altKey: false, shiftKey: true, metaKey: false, |
michael@0 | 41 | keyCode: 0x30, charCode: 0x40 }, |
michael@0 | 42 | |
michael@0 | 43 | { createEventArg: "KeyEvents", |
michael@0 | 44 | type: "foo", bubbles: true, cancelable: true, view: null, |
michael@0 | 45 | ctrlKey: false, altKey: false, shiftKey: false, metaKey: true, |
michael@0 | 46 | keyCode: 0x00, charCode: 0x50 }, |
michael@0 | 47 | |
michael@0 | 48 | { createEventArg: "keyevents", |
michael@0 | 49 | type: "bar", bubbles: false, cancelable: true, view: window, |
michael@0 | 50 | ctrlKey: true, altKey: true, shiftKey: false, metaKey: false, |
michael@0 | 51 | keyCode: 0x00, charCode: 0x60 }, |
michael@0 | 52 | |
michael@0 | 53 | { createEventArg: "Keyevents", |
michael@0 | 54 | type: "keydown", bubbles: true, cancelable: false, view: null, |
michael@0 | 55 | ctrlKey: false, altKey: true, shiftKey: false, metaKey: true, |
michael@0 | 56 | keyCode: 0x30, charCode: 0x00 }, |
michael@0 | 57 | |
michael@0 | 58 | { createEventArg: "keyEvents", |
michael@0 | 59 | type: "keyup", bubbles: false, cancelable: false, view: window, |
michael@0 | 60 | ctrlKey: true, altKey: false, shiftKey: true, metaKey: false, |
michael@0 | 61 | keyCode: 0x10, charCode: 0x80 }, |
michael@0 | 62 | |
michael@0 | 63 | { createEventArg: "KeyboardEvent", |
michael@0 | 64 | type: "keypress", bubbles: false, cancelable: false, view: window, |
michael@0 | 65 | ctrlKey: true, altKey: false, shiftKey: true, metaKey: true, |
michael@0 | 66 | keyCode: 0x10, charCode: 0x80 }, |
michael@0 | 67 | |
michael@0 | 68 | { createEventArg: "KeyboardEvent", |
michael@0 | 69 | type: "foo", bubbles: false, cancelable: false, view: window, |
michael@0 | 70 | ctrlKey: true, altKey: true, shiftKey: true, metaKey: true, |
michael@0 | 71 | keyCode: 0x10, charCode: 0x80 }, |
michael@0 | 72 | ]; |
michael@0 | 73 | |
michael@0 | 74 | const kOtherModifierName = [ |
michael@0 | 75 | "CapsLock", "NumLock", "ScrollLock", "SymbolLock", "Fn", "OS", "AltGraph" |
michael@0 | 76 | ]; |
michael@0 | 77 | |
michael@0 | 78 | const kInvalidModifierName = [ |
michael@0 | 79 | "shift", "control", "alt", "meta", "capslock", "numlock", "scrolllock", |
michael@0 | 80 | "symbollock", "fn", "os", "altgraph", "Invalid", "Shift Control", |
michael@0 | 81 | "Win", "Scroll" |
michael@0 | 82 | ]; |
michael@0 | 83 | |
michael@0 | 84 | for (var i = 0; i < kTests.length; i++) { |
michael@0 | 85 | var description = "testInitializingUntrustedEvent, Index: " + i + ", "; |
michael@0 | 86 | const kTest = kTests[i]; |
michael@0 | 87 | var e = document.createEvent(kTest.createEventArg); |
michael@0 | 88 | e.initKeyEvent(kTest.type, kTest.bubbles, kTest.cancelable, kTest.view, |
michael@0 | 89 | kTest.ctrlKey, kTest.altKey, kTest.shiftKey, kTest.metaKey, |
michael@0 | 90 | kTest.keyCode, kTest.charCode); |
michael@0 | 91 | is(e.toString(), "[object KeyboardEvent]", |
michael@0 | 92 | description + 'class string should be "KeyboardEvent"'); |
michael@0 | 93 | |
michael@0 | 94 | for (var attr in kTest) { |
michael@0 | 95 | if (attr == "createEventArg") { |
michael@0 | 96 | continue; |
michael@0 | 97 | } |
michael@0 | 98 | if (attr == "keyCode") { |
michael@0 | 99 | // If this is keydown, keyup of keypress event, keycod must be correct. |
michael@0 | 100 | if (kTest.type == "keydown" || kTest.type == "keyup" || kTest.type == "keypress") { |
michael@0 | 101 | is(e[attr], kTest[attr], description + attr + " returns wrong value"); |
michael@0 | 102 | // Otherwise, should be always zero (why?) |
michael@0 | 103 | } else { |
michael@0 | 104 | is(e[attr], 0, description + attr + " returns non-zero for invalid event"); |
michael@0 | 105 | } |
michael@0 | 106 | } else if (attr == "charCode") { |
michael@0 | 107 | // If this is keydown or keyup event, charCode always 0. |
michael@0 | 108 | if (kTest.type == "keydown" || kTest.type == "keyup") { |
michael@0 | 109 | is(e[attr], 0, description + attr + " returns non-zero for keydown or keyup event"); |
michael@0 | 110 | // If this is keypress event, charCode must be correct. |
michael@0 | 111 | } else if (kTest.type == "keypress") { |
michael@0 | 112 | is(e[attr], kTest[attr], description + attr + " returns wrong value"); |
michael@0 | 113 | // Otherwise, we have a bug. |
michael@0 | 114 | } else { |
michael@0 | 115 | if (e[attr] != kTest[attr]) { // avoid random unexpected pass. |
michael@0 | 116 | todo_is(e[attr], kTest[attr], description + attr + " returns wrong value"); |
michael@0 | 117 | } |
michael@0 | 118 | } |
michael@0 | 119 | } else { |
michael@0 | 120 | is(e[attr], kTest[attr], description + attr + " returns wrong value"); |
michael@0 | 121 | } |
michael@0 | 122 | } |
michael@0 | 123 | is(e.isTrusted, false, description + "isTrusted returns wrong value"); |
michael@0 | 124 | |
michael@0 | 125 | // Currently, there is no way to initialize char and key attribute values. |
michael@0 | 126 | ok(e.key === "", description + "key must return empty string - got " + e.key); |
michael@0 | 127 | |
michael@0 | 128 | // getModifierState() tests |
michael@0 | 129 | is(e.getModifierState("Shift"), kTest.shiftKey, |
michael@0 | 130 | description + "getModifierState(\"Shift\") returns wrong value"); |
michael@0 | 131 | is(e.getModifierState("Control"), kTest.ctrlKey, |
michael@0 | 132 | description + "getModifierState(\"Control\") returns wrong value"); |
michael@0 | 133 | is(e.getModifierState("Alt"), kTest.altKey, |
michael@0 | 134 | description + "getModifierState(\"Alt\") returns wrong value"); |
michael@0 | 135 | is(e.getModifierState("Meta"), kTest.metaKey, |
michael@0 | 136 | description + "getModifierState(\"Meta\") returns wrong value"); |
michael@0 | 137 | |
michael@0 | 138 | for (var j = 0; j < kOtherModifierName.length; j++) { |
michael@0 | 139 | ok(!e.getModifierState(kOtherModifierName[j]), |
michael@0 | 140 | description + "getModifierState(\"" + kOtherModifierName[j] + "\") returns wrong value"); |
michael@0 | 141 | } |
michael@0 | 142 | for (var k = 0; k < kInvalidModifierName.length; k++) { |
michael@0 | 143 | ok(!e.getModifierState(kInvalidModifierName[k]), |
michael@0 | 144 | description + "getModifierState(\"" + kInvalidModifierName[k] + "\") returns wrong value"); |
michael@0 | 145 | } |
michael@0 | 146 | } |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | function testSynthesizedKeyLocation() |
michael@0 | 150 | { |
michael@0 | 151 | const kTests = [ |
michael@0 | 152 | { key: "a", isModifier: false, |
michael@0 | 153 | event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false, |
michael@0 | 154 | location: KeyboardEvent.DOM_KEY_LOCATION_STANDARD }, |
michael@0 | 155 | }, |
michael@0 | 156 | { key: "VK_SHIFT", isModifier: true, |
michael@0 | 157 | event: { shiftKey: true, ctrlKey: false, altKey: false, metaKey: false, |
michael@0 | 158 | location: KeyboardEvent.DOM_KEY_LOCATION_LEFT }, |
michael@0 | 159 | }, |
michael@0 | 160 | { key: "VK_SHIFT", isModifier: true, |
michael@0 | 161 | event: { shiftKey: true, ctrlKey: false, altKey: false, metaKey: false, |
michael@0 | 162 | location: KeyboardEvent.DOM_KEY_LOCATION_RIGHT }, |
michael@0 | 163 | }, |
michael@0 | 164 | { key: "VK_CONTROL", isModifier: true, |
michael@0 | 165 | event: { shiftKey: false, ctrlKey: true, altKey: false, metaKey: false, |
michael@0 | 166 | location: KeyboardEvent.DOM_KEY_LOCATION_LEFT }, |
michael@0 | 167 | }, |
michael@0 | 168 | { key: "VK_CONTROL", isModifier: true, |
michael@0 | 169 | event: { shiftKey: false, ctrlKey: true, altKey: false, metaKey: false, |
michael@0 | 170 | location: KeyboardEvent.DOM_KEY_LOCATION_RIGHT }, |
michael@0 | 171 | }, |
michael@0 | 172 | /* XXX Alt key activates menubar even if we consume the key events. |
michael@0 | 173 | { key: "VK_ALT", isModifier: true, |
michael@0 | 174 | event: { shiftKey: false, ctrlKey: false, altKey: true, metaKey: false, |
michael@0 | 175 | location: KeyboardEvent.DOM_KEY_LOCATION_LEFT }, |
michael@0 | 176 | }, |
michael@0 | 177 | { key: "VK_ALT", isModifier: true, |
michael@0 | 178 | event: { shiftKey: false, ctrlKey: false, altKey: true, metaKey: false, |
michael@0 | 179 | location: KeyboardEvent.DOM_KEY_LOCATION_RIGHT }, |
michael@0 | 180 | }, |
michael@0 | 181 | */ |
michael@0 | 182 | { key: "VK_META", isModifier: true, |
michael@0 | 183 | event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: true, |
michael@0 | 184 | location: KeyboardEvent.DOM_KEY_LOCATION_LEFT }, |
michael@0 | 185 | }, |
michael@0 | 186 | { key: "VK_META", isModifier: true, |
michael@0 | 187 | event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: true, |
michael@0 | 188 | location: KeyboardEvent.DOM_KEY_LOCATION_RIGHT }, |
michael@0 | 189 | }, |
michael@0 | 190 | { key: "VK_DOWN", isModifier: false, |
michael@0 | 191 | event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false, |
michael@0 | 192 | location: KeyboardEvent.DOM_KEY_LOCATION_STANDARD }, |
michael@0 | 193 | }, |
michael@0 | 194 | { key: "VK_DOWN", isModifier: false, |
michael@0 | 195 | event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false, |
michael@0 | 196 | location: KeyboardEvent.DOM_KEY_LOCATION_NUMPAD }, |
michael@0 | 197 | }, |
michael@0 | 198 | { key: "VK_DOWN", isModifier: false, |
michael@0 | 199 | event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false, |
michael@0 | 200 | location: KeyboardEvent.DOM_KEY_LOCATION_JOYSTICK }, |
michael@0 | 201 | }, |
michael@0 | 202 | { key: "5", isModifier: false, |
michael@0 | 203 | event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false, |
michael@0 | 204 | location: KeyboardEvent.DOM_KEY_LOCATION_STANDARD }, |
michael@0 | 205 | }, |
michael@0 | 206 | { key: "VK_NUMPAD5", isModifier: false, |
michael@0 | 207 | event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false, |
michael@0 | 208 | location: KeyboardEvent.DOM_KEY_LOCATION_NUMPAD }, |
michael@0 | 209 | }, |
michael@0 | 210 | { key: "5", isModifier: false, |
michael@0 | 211 | event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false, |
michael@0 | 212 | location: KeyboardEvent.DOM_KEY_LOCATION_MOBILE }, |
michael@0 | 213 | }, |
michael@0 | 214 | { key: "VK_NUMPAD5", isModifier: false, |
michael@0 | 215 | event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false, |
michael@0 | 216 | location: KeyboardEvent.DOM_KEY_LOCATION_MOBILE }, |
michael@0 | 217 | }, |
michael@0 | 218 | { key: "+", isModifier: false, |
michael@0 | 219 | event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false, |
michael@0 | 220 | location: KeyboardEvent.DOM_KEY_LOCATION_STANDARD }, |
michael@0 | 221 | }, |
michael@0 | 222 | { key: "VK_ADD", isModifier: false, |
michael@0 | 223 | event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false, |
michael@0 | 224 | location: KeyboardEvent.DOM_KEY_LOCATION_NUMPAD }, |
michael@0 | 225 | }, |
michael@0 | 226 | { key: "VK_RETURN", isModifier: false, |
michael@0 | 227 | event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false, |
michael@0 | 228 | location: KeyboardEvent.DOM_KEY_LOCATION_STANDARD }, |
michael@0 | 229 | }, |
michael@0 | 230 | { key: "VK_RETURN", isModifier: false, |
michael@0 | 231 | event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false, |
michael@0 | 232 | location: KeyboardEvent.DOM_KEY_LOCATION_NUMPAD }, |
michael@0 | 233 | }, |
michael@0 | 234 | { key: "VK_NUM_LOCK", isModifier: true, |
michael@0 | 235 | event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false, |
michael@0 | 236 | location: KeyboardEvent.DOM_KEY_LOCATION_STANDARD }, |
michael@0 | 237 | }, |
michael@0 | 238 | { key: "VK_INSERT", isModifier: false, |
michael@0 | 239 | event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false, |
michael@0 | 240 | location: KeyboardEvent.DOM_KEY_LOCATION_STANDARD }, |
michael@0 | 241 | }, |
michael@0 | 242 | { key: "VK_INSERT", isModifier: false, |
michael@0 | 243 | event: { shiftKey: false, ctrlKey: false, altKey: false, metaKey: false, |
michael@0 | 244 | location: KeyboardEvent.DOM_KEY_LOCATION_NUMPAD }, |
michael@0 | 245 | }, |
michael@0 | 246 | ]; |
michael@0 | 247 | |
michael@0 | 248 | function getLocationName(aLocation) |
michael@0 | 249 | { |
michael@0 | 250 | switch (aLocation) { |
michael@0 | 251 | case KeyboardEvent.DOM_KEY_LOCATION_STANDARD: |
michael@0 | 252 | return "DOM_KEY_LOCATION_STANDARD"; |
michael@0 | 253 | case KeyboardEvent.DOM_KEY_LOCATION_LEFT: |
michael@0 | 254 | return "DOM_KEY_LOCATION_LEFT"; |
michael@0 | 255 | case KeyboardEvent.DOM_KEY_LOCATION_RIGHT: |
michael@0 | 256 | return "DOM_KEY_LOCATION_RIGHT"; |
michael@0 | 257 | case KeyboardEvent.DOM_KEY_LOCATION_MOBILE: |
michael@0 | 258 | return "DOM_KEY_LOCATION_MOBILE"; |
michael@0 | 259 | case KeyboardEvent.DOM_KEY_LOCATION_NUMPAD: |
michael@0 | 260 | return "DOM_KEY_LOCATION_NUMPAD"; |
michael@0 | 261 | case KeyboardEvent.DOM_KEY_LOCATION_JOYSTICK: |
michael@0 | 262 | return "DOM_KEY_LOCATION_JOYSTICK"; |
michael@0 | 263 | default: |
michael@0 | 264 | return "Invalid value (" + aLocation + ")"; |
michael@0 | 265 | } |
michael@0 | 266 | } |
michael@0 | 267 | |
michael@0 | 268 | var currentTest, description; |
michael@0 | 269 | var events = { keydown: false, keypress: false, keyup: false }; |
michael@0 | 270 | |
michael@0 | 271 | function handler(aEvent) |
michael@0 | 272 | { |
michael@0 | 273 | is(aEvent.location, currentTest.event.location, |
michael@0 | 274 | description + "location of " + aEvent.type + " was invalid"); |
michael@0 | 275 | events[aEvent.type] = true; |
michael@0 | 276 | if (aEvent.type != "keydown" || |
michael@0 | 277 | (currentTest.event.isModifier && aEvent.type == "keydown")) { |
michael@0 | 278 | aEvent.preventDefault(); |
michael@0 | 279 | } |
michael@0 | 280 | } |
michael@0 | 281 | |
michael@0 | 282 | window.addEventListener("keydown", handler, true); |
michael@0 | 283 | window.addEventListener("keypress", handler, true); |
michael@0 | 284 | window.addEventListener("keyup", handler, true); |
michael@0 | 285 | |
michael@0 | 286 | for (var i = 0; i < kTests.length; i++) { |
michael@0 | 287 | currentTest = kTests[i]; |
michael@0 | 288 | events = { keydown: false, keypress: false, keyup: false }; |
michael@0 | 289 | description = "testSynthesizedKeyLocation, " + i + ", key: " + |
michael@0 | 290 | currentTest.key + ", location: " + |
michael@0 | 291 | getLocationName(currentTest.event.location) + ": "; |
michael@0 | 292 | synthesizeKey(currentTest.key, currentTest.event); |
michael@0 | 293 | ok(events.keydown, description + "keydown event wasn't fired"); |
michael@0 | 294 | if (currentTest.isModifier) { |
michael@0 | 295 | todo(events.keypress, description + "keypress event was fired for modifier key"); |
michael@0 | 296 | } else { |
michael@0 | 297 | ok(events.keypress, description + "keypress event wasn't fired"); |
michael@0 | 298 | } |
michael@0 | 299 | ok(events.keyup, description + "keyup event wasn't fired"); |
michael@0 | 300 | } |
michael@0 | 301 | |
michael@0 | 302 | window.removeEventListener("keydown", handler, true); |
michael@0 | 303 | window.removeEventListener("keypress", handler, true); |
michael@0 | 304 | window.removeEventListener("keyup", handler, true); |
michael@0 | 305 | } |
michael@0 | 306 | |
michael@0 | 307 | function runTests() |
michael@0 | 308 | { |
michael@0 | 309 | testInitializingUntrustedEvent(); |
michael@0 | 310 | testSynthesizedKeyLocation(); |
michael@0 | 311 | SimpleTest.finish(); |
michael@0 | 312 | } |
michael@0 | 313 | |
michael@0 | 314 | </script> |
michael@0 | 315 | </pre> |
michael@0 | 316 | </body> |
michael@0 | 317 | </html> |