]>
git.proxmox.com Git - mirror_novnc.git/blob - tests/test.mouse.js
58200a4eac6c81529f93d8a0b6754fa56b2d628f
1 const expect
= chai
.expect
;
3 import sinon
from '../vendor/sinon.js';
5 import Mouse
from '../core/input/mouse.js';
6 import * as eventUtils
from '../core/util/events.js';
8 describe('Mouse Event Handling', function() {
11 sinon
.stub(eventUtils
, 'setCapture');
12 // This function is only used on target (the canvas)
13 // and for these tests we can assume that the canvas is 100x100
14 // located at coordinates 10x10
15 sinon
.stub(Element
.prototype, 'getBoundingClientRect').returns(
16 {left
: 10, right
: 110, top
: 10, bottom
: 110, width
: 100, height
: 100});
17 const target
= document
.createElement('canvas');
19 // The real constructors might not work everywhere we
20 // want to run these tests
21 const mouseevent = function(typeArg
, MouseEventInit
) {
22 const e
= { type
: typeArg
};
23 for (let key
in MouseEventInit
) {
24 e
[key
] = MouseEventInit
[key
];
26 e
.stopPropagation
= sinon
.spy();
27 e
.preventDefault
= sinon
.spy();
30 const touchevent
= mouseevent
;
32 describe('Decode Mouse Events', function() {
33 it('should decode mousedown events', function(done
) {
34 const mouse
= new Mouse(target
);
35 mouse
.onmousebutton = function(x
, y
, down
, bmask
) {
36 expect(bmask
).to
.be
.equal(0x01);
37 expect(down
).to
.be
.equal(1);
40 mouse
._handleMouseDown(mouseevent('mousedown', { button
: '0x01' }));
42 it('should decode mouseup events', function(done
) {
44 const mouse
= new Mouse(target
);
45 mouse
.onmousebutton = function(x
, y
, down
, bmask
) {
46 expect(bmask
).to
.be
.equal(0x01);
48 expect(down
).to
.not
.be
.equal(1);
52 mouse
._handleMouseDown(mouseevent('mousedown', { button
: '0x01' }));
53 mouse
._handleMouseUp(mouseevent('mouseup', { button
: '0x01' }));
55 it('should decode mousemove events', function(done
) {
56 const mouse
= new Mouse(target
);
57 mouse
.onmousemove = function(x
, y
) {
58 // Note that target relative coordinates are sent
59 expect(x
).to
.be
.equal(40);
60 expect(y
).to
.be
.equal(10);
63 mouse
._handleMouseMove(mouseevent('mousemove',
64 { clientX
: 50, clientY
: 20 }));
66 it('should decode mousewheel events', function(done
) {
68 const mouse
= new Mouse(target
);
69 mouse
.onmousebutton = function(x
, y
, down
, bmask
) {
71 expect(bmask
).to
.be
.equal(1<<6);
73 expect(down
).to
.be
.equal(1);
74 } else if (calls
=== 2) {
75 expect(down
).to
.not
.be
.equal(1);
79 mouse
._handleMouseWheel(mouseevent('mousewheel',
80 { deltaX
: 50, deltaY
: 0,
85 describe('Double-click for Touch', function() {
87 beforeEach(function () { this.clock
= sinon
.useFakeTimers(); });
88 afterEach(function () { this.clock
.restore(); });
90 it('should use same pos for 2nd tap if close enough', function(done
) {
92 const mouse
= new Mouse(target
);
93 mouse
.onmousebutton = function(x
, y
, down
, bmask
) {
96 expect(down
).to
.be
.equal(1);
97 expect(x
).to
.be
.equal(68);
98 expect(y
).to
.be
.equal(36);
99 } else if (calls
=== 3) {
100 expect(down
).to
.be
.equal(1);
101 expect(x
).to
.be
.equal(68);
102 expect(y
).to
.be
.equal(36);
106 // touch events are sent in an array of events
107 // with one item for each touch point
108 mouse
._handleMouseDown(touchevent(
109 'touchstart', { touches
: [{ clientX
: 78, clientY
: 46 }]}));
111 mouse
._handleMouseUp(touchevent(
112 'touchend', { touches
: [{ clientX
: 79, clientY
: 45 }]}));
113 this.clock
.tick(200);
114 mouse
._handleMouseDown(touchevent(
115 'touchstart', { touches
: [{ clientX
: 67, clientY
: 35 }]}));
117 mouse
._handleMouseUp(touchevent(
118 'touchend', { touches
: [{ clientX
: 66, clientY
: 36 }]}));
121 it('should not modify 2nd tap pos if far apart', function(done
) {
123 const mouse
= new Mouse(target
);
124 mouse
.onmousebutton = function(x
, y
, down
, bmask
) {
127 expect(down
).to
.be
.equal(1);
128 expect(x
).to
.be
.equal(68);
129 expect(y
).to
.be
.equal(36);
130 } else if (calls
=== 3) {
131 expect(down
).to
.be
.equal(1);
132 expect(x
).to
.not
.be
.equal(68);
133 expect(y
).to
.not
.be
.equal(36);
137 mouse
._handleMouseDown(touchevent(
138 'touchstart', { touches
: [{ clientX
: 78, clientY
: 46 }]}));
140 mouse
._handleMouseUp(touchevent(
141 'touchend', { touches
: [{ clientX
: 79, clientY
: 45 }]}));
142 this.clock
.tick(200);
143 mouse
._handleMouseDown(touchevent(
144 'touchstart', { touches
: [{ clientX
: 57, clientY
: 35 }]}));
146 mouse
._handleMouseUp(touchevent(
147 'touchend', { touches
: [{ clientX
: 56, clientY
: 36 }]}));
150 it('should not modify 2nd tap pos if not soon enough', function(done
) {
152 const mouse
= new Mouse(target
);
153 mouse
.onmousebutton = function(x
, y
, down
, bmask
) {
156 expect(down
).to
.be
.equal(1);
157 expect(x
).to
.be
.equal(68);
158 expect(y
).to
.be
.equal(36);
159 } else if (calls
=== 3) {
160 expect(down
).to
.be
.equal(1);
161 expect(x
).to
.not
.be
.equal(68);
162 expect(y
).to
.not
.be
.equal(36);
166 mouse
._handleMouseDown(touchevent(
167 'touchstart', { touches
: [{ clientX
: 78, clientY
: 46 }]}));
169 mouse
._handleMouseUp(touchevent(
170 'touchend', { touches
: [{ clientX
: 79, clientY
: 45 }]}));
171 this.clock
.tick(500);
172 mouse
._handleMouseDown(touchevent(
173 'touchstart', { touches
: [{ clientX
: 67, clientY
: 35 }]}));
175 mouse
._handleMouseUp(touchevent(
176 'touchend', { touches
: [{ clientX
: 66, clientY
: 36 }]}));
179 it('should not modify 2nd tap pos if not touch', function(done
) {
181 const mouse
= new Mouse(target
);
182 mouse
.onmousebutton = function(x
, y
, down
, bmask
) {
185 expect(down
).to
.be
.equal(1);
186 expect(x
).to
.be
.equal(68);
187 expect(y
).to
.be
.equal(36);
188 } else if (calls
=== 3) {
189 expect(down
).to
.be
.equal(1);
190 expect(x
).to
.not
.be
.equal(68);
191 expect(y
).to
.not
.be
.equal(36);
195 mouse
._handleMouseDown(mouseevent(
196 'mousedown', { button
: '0x01', clientX
: 78, clientY
: 46 }));
198 mouse
._handleMouseUp(mouseevent(
199 'mouseup', { button
: '0x01', clientX
: 79, clientY
: 45 }));
200 this.clock
.tick(200);
201 mouse
._handleMouseDown(mouseevent(
202 'mousedown', { button
: '0x01', clientX
: 67, clientY
: 35 }));
204 mouse
._handleMouseUp(mouseevent(
205 'mouseup', { button
: '0x01', clientX
: 66, clientY
: 36 }));
210 describe('Accumulate mouse wheel events with small delta', function() {
212 beforeEach(function () { this.clock
= sinon
.useFakeTimers(); });
213 afterEach(function () { this.clock
.restore(); });
215 it('should accumulate wheel events if small enough', function () {
216 const mouse
= new Mouse(target
);
217 mouse
.onmousebutton
= sinon
.spy();
219 mouse
._handleMouseWheel(mouseevent(
220 'mousewheel', { clientX
: 18, clientY
: 40,
221 deltaX
: 4, deltaY
: 0, deltaMode
: 0 }));
223 mouse
._handleMouseWheel(mouseevent(
224 'mousewheel', { clientX
: 18, clientY
: 40,
225 deltaX
: 4, deltaY
: 0, deltaMode
: 0 }));
228 expect(mouse
._accumulatedWheelDeltaX
).to
.be
.equal(8);
231 mouse
._handleMouseWheel(mouseevent(
232 'mousewheel', { clientX
: 18, clientY
: 40,
233 deltaX
: 4, deltaY
: 0, deltaMode
: 0 }));
235 expect(mouse
.onmousebutton
).to
.have
.callCount(2); // mouse down and up
238 mouse
._handleMouseWheel(mouseevent(
239 'mousewheel', { clientX
: 18, clientY
: 40,
240 deltaX
: 4, deltaY
: 9, deltaMode
: 0 }));
242 expect(mouse
._accumulatedWheelDeltaX
).to
.be
.equal(4);
243 expect(mouse
._accumulatedWheelDeltaY
).to
.be
.equal(9);
245 expect(mouse
.onmousebutton
).to
.have
.callCount(2); // still
248 it('should not accumulate large wheel events', function () {
249 const mouse
= new Mouse(target
);
250 mouse
.onmousebutton
= sinon
.spy();
252 mouse
._handleMouseWheel(mouseevent(
253 'mousewheel', { clientX
: 18, clientY
: 40,
254 deltaX
: 11, deltaY
: 0, deltaMode
: 0 }));
256 mouse
._handleMouseWheel(mouseevent(
257 'mousewheel', { clientX
: 18, clientY
: 40,
258 deltaX
: 0, deltaY
: 70, deltaMode
: 0 }));
260 mouse
._handleMouseWheel(mouseevent(
261 'mousewheel', { clientX
: 18, clientY
: 40,
262 deltaX
: 400, deltaY
: 400, deltaMode
: 0 }));
264 expect(mouse
.onmousebutton
).to
.have
.callCount(8); // mouse down and up
267 it('should send even small wheel events after a timeout', function () {
268 const mouse
= new Mouse(target
);
269 mouse
.onmousebutton
= sinon
.spy();
271 mouse
._handleMouseWheel(mouseevent(
272 'mousewheel', { clientX
: 18, clientY
: 40,
273 deltaX
: 1, deltaY
: 0, deltaMode
: 0 }));
274 this.clock
.tick(51); // timeout on 50 ms
276 expect(mouse
.onmousebutton
).to
.have
.callCount(2); // mouse down and up
279 it('should account for non-zero deltaMode', function () {
280 const mouse
= new Mouse(target
);
281 mouse
.onmousebutton
= sinon
.spy();
283 mouse
._handleMouseWheel(mouseevent(
284 'mousewheel', { clientX
: 18, clientY
: 40,
285 deltaX
: 0, deltaY
: 2, deltaMode
: 1 }));
289 mouse
._handleMouseWheel(mouseevent(
290 'mousewheel', { clientX
: 18, clientY
: 40,
291 deltaX
: 1, deltaY
: 0, deltaMode
: 2 }));
293 expect(mouse
.onmousebutton
).to
.have
.callCount(4); // mouse down and up