| @@ -0,0 +1,210 @@ |
| 1 | +新增:// Run `npm run prepare-vnc` in ui first. No Cage server or user session is used: |
| 2 | +新增:// every browser request is fulfilled from local UI modules or rejected. |
| 3 | +新增:// CAGE_PLAYWRIGHT_MODULE and CAGE_TEST_BROWSER_PATH select the test runtime. |
| 4 | +新增:import assert from "node:assert/strict"; |
| 5 | +新增:import { readFile } from "node:fs/promises"; |
| 6 | +新增:import { createRequire } from "node:module"; |
| 7 | +新增:import { fileURLToPath } from "node:url"; |
| 8 | +新增:import path from "node:path"; |
| 9 | +新增: |
| 10 | +新增:const require = createRequire(import.meta.url); |
| 11 | +新增:const { chromium } = require(process.env.CAGE_PLAYWRIGHT_MODULE || "playwright"); |
| 12 | +新增:const root = fileURLToPath(new URL("../", import.meta.url)); |
| 13 | +新增:const origin = "http://127.0.0.1:51234"; |
| 14 | +新增:const browser = await chromium.launch({ |
| 15 | +新增: headless: true, |
| 16 | +新增: executablePath: process.env.CAGE_TEST_BROWSER_PATH, |
| 17 | +新增:}); |
| 18 | +新增:const checks = []; |
| 19 | +新增:const problems = []; |
| 20 | +新增: |
| 21 | +新增:async function fixture(platform) { |
| 22 | +新增: const page = await browser.newPage(); |
| 23 | +新增: page.on("pageerror", (error) => problems.push(error.message)); |
| 24 | +新增: await page.addInitScript((value) => { |
| 25 | +新增: Object.defineProperty(navigator, "platform", { get: () => value }); |
| 26 | +新增: // The fixture uses production RFB methods but never opens a socket. |
| 27 | +新增: window.WebSocket = class { |
| 28 | +新增: constructor() { throw new Error("Network sockets are forbidden in this fixture"); } |
| 29 | +新增: }; |
| 30 | +新增: }, platform); |
| 31 | +新增: await page.route("**/*", async (route) => { |
| 32 | +新增: const url = new URL(route.request().url()); |
| 33 | +新增: if (url.origin !== origin) { |
| 34 | +新增: problems.push(`Unexpected external request: ${url.origin}`); |
| 35 | +新增: await route.abort(); |
| 36 | +新增: return; |
| 37 | +新增: } |
| 38 | +新增: if (url.pathname === "/caps-lock-smoke") { |
| 39 | +新增: await route.fulfill({ contentType: "text/html", body: '<div id="remote"><canvas width="1000" height="500" tabindex="-1"></canvas></div>' }); |
| 40 | +新增: return; |
| 41 | +新增: } |
| 42 | +新增: const relative = decodeURIComponent(url.pathname).slice(1); |
| 43 | +新增: const file = path.resolve(root, relative); |
| 44 | +新增: if (!file.startsWith(root) || !/^(src|\.generated\/novnc)\//.test(relative)) { |
| 45 | +新增: problems.push(`Unexpected fixture request: ${url.pathname}`); |
| 46 | +新增: await route.abort(); |
| 47 | +新增: return; |
| 48 | +新增: } |
| 49 | +新增: try { |
| 50 | +新增: const body = await readFile(path.extname(file) ? file : `${file}.js`); |
| 51 | +新增: await route.fulfill({ contentType: "text/javascript", body }); |
| 52 | +新增: } catch (error) { |
| 53 | +新增: problems.push(`${url.pathname}: ${error.message}`); |
| 54 | +新增: await route.abort(); |
| 55 | +新增: } |
| 56 | +新增: }); |
| 57 | +新增: await page.goto(`${origin}/caps-lock-smoke`); |
| 58 | +新增: await page.evaluate(async (platform) => { |
| 59 | +新增: const [{ default: RFB }, { default: Keyboard }, { installRemoteInput }] = await Promise.all([ |
| 60 | +新增: import("/.generated/novnc/core/rfb.js"), |
| 61 | +新增: import("/.generated/novnc/core/input/keyboard.js"), |
| 62 | +新增: import("/src/remote-input.js"), |
| 63 | +新增: ]); |
| 64 | +新增: const container = document.querySelector("#remote"); |
| 65 | +新增: const rfb = new EventTarget(); |
| 66 | +新增: for (const name of ["sendKey", "_cageSendKey", "_cageQueueInput", "_handleKeyEvent", "cageSetKeyboardTarget", "focus", "blur"]) |
| 67 | +新增: rfb[name] = RFB.prototype[name].bind(rfb); |
| 68 | +新增: rfb._canvas = container.querySelector("canvas"); |
| 69 | +新增: rfb._keyboard = new Keyboard(rfb._canvas); |
| 70 | +新增: rfb._rfbConnectionState = "connected"; |
| 71 | +新增: rfb._remoteCapsLock = rfb._remoteNumLock = null; |
| 72 | +新增: let bytes = []; |
| 73 | +新增: const packets = [], observed = [], requests = []; |
| 74 | +新增: rfb._sock = { |
| 75 | +新增: sQpush8(value) { bytes.push(value); }, |
| 76 | +新增: sQpush16(value) { bytes.push((value >> 8) & 255, value & 255); }, |
| 77 | +新增: sQpush32(value) { bytes.push(value >>> 24, (value >> 16) & 255, (value >> 8) & 255, value & 255); }, |
| 78 | +新增: flush() { packets.push(bytes); bytes = []; }, |
| 79 | +新增: }; |
| 80 | +新增: window.cageNativeIMEAvailable = true; |
| 81 | +新增: window.webkit = { messageHandlers: { cage: { postMessage: async (value) => { |
| 82 | +新增: if (value.action === "clipboardRevision") return 0; |
| 83 | +新增: if (value.action !== "remoteInput") throw new Error(`Unexpected native operation: ${value.action}`); |
| 84 | +新增: requests.push(value); |
| 85 | +新增: return true; |
| 86 | +新增: } } } }; |
| 87 | +新增: const input = installRemoteInput({ |
| 88 | +新增: events: rfb, |
| 89 | +新增: setKeyboardTarget: (target) => rfb.cageSetKeyboardTarget(target), |
| 90 | +新增: sendKey: (...key) => rfb.sendKey(...key), |
| 91 | +新增: focus: () => rfb.focus(), |
| 92 | +新增: pasteText() { throw new Error("ASCII key fixture must not use paste"); }, |
| 93 | +新增: runKeyAction: (action) => rfb._cageQueueInput(() => action((...key) => rfb._cageSendKey(...key))), |
| 94 | +新增: }, container, { |
| 95 | +新增: mode: () => /mac/i.test(platform) ? "mac" : "native", |
| 96 | +新增: isActive: () => true, |
| 97 | +新增: tabId: () => "fixture-tab", |
| 98 | +新增: caretRect: async () => ({ available: true, context: "a".repeat(32), rect: [20, 20, 1, 24] }), |
| 99 | +新增: onStatus() {}, onFallback() {}, |
| 100 | +新增: insertText() { throw new Error("ASCII key fixture must not use text insertion"); }, |
| 101 | +新增: }); |
| 102 | +新增: rfb.focus(); |
| 103 | +新增: const target = container.querySelector("textarea"); |
| 104 | +新增: target.addEventListener("keydown", (event) => { |
| 105 | +新增: observed.push({ key: event.key, caps: event.getModifierState("CapsLock"), shift: event.shiftKey }); |
| 106 | +新增: }, true); |
| 107 | +新增: window.probe = { |
| 108 | +新增: requests, input, |
| 109 | +新增: run({ remoteCapsLock, qemu = false, sequence }) { |
| 110 | +新增: packets.length = 0; |
| 111 | +新增: observed.length = 0; |
| 112 | +新增: rfb._remoteCapsLock = remoteCapsLock; |
| 113 | +新增: rfb._remoteNumLock = null; |
| 114 | +新增: rfb._qemuExtKeyEventSupported = qemu; |
| 115 | +新增: const owner = requests.find((value) => value.operation === "activate").owner; |
| 116 | +新增: for (const value of sequence) { |
| 117 | +新增: if (Object.hasOwn(value, "feedback")) { |
| 118 | +新增: // A server LED-state reply arrives after the CapsLock press. |
| 119 | +新增: rfb._remoteCapsLock = value.feedback; |
| 120 | +新增: } else { |
| 121 | +新增: window.dispatchEvent(new CustomEvent("cage-native-input", { detail: { owner, ...value } })); |
| 122 | +新增: } |
| 123 | +新增: } |
| 124 | +新增: return { |
| 125 | +新增: observed, |
| 126 | +新增: keys: packets.map((packet) => ({ |
| 127 | +新增: sym: ((packet[4] << 24) | (packet[5] << 16) | (packet[6] << 8) | packet[7]) >>> 0, |
| 128 | +新增: down: Boolean(packet[0] === 255 ? packet[3] : packet[1]), |
| 129 | +新增: protocol: packet[0] === 255 ? "qemu" : "rfb", |
| 130 | +新增: })), |
| 131 | +新增: }; |
| 132 | +新增: }, |
| 133 | +新增: }; |
| 134 | +新增: }, platform); |
| 135 | +新增: await page.waitForFunction(() => probe.requests.some((value) => value.operation === "activate")); |
| 136 | +新增: return page; |
| 137 | +新增:} |
| 138 | +新增: |
| 139 | +新增:const keyPair = (key, code, caps, shift = false) => ["keydown", "keyup"].map((kind) => ({ |
| 140 | +新增: kind, key, code, textKey: key.length === 1, shiftKey: shift, |
| 141 | +新增: ...(caps === undefined ? {} : { modifierCapsLock: caps }), |
| 142 | +新增:})); |
| 143 | +新增:const expectKeys = (result, expected, label, qemu) => { |
| 144 | +新增: assert.deepEqual(result.keys.map(({ sym, down }) => [sym, down]), expected, label); |
| 145 | +新增: assert.ok(result.keys.every(({ protocol }) => protocol === (qemu ? "qemu" : "rfb")), label); |
| 146 | +新增: checks.push(label); |
| 147 | +新增:}; |
| 148 | +新增:const press = (sym) => [[sym, true], [sym, false]]; |
| 149 | +新增: |
| 150 | +新增:try { |
| 151 | +新增: const windows = await fixture("Win32"); |
| 152 | +新增: for (const qemu of [false, true]) { |
| 153 | +新增: const suffix = qemu ? "QEMU" : "RFB"; |
| 154 | +新增: const run = (remoteCapsLock, sequence) => windows.evaluate((value) => probe.run(value), { remoteCapsLock, qemu, sequence }); |
| 155 | +新增: const missing = await run(true, keyPair("A", "KeyA")); |
| 156 | +新增: assert.equal(missing.observed[0].caps, false, "Missing native state defaults to false in a real KeyboardEvent"); |
| 157 | +新增: expectKeys(missing, [...press(0xffe5), ...press(65)], `negative control: omitted lock state wrongly toggles CapsLock (${suffix})`, qemu); |
| 158 | +新增: |
| 159 | +新增: const on = await run(true, keyPair("A", "KeyA", true)); |
| 160 | +新增: assert.equal(on.observed[0].caps, true); |
| 161 | +新增: expectKeys(on, press(65), `Windows CapsLock on stays on (${suffix})`, qemu); |
| 162 | +新增: const off = await run(false, keyPair("a", "KeyA", false)); |
| 163 | +新增: assert.equal(off.observed[0].caps, false); |
| 164 | +新增: expectKeys(off, press(97), `Windows CapsLock off stays off (${suffix})`, qemu); |
| 165 | +新增: |
| 166 | +新增: expectKeys(await run(false, keyPair("A", "KeyA", true)), [...press(0xffe5), ...press(65)], |
| 167 | +新增: `Windows local CapsLock on resynchronizes stale remote off (${suffix})`, qemu); |
| 168 | +新增: expectKeys(await run(true, keyPair("a", "KeyA", false)), [...press(0xffe5), ...press(97)], |
| 169 | +新增: `Windows local CapsLock off resynchronizes stale remote on (${suffix})`, qemu); |
| 170 | +新增: |
| 171 | +新增: expectKeys(await run(false, [ |
| 172 | +新增: ...keyPair("CapsLock", "CapsLock", true), { feedback: true }, ...keyPair("A", "KeyA", true), |
| 173 | +新增: ...keyPair("CapsLock", "CapsLock", false), { feedback: false }, ...keyPair("a", "KeyA", false), |
| 174 | +新增: ]), [...press(0xffe5), ...press(65), ...press(0xffe5), ...press(97)], |
| 175 | +新增: `Windows CapsLock on/off sequence has no extra toggles after LED replies (${suffix})`, qemu); |
| 176 | +新增: |
| 177 | +新增: for (const caps of [false, true]) { |
| 178 | +新增: const letter = caps ? "a" : "A"; |
| 179 | +新增: const shifted = await run(caps, [ |
| 180 | +新增: { kind: "keydown", key: "Shift", code: "ShiftLeft", modifierCapsLock: caps, shiftKey: true }, |
| 181 | +新增: ...keyPair(letter, "KeyA", caps, true), |
| 182 | +新增: { kind: "keyup", key: "Shift", code: "ShiftLeft", modifierCapsLock: caps, shiftKey: false }, |
| 183 | +新增: ]); |
| 184 | +新增: assert.ok(shifted.observed.every((event) => event.caps === caps && event.shift)); |
| 185 | +新增: expectKeys(shifted, [[0xffe1, true], ...press(letter.codePointAt(0)), [0xffe1, false]], |
| 186 | +新增: `Windows ${caps ? "CapsLock+Shift lowercase" : "Shift uppercase"} preserves modifiers (${suffix})`, qemu); |
| 187 | +新增: } |
| 188 | +新增: } |
| 189 | +新增: await windows.evaluate(() => probe.input.dispose()); |
| 190 | +新增: await windows.close(); |
| 191 | +新增: |
| 192 | +新增: const mac = await fixture("MacIntel"); |
| 193 | +新增: for (const caps of [true, false]) { |
| 194 | +新增: const letter = caps ? "A" : "a"; |
| 195 | +新增: const result = await mac.evaluate((value) => probe.run(value), { remoteCapsLock: caps, sequence: keyPair(letter, "KeyA", caps) }); |
| 196 | +新增: assert.equal(result.observed[0].caps, caps); |
| 197 | +新增: expectKeys(result, press(letter.codePointAt(0)), `existing macOS native payload preserves CapsLock ${caps ? "on" : "off"}`, false); |
| 198 | +新增: } |
| 199 | +新增: await mac.evaluate(() => probe.input.dispose()); |
| 200 | +新增: await mac.close(); |
| 201 | +新增: assert.deepEqual(problems, [], "The fixture must have no page errors or unexpected requests"); |
| 202 | +新增: console.log(JSON.stringify({ |
| 203 | +新增: passed: true, |
| 204 | +新增: checks, |
| 205 | +新增: coverage: "Real Chromium KeyboardEvent, production installRemoteInput native-event bridge, noVNC keyboard, RFB lock synchronization, and final wire encoding; in-memory transport only.", |
| 206 | +新增: limitation: "Native Windows/GTK/AppKit event production, physical keyboard LED state, and remote X11 application behavior require separate runtime acceptance. Mac platform detection is simulated in Chromium, not WebKit.", |
| 207 | +新增: }, null, 2)); |
| 208 | +新增:} finally { |
| 209 | +新增: await browser.close(); |
| 210 | +新增:} |