oficina / meet Public

Clone
git clone https://amadan.net/oficina/meet

Plain git — no account needed to clone.

Download

Download this file

1// room-wake.test.mjs — the screen kept on through a call (0.4.10), gated
2// without a browser.
3//
4// node --test test/browser/room-wake.test.mjs
5//
6// screenWake is lifted out of the shipped room.js by name, as
7// room-quality.test.mjs lifts its builders, and given a fake navigator
8// and document: a Screen Wake Lock that grants, refuses or is missing,
9// and a tab that is hidden and shown.
10import { test } from 'node:test';
11import assert from 'node:assert/strict';
12import { readFileSync } from 'node:fs';
13import { fileURLToPath } from 'node:url';
14
15const roomSrc = readFileSync(fileURLToPath(new URL('../../internal/calls/static/room.js', import.meta.url)), 'utf8');
16
17function lift(name) {
18 const start = roomSrc.indexOf('function ' + name + '(');
19 assert.notEqual(start, -1, name + ' is not in room.js any more — this test is stale, not passing');
20 let depth = 0;
21 for (let i = roomSrc.indexOf('{', start); i < roomSrc.length; i++) {
22 if (roomSrc[i] === '{') depth += 1;
23 else if (roomSrc[i] === '}') {
24 depth -= 1;
25 if (depth === 0) return roomSrc.slice(start, i + 1);
26 }
27 }
28 assert.fail('unbalanced braces reading ' + name);
29}
30const screenWake = new Function(lift('screenWake') + '\nreturn screenWake;')();
31
32// A browser's wake lock: each grant is a sentinel that the page can
33// release, and that the browser releases itself when the tab is hidden.
34function fakeBrowser({ supported = true, refuse = false } = {}) {
35 const listeners = [];
36 const doc = {
37 visibilityState: 'visible',
38 addEventListener(type, fn) { if (type === 'visibilitychange') listeners.push(fn); },
39 };
40 const live = new Set();
41 const log = [];
42 const sentinel = () => {
43 const handlers = [];
44 const s = {
45 released: false,
46 addEventListener(type, fn) { if (type === 'release') handlers.push(fn); },
47 async release() {
48 if (s.released) return;
49 s.released = true;
50 live.delete(s);
51 log.push('release');
52 handlers.forEach((fn) => fn());
53 },
54 };
55 return s;
56 };
57 const nav = supported ? {
58 wakeLock: {
59 async request(kind) {
60 log.push('request ' + kind);
61 if (refuse) throw Object.assign(new Error('refused'), { name: 'NotAllowedError' });
62 const s = sentinel();
63 live.add(s);
64 return s;
65 },
66 },
67 } : {};
68 const setVisible = async (visible) => {
69 doc.visibilityState = visible ? 'visible' : 'hidden';
70 if (!visible) for (const s of [...live]) await s.release();
71 listeners.forEach((fn) => fn());
72 await new Promise((r) => setTimeout(r, 0));
73 };
74 return { nav, doc, live, log, setVisible };
75}
76
77test('held from the start of the call, released on leaving', async () => {
78 const b = fakeBrowser();
79 const wake = screenWake(b.nav, b.doc);
80 assert.equal(wake.held, false);
81 await wake.hold();
82 assert.equal(wake.held, true);
83 assert.deepEqual(b.log, ['request screen']);
84 await wake.hold();
85 assert.equal(b.live.size, 1, 'never two locks');
86 await wake.release();
87 assert.equal(wake.held, false);
88 assert.equal(b.live.size, 0);
89});
90
91test('asked for again when the tab comes back, while still in the call', async () => {
92 const b = fakeBrowser();
93 const wake = screenWake(b.nav, b.doc);
94 await wake.hold();
95 await b.setVisible(false);
96 assert.equal(wake.held, false, 'the browser let it go with the tab hidden');
97 await b.setVisible(true);
98 assert.equal(wake.held, true);
99 assert.equal(b.log.filter((l) => l === 'request screen').length, 2);
100});
101
102test('not asked for again after leaving', async () => {
103 const b = fakeBrowser();
104 const wake = screenWake(b.nav, b.doc);
105 await wake.hold();
106 await wake.release();
107 await b.setVisible(false);
108 await b.setVisible(true);
109 assert.equal(wake.held, false);
110 assert.equal(b.log.filter((l) => l === 'request screen').length, 1);
111});
112
113test('not asked for while the tab is hidden', async () => {
114 const b = fakeBrowser();
115 b.doc.visibilityState = 'hidden';
116 const wake = screenWake(b.nav, b.doc);
117 await wake.hold();
118 assert.deepEqual(b.log, []);
119 await b.setVisible(true);
120 assert.equal(wake.held, true);
121});
122
123test('a grant that arrives after leaving is let go at once', async () => {
124 const b = fakeBrowser();
125 const wake = screenWake(b.nav, b.doc);
126 const asked = wake.hold();
127 wake.release();
128 await asked;
129 assert.equal(wake.held, false);
130 assert.equal(b.live.size, 0);
131});
132
133test('silent where unsupported or refused', async () => {
134 for (const options of [{ supported: false }, { refuse: true }]) {
135 const b = fakeBrowser(options);
136 const wake = screenWake(b.nav, b.doc);
137 await wake.hold();
138 await b.setVisible(false);
139 await b.setVisible(true);
140 assert.equal(wake.held, false);
141 await wake.release();
142 }
143 const none = screenWake(undefined, { visibilityState: 'visible', addEventListener() {} });
144 await none.hold();
145 await none.release();
146});
147
148test('room.js holds it at the start and lets it go on both ways out', () => {
149 assert.match(roomSrc, /const wake = screenWake\(navigator, document\);/);
150 assert.match(roomSrc, /backdropTimer = setInterval\(paintBackdrops, BACKDROP_EVERY\);\n\s*wake\.hold\(\);/);
151 assert.equal(roomSrc.match(/clearInterval\(backdropTimer\);\n\s*wake\.release\(\);/g).length, 2);
152});
153