mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-07-03 01:30:27 -04:00
test: update io_keyboard tests & test for keyboard events
This commit is contained in:
parent
6d8c3a6bf9
commit
34cfc1f2aa
1 changed files with 98 additions and 2 deletions
|
@ -2,6 +2,37 @@ const test = require('tap').test;
|
||||||
const Keyboard = require('../../src/io/keyboard');
|
const Keyboard = require('../../src/io/keyboard');
|
||||||
const Runtime = require('../../src/engine/runtime');
|
const Runtime = require('../../src/engine/runtime');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a "live" object which tracks events that have been emitted. When an
|
||||||
|
* event is emitted, the event object is pushed onto the corresponding array.
|
||||||
|
* Use t.same() to match these (it strictly compares the array against the
|
||||||
|
* expected value, including the length, i.e. number of tims emitted).
|
||||||
|
*
|
||||||
|
* Note that KEY_ANY_PRESSED doesn't emit any event details and so its array
|
||||||
|
* should only contain `undefined`.
|
||||||
|
*
|
||||||
|
* @param {Runtime} rt - the runtime to listen for events on
|
||||||
|
* @returns {object} live mapping of event name to emitted event values
|
||||||
|
*/
|
||||||
|
const listenForKeyboardEvents = function (rt) {
|
||||||
|
const eventsEmitted = {
|
||||||
|
KEY_PRESSED: [],
|
||||||
|
KEY_ANY_PRESSED: []
|
||||||
|
};
|
||||||
|
|
||||||
|
rt.on('KEY_PRESSED', event => {
|
||||||
|
eventsEmitted.KEY_PRESSED.push(event);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Note that KEY_ANY_PRESSED doesn't emit any event details and so
|
||||||
|
// the event value should be `undefined`.
|
||||||
|
rt.on('KEY_ANY_PRESSED', event => {
|
||||||
|
eventsEmitted.KEY_ANY_PRESSED.push(event);
|
||||||
|
});
|
||||||
|
|
||||||
|
return eventsEmitted;
|
||||||
|
};
|
||||||
|
|
||||||
test('spec', t => {
|
test('spec', t => {
|
||||||
const rt = new Runtime();
|
const rt = new Runtime();
|
||||||
const k = new Keyboard(rt);
|
const k = new Keyboard(rt);
|
||||||
|
@ -15,89 +46,154 @@ test('spec', t => {
|
||||||
test('space key', t => {
|
test('space key', t => {
|
||||||
const rt = new Runtime();
|
const rt = new Runtime();
|
||||||
const k = new Keyboard(rt);
|
const k = new Keyboard(rt);
|
||||||
|
const eventsEmitted = listenForKeyboardEvents(rt);
|
||||||
|
|
||||||
k.postData({
|
k.postData({
|
||||||
key: ' ',
|
key: ' ',
|
||||||
isDown: true
|
isDown: true
|
||||||
});
|
});
|
||||||
|
|
||||||
t.strictDeepEquals(k._keysPressed, ['space']);
|
t.strictDeepEquals(k._keysPressed, ['space']);
|
||||||
|
|
||||||
t.strictEquals(k.getKeyIsDown('space'), true);
|
t.strictEquals(k.getKeyIsDown('space'), true);
|
||||||
t.strictEquals(k.getKeyIsDown('any'), true);
|
t.strictEquals(k.getKeyIsDown('any'), true);
|
||||||
|
|
||||||
|
t.same(eventsEmitted.KEY_PRESSED, ['space']);
|
||||||
|
t.same(eventsEmitted.KEY_ANY_PRESSED, [undefined]);
|
||||||
|
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('letter key', t => {
|
test('letter key', t => {
|
||||||
const rt = new Runtime();
|
const rt = new Runtime();
|
||||||
const k = new Keyboard(rt);
|
const k = new Keyboard(rt);
|
||||||
|
const eventsEmitted = listenForKeyboardEvents(rt);
|
||||||
|
|
||||||
k.postData({
|
k.postData({
|
||||||
key: 'a',
|
key: 'a',
|
||||||
isDown: true
|
isDown: true
|
||||||
});
|
});
|
||||||
|
|
||||||
t.strictDeepEquals(k._keysPressed, ['A']);
|
t.strictDeepEquals(k._keysPressed, ['A']);
|
||||||
|
|
||||||
t.strictEquals(k.getKeyIsDown(65), true);
|
t.strictEquals(k.getKeyIsDown(65), true);
|
||||||
t.strictEquals(k.getKeyIsDown('a'), true);
|
t.strictEquals(k.getKeyIsDown('a'), true);
|
||||||
t.strictEquals(k.getKeyIsDown('A'), true);
|
t.strictEquals(k.getKeyIsDown('A'), true);
|
||||||
t.strictEquals(k.getKeyIsDown('any'), true);
|
t.strictEquals(k.getKeyIsDown('any'), true);
|
||||||
|
|
||||||
|
t.same(eventsEmitted.KEY_PRESSED, ['A']);
|
||||||
|
t.same(eventsEmitted.KEY_ANY_PRESSED, [undefined]);
|
||||||
|
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('number key', t => {
|
test('number key', t => {
|
||||||
const rt = new Runtime();
|
const rt = new Runtime();
|
||||||
const k = new Keyboard(rt);
|
const k = new Keyboard(rt);
|
||||||
|
const eventsEmitted = listenForKeyboardEvents(rt);
|
||||||
|
|
||||||
k.postData({
|
k.postData({
|
||||||
key: '1',
|
key: '1',
|
||||||
isDown: true
|
isDown: true
|
||||||
});
|
});
|
||||||
|
|
||||||
t.strictDeepEquals(k._keysPressed, ['1']);
|
t.strictDeepEquals(k._keysPressed, ['1']);
|
||||||
|
|
||||||
t.strictEquals(k.getKeyIsDown(49), true);
|
t.strictEquals(k.getKeyIsDown(49), true);
|
||||||
t.strictEquals(k.getKeyIsDown('1'), true);
|
t.strictEquals(k.getKeyIsDown('1'), true);
|
||||||
t.strictEquals(k.getKeyIsDown('any'), true);
|
t.strictEquals(k.getKeyIsDown('any'), true);
|
||||||
|
|
||||||
|
t.same(eventsEmitted.KEY_PRESSED, ['1']);
|
||||||
|
t.same(eventsEmitted.KEY_ANY_PRESSED, [undefined]);
|
||||||
|
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('non-english key', t => {
|
test('non-english key', t => {
|
||||||
const rt = new Runtime();
|
const rt = new Runtime();
|
||||||
const k = new Keyboard(rt);
|
const k = new Keyboard(rt);
|
||||||
|
const eventsEmitted = listenForKeyboardEvents(rt);
|
||||||
|
|
||||||
k.postData({
|
k.postData({
|
||||||
key: '日',
|
key: '日',
|
||||||
isDown: true
|
isDown: true
|
||||||
});
|
});
|
||||||
|
|
||||||
t.strictDeepEquals(k._keysPressed, ['日']);
|
t.strictDeepEquals(k._keysPressed, ['日']);
|
||||||
|
|
||||||
t.strictEquals(k.getKeyIsDown('日'), true);
|
t.strictEquals(k.getKeyIsDown('日'), true);
|
||||||
t.strictEquals(k.getKeyIsDown('any'), true);
|
t.strictEquals(k.getKeyIsDown('any'), true);
|
||||||
|
|
||||||
|
t.same(eventsEmitted.KEY_PRESSED, ['日']);
|
||||||
|
t.same(eventsEmitted.KEY_ANY_PRESSED, [undefined]);
|
||||||
|
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('ignore modifier key', t => {
|
test('shift key', t => {
|
||||||
const rt = new Runtime();
|
const rt = new Runtime();
|
||||||
const k = new Keyboard(rt);
|
const k = new Keyboard(rt);
|
||||||
|
const eventsEmitted = listenForKeyboardEvents(rt);
|
||||||
|
|
||||||
k.postData({
|
k.postData({
|
||||||
key: 'Shift',
|
key: 'Shift',
|
||||||
isDown: true
|
isDown: true
|
||||||
});
|
});
|
||||||
t.strictDeepEquals(k._keysPressed, []);
|
|
||||||
|
t.strictDeepEquals(k._keysPressed, ['shift']);
|
||||||
|
|
||||||
|
t.strictEquals(k.getKeyIsDown('shift'), true);
|
||||||
t.strictEquals(k.getKeyIsDown('any'), false);
|
t.strictEquals(k.getKeyIsDown('any'), false);
|
||||||
|
|
||||||
|
t.same(eventsEmitted.KEY_PRESSED, ['shift']);
|
||||||
|
t.same(eventsEmitted.KEY_ANY_PRESSED, []);
|
||||||
|
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('ignore control key', t => {
|
||||||
|
const rt = new Runtime();
|
||||||
|
const k = new Keyboard(rt);
|
||||||
|
const eventsEmitted = listenForKeyboardEvents(rt);
|
||||||
|
|
||||||
|
k.postData({
|
||||||
|
key: 'Control',
|
||||||
|
isDown: true
|
||||||
|
});
|
||||||
|
|
||||||
|
t.strictDeepEquals(k._keysPressed, []);
|
||||||
|
|
||||||
|
t.strictEquals(k.getKeyIsDown('control'), false);
|
||||||
|
t.strictEquals(k.getKeyIsDown('any'), false);
|
||||||
|
|
||||||
|
t.same(eventsEmitted.KEY_PRESSED, []);
|
||||||
|
t.same(eventsEmitted.KEY_ANY_PRESSED, []);
|
||||||
|
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('keyup', t => {
|
test('keyup', t => {
|
||||||
const rt = new Runtime();
|
const rt = new Runtime();
|
||||||
const k = new Keyboard(rt);
|
const k = new Keyboard(rt);
|
||||||
|
const eventsEmitted = listenForKeyboardEvents(rt);
|
||||||
|
|
||||||
k.postData({
|
k.postData({
|
||||||
key: 'ArrowLeft',
|
key: 'ArrowLeft',
|
||||||
isDown: true
|
isDown: true
|
||||||
});
|
});
|
||||||
|
|
||||||
k.postData({
|
k.postData({
|
||||||
key: 'ArrowLeft',
|
key: 'ArrowLeft',
|
||||||
isDown: false
|
isDown: false
|
||||||
});
|
});
|
||||||
|
|
||||||
t.strictDeepEquals(k._keysPressed, []);
|
t.strictDeepEquals(k._keysPressed, []);
|
||||||
|
|
||||||
t.strictEquals(k.getKeyIsDown('left arrow'), false);
|
t.strictEquals(k.getKeyIsDown('left arrow'), false);
|
||||||
t.strictEquals(k.getKeyIsDown('any'), false);
|
t.strictEquals(k.getKeyIsDown('any'), false);
|
||||||
|
|
||||||
|
t.same(eventsEmitted.KEY_PRESSED, ['left arrow']);
|
||||||
|
t.same(eventsEmitted.KEY_ANY_PRESSED, [undefined]);
|
||||||
|
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue