konva/test/unit/PointerEvents-test.ts

247 lines
6.1 KiB
TypeScript
Raw Normal View History

2021-05-04 06:09:18 +08:00
import { assert } from 'chai';
import {
addStage,
Konva,
simulatePointerDown,
simulatePointerMove,
simulatePointerUp,
2021-05-05 22:19:24 +08:00
} from './test-utils';
2021-05-04 06:09:18 +08:00
// TODO: restore it!
describe.skip('PointerEvents', function () {
Konva._pointerEventsEnabled = true;
2019-04-10 22:28:42 +08:00
// ======================================================
2021-05-04 06:09:18 +08:00
it('pointerdown pointerup pointermove', function (done) {
2019-04-10 22:28:42 +08:00
var stage = addStage();
var layer = new Konva.Layer();
var circle = new Konva.Circle({
2021-05-04 06:09:18 +08:00
x: stage.width() / 2,
y: stage.height() / 2,
2019-04-10 22:28:42 +08:00
radius: 70,
fill: 'red',
stroke: 'black',
2020-05-08 22:59:35 +08:00
strokeWidth: 4,
2019-04-10 22:28:42 +08:00
});
// mobile events
var pointerdown = false;
var pointerup = false;
var pointermove = false;
/*
* mobile
*/
2020-05-08 22:59:35 +08:00
circle.on('pointerdown', function () {
2019-04-10 22:28:42 +08:00
pointerdown = true;
});
2020-05-08 22:59:35 +08:00
circle.on('pointerup', function () {
2019-04-10 22:28:42 +08:00
pointerup = true;
});
2020-05-08 22:59:35 +08:00
circle.on('pointermove', function () {
2019-04-10 22:28:42 +08:00
pointermove = true;
});
layer.add(circle);
stage.add(layer);
// touchstart circle
2021-05-04 06:09:18 +08:00
simulatePointerDown(stage, {
x: 289,
y: 100,
2019-04-10 22:28:42 +08:00
pointerId: 1,
2020-05-08 22:59:35 +08:00
preventDefault: function () {},
2019-04-10 22:28:42 +08:00
});
assert(pointerdown, '1) pointerdown should be true');
assert(!pointermove, '1) pointermove should be false');
assert(!pointerup, '1) pointerup should be false');
// pointerup circle
2021-05-04 06:09:18 +08:00
simulatePointerUp(stage, {
2019-04-10 22:28:42 +08:00
touches: [],
2020-05-08 22:59:35 +08:00
preventDefault: function () {},
2019-04-10 22:28:42 +08:00
});
assert(pointerdown, '2) pointerdown should be true');
assert(!pointermove, '2) pointermove should be false');
assert(pointerup, '2) pointerup should be true');
// pointerdown circle
2021-05-04 06:09:18 +08:00
simulatePointerDown(stage, {
2019-04-10 22:28:42 +08:00
touches: [
{
2021-05-04 06:09:18 +08:00
x: 289,
y: 100,
2020-05-08 22:59:35 +08:00
},
2019-04-10 22:28:42 +08:00
],
2020-05-08 22:59:35 +08:00
preventDefault: function () {},
2019-04-10 22:28:42 +08:00
});
assert(pointerdown, '3) pointerdown should be true');
assert(!pointermove, '3) pointermove should be false');
assert(pointerup, '3) pointerup should be true');
// pointerup circle to triger dbltap
2021-05-04 06:09:18 +08:00
simulatePointerUp(stage, {
2019-04-10 22:28:42 +08:00
touches: [],
2020-05-08 22:59:35 +08:00
preventDefault: function () {},
2019-04-10 22:28:42 +08:00
});
// end drag is tied to document mouseup and pointerup event
// which can't be simulated. call _endDrag manually
//Konva.DD._endDrag();
assert(pointerdown, '4) pointerdown should be true');
assert(!pointermove, '4) pointermove should be false');
assert(pointerup, '4) pointerup should be true');
2020-05-08 22:59:35 +08:00
setTimeout(function () {
2019-04-10 22:28:42 +08:00
// pointermove circle
2021-05-04 06:09:18 +08:00
simulatePointerMove(stage, {
2019-04-10 22:28:42 +08:00
touches: [
{
2021-05-04 06:09:18 +08:00
x: 290,
y: 100,
2020-05-08 22:59:35 +08:00
},
2019-04-10 22:28:42 +08:00
],
2020-05-08 22:59:35 +08:00
preventDefault: function () {},
2019-04-10 22:28:42 +08:00
});
assert(pointerdown, '5) pointerdown should be true');
assert(pointermove, '5) pointermove should be true');
assert(pointerup, '5) pointerup should be true');
done();
}, 17);
});
// ======================================================
2021-05-04 06:09:18 +08:00
it('pointer capture', function (done) {
2019-04-10 22:28:42 +08:00
var stage = addStage();
var layer = new Konva.Layer();
var circle = new Konva.Circle({
2021-05-04 06:09:18 +08:00
x: stage.width() / 2,
y: stage.height() / 2,
2019-04-10 22:28:42 +08:00
radius: 70,
fill: 'red',
stroke: 'black',
2020-05-08 22:59:35 +08:00
strokeWidth: 4,
2019-04-10 22:28:42 +08:00
});
var circle2 = new Konva.Circle({
2021-05-04 06:09:18 +08:00
x: stage.width() / 2,
2019-04-10 22:28:42 +08:00
y: 20,
radius: 20,
fill: 'red',
stroke: 'black',
2020-05-08 22:59:35 +08:00
strokeWidth: 4,
2019-04-10 22:28:42 +08:00
});
// mobile events
var downCount = 0;
var otherDownCount = 0;
var pointerup = false;
var pointermove = false;
2020-05-08 22:59:35 +08:00
circle2.on('pointerdown', function () {
2019-04-10 22:28:42 +08:00
otherDownCount++;
});
2020-05-08 22:59:35 +08:00
circle.on('pointerdown', function (event) {
2019-04-10 22:28:42 +08:00
downCount++;
2021-05-04 06:09:18 +08:00
this.setPointerCapture(event['pointerId']);
2019-04-10 22:28:42 +08:00
});
2020-05-08 22:59:35 +08:00
circle.on('pointerup', function (evt) {
2021-05-04 06:09:18 +08:00
assert(
this.hasPointerCapture(evt['pointerId']),
'circle released capture'
);
2019-04-10 22:28:42 +08:00
pointerup = true;
});
2020-05-08 22:59:35 +08:00
circle.on('pointermove', function (evt) {
2021-05-04 06:09:18 +08:00
assert(this.hasPointerCapture(evt['pointerId']), 'circle has capture');
2019-04-10 22:28:42 +08:00
pointermove = true;
});
layer.add(circle);
layer.add(circle2);
stage.add(layer);
var top = stage.content.getBoundingClientRect().top;
// on circle 2 to confirm it works
2021-05-04 06:09:18 +08:00
simulatePointerDown(stage, {
x: 289,
y: 10 + top,
2019-04-10 22:28:42 +08:00
pointerId: 0,
2020-05-08 22:59:35 +08:00
preventDefault: function () {},
2019-04-10 22:28:42 +08:00
});
assert(otherDownCount === 1, '6) otherDownCount should be 1');
assert(downCount === 0, '6) downCount should be 0');
assert(!pointermove, '6) pointermove should be false');
assert(!pointerup, '6) pointerup should be false');
// on circle with capture
2021-05-04 06:09:18 +08:00
simulatePointerDown(stage, {
x: 289,
y: 100 + top,
2019-04-10 22:28:42 +08:00
pointerId: 1,
2020-05-08 22:59:35 +08:00
preventDefault: function () {},
2019-04-10 22:28:42 +08:00
});
assert(otherDownCount === 1, '7) otherDownCount should be 1');
assert(downCount === 1, '7) downCount should be 1');
assert(!pointermove, '7) pointermove should be false');
assert(!pointerup, '7) pointerup should be true');
// second pointerdown
2021-05-04 06:09:18 +08:00
simulatePointerDown(stage, {
x: 289,
y: 10 + top,
2019-04-10 22:28:42 +08:00
pointerId: 1,
2020-05-08 22:59:35 +08:00
preventDefault: function () {},
2019-04-10 22:28:42 +08:00
});
assert(otherDownCount === 1, '8) otherDownCount should be 1');
assert(downCount === 2, '8) pointerdown should be 2');
assert(!pointermove, '8) pointermove should be false');
assert(!pointerup, '8) pointerup should be true');
2020-05-08 22:59:35 +08:00
setTimeout(function () {
2019-04-10 22:28:42 +08:00
// pointermove over circle 2
2021-05-04 06:09:18 +08:00
simulatePointerMove(stage, {
x: 290,
y: 10 + top,
2019-04-10 22:28:42 +08:00
pointerId: 1,
2020-05-08 22:59:35 +08:00
preventDefault: function () {},
2019-04-10 22:28:42 +08:00
});
assert(otherDownCount === 1, '9) otherDownCount should be 1');
assert(pointermove, '9) pointermove should be true');
2021-05-04 06:09:18 +08:00
simulatePointerUp(stage, {
2019-04-10 22:28:42 +08:00
pointerId: 1,
2020-05-08 22:59:35 +08:00
preventDefault: function () {},
2019-04-10 22:28:42 +08:00
});
2021-05-04 06:09:18 +08:00
simulatePointerDown(stage, {
x: 289,
y: 10 + top,
2019-04-10 22:28:42 +08:00
pointerId: 1,
2020-05-08 22:59:35 +08:00
preventDefault: function () {},
2019-04-10 22:28:42 +08:00
});
assert(otherDownCount === 2, '10) otherDownCount should be 1');
assert(pointerup, '10) pointerup should be true');
done();
}, 17);
});
});