| 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 | }); |