]> git.proxmox.com Git - mirror_novnc.git/blobdiff - tests/test.rfb.js
Stop explicitly testing connection states
[mirror_novnc.git] / tests / test.rfb.js
index 09b6d1cc6a512d8f0e412583ff9b520caa850832..21f2f7a4b7bed4c695a11372dfc6ef07e5f402f6 100644 (file)
@@ -140,38 +140,76 @@ 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;
+            beforeEach(function () {
+                open = sinon.spy(Websock.prototype, 'open');
+            });
+            afterEach(function () {
+                open.restore();
+            });
+
+            it('should not connect from constructor', function () {
+                new RFB(document.createElement('div'), 'wss://host:8675');
+                expect(open).to.not.have.been.called;
+                this.clock.tick(); // Flush the pending connection
             });
 
             it('should actually connect to the websocket', function () {
-                const client = new RFB(document.createElement('div'), 'ws://HOST:8675/PATH');
-                sinon.spy(client._sock, 'open');
+                new RFB(document.createElement('div'), 'ws://HOST:8675/PATH');
                 this.clock.tick();
-                expect(client._sock.open).to.have.been.calledOnce;
-                expect(client._sock.open).to.have.been.calledWith('ws://HOST:8675/PATH');
+                expect(open).to.have.been.calledOnceWithExactly('ws://HOST:8675/PATH', []);
             });
         });
 
         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 () {
@@ -790,62 +828,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 () {
@@ -886,106 +868,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 () {
@@ -1706,9 +1588,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;
             });
         });
     });