]>
git.proxmox.com Git - mirror_novnc.git/blob - tests/test.mouse.js
1 var assert
= chai
.assert
;
2 var expect
= chai
.expect
;
4 import sinon
from '../vendor/sinon.js';
6 import Mouse
from '../core/input/mouse.js';
7 import * as eventUtils
from '../core/util/events.js';
9 describe('Mouse Event Handling', function() {
12 sinon
.stub(eventUtils
, 'setCapture');
13 // This function is only used on target (the canvas)
14 // and for these tests we can assume that the canvas is 100x100
15 // located at coordinates 10x10
16 sinon
.stub(Element
.prototype, 'getBoundingClientRect').returns(
17 {left
: 10, right
: 110, top
: 10, bottom
: 110, width
: 100, height
: 100});
18 var target
= document
.createElement('canvas');
20 // The real constructors might not work everywhere we
21 // want to run these tests
22 var mouseevent
, touchevent
;
23 mouseevent
= touchevent = function(typeArg
, MouseEventInit
) {
24 var e
= { type
: typeArg
};
25 for (var key
in MouseEventInit
) {
26 e
[key
] = MouseEventInit
[key
];
28 e
.stopPropagation
= sinon
.spy();
29 e
.preventDefault
= sinon
.spy();
33 describe('Decode Mouse Events', function() {
34 it('should decode mousedown events', function(done
) {
35 var mouse
= new Mouse(target
);
36 mouse
.onmousebutton = function(x
, y
, down
, bmask
) {
37 expect(bmask
).to
.be
.equal(0x01);
38 expect(down
).to
.be
.equal(1);
41 mouse
._handleMouseDown(mouseevent('mousedown', { button
: '0x01' }));
43 it('should decode mouseup events', function(done
) {
45 var mouse
= new Mouse(target
);
46 mouse
.onmousebutton = function(x
, y
, down
, bmask
) {
47 expect(bmask
).to
.be
.equal(0x01);
49 expect(down
).to
.not
.be
.equal(1);
53 mouse
._handleMouseDown(mouseevent('mousedown', { button
: '0x01' }));
54 mouse
._handleMouseUp(mouseevent('mouseup', { button
: '0x01' }));
56 it('should decode mousemove events', function(done
) {
57 var mouse
= new Mouse(target
);
58 mouse
.onmousemove = function(x
, y
) {
59 // Note that target relative coordinates are sent
60 expect(x
).to
.be
.equal(40);
61 expect(y
).to
.be
.equal(10);
64 mouse
._handleMouseMove(mouseevent('mousemove',
65 { clientX
: 50, clientY
: 20 }));
67 it('should decode mousewheel events', function(done
) {
69 var mouse
= new Mouse(target
);
70 mouse
.onmousebutton = function(x
, y
, down
, bmask
) {
72 expect(bmask
).to
.be
.equal(1<<6);
74 expect(down
).to
.be
.equal(1);
75 } else if (calls
=== 2) {
76 expect(down
).to
.not
.be
.equal(1);
80 mouse
._handleMouseWheel(mouseevent('mousewheel',
81 { deltaX
: 50, deltaY
: 0,
86 describe('Double-click for Touch', function() {
88 beforeEach(function () { this.clock
= sinon
.useFakeTimers(); });
89 afterEach(function () { this.clock
.restore(); });
91 it('should use same pos for 2nd tap if close enough', function(done
) {
93 var mouse
= new Mouse(target
);
94 mouse
.onmousebutton = function(x
, y
, down
, bmask
) {
97 expect(down
).to
.be
.equal(1);
98 expect(x
).to
.be
.equal(68);
99 expect(y
).to
.be
.equal(36);
100 } else if (calls
=== 3) {
101 expect(down
).to
.be
.equal(1);
102 expect(x
).to
.be
.equal(68);
103 expect(y
).to
.be
.equal(36);
107 // touch events are sent in an array of events
108 // with one item for each touch point
109 mouse
._handleMouseDown(touchevent(
110 'touchstart', { touches
: [{ clientX
: 78, clientY
: 46 }]}));
112 mouse
._handleMouseUp(touchevent(
113 'touchend', { touches
: [{ clientX
: 79, clientY
: 45 }]}));
114 this.clock
.tick(200);
115 mouse
._handleMouseDown(touchevent(
116 'touchstart', { touches
: [{ clientX
: 67, clientY
: 35 }]}));
118 mouse
._handleMouseUp(touchevent(
119 'touchend', { touches
: [{ clientX
: 66, clientY
: 36 }]}));
122 it('should not modify 2nd tap pos if far apart', function(done
) {
124 var mouse
= new Mouse(target
);
125 mouse
.onmousebutton = function(x
, y
, down
, bmask
) {
128 expect(down
).to
.be
.equal(1);
129 expect(x
).to
.be
.equal(68);
130 expect(y
).to
.be
.equal(36);
131 } else if (calls
=== 3) {
132 expect(down
).to
.be
.equal(1);
133 expect(x
).to
.not
.be
.equal(68);
134 expect(y
).to
.not
.be
.equal(36);
138 mouse
._handleMouseDown(touchevent(
139 'touchstart', { touches
: [{ clientX
: 78, clientY
: 46 }]}));
141 mouse
._handleMouseUp(touchevent(
142 'touchend', { touches
: [{ clientX
: 79, clientY
: 45 }]}));
143 this.clock
.tick(200);
144 mouse
._handleMouseDown(touchevent(
145 'touchstart', { touches
: [{ clientX
: 57, clientY
: 35 }]}));
147 mouse
._handleMouseUp(touchevent(
148 'touchend', { touches
: [{ clientX
: 56, clientY
: 36 }]}));
151 it('should not modify 2nd tap pos if not soon enough', function(done
) {
153 var mouse
= new Mouse(target
);
154 mouse
.onmousebutton = function(x
, y
, down
, bmask
) {
157 expect(down
).to
.be
.equal(1);
158 expect(x
).to
.be
.equal(68);
159 expect(y
).to
.be
.equal(36);
160 } else if (calls
=== 3) {
161 expect(down
).to
.be
.equal(1);
162 expect(x
).to
.not
.be
.equal(68);
163 expect(y
).to
.not
.be
.equal(36);
167 mouse
._handleMouseDown(touchevent(
168 'touchstart', { touches
: [{ clientX
: 78, clientY
: 46 }]}));
170 mouse
._handleMouseUp(touchevent(
171 'touchend', { touches
: [{ clientX
: 79, clientY
: 45 }]}));
172 this.clock
.tick(500);
173 mouse
._handleMouseDown(touchevent(
174 'touchstart', { touches
: [{ clientX
: 67, clientY
: 35 }]}));
176 mouse
._handleMouseUp(touchevent(
177 'touchend', { touches
: [{ clientX
: 66, clientY
: 36 }]}));
180 it('should not modify 2nd tap pos if not touch', function(done
) {
182 var mouse
= new Mouse(target
);
183 mouse
.onmousebutton = function(x
, y
, down
, bmask
) {
186 expect(down
).to
.be
.equal(1);
187 expect(x
).to
.be
.equal(68);
188 expect(y
).to
.be
.equal(36);
189 } else if (calls
=== 3) {
190 expect(down
).to
.be
.equal(1);
191 expect(x
).to
.not
.be
.equal(68);
192 expect(y
).to
.not
.be
.equal(36);
196 mouse
._handleMouseDown(mouseevent(
197 'mousedown', { button
: '0x01', clientX
: 78, clientY
: 46 }));
199 mouse
._handleMouseUp(mouseevent(
200 'mouseup', { button
: '0x01', clientX
: 79, clientY
: 45 }));
201 this.clock
.tick(200);
202 mouse
._handleMouseDown(mouseevent(
203 'mousedown', { button
: '0x01', clientX
: 67, clientY
: 35 }));
205 mouse
._handleMouseUp(mouseevent(
206 'mouseup', { button
: '0x01', clientX
: 66, clientY
: 36 }));
211 describe('Accumulate mouse wheel events with small delta', function() {
213 beforeEach(function () { this.clock
= sinon
.useFakeTimers(); });
214 afterEach(function () { this.clock
.restore(); });
216 it('should accumulate wheel events if small enough', function () {
217 var mouse
= new Mouse(target
);
218 mouse
.onmousebutton
= sinon
.spy();
220 mouse
._handleMouseWheel(mouseevent(
221 'mousewheel', { clientX
: 18, clientY
: 40,
222 deltaX
: 4, deltaY
: 0, deltaMode
: 0 }));
224 mouse
._handleMouseWheel(mouseevent(
225 'mousewheel', { clientX
: 18, clientY
: 40,
226 deltaX
: 4, deltaY
: 0, deltaMode
: 0 }));
229 expect(mouse
._accumulatedWheelDeltaX
).to
.be
.equal(8);
232 mouse
._handleMouseWheel(mouseevent(
233 'mousewheel', { clientX
: 18, clientY
: 40,
234 deltaX
: 4, deltaY
: 0, deltaMode
: 0 }));
236 expect(mouse
.onmousebutton
).to
.have
.callCount(2); // mouse down and up
239 mouse
._handleMouseWheel(mouseevent(
240 'mousewheel', { clientX
: 18, clientY
: 40,
241 deltaX
: 4, deltaY
: 9, deltaMode
: 0 }));
243 expect(mouse
._accumulatedWheelDeltaX
).to
.be
.equal(4);
244 expect(mouse
._accumulatedWheelDeltaY
).to
.be
.equal(9);
246 expect(mouse
.onmousebutton
).to
.have
.callCount(2); // still
249 it('should not accumulate large wheel events', function () {
250 var mouse
= new Mouse(target
);
251 mouse
.onmousebutton
= sinon
.spy();
253 mouse
._handleMouseWheel(mouseevent(
254 'mousewheel', { clientX
: 18, clientY
: 40,
255 deltaX
: 11, deltaY
: 0, deltaMode
: 0 }));
257 mouse
._handleMouseWheel(mouseevent(
258 'mousewheel', { clientX
: 18, clientY
: 40,
259 deltaX
: 0, deltaY
: 70, deltaMode
: 0 }));
261 mouse
._handleMouseWheel(mouseevent(
262 'mousewheel', { clientX
: 18, clientY
: 40,
263 deltaX
: 400, deltaY
: 400, deltaMode
: 0 }));
265 expect(mouse
.onmousebutton
).to
.have
.callCount(8); // mouse down and up
268 it('should send even small wheel events after a timeout', function () {
269 var mouse
= new Mouse(target
);
270 mouse
.onmousebutton
= sinon
.spy();
272 mouse
._handleMouseWheel(mouseevent(
273 'mousewheel', { clientX
: 18, clientY
: 40,
274 deltaX
: 1, deltaY
: 0, deltaMode
: 0 }));
275 this.clock
.tick(51); // timeout on 50 ms
277 expect(mouse
.onmousebutton
).to
.have
.callCount(2); // mouse down and up
280 it('should account for non-zero deltaMode', function () {
281 var mouse
= new Mouse(target
);
282 mouse
.onmousebutton
= sinon
.spy();
284 mouse
._handleMouseWheel(mouseevent(
285 'mousewheel', { clientX
: 18, clientY
: 40,
286 deltaX
: 0, deltaY
: 2, deltaMode
: 1 }));
290 mouse
._handleMouseWheel(mouseevent(
291 'mousewheel', { clientX
: 18, clientY
: 40,
292 deltaX
: 1, deltaY
: 0, deltaMode
: 2 }));
294 expect(mouse
.onmousebutton
).to
.have
.callCount(4); // mouse down and up