Files
archy/tests/lifecycle/companion-top-inset.cjs

69 lines
3.9 KiB
JavaScript

// Exercise the exact injected JavaScript in Chromium. This covers DOM state,
// not Android's delivery of window insets or physical WebView acceptance.
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const { chromium } = require('../../neode-ui/node_modules/@playwright/test');
const source = fs.readFileSync(path.join(__dirname,
'../../Android/app/src/main/java/com/archipelago/app/ui/screens/WebViewScreen.kt'), 'utf8');
const start = source.indexOf('private fun injectTopInset(');
const end = source.indexOf('/** True when a TCP listener', start);
assert.ok(start >= 0 && end > start);
const script = source.slice(start, end).match(/"""([\s\S]*?)"""\.trimIndent\(\)/)?.[1];
assert.ok(script, 'production inset script must be present');
(async () => {
const browser = await chromium.launch({headless: true});
try {
const page = await browser.newPage({viewport: {width: 390, height: 844}});
await page.setContent(`<style>
body {margin:0;background:#123;padding-top:3px}
header {position:fixed;top:0;height:40px}
aside {position:fixed;top:0;bottom:0;padding-top:7px;box-sizing:content-box}
footer {position:fixed;bottom:0;height:30px}
</style><header>Header</header><aside>Modal</aside><footer>Bottom</footer>`);
const inject = sat => page.evaluate(script.replaceAll('$sat', String(sat)));
const snapshot = () => page.evaluate(() => ({
inset: document.documentElement.style.getPropertyValue('--safe-area-top'),
body: getComputedStyle(document.body).paddingTop,
header: getComputedStyle(document.querySelector('header')).top,
overlayTop: getComputedStyle(document.querySelector('aside')).top,
overlayPadding: getComputedStyle(document.querySelector('aside')).paddingTop,
bottomModified: document.querySelector('footer').hasAttribute('data-archy-inset'),
}));
await inject(24);
assert.deepEqual(await snapshot(), {inset:'24px', body:'24px', header:'24px',
overlayTop:'0px', overlayPadding:'31px', bottomModified:false});
// Queue a mutation callback immediately before changing the inset. The old
// closure must never restore its captured 24px on the next task.
await page.evaluate(() => document.body.appendChild(document.createElement('section')));
await inject(12);
await page.waitForTimeout(350);
assert.deepEqual(await snapshot(), {inset:'12px', body:'12px', header:'12px',
overlayTop:'0px', overlayPadding:'19px', bottomModified:false});
await inject(12);
assert.equal((await snapshot()).overlayPadding, '19px', 'reinjection must not accumulate padding');
await inject(0);
await page.waitForTimeout(350);
assert.deepEqual(await snapshot(), {inset:'0px', body:'3px', header:'0px',
overlayTop:'0px', overlayPadding:'7px', bottomModified:false});
assert.equal(await page.locator('[data-archy-inset]').count(), 0);
assert.equal(await page.locator('#archy-top-inset').count(), 0);
await inject(24);
await page.evaluate(() => {
const bar = document.createElement('nav');
bar.id = 'late';
bar.style.cssText = 'position:fixed;top:2px;height:20px';
document.body.appendChild(bar);
});
await page.waitForFunction(() => getComputedStyle(document.querySelector('#late')).top === '26px');
// A page's later inline edit is not ours to undo during fullscreen cleanup.
await page.evaluate(() => document.querySelector('header').style.setProperty('top', '90px', 'important'));
await inject(0);
assert.equal((await snapshot()).header, '90px');
assert.equal(await page.locator('#late').evaluate(el => el.style.top), '2px');
console.log('PASS exact Companion injection: rotation, zero/fullscreen restoration, queued observer retirement, reinjection, overlays, bottom controls, late mounts and page-edit preservation');
} finally { await browser.close(); }
})().catch(error => { console.error(error); process.exitCode = 1; });