2017-04-20 19:17:05 -04:00
|
|
|
const test = require('tap').test;
|
|
|
|
const Mouse = require('../../src/io/mouse');
|
|
|
|
const Runtime = require('../../src/engine/runtime');
|
2016-10-24 11:56:52 -04:00
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('spec', t => {
|
|
|
|
const rt = new Runtime();
|
|
|
|
const m = new Mouse(rt);
|
2016-10-24 11:56:52 -04:00
|
|
|
|
|
|
|
t.type(m, 'object');
|
|
|
|
t.type(m.postData, 'function');
|
2018-01-09 10:36:03 -05:00
|
|
|
t.type(m.getClientX, 'function');
|
|
|
|
t.type(m.getClientY, 'function');
|
|
|
|
t.type(m.getScratchX, 'function');
|
|
|
|
t.type(m.getScratchY, 'function');
|
2016-10-24 11:56:52 -04:00
|
|
|
t.type(m.getIsDown, 'function');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('mouseUp', t => {
|
|
|
|
const rt = new Runtime();
|
|
|
|
const m = new Mouse(rt);
|
2016-10-24 11:56:52 -04:00
|
|
|
|
|
|
|
m.postData({
|
2018-01-09 09:39:52 -05:00
|
|
|
x: -20,
|
2016-10-24 11:56:52 -04:00
|
|
|
y: 10,
|
|
|
|
isDown: false,
|
|
|
|
canvasWidth: 480,
|
|
|
|
canvasHeight: 360
|
|
|
|
});
|
2018-01-09 10:36:03 -05:00
|
|
|
t.strictEquals(m.getClientX(), -20);
|
|
|
|
t.strictEquals(m.getClientY(), 10);
|
|
|
|
t.strictEquals(m.getScratchX(), -240);
|
|
|
|
t.strictEquals(m.getScratchY(), 170);
|
2016-10-24 11:56:52 -04:00
|
|
|
t.strictEquals(m.getIsDown(), false);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('mouseDown', t => {
|
|
|
|
const rt = new Runtime();
|
|
|
|
const m = new Mouse(rt);
|
2016-10-24 11:56:52 -04:00
|
|
|
|
|
|
|
m.postData({
|
|
|
|
x: 10,
|
2018-01-09 09:39:52 -05:00
|
|
|
y: 400,
|
2016-10-24 11:56:52 -04:00
|
|
|
isDown: true,
|
|
|
|
canvasWidth: 480,
|
|
|
|
canvasHeight: 360
|
|
|
|
});
|
2018-01-09 10:36:03 -05:00
|
|
|
t.strictEquals(m.getClientX(), 10);
|
|
|
|
t.strictEquals(m.getClientY(), 400);
|
|
|
|
t.strictEquals(m.getScratchX(), -230);
|
|
|
|
t.strictEquals(m.getScratchY(), -180);
|
2016-10-24 11:56:52 -04:00
|
|
|
t.strictEquals(m.getIsDown(), true);
|
|
|
|
t.end();
|
|
|
|
});
|
2018-01-09 10:36:03 -05:00
|
|
|
|
|
|
|
test('at zoomed scale', t => {
|
|
|
|
const rt = new Runtime();
|
|
|
|
const m = new Mouse(rt);
|
|
|
|
|
|
|
|
m.postData({
|
|
|
|
x: 240,
|
|
|
|
y: 540,
|
|
|
|
canvasWidth: 960,
|
|
|
|
canvasHeight: 720
|
|
|
|
});
|
|
|
|
t.strictEquals(m.getClientX(), 240);
|
|
|
|
t.strictEquals(m.getClientY(), 540);
|
|
|
|
t.strictEquals(m.getScratchX(), -120);
|
|
|
|
t.strictEquals(m.getScratchY(), -90);
|
|
|
|
t.end();
|
|
|
|
});
|
2018-08-02 14:40:05 -04:00
|
|
|
|
|
|
|
test('mousedown activating click hats', t => {
|
|
|
|
const rt = new Runtime();
|
|
|
|
const m = new Mouse(rt);
|
|
|
|
|
|
|
|
const mouseMoveEvent = {
|
|
|
|
x: 10,
|
|
|
|
y: 100,
|
|
|
|
canvasWidth: 480,
|
|
|
|
canvasHeight: 360
|
|
|
|
};
|
|
|
|
|
|
|
|
const mouseDownEvent = Object.assign({}, mouseMoveEvent, {
|
|
|
|
isDown: true
|
|
|
|
});
|
|
|
|
|
|
|
|
const mouseUpEvent = Object.assign({}, mouseMoveEvent, {
|
|
|
|
isDown: false
|
|
|
|
});
|
|
|
|
|
|
|
|
// Stub activateClickHats function for testing
|
|
|
|
let ranClickHats = false;
|
|
|
|
m._activateClickHats = () => {
|
|
|
|
ranClickHats = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Mouse move without mousedown
|
|
|
|
m.postData(mouseMoveEvent);
|
|
|
|
t.strictEquals(ranClickHats, false);
|
|
|
|
|
|
|
|
// Mouse down event triggers the hats
|
|
|
|
m.postData(mouseDownEvent);
|
|
|
|
t.strictEquals(ranClickHats, true);
|
|
|
|
|
|
|
|
// But another mouse move while down doesn't trigger
|
|
|
|
ranClickHats = false;
|
|
|
|
m.postData(mouseDownEvent);
|
|
|
|
t.strictEquals(ranClickHats, false);
|
|
|
|
|
|
|
|
// And it doesn't trigger on mouse up
|
|
|
|
ranClickHats = false;
|
|
|
|
m.postData(mouseUpEvent);
|
|
|
|
t.strictEquals(ranClickHats, false);
|
|
|
|
|
|
|
|
// And hats don't trigger if mouse down is outside canvas
|
|
|
|
ranClickHats = false;
|
|
|
|
m.postData(Object.assign({}, mouseDownEvent, {
|
|
|
|
x: 50000,
|
|
|
|
y: 50
|
|
|
|
}));
|
|
|
|
t.strictEquals(ranClickHats, false);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|