]>
git.proxmox.com Git - mirror_novnc.git/blob - tests/test.mouse.js
1 var assert
= chai
.assert
;
2 var expect
= chai
.expect
;
4 import Mouse
from '../core/input/mouse.js';
5 import * as eventUtils
from '../core/util/events.js';
7 /* jshint newcap: false, expr: true */
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 var target
= document
.createElement('canvas');
19 // The real constructors might not work everywhere we
20 // want to run these tests
21 var mouseevent
, touchevent
;
22 mouseevent
= touchevent = function(typeArg
, MouseEventInit
) {
23 var e
= { type
: typeArg
};
24 for (var key
in MouseEventInit
) {
25 e
[key
] = MouseEventInit
[key
];
27 e
.stopPropagation
= sinon
.spy();
28 e
.preventDefault
= sinon
.spy();
32 describe('Decode Mouse Events', function() {
33 it('should decode mousedown events', function(done
) {
34 var mouse
= new Mouse({
35 onMouseButton: function(x
, y
, down
, bmask
) {
36 expect(bmask
).to
.be
.equal(0x01);
37 expect(down
).to
.be
.equal(1);
42 mouse
._handleMouseDown(mouseevent('mousedown', { button
: '0x01' }));
44 it('should decode mouseup events', function(done
) {
46 var mouse
= new Mouse({
47 onMouseButton: function(x
, y
, down
, bmask
) {
48 expect(bmask
).to
.be
.equal(0x01);
50 expect(down
).to
.not
.be
.equal(1);
56 mouse
._handleMouseDown(mouseevent('mousedown', { button
: '0x01' }));
57 mouse
._handleMouseUp(mouseevent('mouseup', { button
: '0x01' }));
59 it('should decode mousemove events', function(done
) {
60 var mouse
= new Mouse({
61 onMouseMove: function(x
, y
) {
62 // Note that target relative coordinates are sent
63 expect(x
).to
.be
.equal(40);
64 expect(y
).to
.be
.equal(10);
69 mouse
._handleMouseMove(mouseevent('mousemove',
70 { clientX
: 50, clientY
: 20 }));
72 it('should decode mousewheel events', function(done
) {
74 var mouse
= new Mouse({
75 onMouseButton: function(x
, y
, down
, bmask
) {
77 expect(bmask
).to
.be
.equal(1<<6);
79 expect(down
).to
.be
.equal(1);
80 } else if (calls
=== 2) {
81 expect(down
).to
.not
.be
.equal(1);
87 mouse
._handleMouseWheel(mouseevent('mousewheel',
88 { deltaX
: 50, deltaY
: 0,
93 describe('Double-click for Touch', function() {
95 beforeEach(function () { this.clock
= sinon
.useFakeTimers(); });
96 afterEach(function () { this.clock
.restore(); });
98 it('should use same pos for 2nd tap if close enough', function(done
) {
100 var mouse
= new Mouse({
101 onMouseButton: function(x
, y
, down
, bmask
) {
104 expect(down
).to
.be
.equal(1);
105 expect(x
).to
.be
.equal(68);
106 expect(y
).to
.be
.equal(36);
107 } else if (calls
=== 3) {
108 expect(down
).to
.be
.equal(1);
109 expect(x
).to
.be
.equal(68);
110 expect(y
).to
.be
.equal(36);
116 // touch events are sent in an array of events
117 // with one item for each touch point
118 mouse
._handleMouseDown(touchevent(
119 'touchstart', { touches
: [{ clientX
: 78, clientY
: 46 }]}));
121 mouse
._handleMouseUp(touchevent(
122 'touchend', { touches
: [{ clientX
: 79, clientY
: 45 }]}));
123 this.clock
.tick(200);
124 mouse
._handleMouseDown(touchevent(
125 'touchstart', { touches
: [{ clientX
: 67, clientY
: 35 }]}));
127 mouse
._handleMouseUp(touchevent(
128 'touchend', { touches
: [{ clientX
: 66, clientY
: 36 }]}));
131 it('should not modify 2nd tap pos if far apart', function(done
) {
133 var mouse
= new Mouse({
134 onMouseButton: function(x
, y
, down
, bmask
) {
137 expect(down
).to
.be
.equal(1);
138 expect(x
).to
.be
.equal(68);
139 expect(y
).to
.be
.equal(36);
140 } else if (calls
=== 3) {
141 expect(down
).to
.be
.equal(1);
142 expect(x
).to
.not
.be
.equal(68);
143 expect(y
).to
.not
.be
.equal(36);
149 mouse
._handleMouseDown(touchevent(
150 'touchstart', { touches
: [{ clientX
: 78, clientY
: 46 }]}));
152 mouse
._handleMouseUp(touchevent(
153 'touchend', { touches
: [{ clientX
: 79, clientY
: 45 }]}));
154 this.clock
.tick(200);
155 mouse
._handleMouseDown(touchevent(
156 'touchstart', { touches
: [{ clientX
: 57, clientY
: 35 }]}));
158 mouse
._handleMouseUp(touchevent(
159 'touchend', { touches
: [{ clientX
: 56, clientY
: 36 }]}));
162 it('should not modify 2nd tap pos if not soon enough', function(done
) {
164 var mouse
= new Mouse({
165 onMouseButton: function(x
, y
, down
, bmask
) {
168 expect(down
).to
.be
.equal(1);
169 expect(x
).to
.be
.equal(68);
170 expect(y
).to
.be
.equal(36);
171 } else if (calls
=== 3) {
172 expect(down
).to
.be
.equal(1);
173 expect(x
).to
.not
.be
.equal(68);
174 expect(y
).to
.not
.be
.equal(36);
180 mouse
._handleMouseDown(touchevent(
181 'touchstart', { touches
: [{ clientX
: 78, clientY
: 46 }]}));
183 mouse
._handleMouseUp(touchevent(
184 'touchend', { touches
: [{ clientX
: 79, clientY
: 45 }]}));
185 this.clock
.tick(500);
186 mouse
._handleMouseDown(touchevent(
187 'touchstart', { touches
: [{ clientX
: 67, clientY
: 35 }]}));
189 mouse
._handleMouseUp(touchevent(
190 'touchend', { touches
: [{ clientX
: 66, clientY
: 36 }]}));
193 it('should not modify 2nd tap pos if not touch', function(done
) {
195 var mouse
= new Mouse({
196 onMouseButton: function(x
, y
, down
, bmask
) {
199 expect(down
).to
.be
.equal(1);
200 expect(x
).to
.be
.equal(68);
201 expect(y
).to
.be
.equal(36);
202 } else if (calls
=== 3) {
203 expect(down
).to
.be
.equal(1);
204 expect(x
).to
.not
.be
.equal(68);
205 expect(y
).to
.not
.be
.equal(36);
211 mouse
._handleMouseDown(mouseevent(
212 'mousedown', { button
: '0x01', clientX
: 78, clientY
: 46 }));
214 mouse
._handleMouseUp(mouseevent(
215 'mouseup', { button
: '0x01', clientX
: 79, clientY
: 45 }));
216 this.clock
.tick(200);
217 mouse
._handleMouseDown(mouseevent(
218 'mousedown', { button
: '0x01', clientX
: 67, clientY
: 35 }));
220 mouse
._handleMouseUp(mouseevent(
221 'mouseup', { button
: '0x01', clientX
: 66, clientY
: 36 }));