mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-01-10 15:02:06 -05:00
Add minimal test coverage and docs for keyboard module.
This commit is contained in:
parent
7e9745ce3f
commit
faf8a898a0
2 changed files with 90 additions and 0 deletions
|
@ -18,6 +18,7 @@ function Keyboard (runtime) {
|
|||
* Convert a Scratch key name to a DOM keyCode.
|
||||
* @param {Any} keyName Scratch key argument.
|
||||
* @return {number} Key code corresponding to a DOM event.
|
||||
* @private
|
||||
*/
|
||||
Keyboard.prototype._scratchKeyToKeyCode = function (keyName) {
|
||||
if (typeof keyName == 'number') {
|
||||
|
@ -37,6 +38,12 @@ Keyboard.prototype._scratchKeyToKeyCode = function (keyName) {
|
|||
return keyString.toUpperCase().charCodeAt(0);
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert a DOM keyCode into a Scratch key name.
|
||||
* @param {number} Key code from DOM event.
|
||||
* @return {Any} Scratch key argument.
|
||||
* @private
|
||||
*/
|
||||
Keyboard.prototype._keyCodeToScratchKey = function (keyCode) {
|
||||
if (keyCode >= 48 && keyCode <= 90) {
|
||||
// Standard letter.
|
||||
|
@ -52,6 +59,11 @@ Keyboard.prototype._keyCodeToScratchKey = function (keyCode) {
|
|||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Keyboard DOM event handler.
|
||||
* @param {object} DOM event object.
|
||||
* @return {void}
|
||||
*/
|
||||
Keyboard.prototype.postData = function (data) {
|
||||
if (data.keyCode) {
|
||||
var index = this._keysPressed.indexOf(data.keyCode);
|
||||
|
@ -74,6 +86,11 @@ Keyboard.prototype.postData = function (data) {
|
|||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get key down state for a specified Scratch key name.
|
||||
* @param {Any} Scratch key argument.
|
||||
* @return {boolean}
|
||||
*/
|
||||
Keyboard.prototype.getKeyIsDown = function (key) {
|
||||
if (key == 'any') {
|
||||
return this._keysPressed.length > 0;
|
||||
|
|
73
test/unit/io_keyboard.js
Normal file
73
test/unit/io_keyboard.js
Normal file
|
@ -0,0 +1,73 @@
|
|||
var test = require('tap').test;
|
||||
var Keyboard = require('../../src/io/keyboard');
|
||||
var Runtime = require('../../src/engine/runtime');
|
||||
|
||||
test('spec', function (t) {
|
||||
var rt = new Runtime();
|
||||
var k = new Keyboard(rt);
|
||||
|
||||
t.type(k, 'object');
|
||||
t.type(k.postData, 'function');
|
||||
t.type(k.getKeyIsDown, 'function');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('space', function (t) {
|
||||
var rt = new Runtime();
|
||||
var k = new Keyboard(rt);
|
||||
|
||||
k.postData({
|
||||
keyCode: 32,
|
||||
isDown: true
|
||||
});
|
||||
t.strictDeepEquals(k._keysPressed, [32]);
|
||||
t.strictEquals(k.getKeyIsDown('space'), true);
|
||||
t.strictEquals(k.getKeyIsDown('any'), true);
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('letter', function (t) {
|
||||
var rt = new Runtime();
|
||||
var k = new Keyboard(rt);
|
||||
|
||||
k.postData({
|
||||
keyCode: 65,
|
||||
isDown: true
|
||||
});
|
||||
t.strictDeepEquals(k._keysPressed, [65]);
|
||||
t.strictEquals(k.getKeyIsDown('a'), true);
|
||||
t.strictEquals(k.getKeyIsDown('any'), true);
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('number', function (t) {
|
||||
var rt = new Runtime();
|
||||
var k = new Keyboard(rt);
|
||||
|
||||
k.postData({
|
||||
keyCode: 49,
|
||||
isDown: true
|
||||
});
|
||||
t.strictDeepEquals(k._keysPressed, [49]);
|
||||
t.strictEquals(k.getKeyIsDown(49), true);
|
||||
t.strictEquals(k.getKeyIsDown('any'), true);
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('keyup', function (t) {
|
||||
var rt = new Runtime();
|
||||
var k = new Keyboard(rt);
|
||||
|
||||
k.postData({
|
||||
keyCode: 37,
|
||||
isDown: true
|
||||
});
|
||||
k.postData({
|
||||
keyCode: 37,
|
||||
isDown: false
|
||||
});
|
||||
t.strictDeepEquals(k._keysPressed, []);
|
||||
t.strictEquals(k.getKeyIsDown(37), false);
|
||||
t.strictEquals(k.getKeyIsDown('any'), false);
|
||||
t.end();
|
||||
});
|
Loading…
Reference in a new issue