Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.
36 changes: 22 additions & 14 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 @@ -995,20 +995,34 @@ define(function (require, exports, module) {
* Simulate key event. Found this code here:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove these comments now as the implementation is different

* http://stackoverflow.com/questions/10455626/keydown-simulation-in-chrome-fires-normally-but-not-the-correct-key
*
* TODO: need parameter(s) for modifier keys
*
* @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') {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use Object.assign to make this a bit easier:

var defaultOptions = {
  options.view = doc.defaultView;
  options.bubbles = true;
  options.cancelable = true;
  options.keyIdentifier = key;
};
var newOptions = Object.assign({}, options, defaultOptions});
var oEvent = new KeyboardEvent(event, newOptions);

options = {
view: doc.defaultView,
bubbles: true,
cancelable: true,
keyIdentifier: 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);
return;
}
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: trailing whitespace


// Chromium Hack: need to override the 'which' property.
// Note: this code is not designed to work in IE, Safari,
Expand All @@ -1029,15 +1043,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