diff options
author | Samuel Mannehed <samuel@cendio.se> | 2020-05-01 20:26:51 +0200 |
---|---|---|
committer | Samuel Mannehed <samuel@cendio.se> | 2020-05-01 20:37:48 +0200 |
commit | a672168d4dac5735b40cfe618a47f76b69c85882 (patch) | |
tree | 547e0fde9e064ef8697fbd341d814b9011ad4e66 | |
parent | 0f81407c641243df89aa57e23b2e16d7d020c425 (diff) | |
download | novnc-a672168d4dac5735b40cfe618a47f76b69c85882.tar.gz |
Add unit tests for mouse move limit
-rw-r--r-- | tests/test.mouse.js | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/tests/test.mouse.js b/tests/test.mouse.js index 78c74f1..5636a71 100644 --- a/tests/test.mouse.js +++ b/tests/test.mouse.js @@ -301,4 +301,71 @@ describe('Mouse Event Handling', function () { }); }); + describe('Move events should be limited to one each 17 ms', function () { + + let mouse; + beforeEach(function () { + this.clock = sinon.useFakeTimers(Date.now()); + mouse = new Mouse(target); + mouse.onmousemove = sinon.spy(); + }); + afterEach(function () { + this.clock.restore(); + }); + + it('should send a single move instantly', function () { + mouse._handleMouseMove(mouseevent( + 'mousemove', { clientX: 1, clientY: 2 })); + + expect(mouse.onmousemove).to.have.callCount(1); + }); + + it('should delay one if two events are too close', function () { + mouse._handleMouseMove(mouseevent( + 'mousemove', { clientX: 18, clientY: 30 })); + mouse._handleMouseMove(mouseevent( + 'mousemove', { clientX: 20, clientY: 50 })); + + expect(mouse.onmousemove).to.have.callCount(1); + + this.clock.tick(100); + + expect(mouse.onmousemove).to.have.callCount(2); + }); + + it('should only send first and last of many close events', function () { + mouse._handleMouseMove(mouseevent( + 'mousemove', { clientX: 18, clientY: 30 })); + mouse._handleMouseMove(mouseevent( + 'mousemove', { clientX: 20, clientY: 50 })); + mouse._handleMouseMove(mouseevent( + 'mousemove', { clientX: 21, clientY: 55 })); + + // Check positions to verify that the correct calls got through. + // + // The test canvas starts 10px from top and 10 px from left, + // that means the relative coordinates are clientCoords - 10px + expect(mouse.onmousemove).to.have.been.calledWith(8, 20); + + this.clock.tick(60); + + expect(mouse.onmousemove).to.have.callCount(2); + expect(mouse.onmousemove).to.have.been.calledWith(11, 45); + }); + + it('should send events with enough time apart normally', function () { + mouse._handleMouseMove(mouseevent( + 'mousemove', { clientX: 58, clientY: 60 })); + + expect(mouse.onmousemove).to.have.callCount(1); + + this.clock.tick(20); + + mouse._handleMouseMove(mouseevent( + 'mousemove', { clientX: 25, clientY: 60 })); + + expect(mouse.onmousemove).to.have.callCount(2); + }); + }); + }); |