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