Skip to content

Commit 78695ba

Browse files
author
Greg Darke
committed
Add tests for the wakelock feature.
DO NOT MERGE (this is still a Work in Progress). Add tests to for both the `rfb` side (calling into the new wakelock code), and the new wakelock class (which tracks the desired state and how to get there).
1 parent 677125b commit 78695ba

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed

tests/test.rfb.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import KeyTable from '../core/input/keysym.js';
99
import legacyCrypto from '../core/crypto/crypto.js';
1010

1111
import FakeWebSocket from './fake.websocket.js';
12+
import WakeLockManager from '../core/util/wakelock.js';
1213

1314
function push8(arr, num) {
1415
"use strict";
@@ -5148,6 +5149,102 @@ describe('Remote Frame Buffer protocol client', function () {
51485149
expect(RFB.messages.clientEncodings.getCall(0).args[1]).to.include(encodings.pseudoEncodingCompressLevel0 + newCompression);
51495150
});
51505151
});
5152+
5153+
describe('wakelock setting', function () {
5154+
let client;
5155+
5156+
beforeEach(function () {
5157+
sinon.spy(WakeLockManager.prototype, "acquire");
5158+
sinon.spy(WakeLockManager.prototype, "release");
5159+
5160+
client = makeRFB();
5161+
});
5162+
5163+
afterEach(function () {
5164+
WakeLockManager.prototype.acquire.restore();
5165+
WakeLockManager.prototype.release.restore();
5166+
});
5167+
5168+
it('should acquire wakelock when connected', function() {
5169+
expect(WakeLockManager.prototype.acquire).to.not.have.been.called;
5170+
5171+
client.requestLocalWakelock = true;
5172+
expect(WakeLockManager.prototype.acquire).to.have.been.calledOnce;
5173+
});
5174+
5175+
it('should acquire wakelock after connection', function() {
5176+
expect(WakeLockManager.prototype.acquire).to.not.have.been.called;
5177+
client._rfbConnectionState = 'connecting';
5178+
5179+
client.requestLocalWakelock = true;
5180+
expect(WakeLockManager.prototype.acquire).to.not.have.been.called;
5181+
5182+
client._updateConnectionState('connected');
5183+
expect(WakeLockManager.prototype.acquire).to.have.been.calledOnce;
5184+
});
5185+
5186+
it('should release wakelock when disabled', function() {
5187+
client.requestLocalWakelock = true;
5188+
expect(WakeLockManager.prototype.acquire).to.have.been.calledOnce;
5189+
expect(WakeLockManager.prototype.release).to.not.have.been.called;
5190+
5191+
client.requestLocalWakelock = false;
5192+
expect(WakeLockManager.prototype.release).to.have.been.calledOnce;
5193+
});
5194+
5195+
it('should release wakelock when disconnected', function() {
5196+
client.requestLocalWakelock = true;
5197+
expect(WakeLockManager.prototype.acquire).to.have.been.calledOnce;
5198+
expect(WakeLockManager.prototype.release).to.not.have.been.called;
5199+
5200+
client.disconnect();
5201+
expect(WakeLockManager.prototype.release).to.have.been.calledOnce;
5202+
});
5203+
5204+
it('should behave sensibly with non-boolean values', function() {
5205+
// Client starts with requestLocakWakelock = false, setting it to
5206+
// the same value should have no effect.
5207+
client.requestLocalWakelock = false;
5208+
expect(client.requestLocalWakelock).to.be.false;
5209+
expect(WakeLockManager.prototype.acquire).to.not.have.been.called;
5210+
expect(WakeLockManager.prototype.release).to.not.have.been.called;
5211+
5212+
// Setting it to something else falsely should have no effect.
5213+
client.requestLocalWakelock = null;
5214+
expect(client.requestLocalWakelock).to.be.false;
5215+
expect(WakeLockManager.prototype.acquire).to.not.have.been.called;
5216+
expect(WakeLockManager.prototype.release).to.not.have.been.called;
5217+
5218+
// Setting it to something else falsely should have no effect.
5219+
client.requestLocalWakelock = undefined;
5220+
expect(client.requestLocalWakelock).to.be.false;
5221+
expect(WakeLockManager.prototype.acquire).to.not.have.been.called;
5222+
expect(WakeLockManager.prototype.release).to.not.have.been.called;
5223+
5224+
// Switching to something true should trigger a single call to
5225+
// acquire.
5226+
client.requestLocalWakelock = true;
5227+
expect(client.requestLocalWakelock).to.be.true;
5228+
expect(WakeLockManager.prototype.acquire).to.have.been.calledOnce;
5229+
expect(WakeLockManager.prototype.release).to.not.have.been.called;
5230+
5231+
WakeLockManager.prototype.acquire.resetHistory();
5232+
5233+
// Switching to something else trueish should have no effect.
5234+
client.requestLocalWakelock = "some-value";
5235+
expect(client.requestLocalWakelock).to.be.true;
5236+
expect(WakeLockManager.prototype.acquire).to.not.have.been.called;
5237+
expect(WakeLockManager.prototype.release).to.not.have.been.called;
5238+
5239+
// Validate that switching from a trueish value to a falseish value
5240+
// works.
5241+
client.requestLocalWakelock = null;
5242+
expect(client.requestLocalWakelock).to.be.false;
5243+
expect(WakeLockManager.prototype.acquire).to.not.have.been.called;
5244+
expect(WakeLockManager.prototype.release).to.have.been.calledOnce;
5245+
});
5246+
5247+
});
51515248
});
51525249

51535250
describe('RFB messages', function () {

tests/test.wakelock.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/* jshint expr: true */
2+
3+
import WakeLockManager from '../core/util/wakelock.js';
4+
5+
class FakeWakeLockSentinal extends EventTarget {
6+
constructor() {
7+
super();
8+
this.released = false;
9+
let {promise: releaseBlocked, resolve} = Promise.withResolvers();
10+
this.releaseBlocked = releaseBlocked;
11+
this._resolveReleaseBlocked = resolve;
12+
this.blockReleasePromise = null;
13+
}
14+
15+
async release() {
16+
if (this.released) {
17+
return;
18+
}
19+
20+
this._resolveReleaseBlocked();
21+
if (this.blockReleasePromise !== null) {
22+
await this.blockReleasePromise;
23+
this.blockReleasePromise = null;
24+
}
25+
this.released = true;
26+
this.dispatchEvent(new Event("release"));
27+
}
28+
}
29+
30+
function waitForStateTransition(wakelockManager, new_state) {
31+
const {promise, resolve} = Promise.withResolvers();
32+
33+
const eventListener = (event) => {
34+
console.warn(`Got state change: ${event.old_state} -> ${event.new_state}`);
35+
if (event.new_state !== new_state) {
36+
return;
37+
}
38+
wakelockManager.removeEventListener("testOnlyStateChange", eventListener);
39+
resolve();
40+
};
41+
wakelockManager.addEventListener("testOnlyStateChange", eventListener);
42+
43+
return promise;
44+
}
45+
46+
describe('WakeLockManager', function () {
47+
"use strict";
48+
49+
let wakelockRequest;
50+
beforeEach(function() {
51+
wakelockRequest = sinon.stub(navigator.wakeLock, 'request');
52+
})
53+
afterEach(function() {
54+
wakelockRequest.restore();
55+
});
56+
57+
it('does a thing', async function() {
58+
let wakeLockSentinal = new FakeWakeLockSentinal();
59+
wakelockRequest.onFirstCall().resolves(wakeLockSentinal);
60+
61+
let wlm = new WakeLockManager();
62+
expect(wakelockRequest).to.not.have.been.called;
63+
64+
let done = waitForStateTransition(wlm, 'acquired');
65+
wlm.acquire();
66+
await done;
67+
expect(wakelockRequest).to.have.been.calledOnce;
68+
expect(wakeLockSentinal.released).to.be.false;
69+
70+
done = waitForStateTransition(wlm, 'released');
71+
wlm.release();
72+
await done;
73+
expect(wakelockRequest).to.have.been.calledOnce;
74+
expect(wakeLockSentinal.released).to.be.true;
75+
});
76+
77+
});

0 commit comments

Comments
 (0)