import keysyms from '../core/input/keysymdef.js';
import * as KeyboardUtil from "../core/input/util.js";
+function isIE() {
+ return navigator && !!(/trident/i).exec(navigator.userAgent);
+}
+function isEdge() {
+ return navigator && !!(/edge/i).exec(navigator.userAgent);
+}
+
describe('Helpers', function() {
"use strict";
- describe('keysymFromKeyCode', function() {
- it('should map known keycodes to keysyms', function() {
- expect(KeyboardUtil.keysymFromKeyCode(0x41, false), 'a').to.be.equal(0x61);
- expect(KeyboardUtil.keysymFromKeyCode(0x41, true), 'A').to.be.equal(0x41);
- expect(KeyboardUtil.keysymFromKeyCode(0xd, false), 'enter').to.be.equal(0xFF0D);
- expect(KeyboardUtil.keysymFromKeyCode(0x11, false), 'ctrl').to.be.equal(0xFFE3);
- expect(KeyboardUtil.keysymFromKeyCode(0x12, false), 'alt').to.be.equal(0xFFE9);
- expect(KeyboardUtil.keysymFromKeyCode(0xe1, false), 'altgr').to.be.equal(0xFE03);
- expect(KeyboardUtil.keysymFromKeyCode(0x1b, false), 'esc').to.be.equal(0xFF1B);
- expect(KeyboardUtil.keysymFromKeyCode(0x26, false), 'up').to.be.equal(0xFF52);
- });
- it('should return null for unknown keycodes', function() {
- expect(KeyboardUtil.keysymFromKeyCode(0xc0, false), 'DK æ').to.be.null;
- expect(KeyboardUtil.keysymFromKeyCode(0xde, false), 'DK ø').to.be.null;
- });
- });
describe('keysyms.lookup', function() {
it('should map ASCII characters to keysyms', function() {
});
});
- describe('nonCharacterKey', function() {
- it('should recognize the right keys', function() {
- expect(KeyboardUtil.nonCharacterKey({keyCode: 0xd}), 'enter').to.be.defined;
- expect(KeyboardUtil.nonCharacterKey({keyCode: 0x08}), 'backspace').to.be.defined;
- expect(KeyboardUtil.nonCharacterKey({keyCode: 0x09}), 'tab').to.be.defined;
- expect(KeyboardUtil.nonCharacterKey({keyCode: 0x10}), 'shift').to.be.defined;
- expect(KeyboardUtil.nonCharacterKey({keyCode: 0x11}), 'ctrl').to.be.defined;
- expect(KeyboardUtil.nonCharacterKey({keyCode: 0x12}), 'alt').to.be.defined;
- expect(KeyboardUtil.nonCharacterKey({keyCode: 0xe0}), 'meta').to.be.defined;
- });
- it('should not recognize character keys', function() {
- expect(KeyboardUtil.nonCharacterKey({keyCode: 'A'}), 'A').to.be.null;
- expect(KeyboardUtil.nonCharacterKey({keyCode: '1'}), '1').to.be.null;
- expect(KeyboardUtil.nonCharacterKey({keyCode: '.'}), '.').to.be.null;
- expect(KeyboardUtil.nonCharacterKey({keyCode: ' '}), 'space').to.be.null;
+ describe('getKeycode', function() {
+ it('should pass through proper code', function() {
+ expect(KeyboardUtil.getKeycode({code: 'Semicolon'})).to.be.equal('Semicolon');
+ });
+ it('should map legacy values', function() {
+ expect(KeyboardUtil.getKeycode({code: ''})).to.be.equal('Unidentified');
+ expect(KeyboardUtil.getKeycode({code: 'OSLeft'})).to.be.equal('MetaLeft');
+ });
+ it('should map keyCode to code when possible', function() {
+ expect(KeyboardUtil.getKeycode({keyCode: 0x14})).to.be.equal('CapsLock');
+ expect(KeyboardUtil.getKeycode({keyCode: 0x5b})).to.be.equal('MetaLeft');
+ expect(KeyboardUtil.getKeycode({keyCode: 0x35})).to.be.equal('Digit5');
+ expect(KeyboardUtil.getKeycode({keyCode: 0x65})).to.be.equal('Numpad5');
+ });
+ it('should map keyCode left/right side', function() {
+ expect(KeyboardUtil.getKeycode({keyCode: 0x10, location: 1})).to.be.equal('ShiftLeft');
+ expect(KeyboardUtil.getKeycode({keyCode: 0x10, location: 2})).to.be.equal('ShiftRight');
+ expect(KeyboardUtil.getKeycode({keyCode: 0x11, location: 1})).to.be.equal('ControlLeft');
+ expect(KeyboardUtil.getKeycode({keyCode: 0x11, location: 2})).to.be.equal('ControlRight');
+ });
+ it('should map keyCode on numpad', function() {
+ expect(KeyboardUtil.getKeycode({keyCode: 0x0d, location: 0})).to.be.equal('Enter');
+ expect(KeyboardUtil.getKeycode({keyCode: 0x0d, location: 3})).to.be.equal('NumpadEnter');
+ expect(KeyboardUtil.getKeycode({keyCode: 0x23, location: 0})).to.be.equal('End');
+ expect(KeyboardUtil.getKeycode({keyCode: 0x23, location: 3})).to.be.equal('Numpad1');
+ });
+ it('should return Unidentified when it cannot map the keyCode', function() {
+ expect(KeyboardUtil.getKeycode({keycode: 0x42})).to.be.equal('Unidentified');
+ });
+
+ describe('Fix Meta on macOS', function() {
+ var origNavigator;
+ beforeEach(function () {
+ // window.navigator is a protected read-only property in many
+ // environments, so we need to redefine it whilst running these
+ // tests.
+ origNavigator = Object.getOwnPropertyDescriptor(window, "navigator");
+ if (origNavigator === undefined) {
+ // Object.getOwnPropertyDescriptor() doesn't work
+ // properly in any version of IE
+ this.skip();
+ }
+
+ Object.defineProperty(window, "navigator", {value: {}});
+ if (window.navigator.platform !== undefined) {
+ // Object.defineProperty() doesn't work properly in old
+ // versions of Chrome
+ this.skip();
+ }
+
+ window.navigator.platform = "Mac x86_64";
+ });
+ afterEach(function () {
+ Object.defineProperty(window, "navigator", origNavigator);
+ });
+
+ it('should respect ContextMenu on modern browser', function() {
+ expect(KeyboardUtil.getKeycode({code: 'ContextMenu', keyCode: 0x5d})).to.be.equal('ContextMenu');
+ });
+ it('should translate legacy ContextMenu to MetaRight', function() {
+ expect(KeyboardUtil.getKeycode({keyCode: 0x5d})).to.be.equal('MetaRight');
+ });
});
});
- describe('getKeysym', function() {
- it('should prefer char', function() {
- expect(KeyboardUtil.getKeysym({char : 'a', charCode: 'Š'.charCodeAt(), keyCode: 0x42, which: 0x43})).to.be.equal(0x61);
+ describe('getKey', function() {
+ it('should prefer key', function() {
+ if (isIE() || isEdge()) this.skip();
+ expect(KeyboardUtil.getKey({key: 'a', charCode: 'Š'.charCodeAt(), keyCode: 0x42, which: 0x43})).to.be.equal('a');
});
- it('should use charCode if no char', function() {
- expect(KeyboardUtil.getKeysym({char : '', charCode: 'Š'.charCodeAt(), keyCode: 0x42, which: 0x43})).to.be.equal(0x01a9);
- expect(KeyboardUtil.getKeysym({charCode: 'Š'.charCodeAt(), keyCode: 0x42, which: 0x43})).to.be.equal(0x01a9);
- expect(KeyboardUtil.getKeysym({char : 'hello', charCode: 'Š'.charCodeAt(), keyCode: 0x42, which: 0x43})).to.be.equal(0x01a9);
+ it('should map legacy values', function() {
+ expect(KeyboardUtil.getKey({key: 'Spacebar'})).to.be.equal(' ');
+ expect(KeyboardUtil.getKey({key: 'Left'})).to.be.equal('ArrowLeft');
+ expect(KeyboardUtil.getKey({key: 'OS'})).to.be.equal('Meta');
+ expect(KeyboardUtil.getKey({key: 'Win'})).to.be.equal('Meta');
+ expect(KeyboardUtil.getKey({key: 'UIKeyInputLeftArrow'})).to.be.equal('ArrowLeft');
});
- it('should use keyCode if no charCode', function() {
- expect(KeyboardUtil.getKeysym({keyCode: 0x42, which: 0x43, shiftKey: false})).to.be.equal(0x62);
- expect(KeyboardUtil.getKeysym({keyCode: 0x42, which: 0x43, shiftKey: true})).to.be.equal(0x42);
+ it('should use code if no key', function() {
+ expect(KeyboardUtil.getKey({code: 'NumpadBackspace'})).to.be.equal('Backspace');
});
- it('should use which if no keyCode', function() {
- expect(KeyboardUtil.getKeysym({which: 0x43, shiftKey: false})).to.be.equal(0x63);
- expect(KeyboardUtil.getKeysym({which: 0x43, shiftKey: true})).to.be.equal(0x43);
+ it('should not use code fallback for character keys', function() {
+ expect(KeyboardUtil.getKey({code: 'KeyA'})).to.be.equal('Unidentified');
+ expect(KeyboardUtil.getKey({code: 'Digit1'})).to.be.equal('Unidentified');
+ expect(KeyboardUtil.getKey({code: 'Period'})).to.be.equal('Unidentified');
+ expect(KeyboardUtil.getKey({code: 'Numpad1'})).to.be.equal('Unidentified');
});
- });
+ it('should use charCode if no key', function() {
+ expect(KeyboardUtil.getKey({charCode: 'Š'.charCodeAt(), keyCode: 0x42, which: 0x43})).to.be.equal('Š');
+ });
+ it('should return Unidentified when it cannot map the key', function() {
+ expect(KeyboardUtil.getKey({keycode: 0x42})).to.be.equal('Unidentified');
+ });
+
+ describe('Broken key AltGraph on IE/Edge', function() {
+ var origNavigator;
+ beforeEach(function () {
+ // window.navigator is a protected read-only property in many
+ // environments, so we need to redefine it whilst running these
+ // tests.
+ origNavigator = Object.getOwnPropertyDescriptor(window, "navigator");
+ if (origNavigator === undefined) {
+ // Object.getOwnPropertyDescriptor() doesn't work
+ // properly in any version of IE
+ this.skip();
+ }
- describe('Modifier Sync', function() { // return a list of fake events necessary to fix modifier state
- describe('Toggle all modifiers', function() {
- var sync = KeyboardUtil.ModifierSync();
- it ('should do nothing if all modifiers are up as expected', function() {
- expect(sync.keydown({
- keyCode: 0x41,
- ctrlKey: false,
- altKey: false,
- altGraphKey: false,
- shiftKey: false,
- metaKey: false})
- ).to.have.lengthOf(0);
- });
- it ('should synthesize events if all keys are unexpectedly down', function() {
- var result = sync.keydown({
- keyCode: 0x41,
- ctrlKey: true,
- altKey: true,
- altGraphKey: true,
- shiftKey: true,
- metaKey: true
- });
- expect(result).to.have.lengthOf(5);
- var keysyms = {};
- for (var i = 0; i < result.length; ++i) {
- keysyms[result[i].keysym] = (result[i].type == 'keydown');
+ Object.defineProperty(window, "navigator", {value: {}});
+ if (window.navigator.platform !== undefined) {
+ // Object.defineProperty() doesn't work properly in old
+ // versions of Chrome
+ this.skip();
}
- expect(keysyms[0xffe3]);
- expect(keysyms[0xffe9]);
- expect(keysyms[0xfe03]);
- expect(keysyms[0xffe1]);
- expect(keysyms[0xffe7]);
- });
- it ('should do nothing if all modifiers are down as expected', function() {
- expect(sync.keydown({
- keyCode: 0x41,
- ctrlKey: true,
- altKey: true,
- altGraphKey: true,
- shiftKey: true,
- metaKey: true
- })).to.have.lengthOf(0);
- });
- });
- describe('Toggle Ctrl', function() {
- var sync = KeyboardUtil.ModifierSync();
- it('should sync if modifier is suddenly down', function() {
- expect(sync.keydown({
- keyCode: 0x41,
- ctrlKey: true,
- })).to.be.deep.equal([{keysym: 0xffe3, type: 'keydown'}]);
- });
- it('should sync if modifier is suddenly up', function() {
- expect(sync.keydown({
- keyCode: 0x41,
- ctrlKey: false
- })).to.be.deep.equal([{keysym: 0xffe3, type: 'keyup'}]);
- });
- });
- describe('Toggle Alt', function() {
- var sync = KeyboardUtil.ModifierSync();
- it('should sync if modifier is suddenly down', function() {
- expect(sync.keydown({
- keyCode: 0x41,
- altKey: true,
- })).to.be.deep.equal([{keysym: 0xffe9, type: 'keydown'}]);
- });
- it('should sync if modifier is suddenly up', function() {
- expect(sync.keydown({
- keyCode: 0x41,
- altKey: false
- })).to.be.deep.equal([{keysym: 0xffe9, type: 'keyup'}]);
- });
- });
- describe('Toggle AltGr', function() {
- var sync = KeyboardUtil.ModifierSync();
- it('should sync if modifier is suddenly down', function() {
- expect(sync.keydown({
- keyCode: 0x41,
- altGraphKey: true,
- })).to.be.deep.equal([{keysym: 0xfe03, type: 'keydown'}]);
- });
- it('should sync if modifier is suddenly up', function() {
- expect(sync.keydown({
- keyCode: 0x41,
- altGraphKey: false
- })).to.be.deep.equal([{keysym: 0xfe03, type: 'keyup'}]);
- });
- });
- describe('Toggle Shift', function() {
- var sync = KeyboardUtil.ModifierSync();
- it('should sync if modifier is suddenly down', function() {
- expect(sync.keydown({
- keyCode: 0x41,
- shiftKey: true,
- })).to.be.deep.equal([{keysym: 0xffe1, type: 'keydown'}]);
- });
- it('should sync if modifier is suddenly up', function() {
- expect(sync.keydown({
- keyCode: 0x41,
- shiftKey: false
- })).to.be.deep.equal([{keysym: 0xffe1, type: 'keyup'}]);
- });
- });
- describe('Toggle Meta', function() {
- var sync = KeyboardUtil.ModifierSync();
- it('should sync if modifier is suddenly down', function() {
- expect(sync.keydown({
- keyCode: 0x41,
- metaKey: true,
- })).to.be.deep.equal([{keysym: 0xffe7, type: 'keydown'}]);
- });
- it('should sync if modifier is suddenly up', function() {
- expect(sync.keydown({
- keyCode: 0x41,
- metaKey: false
- })).to.be.deep.equal([{keysym: 0xffe7, type: 'keyup'}]);
- });
- });
- describe('Modifier keyevents', function() {
- it('should not sync a modifier on its own events', function() {
- expect(KeyboardUtil.ModifierSync().keydown({
- keyCode: 0x11,
- ctrlKey: false
- })).to.be.deep.equal([]);
- expect(KeyboardUtil.ModifierSync().keydown({
- keyCode: 0x11,
- ctrlKey: true
- }), 'B').to.be.deep.equal([]);
- })
- it('should update state on modifier keyevents', function() {
- var sync = KeyboardUtil.ModifierSync();
- sync.keydown({
- keyCode: 0x11,
- });
- expect(sync.keydown({
- keyCode: 0x41,
- ctrlKey: true,
- })).to.be.deep.equal([]);
- });
- it('should sync other modifiers on ctrl events', function() {
- expect(KeyboardUtil.ModifierSync().keydown({
- keyCode: 0x11,
- altKey: true
- })).to.be.deep.equal([{keysym: 0xffe9, type: 'keydown'}]);
- })
- });
- describe('sync modifiers on non-key events', function() {
- it('should generate sync events when receiving non-keyboard events', function() {
- expect(KeyboardUtil.ModifierSync().syncAny({
- altKey: true
- })).to.be.deep.equal([{keysym: 0xffe9, type: 'keydown'}]);
- });
- });
- describe('do not treat shift as a modifier key', function() {
- it('should not treat shift as a shortcut modifier', function() {
- expect(KeyboardUtil.hasShortcutModifier([], {0xffe1 : true})).to.be.false;
- });
- it('should not treat shift as a char modifier', function() {
- expect(KeyboardUtil.hasCharModifier([], {0xffe1 : true})).to.be.false;
+ });
+ afterEach(function () {
+ Object.defineProperty(window, "navigator", origNavigator);
+ });
+
+ it('should ignore printable character key on IE', function() {
+ window.navigator.userAgent = "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko";
+ expect(KeyboardUtil.getKey({key: 'a'})).to.be.equal('Unidentified');
+ });
+ it('should ignore printable character key on Edge', function() {
+ window.navigator.userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393";
+ expect(KeyboardUtil.getKey({key: 'a'})).to.be.equal('Unidentified');
+ });
+ it('should allow non-printable character key on IE', function() {
+ window.navigator.userAgent = "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko";
+ expect(KeyboardUtil.getKey({key: 'Shift'})).to.be.equal('Shift');
+ });
+ it('should allow non-printable character key on Edge', function() {
+ window.navigator.userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393";
+ expect(KeyboardUtil.getKey({key: 'Shift'})).to.be.equal('Shift');
+ });
+ });
+ });
+
+ describe('getKeysym', function() {
+ describe('Non-character keys', function() {
+ it('should recognize the right keys', function() {
+ expect(KeyboardUtil.getKeysym({key: 'Enter'})).to.be.equal(0xFF0D);
+ expect(KeyboardUtil.getKeysym({key: 'Backspace'})).to.be.equal(0xFF08);
+ expect(KeyboardUtil.getKeysym({key: 'Tab'})).to.be.equal(0xFF09);
+ expect(KeyboardUtil.getKeysym({key: 'Shift'})).to.be.equal(0xFFE1);
+ expect(KeyboardUtil.getKeysym({key: 'Control'})).to.be.equal(0xFFE3);
+ expect(KeyboardUtil.getKeysym({key: 'Alt'})).to.be.equal(0xFFE9);
+ expect(KeyboardUtil.getKeysym({key: 'Meta'})).to.be.equal(0xFFEB);
+ expect(KeyboardUtil.getKeysym({key: 'Escape'})).to.be.equal(0xFF1B);
+ expect(KeyboardUtil.getKeysym({key: 'ArrowUp'})).to.be.equal(0xFF52);
+ });
+ it('should map left/right side', function() {
+ expect(KeyboardUtil.getKeysym({key: 'Shift', location: 1})).to.be.equal(0xFFE1);
+ expect(KeyboardUtil.getKeysym({key: 'Shift', location: 2})).to.be.equal(0xFFE2);
+ expect(KeyboardUtil.getKeysym({key: 'Control', location: 1})).to.be.equal(0xFFE3);
+ expect(KeyboardUtil.getKeysym({key: 'Control', location: 2})).to.be.equal(0xFFE4);
+ });
+ it('should handle AltGraph', function() {
+ expect(KeyboardUtil.getKeysym({code: 'AltRight', key: 'Alt', location: 2})).to.be.equal(0xFFEA);
+ expect(KeyboardUtil.getKeysym({code: 'AltRight', key: 'AltGraph', location: 2})).to.be.equal(0xFE03);
+ });
+ it('should return null for unknown keys', function() {
+ expect(KeyboardUtil.getKeysym({key: 'Semicolon'})).to.be.null;
+ expect(KeyboardUtil.getKeysym({key: 'BracketRight'})).to.be.null;
+ });
+ it('should handle remappings', function() {
+ expect(KeyboardUtil.getKeysym({code: 'ControlLeft', key: 'Tab'})).to.be.equal(0xFF09);
+ });
+ });
+
+ describe('Numpad', function() {
+ it('should handle Numpad numbers', function() {
+ if (isIE() || isEdge()) this.skip();
+ expect(KeyboardUtil.getKeysym({code: 'Digit5', key: '5', location: 0})).to.be.equal(0x0035);
+ expect(KeyboardUtil.getKeysym({code: 'Numpad5', key: '5', location: 3})).to.be.equal(0xFFB5);
+ });
+ it('should handle Numpad non-character keys', function() {
+ expect(KeyboardUtil.getKeysym({code: 'Home', key: 'Home', location: 0})).to.be.equal(0xFF50);
+ expect(KeyboardUtil.getKeysym({code: 'Numpad5', key: 'Home', location: 3})).to.be.equal(0xFF95);
+ expect(KeyboardUtil.getKeysym({code: 'Delete', key: 'Delete', location: 0})).to.be.equal(0xFFFF);
+ expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: 'Delete', location: 3})).to.be.equal(0xFF9F);
+ });
+ it('should handle Numpad Decimal key', function() {
+ if (isIE() || isEdge()) this.skip();
+ expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: '.', location: 3})).to.be.equal(0xFFAE);
+ expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: ',', location: 3})).to.be.equal(0xFFAC);
});
});
});