提交记录新增ui/scripts/overlay-display.test.mjs+63 −0看文件
| @@ -0,0 +1,63 @@ | ||
| 1 | 新增:import { test } from 'node:test'; | |
| 2 | 新增:import assert from 'node:assert/strict'; | |
| 3 | 新增:import http from 'node:http'; | |
| 4 | 新增:import { readFile } from 'node:fs/promises'; | |
| 5 | 新增:import { resolve, sep } from 'node:path'; | |
| 6 | 新增:import { pathToFileURL } from 'node:url'; | |
| 7 | 新增: | |
| 8 | 新增:const enabled = process.env.CAGE_BROWSER_TEST_PLAYWRIGHT && process.env.CAGE_BROWSER_TEST_CHROMIUM; | |
| 9 | 新增:(enabled ? test : test.skip)('noVNC overlays preserve alpha, physical coordinates and removal at HiDPI', async t => { | |
| 10 | 新增: const { chromium } = await import(pathToFileURL(process.env.CAGE_BROWSER_TEST_PLAYWRIGHT)); | |
| 11 | 新增: const root = resolve('ui/vendor/novnc'); | |
| 12 | 新增: const server = http.createServer(async (req, res) => { | |
| 13 | 新增: const path = resolve(root, '.' + new URL(req.url, 'http://localhost').pathname); | |
| 14 | 新增: if (!path.startsWith(root + sep)) { res.end('<html></html>'); return; } | |
| 15 | 新增: try { res.setHeader('Content-Type', 'text/javascript'); res.end(await readFile(path)); } | |
| 16 | 新增: catch { res.writeHead(404); res.end(); } | |
| 17 | 新增: }); | |
| 18 | 新增: await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); | |
| 19 | 新增: const browser = await chromium.launch({ executablePath: process.env.CAGE_BROWSER_TEST_CHROMIUM, headless: true }); | |
| 20 | 新增: t.after(async () => { await browser.close(); await new Promise(resolve => server.close(resolve)); }); | |
| 21 | 新增: for (const dpr of [1, 1.5, 2]) { | |
| 22 | 新增: const context = await browser.newContext({ deviceScaleFactor: dpr }); | |
| 23 | 新增: const page = await context.newPage(); | |
| 24 | 新增: await page.goto(`http://127.0.0.1:${server.address().port}/`); | |
| 25 | 新增: const results = await page.evaluate(async () => { | |
| 26 | 新增: const { default: Display } = await import('/core/display.js'); | |
| 27 | 新增: const output = document.createElement('canvas'); document.body.append(output); | |
| 28 | 新增: const display = new Display(output); display.resize(160, 120); | |
| 29 | 新增: const image = (width, height, color, mime) => { | |
| 30 | 新增: const canvas = document.createElement('canvas'); canvas.width = width; canvas.height = height; | |
| 31 | 新增: const ctx = canvas.getContext('2d'); ctx.fillStyle = color; ctx.fillRect(0, 0, width, height); | |
| 32 | 新增: return Uint8Array.from(atob(canvas.toDataURL(mime).split(',')[1]), c => c.charCodeAt(0)); | |
| 33 | 新增: }; | |
| 34 | 新增: const base = image(160, 120, '#ffffff', 'image/jpeg'); | |
| 35 | 新增: const popup = image(40, 30, 'rgba(255,0,0,0.5)', 'image/png'); | |
| 36 | 新增: const front = image(12, 12, '#0000ff', 'image/png'); | |
| 37 | 新增: const pixel = (x, y) => [...output.getContext('2d').getImageData(x, y, 1, 1).data]; | |
| 38 | 新增: const values = []; | |
| 39 | 新增: for (const zoom of [0.5, 0.75, 1, 1.5]) { | |
| 40 | 新增: display.scale = zoom; | |
| 41 | 新增: display.imageRect(0, 0, 160, 120, 'image/jpeg', base); | |
| 42 | 新增: display.imageRect(60, 40, 40, 30, 'image/png', popup); | |
| 43 | 新增: display.imageRect(70, 50, 12, 12, 'image/png', front); | |
| 44 | 新增: display.flip(); await display.flush(); | |
| 45 | 新增: values.push({ outside: pixel(59, 40), alpha: pixel(60, 40), front: pixel(70, 50), | |
| 46 | 新增: after: pixel(100, 70), x: display.absX(60 * zoom), y: display.absY(40 * zoom) }); | |
| 47 | 新增: display.imageRect(0, 0, 160, 120, 'image/jpeg', base); | |
| 48 | 新增: display.flip(); await display.flush(); | |
| 49 | 新增: values.at(-1).removed = pixel(70, 50); | |
| 50 | 新增: } | |
| 51 | 新增: return values; | |
| 52 | 新增: }); | |
| 53 | 新增: for (const result of results) { | |
| 54 | 新增: assert.deepEqual(result.outside, [255,255,255,255]); | |
| 55 | 新增: assert.deepEqual(result.front, [0,0,255,255]); | |
| 56 | 新增: assert.equal(result.alpha[0], 255); assert(Math.abs(result.alpha[1] - 127) <= 1); | |
| 57 | 新增: assert.deepEqual(result.after, [255,255,255,255]); | |
| 58 | 新增: assert.deepEqual(result.removed, [255,255,255,255]); | |
| 59 | 新增: assert.equal(result.x, 60); assert.equal(result.y, 40); | |
| 60 | 新增: } | |
| 61 | 新增: await context.close(); | |
| 62 | 新增: } | |
| 63 | 新增:}); | |

