]> git.proxmox.com Git - mirror_novnc.git/blobdiff - tests/test.rfb.js
Initiate connection from RFB constructor
[mirror_novnc.git] / tests / test.rfb.js
index c19a2c95ded1c91107e92964fe26df5d922414a0..404f7db1240bce4118c41d2c91c8919f74bee28d 100644 (file)
@@ -7,25 +7,10 @@ import { deflateInit, deflate } from "../vendor/pako/lib/zlib/deflate.js";
 import { encodings } from '../core/encodings.js';
 import { toUnsigned32bit } from '../core/util/int.js';
 import { encodeUTF8 } from '../core/util/strings.js';
+import KeyTable from '../core/input/keysym.js';
 
 import FakeWebSocket from './fake.websocket.js';
 
-/* UIEvent constructor polyfill for IE */
-(() => {
-    if (typeof window.UIEvent === "function") return;
-
-    function UIEvent( event, params ) {
-        params = params || { bubbles: false, cancelable: false, view: window, detail: undefined };
-        const evt = document.createEvent( 'UIEvent' );
-        evt.initUIEvent( event, params.bubbles, params.cancelable, params.view, params.detail );
-        return evt;
-    }
-
-    UIEvent.prototype = window.UIEvent.prototype;
-
-    window.UIEvent = UIEvent;
-})();
-
 function push8(arr, num) {
     "use strict";
     arr.push(num & 0xFF);
@@ -70,11 +55,13 @@ function deflateWithSize(data) {
     strm.output = new Uint8Array(chunkSize);
     deflateInit(strm, 5);
 
+    /* eslint-disable camelcase */
     strm.input = unCompData;
     strm.avail_in = strm.input.length;
     strm.next_in = 0;
     strm.next_out = 0;
     strm.avail_out = chunkSize;
+    /* eslint-enable camelcase */
 
     deflate(strm, 3);
 
@@ -89,7 +76,7 @@ describe('Remote Frame Buffer Protocol Client', function () {
     after(FakeWebSocket.restore);
 
     before(function () {
-        this.clock = clock = sinon.useFakeTimers();
+        this.clock = clock = sinon.useFakeTimers(Date.now());
         // sinon doesn't support this yet
         raf = window.requestAnimationFrame;
         window.requestAnimationFrame = setTimeout;
@@ -99,16 +86,18 @@ describe('Remote Frame Buffer Protocol Client', function () {
         const _sQ = new Uint8Array(sock._sQbufferSize);
         const rQ = new Uint8Array(sock._rQbufferSize);
 
-        Websock.prototype._old_allocate_buffers = Websock.prototype._allocate_buffers;
-        Websock.prototype._allocate_buffers = function () {
+        Websock.prototype._oldAllocateBuffers = Websock.prototype._allocateBuffers;
+        Websock.prototype._allocateBuffers = function () {
             this._sQ = _sQ;
             this._rQ = rQ;
         };
 
+        // Avoiding printing the entire Websock buffer on errors
+        Websock.prototype.toString = function () { return "[object Websock]"; };
     });
 
     after(function () {
-        Websock.prototype._allocate_buffers = Websock.prototype._old_allocate_buffers;
+        delete Websock.prototype.toString;
         this.clock.restore();
         window.requestAnimationFrame = raf;
     });
@@ -151,38 +140,112 @@ describe('Remote Frame Buffer Protocol Client', function () {
     }
 
     describe('Connecting/Disconnecting', function () {
-        describe('#RFB', function () {
-            it('should set the current state to "connecting"', function () {
-                const client = new RFB(document.createElement('div'), 'wss://host:8675');
-                client._rfbConnectionState = '';
-                this.clock.tick();
-                expect(client._rfbConnectionState).to.equal('connecting');
+        describe('#RFB (constructor)', function () {
+            let open, attach;
+            beforeEach(function () {
+                open = sinon.spy(Websock.prototype, 'open');
+                attach = sinon.spy(Websock.prototype, 'attach');
+            });
+            afterEach(function () {
+                open.restore();
+                attach.restore();
             });
 
             it('should actually connect to the websocket', function () {
-                const client = new RFB(document.createElement('div'), 'ws://HOST:8675/PATH');
-                sinon.spy(client._sock, 'open');
-                this.clock.tick();
-                expect(client._sock.open).to.have.been.calledOnce;
-                expect(client._sock.open).to.have.been.calledWith('ws://HOST:8675/PATH');
+                new RFB(document.createElement('div'), 'ws://HOST:8675/PATH');
+                expect(open).to.have.been.calledOnceWithExactly('ws://HOST:8675/PATH', []);
+            });
+
+            it('should pass on connection problems', function () {
+                open.restore();
+                open = sinon.stub(Websock.prototype, 'open');
+                open.throws(new Error('Failure'));
+                expect(() => new RFB(document.createElement('div'), 'ws://HOST:8675/PATH')).to.throw('Failure');
+            });
+
+            it('should handle WebSocket/RTCDataChannel objects', function () {
+                let sock = new FakeWebSocket('ws://HOST:8675/PATH', []);
+                new RFB(document.createElement('div'), sock);
+                expect(open).to.not.have.been.called;
+                expect(attach).to.have.been.calledOnceWithExactly(sock);
+            });
+
+            it('should handle already open WebSocket/RTCDataChannel objects', function () {
+                let sock = new FakeWebSocket('ws://HOST:8675/PATH', []);
+                sock._open();
+                const client = new RFB(document.createElement('div'), sock);
+                let callback = sinon.spy();
+                client.addEventListener('disconnect', callback);
+                expect(open).to.not.have.been.called;
+                expect(attach).to.have.been.calledOnceWithExactly(sock);
+                // Check if it is ready for some data
+                sock._receiveData(new Uint8Array(['R', 'F', 'B', '0', '0', '3', '0', '0', '8']));
+                expect(callback).to.not.have.been.called;
+            });
+
+            it('should refuse closed WebSocket/RTCDataChannel objects', function () {
+                let sock = new FakeWebSocket('ws://HOST:8675/PATH', []);
+                sock.readyState = WebSocket.CLOSED;
+                expect(() => new RFB(document.createElement('div'), sock)).to.throw();
+            });
+
+            it('should pass on attach problems', function () {
+                attach.restore();
+                attach = sinon.stub(Websock.prototype, 'attach');
+                attach.throws(new Error('Failure'));
+                let sock = new FakeWebSocket('ws://HOST:8675/PATH', []);
+                expect(() => new RFB(document.createElement('div'), sock)).to.throw('Failure');
             });
         });
 
         describe('#disconnect', function () {
             let client;
+            let close;
+
             beforeEach(function () {
                 client = makeRFB();
+                close = sinon.stub(Websock.prototype, "close");
+            });
+            afterEach(function () {
+                close.restore();
             });
 
-            it('should go to state "disconnecting" before "disconnected"', function () {
-                sinon.spy(client, '_updateConnectionState');
+            it('should start closing WebSocket', function () {
+                let callback = sinon.spy();
+                client.addEventListener('disconnect', callback);
                 client.disconnect();
-                expect(client._updateConnectionState).to.have.been.calledTwice;
-                expect(client._updateConnectionState.getCall(0).args[0])
-                    .to.equal('disconnecting');
-                expect(client._updateConnectionState.getCall(1).args[0])
-                    .to.equal('disconnected');
-                expect(client._rfbConnectionState).to.equal('disconnected');
+                expect(close).to.have.been.calledOnceWithExactly();
+                expect(callback).to.not.have.been.called;
+            });
+
+            it('should send disconnect event', function () {
+                let callback = sinon.spy();
+                client.addEventListener('disconnect', callback);
+                client.disconnect();
+                close.thisValues[0]._eventHandlers.close(new CloseEvent("close", { 'code': 1000, 'reason': "", 'wasClean': true }));
+                expect(callback).to.have.been.calledOnce;
+                expect(callback.args[0][0].detail.clean).to.be.true;
+            });
+
+            it('should force disconnect if disconnecting takes too long', function () {
+                let callback = sinon.spy();
+                client.addEventListener('disconnect', callback);
+                client.disconnect();
+                this.clock.tick(3 * 1000);
+                expect(callback).to.have.been.calledOnce;
+                expect(callback.args[0][0].detail.clean).to.be.true;
+            });
+
+            it('should not fail if disconnect completes before timeout', function () {
+                let callback = sinon.spy();
+                client.addEventListener('disconnect', callback);
+                client.disconnect();
+                client._updateConnectionState('disconnecting');
+                this.clock.tick(3 * 1000 / 2);
+                close.thisValues[0]._eventHandlers.close(new CloseEvent("close", { 'code': 1000, 'reason': "", 'wasClean': true }));
+                this.clock.tick(3 * 1000 / 2 + 1);
+                expect(callback).to.have.been.calledOnce;
+                expect(callback.args[0][0].detail.clean).to.be.true;
             });
 
             it('should unregister error event handler', function () {
@@ -514,6 +577,13 @@ describe('Remote Frame Buffer Protocol Client', function () {
                 expect(RFB.messages.pointerEvent).to.have.been.calledTwice;
             });
 
+            it('should not send button messages when in view only', function () {
+                client._viewOnly = true;
+                client._handleMouseButton(13, 9, 0x001);
+                client._handleMouseButton(13, 9, 0x000);
+                expect(RFB.messages.pointerEvent).to.not.have.been.called;
+            });
+
             it('should send button message directly when drag is disabled', function () {
                 client.dragViewport = false;
                 client._handleMouseButton(13, 9, 0x001);
@@ -794,62 +864,6 @@ describe('Remote Frame Buffer Protocol Client', function () {
     });
 
     describe('Misc Internals', function () {
-        describe('#_updateConnectionState', function () {
-            let client;
-            beforeEach(function () {
-                client = makeRFB();
-            });
-
-            it('should clear the disconnect timer if the state is not "disconnecting"', function () {
-                const spy = sinon.spy();
-                client._disconnTimer = setTimeout(spy, 50);
-                client._rfbConnectionState = 'connecting';
-                client._updateConnectionState('connected');
-                this.clock.tick(51);
-                expect(spy).to.not.have.been.called;
-                expect(client._disconnTimer).to.be.null;
-            });
-
-            it('should set the rfbConnectionState', function () {
-                client._rfbConnectionState = 'connecting';
-                client._updateConnectionState('connected');
-                expect(client._rfbConnectionState).to.equal('connected');
-            });
-
-            it('should not change the state when we are disconnected', function () {
-                client.disconnect();
-                expect(client._rfbConnectionState).to.equal('disconnected');
-                client._updateConnectionState('connecting');
-                expect(client._rfbConnectionState).to.not.equal('connecting');
-            });
-
-            it('should ignore state changes to the same state', function () {
-                const connectSpy = sinon.spy();
-                client.addEventListener("connect", connectSpy);
-
-                expect(client._rfbConnectionState).to.equal('connected');
-                client._updateConnectionState('connected');
-                expect(connectSpy).to.not.have.been.called;
-
-                client.disconnect();
-
-                const disconnectSpy = sinon.spy();
-                client.addEventListener("disconnect", disconnectSpy);
-
-                expect(client._rfbConnectionState).to.equal('disconnected');
-                client._updateConnectionState('disconnected');
-                expect(disconnectSpy).to.not.have.been.called;
-            });
-
-            it('should ignore illegal state changes', function () {
-                const spy = sinon.spy();
-                client.addEventListener("disconnect", spy);
-                client._updateConnectionState('disconnected');
-                expect(client._rfbConnectionState).to.not.equal('disconnected');
-                expect(spy).to.not.have.been.called;
-            });
-        });
-
         describe('#_fail', function () {
             let client;
             beforeEach(function () {
@@ -890,106 +904,6 @@ describe('Remote Frame Buffer Protocol Client', function () {
         });
     });
 
-    describe('Connection States', function () {
-        describe('connecting', function () {
-            it('should open the websocket connection', function () {
-                const client = new RFB(document.createElement('div'),
-                                       'ws://HOST:8675/PATH');
-                sinon.spy(client._sock, 'open');
-                this.clock.tick();
-                expect(client._sock.open).to.have.been.calledOnce;
-            });
-        });
-
-        describe('connected', function () {
-            let client;
-            beforeEach(function () {
-                client = makeRFB();
-            });
-
-            it('should result in a connect event if state becomes connected', function () {
-                const spy = sinon.spy();
-                client.addEventListener("connect", spy);
-                client._rfbConnectionState = 'connecting';
-                client._updateConnectionState('connected');
-                expect(spy).to.have.been.calledOnce;
-            });
-
-            it('should not result in a connect event if the state is not "connected"', function () {
-                const spy = sinon.spy();
-                client.addEventListener("connect", spy);
-                client._sock._websocket.open = () => {};  // explicitly don't call onopen
-                client._updateConnectionState('connecting');
-                expect(spy).to.not.have.been.called;
-            });
-        });
-
-        describe('disconnecting', function () {
-            let client;
-            beforeEach(function () {
-                client = makeRFB();
-            });
-
-            it('should force disconnect if we do not call Websock.onclose within the disconnection timeout', function () {
-                sinon.spy(client, '_updateConnectionState');
-                client._sock._websocket.close = () => {};  // explicitly don't call onclose
-                client._updateConnectionState('disconnecting');
-                this.clock.tick(3 * 1000);
-                expect(client._updateConnectionState).to.have.been.calledTwice;
-                expect(client._rfbDisconnectReason).to.not.equal("");
-                expect(client._rfbConnectionState).to.equal("disconnected");
-            });
-
-            it('should not fail if Websock.onclose gets called within the disconnection timeout', function () {
-                client._updateConnectionState('disconnecting');
-                this.clock.tick(3 * 1000 / 2);
-                client._sock._websocket.close();
-                this.clock.tick(3 * 1000 / 2 + 1);
-                expect(client._rfbConnectionState).to.equal('disconnected');
-            });
-
-            it('should close the WebSocket connection', function () {
-                sinon.spy(client._sock, 'close');
-                client._updateConnectionState('disconnecting');
-                expect(client._sock.close).to.have.been.calledOnce;
-            });
-
-            it('should not result in a disconnect event', function () {
-                const spy = sinon.spy();
-                client.addEventListener("disconnect", spy);
-                client._sock._websocket.close = () => {};  // explicitly don't call onclose
-                client._updateConnectionState('disconnecting');
-                expect(spy).to.not.have.been.called;
-            });
-        });
-
-        describe('disconnected', function () {
-            let client;
-            beforeEach(function () {
-                client = new RFB(document.createElement('div'), 'ws://HOST:8675/PATH');
-            });
-
-            it('should result in a disconnect event if state becomes "disconnected"', function () {
-                const spy = sinon.spy();
-                client.addEventListener("disconnect", spy);
-                client._rfbConnectionState = 'disconnecting';
-                client._updateConnectionState('disconnected');
-                expect(spy).to.have.been.calledOnce;
-                expect(spy.args[0][0].detail.clean).to.be.true;
-            });
-
-            it('should result in a disconnect event without msg when no reason given', function () {
-                const spy = sinon.spy();
-                client.addEventListener("disconnect", spy);
-                client._rfbConnectionState = 'disconnecting';
-                client._rfbDisconnectReason = "";
-                client._updateConnectionState('disconnected');
-                expect(spy).to.have.been.calledOnce;
-                expect(spy.args[0].length).to.equal(1);
-            });
-        });
-    });
-
     describe('Protocol Initialization States', function () {
         let client;
         beforeEach(function () {
@@ -1382,6 +1296,102 @@ describe('Remote Frame Buffer Protocol Client', function () {
                     expect(client._fail).to.have.been.calledOnce;
                 });
             });
+
+            describe('VeNCrypt Authentication (type 19) Handler', function () {
+                beforeEach(function () {
+                    client._rfbInitState = 'Security';
+                    client._rfbVersion = 3.8;
+                    sendSecurity(19, client);
+                    expect(client._sock).to.have.sent(new Uint8Array([19]));
+                });
+
+                it('should fail with non-0.2 versions', function () {
+                    sinon.spy(client, "_fail");
+                    client._sock._websocket._receiveData(new Uint8Array([0, 1]));
+                    expect(client._fail).to.have.been.calledOnce;
+                });
+
+                it('should fail if the Plain authentication is not present', function () {
+                    // VeNCrypt version
+                    client._sock._websocket._receiveData(new Uint8Array([0, 2]));
+                    expect(client._sock).to.have.sent(new Uint8Array([0, 2]));
+                    // Server ACK.
+                    client._sock._websocket._receiveData(new Uint8Array([0]));
+                    // Subtype list, only list subtype 1.
+                    sinon.spy(client, "_fail");
+                    client._sock._websocket._receiveData(new Uint8Array([1, 0, 0, 0, 1]));
+                    expect(client._fail).to.have.been.calledOnce;
+                });
+
+                it('should support Plain authentication', function () {
+                    client._rfbCredentials = { username: 'username', password: 'password' };
+                    // VeNCrypt version
+                    client._sock._websocket._receiveData(new Uint8Array([0, 2]));
+                    expect(client._sock).to.have.sent(new Uint8Array([0, 2]));
+                    // Server ACK.
+                    client._sock._websocket._receiveData(new Uint8Array([0]));
+                    // Subtype list.
+                    client._sock._websocket._receiveData(new Uint8Array([1, 0, 0, 1, 0]));
+
+                    const expectedResponse = [];
+                    push32(expectedResponse, 256); // Chosen subtype.
+                    push32(expectedResponse, client._rfbCredentials.username.length);
+                    push32(expectedResponse, client._rfbCredentials.password.length);
+                    pushString(expectedResponse, client._rfbCredentials.username);
+                    pushString(expectedResponse, client._rfbCredentials.password);
+                    expect(client._sock).to.have.sent(new Uint8Array(expectedResponse));
+
+                    client._initMsg = sinon.spy();
+                    client._sock._websocket._receiveData(new Uint8Array([0, 0, 0, 0]));
+                    expect(client._initMsg).to.have.been.called;
+                });
+
+                it('should support Plain authentication with an empty password', function () {
+                    client._rfbCredentials = { username: 'username', password: '' };
+                    // VeNCrypt version
+                    client._sock._websocket._receiveData(new Uint8Array([0, 2]));
+                    expect(client._sock).to.have.sent(new Uint8Array([0, 2]));
+                    // Server ACK.
+                    client._sock._websocket._receiveData(new Uint8Array([0]));
+                    // Subtype list.
+                    client._sock._websocket._receiveData(new Uint8Array([1, 0, 0, 1, 0]));
+
+                    const expectedResponse = [];
+                    push32(expectedResponse, 256); // Chosen subtype.
+                    push32(expectedResponse, client._rfbCredentials.username.length);
+                    push32(expectedResponse, client._rfbCredentials.password.length);
+                    pushString(expectedResponse, client._rfbCredentials.username);
+                    pushString(expectedResponse, client._rfbCredentials.password);
+                    expect(client._sock).to.have.sent(new Uint8Array(expectedResponse));
+
+                    client._initMsg = sinon.spy();
+                    client._sock._websocket._receiveData(new Uint8Array([0, 0, 0, 0]));
+                    expect(client._initMsg).to.have.been.called;
+                });
+
+                it('should support Plain authentication with a very long username and password', function () {
+                    client._rfbCredentials = { username: 'a'.repeat(300), password: 'a'.repeat(300) };
+                    // VeNCrypt version
+                    client._sock._websocket._receiveData(new Uint8Array([0, 2]));
+                    expect(client._sock).to.have.sent(new Uint8Array([0, 2]));
+                    // Server ACK.
+                    client._sock._websocket._receiveData(new Uint8Array([0]));
+                    // Subtype list.
+                    client._sock._websocket._receiveData(new Uint8Array([1, 0, 0, 1, 0]));
+
+                    const expectedResponse = [];
+                    push32(expectedResponse, 256); // Chosen subtype.
+                    push32(expectedResponse, client._rfbCredentials.username.length);
+                    push32(expectedResponse, client._rfbCredentials.password.length);
+                    pushString(expectedResponse, client._rfbCredentials.username);
+                    pushString(expectedResponse, client._rfbCredentials.password);
+                    expect(client._sock).to.have.sent(new Uint8Array(expectedResponse));
+
+                    client._initMsg = sinon.spy();
+                    client._sock._websocket._receiveData(new Uint8Array([0, 0, 0, 0]));
+                    expect(client._initMsg).to.have.been.called;
+                });
+            });
         });
 
         describe('SecurityResult', function () {
@@ -1566,12 +1576,10 @@ describe('Remote Frame Buffer Protocol Client', function () {
                 expect(client._display.resize).to.have.been.calledWith(27, 32);
             });
 
-            it('should grab the mouse and keyboard', function () {
+            it('should grab the keyboard', function () {
                 sinon.spy(client._keyboard, 'grab');
-                sinon.spy(client._mouse, 'grab');
                 sendServerInit({}, client);
                 expect(client._keyboard.grab).to.have.been.calledOnce;
-                expect(client._mouse.grab).to.have.been.calledOnce;
             });
 
             describe('Initial Update Request', function () {
@@ -1616,9 +1624,11 @@ describe('Remote Frame Buffer Protocol Client', function () {
                 });
             });
 
-            it('should transition to the "connected" state', function () {
+            it('should send the "connect" event', function () {
+                let spy = sinon.spy();
+                client.addEventListener('connect', spy);
                 sendServerInit({}, client);
-                expect(client._rfbConnectionState).to.equal('connected');
+                expect(spy).to.have.been.calledOnce;
             });
         });
     });
@@ -1634,28 +1644,6 @@ describe('Remote Frame Buffer Protocol Client', function () {
         });
 
         describe('Framebuffer Update Handling', function () {
-            const targetDataArr = [
-                0xff, 0x00, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
-                0x00, 0xff, 0x00, 255, 0xff, 0x00, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
-                0xee, 0x00, 0xff, 255, 0x00, 0xee, 0xff, 255, 0xaa, 0xee, 0xff, 255, 0xab, 0xee, 0xff, 255,
-                0xee, 0x00, 0xff, 255, 0x00, 0xee, 0xff, 255, 0xaa, 0xee, 0xff, 255, 0xab, 0xee, 0xff, 255
-            ];
-            let targetData;
-
-            const targetDataCheckArr = [
-                0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
-                0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255, 0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255,
-                0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255,
-                0x00, 0xff, 0x00, 255, 0x00, 0xff, 0x00, 255, 0x00, 0x00, 0xff, 255, 0x00, 0x00, 0xff, 255
-            ];
-            let targetDataCheck;
-
-            before(function () {
-                // NB(directxman12): PhantomJS 1.x doesn't implement Uint8ClampedArray
-                targetData = new Uint8Array(targetDataArr);
-                targetDataCheck = new Uint8Array(targetDataCheckArr);
-            });
-
             function sendFbuMsg(rectInfo, rectData, client, rectCnt) {
                 let data = [];
 
@@ -1724,22 +1712,6 @@ describe('Remote Frame Buffer Protocol Client', function () {
                 expect(client._fail).to.have.been.calledOnce;
             });
 
-            it('should be able to pause and resume receiving rects if not enought data', function () {
-                // seed some initial data to copy
-                client._fbWidth = 4;
-                client._fbHeight = 4;
-                client._display.resize(4, 4);
-                client._display.blitRgbxImage(0, 0, 4, 2, new Uint8Array(targetDataCheckArr.slice(0, 32)), 0);
-
-                const info = [{ x: 0, y: 2, width: 2, height: 2, encoding: 0x01},
-                              { x: 2, y: 2, width: 2, height: 2, encoding: 0x01}];
-                // data says [{ old_x: 2, old_y: 0 }, { old_x: 0, old_y: 0 }]
-                const rects = [[0, 2, 0, 0], [0, 0, 0, 0]];
-                sendFbuMsg([info[0]], [rects[0]], client, 2);
-                sendFbuMsg([info[1]], [rects[1]], client, -1);
-                expect(client._display).to.have.displayed(targetDataCheck);
-            });
-
             describe('Message Encoding Handlers', function () {
                 beforeEach(function () {
                     // a really small frame
@@ -1749,216 +1721,6 @@ describe('Remote Frame Buffer Protocol Client', function () {
                     client._display.resize(4, 4);
                 });
 
-                it('should handle the RAW encoding', function () {
-                    const info = [{ x: 0, y: 0, width: 2, height: 2, encoding: 0x00 },
-                                  { x: 2, y: 0, width: 2, height: 2, encoding: 0x00 },
-                                  { x: 0, y: 2, width: 4, height: 1, encoding: 0x00 },
-                                  { x: 0, y: 3, width: 4, height: 1, encoding: 0x00 }];
-                    // data is in bgrx
-                    const rects = [
-                        [0x00, 0x00, 0xff, 0, 0x00, 0xff, 0x00, 0, 0x00, 0xff, 0x00, 0, 0x00, 0x00, 0xff, 0],
-                        [0xff, 0x00, 0x00, 0, 0xff, 0x00, 0x00, 0, 0xff, 0x00, 0x00, 0, 0xff, 0x00, 0x00, 0],
-                        [0xff, 0x00, 0xee, 0, 0xff, 0xee, 0x00, 0, 0xff, 0xee, 0xaa, 0, 0xff, 0xee, 0xab, 0],
-                        [0xff, 0x00, 0xee, 0, 0xff, 0xee, 0x00, 0, 0xff, 0xee, 0xaa, 0, 0xff, 0xee, 0xab, 0]];
-                    sendFbuMsg(info, rects, client);
-                    expect(client._display).to.have.displayed(targetData);
-                });
-
-                it('should handle the RAW encoding in low colour mode', function () {
-                    const info = [{ x: 0, y: 0, width: 2, height: 2, encoding: 0x00 },
-                                  { x: 2, y: 0, width: 2, height: 2, encoding: 0x00 },
-                                  { x: 0, y: 2, width: 4, height: 1, encoding: 0x00 },
-                                  { x: 0, y: 3, width: 4, height: 1, encoding: 0x00 }];
-                    const rects = [
-                        [0x03, 0x03, 0x03, 0x03],
-                        [0x0c, 0x0c, 0x0c, 0x0c],
-                        [0x0c, 0x0c, 0x03, 0x03],
-                        [0x0c, 0x0c, 0x03, 0x03]];
-                    client._fbDepth = 8;
-                    sendFbuMsg(info, rects, client);
-                    expect(client._display).to.have.displayed(targetDataCheck);
-                });
-
-                it('should handle the COPYRECT encoding', function () {
-                    // seed some initial data to copy
-                    client._display.blitRgbxImage(0, 0, 4, 2, new Uint8Array(targetDataCheckArr.slice(0, 32)), 0);
-
-                    const info = [{ x: 0, y: 2, width: 2, height: 2, encoding: 0x01},
-                                  { x: 2, y: 2, width: 2, height: 2, encoding: 0x01}];
-                    // data says [{ old_x: 0, old_y: 0 }, { old_x: 0, old_y: 0 }]
-                    const rects = [[0, 2, 0, 0], [0, 0, 0, 0]];
-                    sendFbuMsg(info, rects, client);
-                    expect(client._display).to.have.displayed(targetDataCheck);
-                });
-
-                // TODO(directxman12): for encodings with subrects, test resuming on partial send?
-                // TODO(directxman12): test rre_chunk_sz (related to above about subrects)?
-
-                it('should handle the RRE encoding', function () {
-                    const info = [{ x: 0, y: 0, width: 4, height: 4, encoding: 0x02 }];
-                    const rect = [];
-                    push32(rect, 2); // 2 subrects
-                    push32(rect, 0xff00ff); // becomes 00ff00ff --> #00FF00 bg color
-                    rect.push(0xff); // becomes ff0000ff --> #0000FF color
-                    rect.push(0x00);
-                    rect.push(0x00);
-                    rect.push(0xff);
-                    push16(rect, 0); // x: 0
-                    push16(rect, 0); // y: 0
-                    push16(rect, 2); // width: 2
-                    push16(rect, 2); // height: 2
-                    rect.push(0xff); // becomes ff0000ff --> #0000FF color
-                    rect.push(0x00);
-                    rect.push(0x00);
-                    rect.push(0xff);
-                    push16(rect, 2); // x: 2
-                    push16(rect, 2); // y: 2
-                    push16(rect, 2); // width: 2
-                    push16(rect, 2); // height: 2
-                    sendFbuMsg(info, [rect], client);
-                    expect(client._display).to.have.displayed(targetDataCheck);
-                });
-
-                describe('the HEXTILE encoding handler', function () {
-                    it('should handle a tile with fg, bg specified, normal subrects', function () {
-                        const info = [{ x: 0, y: 0, width: 4, height: 4, encoding: 0x05 }];
-                        const rect = [];
-                        rect.push(0x02 | 0x04 | 0x08); // bg spec, fg spec, anysubrects
-                        push32(rect, 0xff00ff); // becomes 00ff00ff --> #00FF00 bg color
-                        rect.push(0xff); // becomes ff0000ff --> #0000FF fg color
-                        rect.push(0x00);
-                        rect.push(0x00);
-                        rect.push(0xff);
-                        rect.push(2); // 2 subrects
-                        rect.push(0); // x: 0, y: 0
-                        rect.push(1 | (1 << 4)); // width: 2, height: 2
-                        rect.push(2 | (2 << 4)); // x: 2, y: 2
-                        rect.push(1 | (1 << 4)); // width: 2, height: 2
-                        sendFbuMsg(info, [rect], client);
-                        expect(client._display).to.have.displayed(targetDataCheck);
-                    });
-
-                    it('should handle a raw tile', function () {
-                        const info = [{ x: 0, y: 0, width: 4, height: 4, encoding: 0x05 }];
-                        const rect = [];
-                        rect.push(0x01); // raw
-                        for (let i = 0; i < targetData.length; i += 4) {
-                            rect.push(targetData[i + 2]);
-                            rect.push(targetData[i + 1]);
-                            rect.push(targetData[i]);
-                            rect.push(targetData[i + 3]);
-                        }
-                        sendFbuMsg(info, [rect], client);
-                        expect(client._display).to.have.displayed(targetData);
-                    });
-
-                    it('should handle a tile with only bg specified (solid bg)', function () {
-                        const info = [{ x: 0, y: 0, width: 4, height: 4, encoding: 0x05 }];
-                        const rect = [];
-                        rect.push(0x02);
-                        push32(rect, 0xff00ff); // becomes 00ff00ff --> #00FF00 bg color
-                        sendFbuMsg(info, [rect], client);
-
-                        const expected = [];
-                        for (let i = 0; i < 16; i++) { push32(expected, 0xff00ff); }
-                        expect(client._display).to.have.displayed(new Uint8Array(expected));
-                    });
-
-                    it('should handle a tile with only bg specified and an empty frame afterwards', function () {
-                        // set the width so we can have two tiles
-                        client._fbWidth = 8;
-                        client._display.resize(8, 4);
-
-                        const info = [{ x: 0, y: 0, width: 32, height: 4, encoding: 0x05 }];
-
-                        const rect = [];
-
-                        // send a bg frame
-                        rect.push(0x02);
-                        push32(rect, 0xff00ff); // becomes 00ff00ff --> #00FF00 bg color
-
-                        // send an empty frame
-                        rect.push(0x00);
-
-                        sendFbuMsg(info, [rect], client);
-
-                        const expected = [];
-                        for (let i = 0; i < 16; i++) { push32(expected, 0xff00ff); }     // rect 1: solid
-                        for (let i = 0; i < 16; i++) { push32(expected, 0xff00ff); }    // rect 2: same bkground color
-                        expect(client._display).to.have.displayed(new Uint8Array(expected));
-                    });
-
-                    it('should handle a tile with bg and coloured subrects', function () {
-                        const info = [{ x: 0, y: 0, width: 4, height: 4, encoding: 0x05 }];
-                        const rect = [];
-                        rect.push(0x02 | 0x08 | 0x10); // bg spec, anysubrects, colouredsubrects
-                        push32(rect, 0xff00ff); // becomes 00ff00ff --> #00FF00 bg color
-                        rect.push(2); // 2 subrects
-                        rect.push(0xff); // becomes ff0000ff --> #0000FF fg color
-                        rect.push(0x00);
-                        rect.push(0x00);
-                        rect.push(0xff);
-                        rect.push(0); // x: 0, y: 0
-                        rect.push(1 | (1 << 4)); // width: 2, height: 2
-                        rect.push(0xff); // becomes ff0000ff --> #0000FF fg color
-                        rect.push(0x00);
-                        rect.push(0x00);
-                        rect.push(0xff);
-                        rect.push(2 | (2 << 4)); // x: 2, y: 2
-                        rect.push(1 | (1 << 4)); // width: 2, height: 2
-                        sendFbuMsg(info, [rect], client);
-                        expect(client._display).to.have.displayed(targetDataCheck);
-                    });
-
-                    it('should carry over fg and bg colors from the previous tile if not specified', function () {
-                        client._fbWidth = 4;
-                        client._fbHeight = 17;
-                        client._display.resize(4, 17);
-
-                        const info = [{ x: 0, y: 0, width: 4, height: 17, encoding: 0x05}];
-                        const rect = [];
-                        rect.push(0x02 | 0x04 | 0x08); // bg spec, fg spec, anysubrects
-                        push32(rect, 0xff00ff); // becomes 00ff00ff --> #00FF00 bg color
-                        rect.push(0xff); // becomes ff0000ff --> #0000FF fg color
-                        rect.push(0x00);
-                        rect.push(0x00);
-                        rect.push(0xff);
-                        rect.push(8); // 8 subrects
-                        for (let i = 0; i < 4; i++) {
-                            rect.push((0 << 4) | (i * 4)); // x: 0, y: i*4
-                            rect.push(1 | (1 << 4)); // width: 2, height: 2
-                            rect.push((2 << 4) | (i * 4 + 2)); // x: 2, y: i * 4 + 2
-                            rect.push(1 | (1 << 4)); // width: 2, height: 2
-                        }
-                        rect.push(0x08); // anysubrects
-                        rect.push(1); // 1 subrect
-                        rect.push(0); // x: 0, y: 0
-                        rect.push(1 | (1 << 4)); // width: 2, height: 2
-                        sendFbuMsg(info, [rect], client);
-
-                        let expected = [];
-                        for (let i = 0; i < 4; i++) { expected = expected.concat(targetDataCheckArr); }
-                        expected = expected.concat(targetDataCheckArr.slice(0, 16));
-                        expect(client._display).to.have.displayed(new Uint8Array(expected));
-                    });
-
-                    it('should fail on an invalid subencoding', function () {
-                        sinon.spy(client, "_fail");
-                        const info = [{ x: 0, y: 0, width: 4, height: 4, encoding: 0x05 }];
-                        const rects = [[45]];  // an invalid subencoding
-                        sendFbuMsg(info, rects, client);
-                        expect(client._fail).to.have.been.calledOnce;
-                    });
-                });
-
-                it.skip('should handle the TIGHT encoding', function () {
-                    // TODO(directxman12): test this
-                });
-
-                it.skip('should handle the TIGHT_PNG encoding', function () {
-                    // TODO(directxman12): test this
-                });
-
                 it('should handle the DesktopSize pseduo-encoding', function () {
                     sinon.spy(client._display, 'resize');
                     sendFbuMsg([{ x: 0, y: 0, width: 20, height: 50, encoding: -223 }], [[]], client);
@@ -2514,12 +2276,7 @@ describe('Remote Frame Buffer Protocol Client', function () {
                     });
 
                     it('should be able to handle large Provide messages', function () {
-                        // repeat() is not supported in IE so a loop is needed instead
-                        let expectedData = "hello";
-                        for (let i = 1; i <= 100000; i++) {
-                            expectedData += "hello";
-                        }
-
+                        let expectedData = "hello".repeat(100000);
                         let data = [3, 0, 0, 0];
                         const flags = [0x10, 0x00, 0x00, 0x01];
 
@@ -2718,81 +2475,1119 @@ describe('Remote Frame Buffer Protocol Client', function () {
 
     describe('Asynchronous Events', function () {
         let client;
+        let pointerEvent;
+        let keyEvent;
+        let qemuKeyEvent;
+
         beforeEach(function () {
             client = makeRFB();
+            client._display.resize(100, 100);
+
+            // We need to disable this as focusing the canvas will
+            // cause the browser to scoll to it, messing up our
+            // client coordinate calculations
+            client.focusOnClick = false;
+
+            pointerEvent = sinon.spy(RFB.messages, 'pointerEvent');
+            keyEvent = sinon.spy(RFB.messages, 'keyEvent');
+            qemuKeyEvent = sinon.spy(RFB.messages, 'QEMUExtendedKeyEvent');
         });
 
-        describe('Mouse event handlers', function () {
+        afterEach(function () {
+            pointerEvent.restore();
+            keyEvent.restore();
+            qemuKeyEvent.restore();
+        });
+
+        function elementToClient(x, y) {
+            let res = { x: 0, y: 0 };
+
+            let bounds = client._canvas.getBoundingClientRect();
+
+            /*
+             * If the canvas is on a fractional position we will calculate
+             * a fractional mouse position. But that gets truncated when we
+             * send the event, AND the same thing happens in RFB when it
+             * generates the PointerEvent message. To compensate for that
+             * fact we round the value upwards here.
+             */
+            res.x = Math.ceil(bounds.left + x);
+            res.y = Math.ceil(bounds.top + y);
+
+            return res;
+        }
+
+        describe('Mouse Events', function () {
+            function sendMouseMoveEvent(x, y) {
+                let pos = elementToClient(x, y);
+                let ev;
+
+                ev = new MouseEvent('mousemove',
+                                    { 'screenX': pos.x + window.screenX,
+                                      'screenY': pos.y + window.screenY,
+                                      'clientX': pos.x,
+                                      'clientY': pos.y });
+                client._canvas.dispatchEvent(ev);
+            }
+
+            function sendMouseButtonEvent(x, y, down, button) {
+                let pos = elementToClient(x, y);
+                let ev;
+
+                ev = new MouseEvent(down ? 'mousedown' : 'mouseup',
+                                    { 'screenX': pos.x + window.screenX,
+                                      'screenY': pos.y + window.screenY,
+                                      'clientX': pos.x,
+                                      'clientY': pos.y,
+                                      'button': button,
+                                      'buttons': 1 << button });
+                client._canvas.dispatchEvent(ev);
+            }
+
             it('should not send button messages in view-only mode', function () {
                 client._viewOnly = true;
-                sinon.spy(client._sock, 'flush');
-                client._handleMouseButton(0, 0, 1, 0x001);
-                expect(client._sock.flush).to.not.have.been.called;
+                sendMouseButtonEvent(10, 10, true, 0);
+                clock.tick(50);
+                expect(pointerEvent).to.not.have.been.called;
             });
 
             it('should not send movement messages in view-only mode', function () {
                 client._viewOnly = true;
-                sinon.spy(client._sock, 'flush');
-                client._handleMouseMove(0, 0);
-                expect(client._sock.flush).to.not.have.been.called;
+                sendMouseMoveEvent(10, 10);
+                clock.tick(50);
+                expect(pointerEvent).to.not.have.been.called;
             });
 
-            it('should send a pointer event on mouse button presses', function () {
-                client._handleMouseButton(10, 12, 1, 0x001);
-                const pointerMsg = {_sQ: new Uint8Array(6), _sQlen: 0, flush: () => {}};
-                RFB.messages.pointerEvent(pointerMsg, 10, 12, 0x001);
-                expect(client._sock).to.have.sent(pointerMsg._sQ);
+            it('should handle left mouse button', function () {
+                sendMouseButtonEvent(10, 10, true, 0);
+
+                expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                 10, 10, 0x1);
+                pointerEvent.resetHistory();
+
+                sendMouseButtonEvent(10, 10, false, 0);
+
+                expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                 10, 10, 0x0);
             });
 
-            it('should send a mask of 1 on mousedown', function () {
-                client._handleMouseButton(10, 12, 1, 0x001);
-                const pointerMsg = {_sQ: new Uint8Array(6), _sQlen: 0, flush: () => {}};
-                RFB.messages.pointerEvent(pointerMsg, 10, 12, 0x001);
-                expect(client._sock).to.have.sent(pointerMsg._sQ);
+            it('should handle middle mouse button', function () {
+                sendMouseButtonEvent(10, 10, true, 1);
+
+                expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                 10, 10, 0x2);
+                pointerEvent.resetHistory();
+
+                sendMouseButtonEvent(10, 10, false, 1);
+
+                expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                 10, 10, 0x0);
             });
 
-            it('should send a mask of 0 on mouseup', function () {
-                client._mouseButtonMask = 0x001;
-                client._handleMouseButton(10, 12, 0, 0x001);
-                const pointerMsg = {_sQ: new Uint8Array(6), _sQlen: 0, flush: () => {}};
-                RFB.messages.pointerEvent(pointerMsg, 10, 12, 0x000);
-                expect(client._sock).to.have.sent(pointerMsg._sQ);
+            it('should handle right mouse button', function () {
+                sendMouseButtonEvent(10, 10, true, 2);
+
+                expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                 10, 10, 0x4);
+                pointerEvent.resetHistory();
+
+                sendMouseButtonEvent(10, 10, false, 2);
+
+                expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                 10, 10, 0x0);
             });
 
-            it('should send a pointer event on mouse movement', function () {
-                client._handleMouseMove(10, 12);
-                const pointerMsg = {_sQ: new Uint8Array(6), _sQlen: 0, flush: () => {}};
-                RFB.messages.pointerEvent(pointerMsg, 10, 12, 0x000);
-                expect(client._sock).to.have.sent(pointerMsg._sQ);
+            it('should handle multiple mouse buttons', function () {
+                sendMouseButtonEvent(10, 10, true, 0);
+                sendMouseButtonEvent(10, 10, true, 2);
+
+                expect(pointerEvent).to.have.been.calledTwice;
+                expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                       10, 10, 0x1);
+                expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                        10, 10, 0x5);
+
+                pointerEvent.resetHistory();
+
+                sendMouseButtonEvent(10, 10, false, 0);
+                sendMouseButtonEvent(10, 10, false, 2);
+
+                expect(pointerEvent).to.have.been.calledTwice;
+                expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                       10, 10, 0x4);
+                expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                        10, 10, 0x0);
             });
 
-            it('should set the button mask so that future mouse movements use it', function () {
-                client._handleMouseButton(10, 12, 1, 0x010);
-                client._handleMouseMove(13, 9);
-                const pointerMsg = {_sQ: new Uint8Array(12), _sQlen: 0, flush: () => {}};
-                RFB.messages.pointerEvent(pointerMsg, 10, 12, 0x010);
-                RFB.messages.pointerEvent(pointerMsg, 13, 9, 0x010);
-                expect(client._sock).to.have.sent(pointerMsg._sQ);
+            it('should handle mouse movement', function () {
+                sendMouseMoveEvent(50, 70);
+                expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                 50, 70, 0x0);
             });
-        });
 
-        describe('Keyboard Event Handlers', function () {
-            it('should send a key message on a key press', function () {
-                client._handleKeyEvent(0x41, 'KeyA', true);
-                const keyMsg = {_sQ: new Uint8Array(8), _sQlen: 0, flush: () => {}};
-                RFB.messages.keyEvent(keyMsg, 0x41, 1);
-                expect(client._sock).to.have.sent(keyMsg._sQ);
+            it('should handle click and drag', function () {
+                sendMouseButtonEvent(10, 10, true, 0);
+                sendMouseMoveEvent(50, 70);
+
+                expect(pointerEvent).to.have.been.calledTwice;
+                expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                       10, 10, 0x1);
+                expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                        50, 70, 0x1);
+
+                pointerEvent.resetHistory();
+
+                sendMouseButtonEvent(50, 70, false, 0);
+
+                expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                 50, 70, 0x0);
             });
 
-            it('should not send messages in view-only mode', function () {
-                client._viewOnly = true;
-                sinon.spy(client._sock, 'flush');
-                client._handleKeyEvent('a', 'KeyA', true);
-                expect(client._sock.flush).to.not.have.been.called;
+            describe('Event Aggregation', function () {
+                it('should send a single pointer event on mouse movement', function () {
+                    sendMouseMoveEvent(50, 70);
+                    clock.tick(100);
+                    expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                     50, 70, 0x0);
+                });
+
+                it('should delay one move if two events are too close', function () {
+                    sendMouseMoveEvent(18, 30);
+                    sendMouseMoveEvent(20, 50);
+
+                    expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                     18, 30, 0x0);
+                    pointerEvent.resetHistory();
+
+                    clock.tick(100);
+
+                    expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                     20, 50, 0x0);
+                });
+
+                it('should only send first and last move of many close events', function () {
+                    sendMouseMoveEvent(18, 30);
+                    sendMouseMoveEvent(20, 50);
+                    sendMouseMoveEvent(21, 55);
+
+                    expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                     18, 30, 0x0);
+                    pointerEvent.resetHistory();
+
+                    clock.tick(100);
+
+                    expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                     21, 55, 0x0);
+                });
+
+                // We selected the 17ms since that is ~60 FPS
+                it('should send move events every 17 ms', function () {
+                    sendMouseMoveEvent(1, 10);  // instant send
+                    clock.tick(10);
+
+                    expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                     1, 10, 0x0);
+                    pointerEvent.resetHistory();
+
+                    sendMouseMoveEvent(2, 20);  // delayed
+                    clock.tick(10);        // timeout send
+
+                    expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                     2, 20, 0x0);
+                    pointerEvent.resetHistory();
+
+                    sendMouseMoveEvent(3, 30);  // delayed
+                    clock.tick(10);
+                    sendMouseMoveEvent(4, 40);  // delayed
+                    clock.tick(10);        // timeout send
+
+                    expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                     4, 40, 0x0);
+                    pointerEvent.resetHistory();
+
+                    sendMouseMoveEvent(5, 50);  // delayed
+
+                    expect(pointerEvent).to.not.have.been.called;
+                });
+
+                it('should send waiting move events before a button press', function () {
+                    sendMouseMoveEvent(13, 9);
+
+                    expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                     13, 9, 0x0);
+                    pointerEvent.resetHistory();
+
+                    sendMouseMoveEvent(20, 70);
+
+                    expect(pointerEvent).to.not.have.been.called;
+
+                    sendMouseButtonEvent(20, 70, true, 0);
+
+                    expect(pointerEvent).to.have.been.calledTwice;
+                    expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                           20, 70, 0x0);
+                    expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                            20, 70, 0x1);
+                });
+
+                it('should send move events with enough time apart normally', function () {
+                    sendMouseMoveEvent(58, 60);
+
+                    expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                     58, 60, 0x0);
+                    pointerEvent.resetHistory();
+
+                    clock.tick(20);
+
+                    sendMouseMoveEvent(25, 60);
+
+                    expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                     25, 60, 0x0);
+                    pointerEvent.resetHistory();
+                });
+
+                it('should not send waiting move events if disconnected', function () {
+                    sendMouseMoveEvent(88, 99);
+
+                    expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                     88, 99, 0x0);
+                    pointerEvent.resetHistory();
+
+                    sendMouseMoveEvent(66, 77);
+                    client.disconnect();
+                    clock.tick(20);
+
+                    expect(pointerEvent).to.not.have.been.called;
+                });
+            });
+
+            it.skip('should block click events', function () {
+                /* FIXME */
+            });
+
+            it.skip('should block contextmenu events', function () {
+                /* FIXME */
+            });
+        });
+
+        describe('Wheel Events', function () {
+            function sendWheelEvent(x, y, dx, dy, mode=0) {
+                let pos = elementToClient(x, y);
+                let ev;
+
+                ev = new WheelEvent('wheel',
+                                    { 'screenX': pos.x + window.screenX,
+                                      'screenY': pos.y + window.screenY,
+                                      'clientX': pos.x,
+                                      'clientY': pos.y,
+                                      'deltaX': dx,
+                                      'deltaY': dy,
+                                      'deltaMode': mode });
+                client._canvas.dispatchEvent(ev);
+            }
+
+            it('should handle wheel up event', function () {
+                sendWheelEvent(10, 10, 0, -50);
+
+                expect(pointerEvent).to.have.been.calledTwice;
+                expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                       10, 10, 1<<3);
+                expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                        10, 10, 0);
+            });
+
+            it('should handle wheel down event', function () {
+                sendWheelEvent(10, 10, 0, 50);
+
+                expect(pointerEvent).to.have.been.calledTwice;
+                expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                       10, 10, 1<<4);
+                expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                        10, 10, 0);
+            });
+
+            it('should handle wheel left event', function () {
+                sendWheelEvent(10, 10, -50, 0);
+
+                expect(pointerEvent).to.have.been.calledTwice;
+                expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                       10, 10, 1<<5);
+                expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                        10, 10, 0);
+            });
+
+            it('should handle wheel right event', function () {
+                sendWheelEvent(10, 10, 50, 0);
+
+                expect(pointerEvent).to.have.been.calledTwice;
+                expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                       10, 10, 1<<6);
+                expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                        10, 10, 0);
+            });
+
+            it('should ignore wheel when in view only', function () {
+                client._viewOnly = true;
+
+                sendWheelEvent(10, 10, 50, 0);
+
+                expect(pointerEvent).to.not.have.been.called;
+            });
+
+            it('should accumulate wheel events if small enough', function () {
+                sendWheelEvent(10, 10, 0, 20);
+                sendWheelEvent(10, 10, 0, 20);
+
+                expect(pointerEvent).to.not.have.been.called;
+
+                sendWheelEvent(10, 10, 0, 20);
+
+                expect(pointerEvent).to.have.been.calledTwice;
+                expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                       10, 10, 1<<4);
+                expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                        10, 10, 0);
+            });
+
+            it('should not accumulate large wheel events', function () {
+                sendWheelEvent(10, 10, 0, 400);
+
+                expect(pointerEvent).to.have.been.calledTwice;
+                expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                       10, 10, 1<<4);
+                expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                        10, 10, 0);
+            });
+
+            it('should handle line based wheel event', function () {
+                sendWheelEvent(10, 10, 0, 3, 1);
+
+                expect(pointerEvent).to.have.been.calledTwice;
+                expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                       10, 10, 1<<4);
+                expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                        10, 10, 0);
+            });
+
+            it('should handle page based wheel event', function () {
+                sendWheelEvent(10, 10, 0, 3, 2);
+
+                expect(pointerEvent).to.have.been.calledTwice;
+                expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                       10, 10, 1<<4);
+                expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                        10, 10, 0);
+            });
+        });
+
+        describe('Keyboard Events', function () {
+            it('should send a key message on a key press', function () {
+                client._handleKeyEvent(0x41, 'KeyA', true);
+                const keyMsg = {_sQ: new Uint8Array(8), _sQlen: 0, flush: () => {}};
+                RFB.messages.keyEvent(keyMsg, 0x41, 1);
+                expect(client._sock).to.have.sent(keyMsg._sQ);
+            });
+
+            it('should not send messages in view-only mode', function () {
+                client._viewOnly = true;
+                sinon.spy(client._sock, 'flush');
+                client._handleKeyEvent('a', 'KeyA', true);
+                expect(client._sock.flush).to.not.have.been.called;
+            });
+        });
+
+        describe('Gesture event handlers', function () {
+            function gestureStart(gestureType, x, y,
+                                  magnitudeX = 0, magnitudeY = 0) {
+                let pos = elementToClient(x, y);
+                let detail = {type: gestureType, clientX: pos.x, clientY: pos.y};
+
+                detail.magnitudeX = magnitudeX;
+                detail.magnitudeY = magnitudeY;
+
+                let ev = new CustomEvent('gesturestart', { detail: detail });
+                client._canvas.dispatchEvent(ev);
+            }
+
+            function gestureMove(gestureType, x, y,
+                                 magnitudeX = 0, magnitudeY = 0) {
+                let pos = elementToClient(x, y);
+                let detail = {type: gestureType, clientX: pos.x, clientY: pos.y};
+
+                detail.magnitudeX = magnitudeX;
+                detail.magnitudeY = magnitudeY;
+
+                let ev = new CustomEvent('gesturemove', { detail: detail });
+                client._canvas.dispatchEvent(ev);
+            }
+
+            function gestureEnd(gestureType, x, y) {
+                let pos = elementToClient(x, y);
+                let detail = {type: gestureType, clientX: pos.x, clientY: pos.y};
+                let ev = new CustomEvent('gestureend', { detail: detail });
+                client._canvas.dispatchEvent(ev);
+            }
+
+            describe('Gesture onetap', function () {
+                it('should handle onetap events', function () {
+                    let bmask = 0x1;
+
+                    gestureStart('onetap', 20, 40);
+                    gestureEnd('onetap', 20, 40);
+
+                    expect(pointerEvent).to.have.been.calledThrice;
+                    expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+                    expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                            20, 40, bmask);
+                    expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+                });
+
+                it('should keep same position for multiple onetap events', function () {
+                    let bmask = 0x1;
+
+                    gestureStart('onetap', 20, 40);
+                    gestureEnd('onetap', 20, 40);
+
+                    expect(pointerEvent).to.have.been.calledThrice;
+                    expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+                    expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                            20, 40, bmask);
+                    expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+
+                    pointerEvent.resetHistory();
+
+                    gestureStart('onetap', 20, 50);
+                    gestureEnd('onetap', 20, 50);
+
+                    expect(pointerEvent).to.have.been.calledThrice;
+                    expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+                    expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                            20, 40, bmask);
+                    expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+
+                    pointerEvent.resetHistory();
+
+                    gestureStart('onetap', 30, 50);
+                    gestureEnd('onetap', 30, 50);
+
+                    expect(pointerEvent).to.have.been.calledThrice;
+                    expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+                    expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                            20, 40, bmask);
+                    expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+                });
+
+                it('should not keep same position for onetap events when too far apart', function () {
+                    let bmask = 0x1;
+
+                    gestureStart('onetap', 20, 40);
+                    gestureEnd('onetap', 20, 40);
+
+                    expect(pointerEvent).to.have.been.calledThrice;
+                    expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+                    expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                            20, 40, bmask);
+                    expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+
+                    pointerEvent.resetHistory();
+
+                    gestureStart('onetap', 80, 95);
+                    gestureEnd('onetap', 80, 95);
+
+                    expect(pointerEvent).to.have.been.calledThrice;
+                    expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                           80, 95, 0x0);
+                    expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                            80, 95, bmask);
+                    expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+                                                                           80, 95, 0x0);
+                });
+
+                it('should not keep same position for onetap events when enough time inbetween', function () {
+                    let bmask = 0x1;
+
+                    gestureStart('onetap', 10, 20);
+                    gestureEnd('onetap', 10, 20);
+
+                    expect(pointerEvent).to.have.been.calledThrice;
+                    expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                           10, 20, 0x0);
+                    expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                            10, 20, bmask);
+                    expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+                                                                           10, 20, 0x0);
+
+                    pointerEvent.resetHistory();
+                    this.clock.tick(1500);
+
+                    gestureStart('onetap', 15, 20);
+                    gestureEnd('onetap', 15, 20);
+
+                    expect(pointerEvent).to.have.been.calledThrice;
+                    expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                           15, 20, 0x0);
+                    expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                            15, 20, bmask);
+                    expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+                                                                           15, 20, 0x0);
+
+                    pointerEvent.resetHistory();
+                });
+            });
+
+            describe('Gesture twotap', function () {
+                it('should handle gesture twotap events', function () {
+                    let bmask = 0x4;
+
+                    gestureStart("twotap", 20, 40);
+
+                    expect(pointerEvent).to.have.been.calledThrice;
+                    expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+                    expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                            20, 40, bmask);
+                    expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+                });
+
+                it('should keep same position for multiple twotap events', function () {
+                    let bmask = 0x4;
+
+                    for (let offset = 0;offset < 30;offset += 10) {
+                        pointerEvent.resetHistory();
+
+                        gestureStart('twotap', 20, 40 + offset);
+                        gestureEnd('twotap', 20, 40 + offset);
+
+                        expect(pointerEvent).to.have.been.calledThrice;
+                        expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                               20, 40, 0x0);
+                        expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                                20, 40, bmask);
+                        expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+                                                                               20, 40, 0x0);
+                    }
+                });
+            });
+
+            describe('Gesture threetap', function () {
+                it('should handle gesture start for threetap events', function () {
+                    let bmask = 0x2;
+
+                    gestureStart("threetap", 20, 40);
+
+                    expect(pointerEvent).to.have.been.calledThrice;
+                    expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+                    expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                            20, 40, bmask);
+                    expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+                });
+
+                it('should keep same position for multiple threetap events', function () {
+                    let bmask = 0x2;
+
+                    for (let offset = 0;offset < 30;offset += 10) {
+                        pointerEvent.resetHistory();
+
+                        gestureStart('threetap', 20, 40 + offset);
+                        gestureEnd('threetap', 20, 40 + offset);
+
+                        expect(pointerEvent).to.have.been.calledThrice;
+                        expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                               20, 40, 0x0);
+                        expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                                20, 40, bmask);
+                        expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+                                                                               20, 40, 0x0);
+                    }
+                });
+            });
+
+            describe('Gesture drag', function () {
+                it('should handle gesture drag events', function () {
+                    let bmask = 0x1;
+
+                    gestureStart('drag', 20, 40);
+
+                    expect(pointerEvent).to.have.been.calledTwice;
+                    expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+                    expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                            20, 40, bmask);
+
+                    pointerEvent.resetHistory();
+
+                    gestureMove('drag', 30, 50);
+                    clock.tick(50);
+
+                    expect(pointerEvent).to.have.been.calledOnce;
+                    expect(pointerEvent).to.have.been.calledWith(client._sock,
+                                                                 30, 50, bmask);
+
+                    pointerEvent.resetHistory();
+
+                    gestureEnd('drag', 30, 50);
+
+                    expect(pointerEvent).to.have.been.calledTwice;
+                    expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                           30, 50, bmask);
+                    expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                            30, 50, 0x0);
+                });
+            });
+
+            describe('Gesture long press', function () {
+                it('should handle long press events', function () {
+                    let bmask = 0x4;
+
+                    gestureStart('longpress', 20, 40);
+
+                    expect(pointerEvent).to.have.been.calledTwice;
+                    expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+                    expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                            20, 40, bmask);
+                    pointerEvent.resetHistory();
+
+                    gestureMove('longpress', 40, 60);
+                    clock.tick(50);
+
+                    expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                     40, 60, bmask);
+
+                    pointerEvent.resetHistory();
+
+                    gestureEnd('longpress', 40, 60);
+
+                    expect(pointerEvent).to.have.been.calledTwice;
+                    expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                           40, 60, bmask);
+                    expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                            40, 60, 0x0);
+                });
+            });
+
+            describe('Gesture twodrag', function () {
+                it('should handle gesture twodrag up events', function () {
+                    let bmask = 0x10; // Button mask for scroll down
+
+                    gestureStart('twodrag', 20, 40, 0, 0);
+
+                    expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                     20, 40, 0x0);
+
+                    pointerEvent.resetHistory();
+
+                    gestureMove('twodrag', 20, 40, 0, -60);
+
+                    expect(pointerEvent).to.have.been.calledThrice;
+                    expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+                    expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                            20, 40, bmask);
+                    expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+                });
+
+                it('should handle gesture twodrag down events', function () {
+                    let bmask = 0x8; // Button mask for scroll up
+
+                    gestureStart('twodrag', 20, 40, 0, 0);
+
+                    expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                     20, 40, 0x0);
+
+                    pointerEvent.resetHistory();
+
+                    gestureMove('twodrag', 20, 40, 0, 60);
+
+                    expect(pointerEvent).to.have.been.calledThrice;
+                    expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+                    expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                            20, 40, bmask);
+                    expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+                });
+
+                it('should handle gesture twodrag right events', function () {
+                    let bmask = 0x20; // Button mask for scroll right
+
+                    gestureStart('twodrag', 20, 40, 0, 0);
+
+                    expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                     20, 40, 0x0);
+
+                    pointerEvent.resetHistory();
+
+                    gestureMove('twodrag', 20, 40, 60, 0);
+
+                    expect(pointerEvent).to.have.been.calledThrice;
+                    expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+                    expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                            20, 40, bmask);
+                    expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+                });
+
+                it('should handle gesture twodrag left events', function () {
+                    let bmask = 0x40; // Button mask for scroll left
+
+                    gestureStart('twodrag', 20, 40, 0, 0);
+
+                    expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                     20, 40, 0x0);
+
+                    pointerEvent.resetHistory();
+
+                    gestureMove('twodrag', 20, 40, -60, 0);
+
+                    expect(pointerEvent).to.have.been.calledThrice;
+                    expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+                    expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                            20, 40, bmask);
+                    expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+                });
+
+                it('should handle gesture twodrag diag events', function () {
+                    let scrlUp = 0x8; // Button mask for scroll up
+                    let scrlRight = 0x20; // Button mask for scroll right
+
+                    gestureStart('twodrag', 20, 40, 0, 0);
+
+                    expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                     20, 40, 0x0);
+
+                    pointerEvent.resetHistory();
+
+                    gestureMove('twodrag', 20, 40, 60, 60);
+
+                    expect(pointerEvent).to.have.been.callCount(5);
+                    expect(pointerEvent.getCall(0)).to.have.been.calledWith(client._sock,
+                                                                            20, 40, 0x0);
+                    expect(pointerEvent.getCall(1)).to.have.been.calledWith(client._sock,
+                                                                            20, 40, scrlUp);
+                    expect(pointerEvent.getCall(2)).to.have.been.calledWith(client._sock,
+                                                                            20, 40, 0x0);
+                    expect(pointerEvent.getCall(3)).to.have.been.calledWith(client._sock,
+                                                                            20, 40, scrlRight);
+                    expect(pointerEvent.getCall(4)).to.have.been.calledWith(client._sock,
+                                                                            20, 40, 0x0);
+                });
+
+                it('should handle multiple small gesture twodrag events', function () {
+                    let bmask = 0x8; // Button mask for scroll up
+
+                    gestureStart('twodrag', 20, 40, 0, 0);
+
+                    expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                     20, 40, 0x0);
+
+                    pointerEvent.resetHistory();
+
+                    gestureMove('twodrag', 20, 40, 0, 10);
+                    clock.tick(50);
+
+                    expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                     20, 40, 0x0);
+
+                    pointerEvent.resetHistory();
+
+                    gestureMove('twodrag', 20, 40, 0, 20);
+                    clock.tick(50);
+
+                    expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                     20, 40, 0x0);
+
+                    pointerEvent.resetHistory();
+
+                    gestureMove('twodrag', 20, 40, 0, 60);
+
+                    expect(pointerEvent).to.have.been.calledThrice;
+                    expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+                    expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                            20, 40, bmask);
+                    expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+                });
+
+                it('should handle large gesture twodrag events', function () {
+                    let bmask = 0x8; // Button mask for scroll up
+
+                    gestureStart('twodrag', 30, 50, 0, 0);
+
+                    expect(pointerEvent).
+                        to.have.been.calledOnceWith(client._sock, 30, 50, 0x0);
+
+                    pointerEvent.resetHistory();
+
+                    gestureMove('twodrag', 30, 50, 0, 200);
+
+                    expect(pointerEvent).to.have.callCount(7);
+                    expect(pointerEvent.getCall(0)).to.have.been.calledWith(client._sock,
+                                                                            30, 50, 0x0);
+                    expect(pointerEvent.getCall(1)).to.have.been.calledWith(client._sock,
+                                                                            30, 50, bmask);
+                    expect(pointerEvent.getCall(2)).to.have.been.calledWith(client._sock,
+                                                                            30, 50, 0x0);
+                    expect(pointerEvent.getCall(3)).to.have.been.calledWith(client._sock,
+                                                                            30, 50, bmask);
+                    expect(pointerEvent.getCall(4)).to.have.been.calledWith(client._sock,
+                                                                            30, 50, 0x0);
+                    expect(pointerEvent.getCall(5)).to.have.been.calledWith(client._sock,
+                                                                            30, 50, bmask);
+                    expect(pointerEvent.getCall(6)).to.have.been.calledWith(client._sock,
+                                                                            30, 50, 0x0);
+                });
+            });
+
+            describe('Gesture pinch', function () {
+                it('should handle gesture pinch in events', function () {
+                    let keysym = KeyTable.XK_Control_L;
+                    let bmask = 0x10; // Button mask for scroll down
+
+                    gestureStart('pinch', 20, 40, 90, 90);
+
+                    expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                     20, 40, 0x0);
+                    expect(keyEvent).to.not.have.been.called;
+
+                    pointerEvent.resetHistory();
+
+                    gestureMove('pinch', 20, 40, 30, 30);
+
+                    expect(pointerEvent).to.have.been.calledThrice;
+                    expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+                    expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                            20, 40, bmask);
+                    expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+
+                    expect(keyEvent).to.have.been.calledTwice;
+                    expect(keyEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                       keysym, 1);
+                    expect(keyEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                        keysym, 0);
+
+                    expect(keyEvent.firstCall).to.have.been.calledBefore(pointerEvent.secondCall);
+                    expect(keyEvent.lastCall).to.have.been.calledAfter(pointerEvent.lastCall);
+
+                    pointerEvent.resetHistory();
+                    keyEvent.resetHistory();
+
+                    gestureEnd('pinch', 20, 40);
+
+                    expect(pointerEvent).to.not.have.been.called;
+                    expect(keyEvent).to.not.have.been.called;
+                });
+
+                it('should handle gesture pinch out events', function () {
+                    let keysym = KeyTable.XK_Control_L;
+                    let bmask = 0x8; // Button mask for scroll up
+
+                    gestureStart('pinch', 10, 20, 10, 20);
+
+                    expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                     10, 20, 0x0);
+                    expect(keyEvent).to.not.have.been.called;
+
+                    pointerEvent.resetHistory();
+
+                    gestureMove('pinch', 10, 20, 70, 80);
+
+                    expect(pointerEvent).to.have.been.calledThrice;
+                    expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                           10, 20, 0x0);
+                    expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                            10, 20, bmask);
+                    expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+                                                                           10, 20, 0x0);
+
+                    expect(keyEvent).to.have.been.calledTwice;
+                    expect(keyEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                       keysym, 1);
+                    expect(keyEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                        keysym, 0);
+
+                    expect(keyEvent.firstCall).to.have.been.calledBefore(pointerEvent.secondCall);
+                    expect(keyEvent.lastCall).to.have.been.calledAfter(pointerEvent.lastCall);
+
+                    pointerEvent.resetHistory();
+                    keyEvent.resetHistory();
+
+                    gestureEnd('pinch', 10, 20);
+
+                    expect(pointerEvent).to.not.have.been.called;
+                    expect(keyEvent).to.not.have.been.called;
+                });
+
+                it('should handle large gesture pinch', function () {
+                    let keysym = KeyTable.XK_Control_L;
+                    let bmask = 0x10; // Button mask for scroll down
+
+                    gestureStart('pinch', 20, 40, 150, 150);
+
+                    expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                     20, 40, 0x0);
+                    expect(keyEvent).to.not.have.been.called;
+
+                    pointerEvent.resetHistory();
+
+                    gestureMove('pinch', 20, 40, 30, 30);
+
+                    expect(pointerEvent).to.have.been.callCount(5);
+                    expect(pointerEvent.getCall(0)).to.have.been.calledWith(client._sock,
+                                                                            20, 40, 0x0);
+                    expect(pointerEvent.getCall(1)).to.have.been.calledWith(client._sock,
+                                                                            20, 40, bmask);
+                    expect(pointerEvent.getCall(2)).to.have.been.calledWith(client._sock,
+                                                                            20, 40, 0x0);
+                    expect(pointerEvent.getCall(3)).to.have.been.calledWith(client._sock,
+                                                                            20, 40, bmask);
+                    expect(pointerEvent.getCall(4)).to.have.been.calledWith(client._sock,
+                                                                            20, 40, 0x0);
+
+                    expect(keyEvent).to.have.been.calledTwice;
+                    expect(keyEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                       keysym, 1);
+                    expect(keyEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                        keysym, 0);
+
+                    expect(keyEvent.firstCall).to.have.been.calledBefore(pointerEvent.secondCall);
+                    expect(keyEvent.lastCall).to.have.been.calledAfter(pointerEvent.lastCall);
+
+                    pointerEvent.resetHistory();
+                    keyEvent.resetHistory();
+
+                    gestureEnd('pinch', 20, 40);
+
+                    expect(pointerEvent).to.not.have.been.called;
+                    expect(keyEvent).to.not.have.been.called;
+                });
+
+                it('should handle multiple small gesture pinch out events', function () {
+                    let keysym = KeyTable.XK_Control_L;
+                    let bmask = 0x8; // Button mask for scroll down
+
+                    gestureStart('pinch', 20, 40, 0, 10);
+
+                    expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                     20, 40, 0x0);
+                    expect(keyEvent).to.not.have.been.called;
+
+                    pointerEvent.resetHistory();
+
+                    gestureMove('pinch', 20, 40, 0, 30);
+                    clock.tick(50);
+
+                    expect(pointerEvent).to.have.been.calledWith(client._sock,
+                                                                 20, 40, 0x0);
+
+                    pointerEvent.resetHistory();
+
+                    gestureMove('pinch', 20, 40, 0, 60);
+                    clock.tick(50);
+
+                    expect(pointerEvent).to.have.been.calledWith(client._sock,
+                                                                 20, 40, 0x0);
+
+                    pointerEvent.resetHistory();
+                    keyEvent.resetHistory();
+
+                    gestureMove('pinch', 20, 40, 0, 90);
+
+                    expect(pointerEvent).to.have.been.calledThrice;
+                    expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+                    expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                            20, 40, bmask);
+                    expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+
+                    expect(keyEvent).to.have.been.calledTwice;
+                    expect(keyEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                       keysym, 1);
+                    expect(keyEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                        keysym, 0);
+
+                    expect(keyEvent.firstCall).to.have.been.calledBefore(pointerEvent.secondCall);
+                    expect(keyEvent.lastCall).to.have.been.calledAfter(pointerEvent.lastCall);
+
+                    pointerEvent.resetHistory();
+                    keyEvent.resetHistory();
+
+                    gestureEnd('pinch', 20, 40);
+
+                    expect(keyEvent).to.not.have.been.called;
+                });
+
+                it('should send correct key control code', function () {
+                    let keysym = KeyTable.XK_Control_L;
+                    let code = 0x1d;
+                    let bmask = 0x10; // Button mask for scroll down
+
+                    client._qemuExtKeyEventSupported = true;
+
+                    gestureStart('pinch', 20, 40, 90, 90);
+
+                    expect(pointerEvent).to.have.been.calledOnceWith(client._sock,
+                                                                     20, 40, 0x0);
+                    expect(qemuKeyEvent).to.not.have.been.called;
+
+                    pointerEvent.resetHistory();
+
+                    gestureMove('pinch', 20, 40, 30, 30);
+
+                    expect(pointerEvent).to.have.been.calledThrice;
+                    expect(pointerEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+                    expect(pointerEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                            20, 40, bmask);
+                    expect(pointerEvent.thirdCall).to.have.been.calledWith(client._sock,
+                                                                           20, 40, 0x0);
+
+                    expect(qemuKeyEvent).to.have.been.calledTwice;
+                    expect(qemuKeyEvent.firstCall).to.have.been.calledWith(client._sock,
+                                                                           keysym,
+                                                                           true,
+                                                                           code);
+                    expect(qemuKeyEvent.secondCall).to.have.been.calledWith(client._sock,
+                                                                            keysym,
+                                                                            false,
+                                                                            code);
+
+                    expect(qemuKeyEvent.firstCall).to.have.been.calledBefore(pointerEvent.secondCall);
+                    expect(qemuKeyEvent.lastCall).to.have.been.calledAfter(pointerEvent.lastCall);
+
+                    pointerEvent.resetHistory();
+                    qemuKeyEvent.resetHistory();
+
+                    gestureEnd('pinch', 20, 40);
+
+                    expect(pointerEvent).to.not.have.been.called;
+                    expect(qemuKeyEvent).to.not.have.been.called;
+                });
             });
         });
 
-        describe('WebSocket event handlers', function () {
+        describe('WebSocket Events', function () {
             // message events
             it('should do nothing if we receive an empty message and have nothing in the queue', function () {
                 client._normalMsg = sinon.spy();