Skip to content

Commit

Permalink
Modify unit tests to work with ResizeObserver
Browse files Browse the repository at this point in the history
  • Loading branch information
samhed committed Sep 3, 2021
1 parent a9c2ff3 commit 375f36c
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions tests/test.rfb.js
Expand Up @@ -71,6 +71,18 @@ function deflateWithSize(data) {
describe('Remote Frame Buffer Protocol Client', function () {
let clock;
let raf;
let fakeResizeObserver = null;
const realObserver = window.ResizeObserver;

class FakeResizeObserver {
constructor(handler) {
this.fire = handler;
fakeResizeObserver = this;
}
disconnect() {}
observe(target, options) {}
unobserve(target) {}
}

before(FakeWebSocket.replace);
after(FakeWebSocket.restore);
Expand All @@ -80,6 +92,9 @@ describe('Remote Frame Buffer Protocol Client', function () {
// sinon doesn't support this yet
raf = window.requestAnimationFrame;
window.requestAnimationFrame = setTimeout;
// We must do this in a 'before' since it needs to be set before
// the RFB constructor, which runs in beforeEach further down
window.ResizeObserver = FakeResizeObserver;
// Use a single set of buffers instead of reallocating to
// speed up tests
const sock = new Websock();
Expand All @@ -100,6 +115,7 @@ describe('Remote Frame Buffer Protocol Client', function () {
delete Websock.prototype.toString;
this.clock.restore();
window.requestAnimationFrame = raf;
window.ResizeObserver = realObserver;
});

let container;
Expand Down Expand Up @@ -470,6 +486,7 @@ describe('Remote Frame Buffer Protocol Client', function () {

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

beforeEach(function () {
client = makeRFB();
container.style.width = '70px';
Expand All @@ -495,8 +512,7 @@ describe('Remote Frame Buffer Protocol Client', function () {

container.style.width = '40px';
container.style.height = '50px';
const event = new UIEvent('resize');
window.dispatchEvent(event);
fakeResizeObserver.fire();
clock.tick();

expect(client._display.viewportChangeSize).to.have.been.calledOnce;
Expand Down Expand Up @@ -692,8 +708,7 @@ describe('Remote Frame Buffer Protocol Client', function () {

container.style.width = '40px';
container.style.height = '50px';
const event = new UIEvent('resize');
window.dispatchEvent(event);
fakeResizeObserver.fire();
clock.tick();

expect(client._display.autoscale).to.have.been.calledOnce;
Expand Down Expand Up @@ -782,8 +797,7 @@ describe('Remote Frame Buffer Protocol Client', function () {
it('should request a resize when the container resizes', function () {
container.style.width = '40px';
container.style.height = '50px';
const event = new UIEvent('resize');
window.dispatchEvent(event);
fakeResizeObserver.fire();
clock.tick(1000);

expect(RFB.messages.setDesktopSize).to.have.been.calledOnce;
Expand All @@ -793,16 +807,14 @@ describe('Remote Frame Buffer Protocol Client', function () {
it('should not resize until the container size is stable', function () {
container.style.width = '20px';
container.style.height = '30px';
const event1 = new UIEvent('resize');
window.dispatchEvent(event1);
fakeResizeObserver.fire();
clock.tick(400);

expect(RFB.messages.setDesktopSize).to.not.have.been.called;

container.style.width = '40px';
container.style.height = '50px';
const event2 = new UIEvent('resize');
window.dispatchEvent(event2);
fakeResizeObserver.fire();
clock.tick(400);

expect(RFB.messages.setDesktopSize).to.not.have.been.called;
Expand Down

0 comments on commit 375f36c

Please sign in to comment.