You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/UserTouchHighlightContractT...

84 lines
3.7 KiB
JavaScript

import assert from 'node:assert/strict';
import { execFile } from 'node:child_process';
import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { promisify } from 'node:util';
const execFileAsync = promisify(execFile);
const root = new URL('../', import.meta.url);
const css = await readFile(new URL('public/cloud/styles.css', root), 'utf8');
const layouts = await Promise.all([
'resources/views/venix/cloud/page.tpl',
'resources/views/venix/user/layout_header.tpl',
'resources/views/venix/user/layout_plain_header.tpl',
'resources/views/venix/auth/layout_header.tpl',
].map(path => readFile(new URL(path, root), 'utf8')));
for (const layout of layouts) {
assert.match(layout, /\/cloud\/styles\.css\?v=/,
'A user-facing layout does not load the shared interaction stylesheet.');
}
const chromeCandidates = [
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
'/Applications/Chromium.app/Contents/MacOS/Chromium',
'/usr/bin/google-chrome',
'/usr/bin/chromium',
'/usr/bin/chromium-browser',
];
let chrome = '';
for (const candidate of chromeCandidates) {
try {
await execFileAsync(candidate, ['--version']);
chrome = candidate;
break;
} catch { /* Try the next local browser. */ }
}
if (chrome) {
const directory = await mkdtemp(join(tmpdir(), 'user-touch-highlight-'));
const fixture = join(directory, 'fixture.html');
const safeCss = css.replaceAll('</style', '<\\/style');
await writeFile(fixture, `<!doctype html><html><head>
<meta name="viewport" content="width=device-width,initial-scale=1">
<style>${safeCss}</style></head><body>
<button id="native-button">普通按钮</button>
<a id="button-link" class="button" href="#target">链接按钮</a>
<a id="row-link" class="row-button" href="#target">行按钮</a>
<input id="submit-input" type="submit" value="提交">
<div id="role-button" role="button" tabindex="0">角色按钮</div>
<details><summary id="summary"><span id="summary-child">折叠按钮</span></summary></details>
<script>
const ids = ['native-button', 'button-link', 'row-link', 'submit-input',
'role-button', 'summary', 'summary-child'];
const metrics = Object.fromEntries(ids.map(id => [
id, getComputedStyle(document.getElementById(id)).webkitTapHighlightColor,
]));
document.body.dataset.metrics = encodeURIComponent(JSON.stringify(metrics));
</script></body></html>`);
try {
const { stdout } = await execFileAsync(chrome, [
'--headless=new', '--disable-gpu', '--no-sandbox', '--hide-scrollbars',
'--touch-events=enabled', '--window-size=390,844', '--dump-dom', `file://${fixture}`,
], { maxBuffer: 8 * 1024 * 1024 });
const encoded = stdout.match(/data-metrics="([^"]+)"/)?.[1];
assert.ok(encoded, 'Headless browser did not return touch-highlight metrics.');
const metrics = JSON.parse(decodeURIComponent(encoded));
for (const [id, value] of Object.entries(metrics)) {
assert.ok(value === 'rgba(0, 0, 0, 0)' || value === 'transparent',
`${id} still renders the mobile black tap highlight (${value}).`);
}
} finally {
await rm(directory, { recursive: true, force: true });
}
}
const rootRule = css.match(/:root\s*\{([^}]*)\}/)?.[1] || '';
assert.match(rootRule, /-webkit-tap-highlight-color\s*:\s*transparent/,
'The shared user stylesheet does not disable the WebKit tap overlay.');
assert.doesNotMatch(rootRule, /outline\s*:/,
'The global touch fix must not remove keyboard focus outlines.');
console.log(`User touch-highlight checks passed${chrome ? ' with headless browser.' : ' (browser unavailable; static contract only).'}`);