]> git.proxmox.com Git - mirror_novnc.git/blame - tests/test.helper.js
Move keyboard handling in to Keyboard class
[mirror_novnc.git] / tests / test.helper.js
CommitLineData
dfae3209 1var assert = chai.assert;
f00b6fb6 2var expect = chai.expect;
3
dfae3209
SR
4import keysyms from '../core/input/keysymdef.js';
5import * as KeyboardUtil from "../core/input/util.js";
6
f00b6fb6 7describe('Helpers', function() {
8 "use strict";
f00b6fb6 9
524d67f2 10 describe('keysyms.lookup', function() {
f00b6fb6 11 it('should map ASCII characters to keysyms', function() {
524d67f2
PO
12 expect(keysyms.lookup('a'.charCodeAt())).to.be.equal(0x61);
13 expect(keysyms.lookup('A'.charCodeAt())).to.be.equal(0x41);
f00b6fb6 14 });
15 it('should map Latin-1 characters to keysyms', function() {
524d67f2 16 expect(keysyms.lookup('ø'.charCodeAt())).to.be.equal(0xf8);
f00b6fb6 17
524d67f2 18 expect(keysyms.lookup('é'.charCodeAt())).to.be.equal(0xe9);
f00b6fb6 19 });
20 it('should map characters that are in Windows-1252 but not in Latin-1 to keysyms', function() {
524d67f2 21 expect(keysyms.lookup('Š'.charCodeAt())).to.be.equal(0x01a9);
f00b6fb6 22 });
23 it('should map characters which aren\'t in Latin1 *or* Windows-1252 to keysyms', function() {
278a5e7f 24 expect(keysyms.lookup('ũ'.charCodeAt())).to.be.equal(0x03fd);
f00b6fb6 25 });
115eedf6 26 it('should map unknown codepoints to the Unicode range', function() {
524d67f2
PO
27 expect(keysyms.lookup('\n'.charCodeAt())).to.be.equal(0x100000a);
28 expect(keysyms.lookup('\u262D'.charCodeAt())).to.be.equal(0x100262d);
e9ddbec5 29 });
331ae153
SM
30 // This requires very recent versions of most browsers... skipping for now
31 it.skip('should map UCS-4 codepoints to the Unicode range', function() {
524d67f2 32 //expect(keysyms.lookup('\u{1F686}'.codePointAt())).to.be.equal(0x101f686);
f00b6fb6 33 });
34 });
35
80cb8ffd
PO
36 describe('getKeycode', function() {
37 it('should pass through proper code', function() {
38 expect(KeyboardUtil.getKeycode({code: 'Semicolon'})).to.be.equal('Semicolon');
39 });
40 it('should map legacy values', function() {
41 expect(KeyboardUtil.getKeycode({code: ''})).to.be.equal('Unidentified');
42 expect(KeyboardUtil.getKeycode({code: 'OSLeft'})).to.be.equal('MetaLeft');
43 });
44 it('should map keyCode to code when possible', function() {
45 expect(KeyboardUtil.getKeycode({keyCode: 0x14})).to.be.equal('CapsLock');
46 expect(KeyboardUtil.getKeycode({keyCode: 0x5b})).to.be.equal('MetaLeft');
47 expect(KeyboardUtil.getKeycode({keyCode: 0x35})).to.be.equal('Digit5');
48 expect(KeyboardUtil.getKeycode({keyCode: 0x65})).to.be.equal('Numpad5');
49 });
50 it('should map keyCode left/right side', function() {
51 expect(KeyboardUtil.getKeycode({keyCode: 0x10, location: 1})).to.be.equal('ShiftLeft');
52 expect(KeyboardUtil.getKeycode({keyCode: 0x10, location: 2})).to.be.equal('ShiftRight');
53 expect(KeyboardUtil.getKeycode({keyCode: 0x11, location: 1})).to.be.equal('ControlLeft');
54 expect(KeyboardUtil.getKeycode({keyCode: 0x11, location: 2})).to.be.equal('ControlRight');
55 });
56 it('should map keyCode on numpad', function() {
57 expect(KeyboardUtil.getKeycode({keyCode: 0x0d, location: 0})).to.be.equal('Enter');
58 expect(KeyboardUtil.getKeycode({keyCode: 0x0d, location: 3})).to.be.equal('NumpadEnter');
59 expect(KeyboardUtil.getKeycode({keyCode: 0x23, location: 0})).to.be.equal('End');
60 expect(KeyboardUtil.getKeycode({keyCode: 0x23, location: 3})).to.be.equal('Numpad1');
61 });
62 it('should return Unidentified when it cannot map the keyCode', function() {
63 expect(KeyboardUtil.getKeycode({keycode: 0x42})).to.be.equal('Unidentified');
64 });
65
66 describe('Fix Meta on macOS', function() {
67 var origNavigator;
68 beforeEach(function () {
69 // window.navigator is a protected read-only property in many
70 // environments, so we need to redefine it whilst running these
71 // tests.
72 origNavigator = Object.getOwnPropertyDescriptor(window, "navigator");
73 if (origNavigator === undefined) {
74 // Object.getOwnPropertyDescriptor() doesn't work
75 // properly in any version of IE
76 this.skip();
77 }
78
79 Object.defineProperty(window, "navigator", {value: {}});
80 if (window.navigator.platform !== undefined) {
81 // Object.defineProperty() doesn't work properly in old
82 // versions of Chrome
83 this.skip();
84 }
85
86 window.navigator.platform = "Mac x86_64";
87 });
88 afterEach(function () {
89 Object.defineProperty(window, "navigator", origNavigator);
90 });
91
92 it('should respect ContextMenu on modern browser', function() {
93 expect(KeyboardUtil.getKeycode({code: 'ContextMenu', keyCode: 0x5d})).to.be.equal('ContextMenu');
94 });
95 it('should translate legacy ContextMenu to MetaRight', function() {
96 expect(KeyboardUtil.getKeycode({keyCode: 0x5d})).to.be.equal('MetaRight');
97 });
98 });
99 });
100
f00b6fb6 101 describe('getKeysym', function() {
bfa1b237
PO
102 it('should prefer key', function() {
103 expect(KeyboardUtil.getKeysym({key: 'a', charCode: 'Š'.charCodeAt(), keyCode: 0x42, which: 0x43})).to.be.equal(0x61);
f00b6fb6 104 });
bfa1b237 105 it('should use charCode if no key', function() {
524d67f2 106 expect(KeyboardUtil.getKeysym({charCode: 'Š'.charCodeAt(), keyCode: 0x42, which: 0x43})).to.be.equal(0x01a9);
f00b6fb6 107 });
a5c8a755
PO
108
109 describe('Non-character keys', function() {
110 it('should recognize the right keys', function() {
f714f7de
PO
111 expect(KeyboardUtil.getKeysym({code: 'Enter'})).to.be.equal(0xFF0D);
112 expect(KeyboardUtil.getKeysym({code: 'Backspace'})).to.be.equal(0xFF08);
113 expect(KeyboardUtil.getKeysym({code: 'Tab'})).to.be.equal(0xFF09);
114 expect(KeyboardUtil.getKeysym({code: 'ShiftLeft'})).to.be.equal(0xFFE1);
115 expect(KeyboardUtil.getKeysym({code: 'ControlLeft'})).to.be.equal(0xFFE3);
116 expect(KeyboardUtil.getKeysym({code: 'AltLeft'})).to.be.equal(0xFFE9);
117 expect(KeyboardUtil.getKeysym({code: 'MetaLeft'})).to.be.equal(0xFFEB);
118 expect(KeyboardUtil.getKeysym({code: 'Escape'})).to.be.equal(0xFF1B);
119 expect(KeyboardUtil.getKeysym({code: 'ArrowUp'})).to.be.equal(0xFF52);
a5c8a755 120 });
f714f7de
PO
121 it('should handle AltGraph', function() {
122 expect(KeyboardUtil.getKeysym({code: 'AltRight', key: 'AltRight'})).to.be.equal(0xFFEA);
123 expect(KeyboardUtil.getKeysym({code: 'AltRight', key: 'AltGraph'})).to.be.equal(0xFE03);
124 });
125 it('should return null for unknown codes', function() {
126 expect(KeyboardUtil.getKeysym({code: 'Semicolon'})).to.be.null;
127 expect(KeyboardUtil.getKeysym({code: 'BracketRight'})).to.be.null;
bfa1b237 128 });
a5c8a755 129 it('should not recognize character keys', function() {
f714f7de
PO
130 expect(KeyboardUtil.getKeysym({code: 'KeyA'})).to.be.null;
131 expect(KeyboardUtil.getKeysym({code: 'Digit1'})).to.be.null;
132 expect(KeyboardUtil.getKeysym({code: 'Period'})).to.be.null;
133 expect(KeyboardUtil.getKeysym({code: 'Numpad1'})).to.be.null;
134 });
135 });
136
137 describe('Numpad', function() {
138 it('should handle Numpad numbers', function() {
139 expect(KeyboardUtil.getKeysym({code: 'Digit5', key: '5', location: 0})).to.be.equal(0x0035);
140 expect(KeyboardUtil.getKeysym({code: 'Numpad5', key: '5', location: 3})).to.be.equal(0xFFB5);
141 });
142 it('should handle Numpad non-character keys', function() {
143 expect(KeyboardUtil.getKeysym({code: 'Home', key: 'Home', location: 0})).to.be.equal(0xFF50);
144 expect(KeyboardUtil.getKeysym({code: 'Numpad5', key: 'Home', location: 3})).to.be.equal(0xFF95);
145 expect(KeyboardUtil.getKeysym({code: 'Delete', key: 'Delete', location: 0})).to.be.equal(0xFFFF);
146 expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: 'Delete', location: 3})).to.be.equal(0xFF9F);
147 });
148 it('should handle IE/Edge key names', function() {
149 expect(KeyboardUtil.getKeysym({code: 'Numpad6', key: 'Right', location: 3})).to.be.equal(0xFF98);
150 expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: 'Del', location: 3})).to.be.equal(0xFF9F);
151 });
152 it('should handle Numpad Decimal key', function() {
153 expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: '.', location: 3})).to.be.equal(0xFFAE);
154 expect(KeyboardUtil.getKeysym({code: 'NumpadDecimal', key: ',', location: 3})).to.be.equal(0xFFAC);
a5c8a755
PO
155 });
156 });
f00b6fb6 157 });
f00b6fb6 158});