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

Commit

Permalink
Issue #12859 Keyboard modifiers support for simulateKeyEvent (#12863)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
haslam22 authored and petetnt committed Dec 23, 2016
1 parent 4be271e commit 7732d93
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 16 deletions.
1 change: 1 addition & 0 deletions test/UnitTestSuite.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ define(function (require, exports, module) {
require("spec/QuickOpen-test");
require("spec/QuickSearchField-test");
require("spec/RemoteFunctions-test");
require("spec/SpecRunnerUtils-test");
require("spec/StringMatch-test");
require("spec/StringUtils-test");
require("spec/TextRange-test");
Expand Down
68 changes: 68 additions & 0 deletions test/spec/SpecRunnerUtils-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2013 - present Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/

/*global describe, it, expect, beforeEach, afterEach */

define(function (require, exports, module) {
'use strict';

var SpecRunnerUtils = require("spec/SpecRunnerUtils");

describe("SpecRunnerUtils", function () {
describe("simulateKeyEvent", function () {
var mockElement, capturedEvent;

beforeEach(function () {
mockElement = SpecRunnerUtils.createMockElement();
mockElement.on("keydown", function(event) {
capturedEvent = event;
});
});

afterEach(function () {
mockElement.remove();
capturedEvent = null;
});

it("should create and dispatch a key event to an element", function () {
SpecRunnerUtils.simulateKeyEvent(82, "keydown", mockElement[0]);
expect(capturedEvent.keyCode).toEqual(82);
expect(capturedEvent.which).toEqual(82);
expect(capturedEvent.charCode).toEqual(82);
});

it("should create and dispatch a key event with modifiers to an element", function () {
var modifiers = {
ctrlKey: true,
altKey: true
};
SpecRunnerUtils.simulateKeyEvent(82, "keydown", mockElement[0], modifiers);
expect(capturedEvent.keyCode).toEqual(82);
expect(capturedEvent.which).toEqual(82);
expect(capturedEvent.charCode).toEqual(82);
expect(capturedEvent.ctrlKey).toEqual(true);
expect(capturedEvent.altKey).toEqual(true);
});
});
});
});
37 changes: 21 additions & 16 deletions test/spec/SpecRunnerUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
*/

/*global jasmine, expect, beforeEach, waitsFor, waitsForDone, runs, spyOn */
/*global jasmine, expect, beforeEach, waitsFor, waitsForDone, runs, spyOn, KeyboardEvent */

define(function (require, exports, module) {
'use strict';
Expand Down Expand Up @@ -992,18 +992,29 @@ define(function (require, exports, module) {
}

/**
* Simulate key event. Found this code here:
* http://stackoverflow.com/questions/10455626/keydown-simulation-in-chrome-fires-normally-but-not-the-correct-key
*
* TODO: need parameter(s) for modifier keys
*
* Simulate a key event.
* @param {Number} key Key code
* @param (String) event Key event to simulate
* @param {HTMLElement} element Element to receive event
* @param {KeyboardEventInit} options Optional arguments for key event
*/
function simulateKeyEvent(key, event, element) {
var doc = element.ownerDocument,
oEvent = doc.createEvent('KeyboardEvent');
function simulateKeyEvent(key, event, element, options) {
var doc = element.ownerDocument;

if(typeof options === 'undefined') {
options = {
view: doc.defaultView,
bubbles: true,
cancelable: true,
keyIdentifer: key
};
} else {
options.view = doc.defaultView;
options.bubbles = true;
options.cancelable = true;
options.keyIdentifier = key;
}
var oEvent = new KeyboardEvent(event, options);

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

if (oEvent.initKeyboardEvent) {
oEvent.initKeyboardEvent(event, true, true, doc.defaultView, key, 0, false, false, false, false);
} else {
oEvent.initKeyEvent(event, true, true, doc.defaultView, false, false, false, false, key, 0);
}

oEvent.keyCodeVal = key;
if (oEvent.keyCode !== key) {
console.log("keyCode mismatch " + oEvent.keyCode + "(" + oEvent.which + ")");
console.log("SpecRunnerUtils.simulateKeyEvent() - keyCode mismatch: " + oEvent.keyCode);
}

element.dispatchEvent(oEvent);
Expand Down

0 comments on commit 7732d93

Please sign in to comment.