Forced cleanup of RFB objects in tests

We need to make sure RFB objects are properly disposed or they
might have event listeners and other stuff hanging around that can
influence subsequent tests.
This commit is contained in:
Pierre Ossman 2017-11-29 16:37:35 +01:00
parent 2efa8a5a7c
commit bb25d3d6c5
1 changed files with 52 additions and 33 deletions

View File

@ -55,12 +55,30 @@ describe('Remote Frame Buffer Protocol Client', function() {
this.clock.restore(); this.clock.restore();
}); });
var rfbs;
beforeEach(function () {
// Track all created RFB objects
rfbs = [];
});
afterEach(function () {
// Make sure every created RFB object is properly cleaned up
// or they might affect subsequent tests
rfbs.forEach(function (rfb) {
rfb.disconnect();
expect(rfb._disconnect).to.have.been.called;
});
rfbs = [];
});
function make_rfb (url, options) { function make_rfb (url, options) {
url = url || 'wss://host:8675'; url = url || 'wss://host:8675';
var rfb = new RFB(document.createElement('canvas'), url, options); var rfb = new RFB(document.createElement('canvas'), url, options);
clock.tick(); clock.tick();
rfb._sock._websocket._open(); rfb._sock._websocket._open();
rfb._rfb_connection_state = 'connected'; rfb._rfb_connection_state = 'connected';
sinon.spy(rfb, "_disconnect");
rfbs.push(rfb);
return rfb; return rfb;
} }
@ -161,7 +179,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should not send the keys if we are not in a normal state', function () { it('should not send the keys if we are not in a normal state', function () {
sinon.spy(client._sock, 'flush'); sinon.spy(client._sock, 'flush');
client._rfb_connection_state = "broken"; client._rfb_connection_state = "connecting";
client.sendCtrlAltDel(); client.sendCtrlAltDel();
expect(client._sock.flush).to.not.have.been.called; expect(client._sock.flush).to.not.have.been.called;
}); });
@ -192,7 +210,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should not send the key if we are not in a normal state', function () { it('should not send the key if we are not in a normal state', function () {
sinon.spy(client._sock, 'flush'); sinon.spy(client._sock, 'flush');
client._rfb_connection_state = "broken"; client._rfb_connection_state = "connecting";
client.sendKey(123, 'Key123'); client.sendKey(123, 'Key123');
expect(client._sock.flush).to.not.have.been.called; expect(client._sock.flush).to.not.have.been.called;
}); });
@ -231,7 +249,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should not send the text if we are not in a normal state', function () { it('should not send the text if we are not in a normal state', function () {
sinon.spy(client._sock, 'flush'); sinon.spy(client._sock, 'flush');
client._rfb_connection_state = "broken"; client._rfb_connection_state = "connecting";
client.clipboardPasteFrom('abc'); client.clipboardPasteFrom('abc');
expect(client._sock.flush).to.not.have.been.called; expect(client._sock.flush).to.not.have.been.called;
}); });
@ -269,7 +287,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should not send the request if we are not in a normal state', function () { it('should not send the request if we are not in a normal state', function () {
sinon.spy(client._sock, 'flush'); sinon.spy(client._sock, 'flush');
client._rfb_connection_state = "broken"; client._rfb_connection_state = "connecting";
client.requestDesktopSize(1,2); client.requestDesktopSize(1,2);
expect(client._sock.flush).to.not.have.been.called; expect(client._sock.flush).to.not.have.been.called;
}); });
@ -321,28 +339,32 @@ describe('Remote Frame Buffer Protocol Client', function() {
}); });
it('should set the rfb_connection_state', function () { it('should set the rfb_connection_state', function () {
client._rfb_connection_state = 'disconnecting'; client._rfb_connection_state = 'connecting';
client._updateConnectionState('disconnected'); client._updateConnectionState('connected');
expect(client._rfb_connection_state).to.equal('disconnected'); expect(client._rfb_connection_state).to.equal('connected');
}); });
it('should not change the state when we are disconnected', function () { it('should not change the state when we are disconnected', function () {
client._rfb_connection_state = 'disconnected'; client.disconnect();
expect(client._rfb_connection_state).to.equal('disconnected');
client._updateConnectionState('connecting'); client._updateConnectionState('connecting');
expect(client._rfb_connection_state).to.not.equal('connecting'); expect(client._rfb_connection_state).to.not.equal('connecting');
}); });
it('should ignore state changes to the same state', function () { it('should ignore state changes to the same state', function () {
var connectSpy = sinon.spy(); var connectSpy = sinon.spy();
var disconnectSpy = sinon.spy();
client.addEventListener("connect", connectSpy); client.addEventListener("connect", connectSpy);
client.addEventListener("disconnect", disconnectSpy);
client._rfb_connection_state = 'connected'; expect(client._rfb_connection_state).to.equal('connected');
client._updateConnectionState('connected'); client._updateConnectionState('connected');
expect(connectSpy).to.not.have.been.called; expect(connectSpy).to.not.have.been.called;
client._rfb_connection_state = 'disconnected'; client.disconnect();
var disconnectSpy = sinon.spy();
client.addEventListener("disconnect", disconnectSpy);
expect(client._rfb_connection_state).to.equal('disconnected');
client._updateConnectionState('disconnected'); client._updateConnectionState('disconnected');
expect(disconnectSpy).to.not.have.been.called; expect(disconnectSpy).to.not.have.been.called;
}); });
@ -350,7 +372,6 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should ignore illegal state changes', function () { it('should ignore illegal state changes', function () {
var spy = sinon.spy(); var spy = sinon.spy();
client.addEventListener("disconnect", spy); client.addEventListener("disconnect", spy);
client._rfb_connection_state = 'connected';
client._updateConnectionState('disconnected'); client._updateConnectionState('disconnected');
expect(client._rfb_connection_state).to.not.equal('disconnected'); expect(client._rfb_connection_state).to.not.equal('disconnected');
expect(spy).to.not.have.been.called; expect(spy).to.not.have.been.called;
@ -460,11 +481,21 @@ describe('Remote Frame Buffer Protocol Client', function() {
client._updateConnectionState('disconnecting'); client._updateConnectionState('disconnecting');
expect(client._sock.close).to.have.been.calledOnce; expect(client._sock.close).to.have.been.calledOnce;
}); });
it('should not result in a disconnect event', function () {
var spy = sinon.spy();
client.addEventListener("disconnect", spy);
client._sock._websocket.close = function () {}; // explicitly don't call onclose
client._updateConnectionState('disconnecting');
expect(spy).to.not.have.been.called;
});
}); });
describe('disconnected', function () { describe('disconnected', function () {
var client; var client;
beforeEach(function () { client = make_rfb(); }); beforeEach(function () {
client = new RFB(document.createElement('canvas'), 'ws://HOST:8675/PATH');
});
it('should result in a disconnect event if state becomes "disconnected"', function () { it('should result in a disconnect event if state becomes "disconnected"', function () {
var spy = sinon.spy(); var spy = sinon.spy();
@ -475,14 +506,6 @@ describe('Remote Frame Buffer Protocol Client', function() {
expect(spy.args[0][0].detail.clean).to.be.true; expect(spy.args[0][0].detail.clean).to.be.true;
}); });
it('should not result in a disconnect event if the state is not "disconnected"', function () {
var spy = sinon.spy();
client.addEventListener("disconnect", spy);
client._sock._websocket.close = function () {}; // explicitly don't call onclose
client._updateConnectionState('disconnecting');
expect(spy).to.not.have.been.called;
});
it('should result in a disconnect event without msg when no reason given', function () { it('should result in a disconnect event without msg when no reason given', function () {
var spy = sinon.spy(); var spy = sinon.spy();
client.addEventListener("disconnect", spy); client.addEventListener("disconnect", spy);
@ -892,7 +915,6 @@ describe('Remote Frame Buffer Protocol Client', function() {
}); });
it('should fall through to ServerInitialisation on a response code of 0', function () { it('should fall through to ServerInitialisation on a response code of 0', function () {
client._updateConnectionState = sinon.spy();
client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0])); client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0]));
expect(client._rfb_init_state).to.equal('ServerInitialisation'); expect(client._rfb_init_state).to.equal('ServerInitialisation');
}); });
@ -1898,14 +1920,18 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should fail if we are not currently ready to connect and we get an "open" event', function () { it('should fail if we are not currently ready to connect and we get an "open" event', function () {
sinon.spy(client, "_fail"); sinon.spy(client, "_fail");
client._rfb_connection_state = 'some_other_state'; client._rfb_connection_state = 'connected';
client._sock._websocket._open(); client._sock._websocket._open();
expect(client._fail).to.have.been.calledOnce; expect(client._fail).to.have.been.calledOnce;
}); });
// close events // close events
it('should transition to "disconnected" from "disconnecting" on a close event', function () { it('should transition to "disconnected" from "disconnecting" on a close event', function () {
client._rfb_connection_state = 'disconnecting'; var real = client._sock._websocket.close;
client._sock._websocket.close = function () {};
client.disconnect();
expect(client._rfb_connection_state).to.equal('disconnecting');
client._sock._websocket.close = real;
client._sock._websocket.close(); client._sock._websocket.close();
expect(client._rfb_connection_state).to.equal('disconnected'); expect(client._rfb_connection_state).to.equal('disconnected');
}); });
@ -1917,16 +1943,9 @@ describe('Remote Frame Buffer Protocol Client', function() {
expect(client._fail).to.have.been.calledOnce; expect(client._fail).to.have.been.calledOnce;
}); });
it('should fail if we get a close event while disconnected', function () {
sinon.spy(client, "_fail");
client._rfb_connection_state = 'disconnected';
client._sock._websocket.close();
expect(client._fail).to.have.been.calledOnce;
});
it('should unregister close event handler', function () { it('should unregister close event handler', function () {
sinon.spy(client._sock, 'off'); sinon.spy(client._sock, 'off');
client._rfb_connection_state = 'disconnecting'; client.disconnect();
client._sock._websocket.close(); client._sock._websocket.close();
expect(client._sock.off).to.have.been.calledWith('close'); expect(client._sock.off).to.have.been.calledWith('close');
}); });