Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 7732d93

Browse files
haslam22petetnt
authored andcommitted
Issue #12859 Keyboard modifiers support for simulateKeyEvent (#12863)
* Update simulateKeyEvent() to accept additional options * Fix missing ; error * Update error text * Add KeyboardEvent global to fix no-undef error * API limits . . . * Add requested changes & unit test * Remove object.assign() * Update method comments
1 parent 4be271e commit 7732d93

File tree

3 files changed

+90
-16
lines changed

3 files changed

+90
-16
lines changed

test/UnitTestSuite.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ define(function (require, exports, module) {
7878
require("spec/QuickOpen-test");
7979
require("spec/QuickSearchField-test");
8080
require("spec/RemoteFunctions-test");
81+
require("spec/SpecRunnerUtils-test");
8182
require("spec/StringMatch-test");
8283
require("spec/StringUtils-test");
8384
require("spec/TextRange-test");

test/spec/SpecRunnerUtils-test.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2013 - present Adobe Systems Incorporated. All rights reserved.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a
5+
* copy of this software and associated documentation files (the "Software"),
6+
* to deal in the Software without restriction, including without limitation
7+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+
* and/or sell copies of the Software, and to permit persons to whom the
9+
* Software is furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20+
* DEALINGS IN THE SOFTWARE.
21+
*
22+
*/
23+
24+
/*global describe, it, expect, beforeEach, afterEach */
25+
26+
define(function (require, exports, module) {
27+
'use strict';
28+
29+
var SpecRunnerUtils = require("spec/SpecRunnerUtils");
30+
31+
describe("SpecRunnerUtils", function () {
32+
describe("simulateKeyEvent", function () {
33+
var mockElement, capturedEvent;
34+
35+
beforeEach(function () {
36+
mockElement = SpecRunnerUtils.createMockElement();
37+
mockElement.on("keydown", function(event) {
38+
capturedEvent = event;
39+
});
40+
});
41+
42+
afterEach(function () {
43+
mockElement.remove();
44+
capturedEvent = null;
45+
});
46+
47+
it("should create and dispatch a key event to an element", function () {
48+
SpecRunnerUtils.simulateKeyEvent(82, "keydown", mockElement[0]);
49+
expect(capturedEvent.keyCode).toEqual(82);
50+
expect(capturedEvent.which).toEqual(82);
51+
expect(capturedEvent.charCode).toEqual(82);
52+
});
53+
54+
it("should create and dispatch a key event with modifiers to an element", function () {
55+
var modifiers = {
56+
ctrlKey: true,
57+
altKey: true
58+
};
59+
SpecRunnerUtils.simulateKeyEvent(82, "keydown", mockElement[0], modifiers);
60+
expect(capturedEvent.keyCode).toEqual(82);
61+
expect(capturedEvent.which).toEqual(82);
62+
expect(capturedEvent.charCode).toEqual(82);
63+
expect(capturedEvent.ctrlKey).toEqual(true);
64+
expect(capturedEvent.altKey).toEqual(true);
65+
});
66+
});
67+
});
68+
});

test/spec/SpecRunnerUtils.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*
2222
*/
2323

24-
/*global jasmine, expect, beforeEach, waitsFor, waitsForDone, runs, spyOn */
24+
/*global jasmine, expect, beforeEach, waitsFor, waitsForDone, runs, spyOn, KeyboardEvent */
2525

2626
define(function (require, exports, module) {
2727
'use strict';
@@ -992,18 +992,29 @@ define(function (require, exports, module) {
992992
}
993993

994994
/**
995-
* Simulate key event. Found this code here:
996-
* http://stackoverflow.com/questions/10455626/keydown-simulation-in-chrome-fires-normally-but-not-the-correct-key
997-
*
998-
* TODO: need parameter(s) for modifier keys
999-
*
995+
* Simulate a key event.
1000996
* @param {Number} key Key code
1001997
* @param (String) event Key event to simulate
1002998
* @param {HTMLElement} element Element to receive event
999+
* @param {KeyboardEventInit} options Optional arguments for key event
10031000
*/
1004-
function simulateKeyEvent(key, event, element) {
1005-
var doc = element.ownerDocument,
1006-
oEvent = doc.createEvent('KeyboardEvent');
1001+
function simulateKeyEvent(key, event, element, options) {
1002+
var doc = element.ownerDocument;
1003+
1004+
if(typeof options === 'undefined') {
1005+
options = {
1006+
view: doc.defaultView,
1007+
bubbles: true,
1008+
cancelable: true,
1009+
keyIdentifer: key
1010+
};
1011+
} else {
1012+
options.view = doc.defaultView;
1013+
options.bubbles = true;
1014+
options.cancelable = true;
1015+
options.keyIdentifier = key;
1016+
}
1017+
var oEvent = new KeyboardEvent(event, options);
10071018

10081019
if (event !== "keydown" && event !== "keyup" && event !== "keypress") {
10091020
console.log("SpecRunnerUtils.simulateKeyEvent() - unsupported keyevent: " + event);
@@ -1029,15 +1040,9 @@ define(function (require, exports, module) {
10291040
}
10301041
});
10311042

1032-
if (oEvent.initKeyboardEvent) {
1033-
oEvent.initKeyboardEvent(event, true, true, doc.defaultView, key, 0, false, false, false, false);
1034-
} else {
1035-
oEvent.initKeyEvent(event, true, true, doc.defaultView, false, false, false, false, key, 0);
1036-
}
1037-
10381043
oEvent.keyCodeVal = key;
10391044
if (oEvent.keyCode !== key) {
1040-
console.log("keyCode mismatch " + oEvent.keyCode + "(" + oEvent.which + ")");
1045+
console.log("SpecRunnerUtils.simulateKeyEvent() - keyCode mismatch: " + oEvent.keyCode);
10411046
}
10421047

10431048
element.dispatchEvent(oEvent);

0 commit comments

Comments
 (0)