Skip to content

Commit

Permalink
Combine Caps Lock and Num Lock remote fixup tests with led state deco…
Browse files Browse the repository at this point in the history
…de tests
  • Loading branch information
otthou committed Sep 11, 2023
1 parent df8df55 commit 5087122
Showing 1 changed file with 130 additions and 153 deletions.
283 changes: 130 additions & 153 deletions tests/test.rfb.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,143 +517,6 @@ describe('Remote Frame Buffer Protocol Client', function () {
});
});

describe('Caps Lock and Num Lock remote fixup', function () {
let client;
beforeEach(function () {
client = makeRFB();
sinon.stub(client, 'sendKey');
});

it('should toggle caps lock if remote caps lock is on and local is off', function () {
client._remoteCapsLock = true;
client._handleKeyEvent(0x61, 'KeyA', true, null, false);

expect(client.sendKey).to.have.been.calledThrice;
expect(client.sendKey.firstCall).to.have.been.calledWith(0xFFE5, "CapsLock", true);
expect(client.sendKey.secondCall).to.have.been.calledWith(0xFFE5, "CapsLock", false);
expect(client.sendKey.thirdCall).to.have.been.calledWith(0x61, "KeyA", true);
});

it('should toggle caps lock if remote caps lock is off and local is on', function () {
client._remoteCapsLock = false;
client._handleKeyEvent(0x41, 'KeyA', true, null, true);

expect(client.sendKey).to.have.been.calledThrice;
expect(client.sendKey.firstCall).to.have.been.calledWith(0xFFE5, "CapsLock", true);
expect(client.sendKey.secondCall).to.have.been.calledWith(0xFFE5, "CapsLock", false);
expect(client.sendKey.thirdCall).to.have.been.calledWith(0x41, "KeyA", true);
});

it('should not toggle caps lock if remote caps lock is on and local is on', function () {
client._remoteCapsLock = true;
client._handleKeyEvent(0x41, 'KeyA', true, null, true);

expect(client.sendKey).to.have.been.calledOnce;
expect(client.sendKey.firstCall).to.have.been.calledWith(0x41, "KeyA", true);
});

it('should not toggle caps lock if remote caps lock is off and local is off', function () {
client._remoteCapsLock = false;
client._handleKeyEvent(0x61, 'KeyA', true, null, false);

expect(client.sendKey).to.have.been.calledOnce;
expect(client.sendKey.firstCall).to.have.been.calledWith(0x61, "KeyA", true);
});

it('should not toggle caps lock if the key is caps lock', function () {
client._remoteCapsLock = false;
client._handleKeyEvent(0xFFE5, 'CapsLock', true, null, true);

expect(client.sendKey).to.have.been.calledOnce;
expect(client.sendKey.firstCall).to.have.been.calledWith(0xFFE5, "CapsLock", true);
});

it('should toggle caps lock only once', function () {
client._remoteCapsLock = true;
client._handleKeyEvent(0x61, 'KeyA', true, null, false);
client._handleKeyEvent(0x61, 'KeyA', true, null, false);

expect(client.sendKey).to.have.callCount(4);
expect(client.sendKey.firstCall).to.have.been.calledWith(0xFFE5, "CapsLock", true);
expect(client.sendKey.secondCall).to.have.been.calledWith(0xFFE5, "CapsLock", false);
expect(client.sendKey.thirdCall).to.have.been.calledWith(0x61, "KeyA", true);
expect(client.sendKey.lastCall).to.have.been.calledWith(0x61, "KeyA", true);
});

it('should retain remote caps lock state on capslock key up', function () {
client._remoteCapsLock = true;
client._handleKeyEvent(0xFFE5, 'CapsLock', false, null, true);

expect(client.sendKey).to.have.been.calledOnce;
expect(client.sendKey.firstCall).to.have.been.calledWith(0xFFE5, "CapsLock", false);
expect(client._remoteCapsLock).to.equal(true);
});

it('should toggle num lock if remote num lock is on and local is off', function () {
client._remoteNumLock = true;
client._handleKeyEvent(0xFF9C, 'NumPad1', true, false, null);

expect(client.sendKey).to.have.been.calledThrice;
expect(client.sendKey.firstCall).to.have.been.calledWith(0xFF7F, "NumLock", true);
expect(client.sendKey.secondCall).to.have.been.calledWith(0xFF7F, "NumLock", false);
expect(client.sendKey.thirdCall).to.have.been.calledWith(0xFF9C, "NumPad1", true);
});

it('should toggle num lock if remote num lock is off and local is on', function () {
client._remoteNumLock = false;
client._handleKeyEvent(0xFFB1, 'NumPad1', true, true, null);

expect(client.sendKey).to.have.been.calledThrice;
expect(client.sendKey.firstCall).to.have.been.calledWith(0xFF7F, "NumLock", true);
expect(client.sendKey.secondCall).to.have.been.calledWith(0xFF7F, "NumLock", false);
expect(client.sendKey.thirdCall).to.have.been.calledWith(0xFFB1, "NumPad1", true);
});

it('should not toggle num lock if remote num lock is on and local is on', function () {
client._remoteNumLock = true;
client._handleKeyEvent(0xFFB1, 'NumPad1', true, true, null);

expect(client.sendKey).to.have.been.calledOnce;
expect(client.sendKey.firstCall).to.have.been.calledWith(0xFFB1, "NumPad1", true);
});

it('should not toggle num lock if remote num lock is off and local is off', function () {
client._remoteNumLock = false;
client._handleKeyEvent(0xFF9C, 'NumPad1', true, false, null);

expect(client.sendKey).to.have.been.calledOnce;
expect(client.sendKey.firstCall).to.have.been.calledWith(0xFF9C, "NumPad1", true);
});

it('should not toggle num lock if the key is num lock', function () {
client._remoteNumLock = false;
client._handleKeyEvent(0xFF7F, 'NumLock', true, true, null);

expect(client.sendKey).to.have.been.calledOnce;
expect(client.sendKey.firstCall).to.have.been.calledWith(0xFF7F, "NumLock", true);
});

it('should not toggle num lock if local state is unknown', function () {
client._remoteNumLock = true;
client._handleKeyEvent(0xFFB1, 'NumPad1', true, null, null);

expect(client.sendKey).to.have.been.calledOnce;
expect(client.sendKey.firstCall).to.have.been.calledWith(0xFFB1, "NumPad1", true);
});

it('should toggle num lock only once', function () {
client._remoteNumLock = true;
client._handleKeyEvent(0xFF9C, 'NumPad1', true, false, null);
client._handleKeyEvent(0xFF9C, 'NumPad1', true, false, null);

expect(client.sendKey).to.have.callCount(4);
expect(client.sendKey.firstCall).to.have.been.calledWith(0xFF7F, "NumLock", true);
expect(client.sendKey.secondCall).to.have.been.calledWith(0xFF7F, "NumLock", false);
expect(client.sendKey.thirdCall).to.have.been.calledWith(0xFF9C, "NumPad1", true);
expect(client.sendKey.lastCall).to.have.been.calledWith(0xFF9C, "NumPad1", true);
});
});

describe('Clipping', function () {
let client;

Expand Down Expand Up @@ -3115,34 +2978,148 @@ describe('Remote Frame Buffer Protocol Client', function () {
expect(spy).to.have.been.calledOnce;
expect(spy.args[0][0].detail.name).to.equal('som€ nam€');
});
});

it('should handle the QEMU LED State pseudo-encoding', function () {
describe('Caps Lock and Num Lock remote fixup', function () {
function sendLedStateUpdate(state) {
let data = [];
push8(data, 0b110);
push8(data, state);
sendFbuMsg([{ x: 0, y: 0, width: 0, height: 0, encoding: -261 }], [data], client);
}

const spy = sinon.spy();
client.addEventListener("ledstatus", spy);
let client;
beforeEach(function () {
client = makeRFB();
sinon.stub(client, 'sendKey');
});

sendFbuMsg([{ x: 0, y: 0, width: 0, height: 0, encoding: -261 }], [data], client);
it('should toggle caps lock if remote caps lock is on and local is off', function () {
sendLedStateUpdate(0b100);
client._handleKeyEvent(0x61, 'KeyA', true, null, false);

expect(client.sendKey).to.have.been.calledThrice;
expect(client.sendKey.firstCall).to.have.been.calledWith(0xFFE5, "CapsLock", true);
expect(client.sendKey.secondCall).to.have.been.calledWith(0xFFE5, "CapsLock", false);
expect(client.sendKey.thirdCall).to.have.been.calledWith(0x61, "KeyA", true);
});

it('should toggle caps lock if remote caps lock is off and local is on', function () {
sendLedStateUpdate(0b011);
client._handleKeyEvent(0x41, 'KeyA', true, null, true);

expect(client.sendKey).to.have.been.calledThrice;
expect(client.sendKey.firstCall).to.have.been.calledWith(0xFFE5, "CapsLock", true);
expect(client.sendKey.secondCall).to.have.been.calledWith(0xFFE5, "CapsLock", false);
expect(client.sendKey.thirdCall).to.have.been.calledWith(0x41, "KeyA", true);
});

it('should not toggle caps lock if remote caps lock is on and local is on', function () {
sendLedStateUpdate(0b100);
client._handleKeyEvent(0x41, 'KeyA', true, null, true);

expect(client.sendKey).to.have.been.calledOnce;
expect(client.sendKey.firstCall).to.have.been.calledWith(0x41, "KeyA", true);
});

it('should not toggle caps lock if remote caps lock is off and local is off', function () {
sendLedStateUpdate(0b011);
client._handleKeyEvent(0x61, 'KeyA', true, null, false);

expect(client.sendKey).to.have.been.calledOnce;
expect(client.sendKey.firstCall).to.have.been.calledWith(0x61, "KeyA", true);
});

it('should not toggle caps lock if the key is caps lock', function () {
sendLedStateUpdate(0b011);
client._handleKeyEvent(0xFFE5, 'CapsLock', true, null, true);

expect(client.sendKey).to.have.been.calledOnce;
expect(client.sendKey.firstCall).to.have.been.calledWith(0xFFE5, "CapsLock", true);
});

it('should toggle caps lock only once', function () {
sendLedStateUpdate(0b100);
client._handleKeyEvent(0x61, 'KeyA', true, null, false);
client._handleKeyEvent(0x61, 'KeyA', true, null, false);

expect(client.sendKey).to.have.callCount(4);
expect(client.sendKey.firstCall).to.have.been.calledWith(0xFFE5, "CapsLock", true);
expect(client.sendKey.secondCall).to.have.been.calledWith(0xFFE5, "CapsLock", false);
expect(client.sendKey.thirdCall).to.have.been.calledWith(0x61, "KeyA", true);
expect(client.sendKey.lastCall).to.have.been.calledWith(0x61, "KeyA", true);
});

it('should retain remote caps lock state on capslock key up', function () {
sendLedStateUpdate(0b100);
client._handleKeyEvent(0xFFE5, 'CapsLock', false, null, true);

expect(client.sendKey).to.have.been.calledOnce;
expect(client.sendKey.firstCall).to.have.been.calledWith(0xFFE5, "CapsLock", false);
expect(client._remoteCapsLock).to.equal(true);
expect(client._remoteNumLock).to.equal(true);
});

data = [];
push8(data, 0b011);
it('should toggle num lock if remote num lock is on and local is off', function () {
sendLedStateUpdate(0b010);
client._handleKeyEvent(0xFF9C, 'NumPad1', true, false, null);

sendFbuMsg([{ x: 0, y: 0, width: 0, height: 0, encoding: -261 }], [data], client);
expect(client.sendKey).to.have.been.calledThrice;
expect(client.sendKey.firstCall).to.have.been.calledWith(0xFF7F, "NumLock", true);
expect(client.sendKey.secondCall).to.have.been.calledWith(0xFF7F, "NumLock", false);
expect(client.sendKey.thirdCall).to.have.been.calledWith(0xFF9C, "NumPad1", true);
});

expect(client._remoteCapsLock).to.equal(false);
expect(client._remoteNumLock).to.equal(true);
it('should toggle num lock if remote num lock is off and local is on', function () {
sendLedStateUpdate(0b101);
client._handleKeyEvent(0xFFB1, 'NumPad1', true, true, null);

data = [];
push8(data, 0b000);
expect(client.sendKey).to.have.been.calledThrice;
expect(client.sendKey.firstCall).to.have.been.calledWith(0xFF7F, "NumLock", true);
expect(client.sendKey.secondCall).to.have.been.calledWith(0xFF7F, "NumLock", false);
expect(client.sendKey.thirdCall).to.have.been.calledWith(0xFFB1, "NumPad1", true);
});

sendFbuMsg([{ x: 0, y: 0, width: 0, height: 0, encoding: -261 }], [data], client);
it('should not toggle num lock if remote num lock is on and local is on', function () {
sendLedStateUpdate(0b010);
client._handleKeyEvent(0xFFB1, 'NumPad1', true, true, null);

expect(client.sendKey).to.have.been.calledOnce;
expect(client.sendKey.firstCall).to.have.been.calledWith(0xFFB1, "NumPad1", true);
});

it('should not toggle num lock if remote num lock is off and local is off', function () {
sendLedStateUpdate(0b101);
client._handleKeyEvent(0xFF9C, 'NumPad1', true, false, null);

expect(client.sendKey).to.have.been.calledOnce;
expect(client.sendKey.firstCall).to.have.been.calledWith(0xFF9C, "NumPad1", true);
});

it('should not toggle num lock if the key is num lock', function () {
sendLedStateUpdate(0b101);
client._handleKeyEvent(0xFF7F, 'NumLock', true, true, null);

expect(client.sendKey).to.have.been.calledOnce;
expect(client.sendKey.firstCall).to.have.been.calledWith(0xFF7F, "NumLock", true);
});

it('should not toggle num lock if local state is unknown', function () {
sendLedStateUpdate(0b010);
client._handleKeyEvent(0xFFB1, 'NumPad1', true, null, null);

expect(client.sendKey).to.have.been.calledOnce;
expect(client.sendKey.firstCall).to.have.been.calledWith(0xFFB1, "NumPad1", true);
});

it('should toggle num lock only once', function () {
sendLedStateUpdate(0b010);
client._handleKeyEvent(0xFF9C, 'NumPad1', true, false, null);
client._handleKeyEvent(0xFF9C, 'NumPad1', true, false, null);

expect(client._remoteCapsLock).to.equal(false);
expect(client._remoteNumLock).to.equal(false);
expect(client.sendKey).to.have.callCount(4);
expect(client.sendKey.firstCall).to.have.been.calledWith(0xFF7F, "NumLock", true);
expect(client.sendKey.secondCall).to.have.been.calledWith(0xFF7F, "NumLock", false);
expect(client.sendKey.thirdCall).to.have.been.calledWith(0xFF9C, "NumPad1", true);
expect(client.sendKey.lastCall).to.have.been.calledWith(0xFF9C, "NumPad1", true);
});
});
});
Expand Down

0 comments on commit 5087122

Please sign in to comment.