2017-04-20 19:17:05 -04:00
|
|
|
const test = require('tap').test;
|
|
|
|
const Keyboard = require('../../src/io/keyboard');
|
|
|
|
const Runtime = require('../../src/engine/runtime');
|
2016-10-21 18:31:07 -04:00
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('spec', t => {
|
|
|
|
const rt = new Runtime();
|
|
|
|
const k = new Keyboard(rt);
|
2016-10-21 18:31:07 -04:00
|
|
|
|
|
|
|
t.type(k, 'object');
|
|
|
|
t.type(k.postData, 'function');
|
|
|
|
t.type(k.getKeyIsDown, 'function');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2018-04-25 12:26:52 -04:00
|
|
|
test('space key', t => {
|
2017-04-20 19:17:05 -04:00
|
|
|
const rt = new Runtime();
|
|
|
|
const k = new Keyboard(rt);
|
2016-10-21 18:31:07 -04:00
|
|
|
|
|
|
|
k.postData({
|
2018-04-25 12:26:52 -04:00
|
|
|
key: ' ',
|
2016-10-21 18:31:07 -04:00
|
|
|
isDown: true
|
|
|
|
});
|
2018-04-25 12:26:52 -04:00
|
|
|
t.strictDeepEquals(k._keysPressed, ['space']);
|
2016-10-21 18:31:07 -04:00
|
|
|
t.strictEquals(k.getKeyIsDown('space'), true);
|
|
|
|
t.strictEquals(k.getKeyIsDown('any'), true);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2018-04-25 12:26:52 -04:00
|
|
|
test('letter key', t => {
|
2017-04-20 19:17:05 -04:00
|
|
|
const rt = new Runtime();
|
|
|
|
const k = new Keyboard(rt);
|
2016-10-21 18:31:07 -04:00
|
|
|
|
|
|
|
k.postData({
|
2018-04-25 12:26:52 -04:00
|
|
|
key: 'a',
|
2016-10-21 18:31:07 -04:00
|
|
|
isDown: true
|
|
|
|
});
|
2018-04-25 12:26:52 -04:00
|
|
|
t.strictDeepEquals(k._keysPressed, ['A']);
|
|
|
|
t.strictEquals(k.getKeyIsDown(65), true);
|
2016-10-21 18:31:07 -04:00
|
|
|
t.strictEquals(k.getKeyIsDown('a'), true);
|
2018-04-25 12:26:52 -04:00
|
|
|
t.strictEquals(k.getKeyIsDown('A'), true);
|
2016-10-21 18:31:07 -04:00
|
|
|
t.strictEquals(k.getKeyIsDown('any'), true);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2018-04-25 12:26:52 -04:00
|
|
|
test('number key', t => {
|
2017-04-20 19:17:05 -04:00
|
|
|
const rt = new Runtime();
|
|
|
|
const k = new Keyboard(rt);
|
2016-10-21 18:31:07 -04:00
|
|
|
|
|
|
|
k.postData({
|
2018-04-25 12:26:52 -04:00
|
|
|
key: '1',
|
2016-10-21 18:31:07 -04:00
|
|
|
isDown: true
|
|
|
|
});
|
2018-04-25 12:26:52 -04:00
|
|
|
t.strictDeepEquals(k._keysPressed, ['1']);
|
2016-10-21 18:31:07 -04:00
|
|
|
t.strictEquals(k.getKeyIsDown(49), true);
|
2018-04-25 12:26:52 -04:00
|
|
|
t.strictEquals(k.getKeyIsDown('1'), true);
|
2016-10-21 18:31:07 -04:00
|
|
|
t.strictEquals(k.getKeyIsDown('any'), true);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2018-04-25 12:26:52 -04:00
|
|
|
test('non-english key', t => {
|
|
|
|
const rt = new Runtime();
|
|
|
|
const k = new Keyboard(rt);
|
|
|
|
|
|
|
|
k.postData({
|
|
|
|
key: '日',
|
|
|
|
isDown: true
|
|
|
|
});
|
|
|
|
t.strictDeepEquals(k._keysPressed, ['日']);
|
|
|
|
t.strictEquals(k.getKeyIsDown('日'), true);
|
|
|
|
t.strictEquals(k.getKeyIsDown('any'), true);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('ignore modifier key', t => {
|
|
|
|
const rt = new Runtime();
|
|
|
|
const k = new Keyboard(rt);
|
|
|
|
|
|
|
|
k.postData({
|
|
|
|
key: 'Shift',
|
|
|
|
isDown: true
|
|
|
|
});
|
|
|
|
t.strictDeepEquals(k._keysPressed, []);
|
|
|
|
t.strictEquals(k.getKeyIsDown('any'), false);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('keyup', t => {
|
|
|
|
const rt = new Runtime();
|
|
|
|
const k = new Keyboard(rt);
|
2016-10-21 18:31:07 -04:00
|
|
|
|
|
|
|
k.postData({
|
2018-04-25 12:26:52 -04:00
|
|
|
key: 'ArrowLeft',
|
2016-10-21 18:31:07 -04:00
|
|
|
isDown: true
|
|
|
|
});
|
|
|
|
k.postData({
|
2018-04-25 12:26:52 -04:00
|
|
|
key: 'ArrowLeft',
|
2016-10-21 18:31:07 -04:00
|
|
|
isDown: false
|
|
|
|
});
|
|
|
|
t.strictDeepEquals(k._keysPressed, []);
|
2018-04-25 12:26:52 -04:00
|
|
|
t.strictEquals(k.getKeyIsDown('left arrow'), false);
|
2016-10-21 18:31:07 -04:00
|
|
|
t.strictEquals(k.getKeyIsDown('any'), false);
|
|
|
|
t.end();
|
|
|
|
});
|