Files
corescope-rx/test/wakelock.test.mjs
efiten f280dec686 android: keep the WebView running with the screen off
Three v1.24.3 field logs of 2026-11-11 (three phones) show the same thing:
about 60 s after "app hidden" every upload, RF sample, region ask and uplink
ping stops until the screen is back on, for 7 to 25 minutes, while receptions
and discover go on. The catch-up of 149e39b keeps monitorTick alive but not
the jobs that wait on a timer, on IndexedDB or on the broker.

What changes:
- AlwaysVisibleWebView reports its window as visible at all times, so
  Chromium never treats the page as hidden. An app copy of
  capacitor_bridge_layout_main.xml puts it in place of CapacitorWebView.
- MainActivity sends appHidden / appVisible on onStop / onStart, because
  visibilitychange no longer fires. app.js logs both, wakelock.js re-acquires
  the screen lock on appVisible and leaves one that is still held alone.
- Service worker cache v64.

Verified: npm test, 485 of 485 pass. assembleDebug builds, and aapt2 shows
eu.on8ar.coredriverx.AlwaysVisibleWebView in the APK's layout.

Not verified: that this stops the freeze. No phone has run this build yet.
Battery use with the screen off is not measured. No version bump and no
release notes: those follow once a screen-off drive confirms it.
The web app (Chrome, Bluefy) is not changed by this and still freezes.
2026-11-11 20:09:21 +01:00

121 lines
4.6 KiB
JavaScript

// Wake-lock selection logic: native Screen Wake Lock where present, hidden looping
// video fallback otherwise, re-acquire on visibility. Verified with fake
// navigator/document (no real browser). Run: node --test
import { test } from 'node:test';
import assert from 'node:assert';
import { createWakeLock } from '../src/wakelock.js';
function fakeDoc() {
const listeners = {};
const el = (tag) => ({
tag, attrs: {}, style: {}, children: [], muted: false, playCount: 0, paused: false,
setAttribute(k, v) { this.attrs[k] = v; },
appendChild(c) { this.children.push(c); },
play() { this.playCount++; this.paused = false; return Promise.resolve(); },
pause() { this.paused = true; },
});
return {
visibilityState: 'visible',
body: { appended: [], appendChild(e) { this.appended.push(e); } },
createElement: el,
addEventListener(type, fn) { (listeners[type] ||= []).push(fn); },
removeEventListener(type, fn) { if (listeners[type]) listeners[type] = listeners[type].filter((f) => f !== fn); },
fire(type) { (listeners[type] || []).forEach((f) => f()); },
};
}
function nativeNav() {
const calls = [];
return { calls, wakeLock: { async request(type) { calls.push(type); return { async release() {} }; } } };
}
test('native present: enable requests a screen wake lock, no fallback video', async () => {
const nav = nativeNav();
const doc = fakeDoc();
await createWakeLock({ navigator: nav, document: doc }).enable();
assert.deepStrictEqual(nav.calls, ['screen']);
assert.strictEqual(doc.body.appended.length, 0);
});
test('native absent: enable creates and plays a fallback video', async () => {
const doc = fakeDoc();
await createWakeLock({ navigator: {}, document: doc }).enable();
assert.strictEqual(doc.body.appended.length, 1);
assert.strictEqual(doc.body.appended[0].playCount, 1);
});
test('native request rejects: falls back to the video', async () => {
const nav = { wakeLock: { async request() { throw new Error('not allowed'); } } };
const doc = fakeDoc();
await createWakeLock({ navigator: nav, document: doc }).enable();
assert.strictEqual(doc.body.appended.length, 1);
assert.strictEqual(doc.body.appended[0].playCount, 1);
});
test('disable releases the native sentinel', async () => {
const released = [];
const nav = { wakeLock: { async request() { return { async release() { released.push(true); } }; } } };
const wl = createWakeLock({ navigator: nav, document: fakeDoc() });
await wl.enable();
await wl.disable();
assert.strictEqual(released.length, 1);
});
test('disable pauses the fallback video', async () => {
const doc = fakeDoc();
const wl = createWakeLock({ navigator: {}, document: doc });
await wl.enable();
await wl.disable();
assert.strictEqual(doc.body.appended[0].paused, true);
});
test('visibilitychange→visible re-acquires the native lock', async () => {
const nav = nativeNav();
const doc = fakeDoc();
await createWakeLock({ navigator: nav, document: doc }).enable();
assert.deepStrictEqual(nav.calls, ['screen']);
doc.fire('visibilitychange');
assert.deepStrictEqual(nav.calls, ['screen', 'screen']);
});
test('double enable does not stack; disable when idle is a no-op', async () => {
const nav = nativeNav();
const wl = createWakeLock({ navigator: nav, document: fakeDoc() });
await wl.enable();
await wl.enable();
assert.deepStrictEqual(nav.calls, ['screen']);
await createWakeLock({ navigator: nav, document: fakeDoc() }).disable(); // no throw
});
// The Android app's WebView never reports the page hidden (AlwaysVisibleWebView), so
// no visibilitychange fires there: MainActivity sends appVisible instead.
test('appVisible re-acquires a native lock the system released', async () => {
const nav = nativeNav();
const doc = fakeDoc();
await createWakeLock({ navigator: nav, document: doc }).enable();
doc.fire('appVisible');
await Promise.resolve();
assert.deepStrictEqual(nav.calls, ['screen', 'screen']);
});
test('appVisible leaves a native lock that is still held alone', async () => {
const calls = [];
const nav = { wakeLock: { async request(type) { calls.push(type); return { released: false, async release() {} }; } } };
const doc = fakeDoc();
await createWakeLock({ navigator: nav, document: doc }).enable();
doc.fire('appVisible');
await Promise.resolve();
assert.deepStrictEqual(calls, ['screen']);
});
test('disable stops listening for appVisible', async () => {
const nav = nativeNav();
const doc = fakeDoc();
const wl = createWakeLock({ navigator: nav, document: doc });
await wl.enable();
await wl.disable();
doc.fire('appVisible');
await Promise.resolve();
assert.deepStrictEqual(nav.calls, ['screen']);
});