| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { readFileSync } from "node:fs"; |
| 4 | import vm from "node:vm"; |
| 5 | import { webcrypto } from "node:crypto"; |
| 6 | import { caretPollDelay, caretPollFast, caretFreshFor } from "./caret-poll.mjs"; |
| 7 | const source = readFileSync( |
| 8 | new URL("./remote-input.js", import.meta.url), |
| 9 | "utf8", |
| 10 | ) |
| 11 | .replace(/^import[\s\S]*?;\n/gm, "") |
| 12 | .replace("export function installRemoteInput", "function installRemoteInput"); |
| 13 | function fixture(native = false, browserComposition = false, options = {}) { |
| 14 | const doc = new EventTarget(), |
| 15 | win = new EventTarget(), |
| 16 | canvas = {}, |
| 17 | local = {}, |
| 18 | keys = [], |
| 19 | pastes = [], |
| 20 | texts = [], |
| 21 | compositions = [], |
| 22 | copyContexts = [], |
| 23 | writes = [], |
| 24 | statuses = []; |
| 25 | let reads = 0, |
| 26 | mode = "mac", |
| 27 | currentTabId = "tab-1", |
| 28 | guestPlatform = "linux", |
| 29 | systemClipboard = true; |
| 30 | doc.activeElement = canvas; |
| 31 | doc.hasFocus = () => true; |
| 32 | doc.body = { append() {} }; |
| 33 | const target = Object.assign(new EventTarget(), { |
| 34 | value: "", |
| 35 | style: {}, |
| 36 | classList: { add() {}, remove() {} }, |
| 37 | setAttribute() {}, |
| 38 | focus() { |
| 39 | doc.activeElement = this; |
| 40 | }, |
| 41 | remove() {}, |
| 42 | }); |
| 43 | doc.createElement = () => target; |
| 44 | win.innerWidth = 1280; |
| 45 | win.innerHeight = 800; |
| 46 | const rfb = new EventTarget(); |
| 47 | rfb.setKeyboardTarget = (element) => { |
| 48 | rfb.target = element; |
| 49 | }; |
| 50 | rfb.events = rfb; |
| 51 | rfb.sendKey = (...args) => keys.push(args); |
| 52 | rfb.runKeyAction = action => Promise.resolve().then(() => action(rfb.sendKey)); |
| 53 | rfb.focus = () => { |
| 54 | doc.activeElement = rfb.target || canvas; |
| 55 | }; |
| 56 | rfb.pasteText = (text) => { |
| 57 | pastes.push(text); |
| 58 | return Promise.resolve(); |
| 59 | }; |
| 60 | const context = { |
| 61 | document: doc, |
| 62 | window: win, |
| 63 | setTimeout: options.setTimeout || setTimeout, |
| 64 | clearTimeout: options.clearTimeout || clearTimeout, |
| 65 | Date: options.Date || Date, |
| 66 | caretPollDelay, |
| 67 | caretPollFast, |
| 68 | caretFreshFor, |
| 69 | Promise, |
| 70 | Map, |
| 71 | Set, |
| 72 | console, |
| 73 | crypto: webcrypto, |
| 74 | TextEncoder, |
| 75 | Blob, |
| 76 | Uint8Array, |
| 77 | atob, |
| 78 | ClipboardItem: class { constructor(values) { this.values = values; } }, |
| 79 | navigator: { clipboard: { write: async values => writes.push(values) } }, |
| 80 | api: options.api || (async () => { throw new Error("unexpected API request"); }), |
| 81 | native: options.native || (async (operation, value) => { |
| 82 | if (operation === "clipboardRevision") return 1; |
| 83 | if (operation === "clipboardSyncWrite") { writes.push(value.text || value.png); return { revision: 2 }; } |
| 84 | return true; |
| 85 | }), |
| 86 | readClipboardContent: async () => { |
| 87 | reads++; |
| 88 | options.onRead?.(); |
| 89 | return { text: "中文🙂" }; |
| 90 | }, |
| 91 | transferFiles: (data) => Array.from(data?.files || []), |
| 92 | hasFiles: (data) => Array.from(data?.types || []).includes("Files"), |
| 93 | imagePayload: async () => ({ png: "fixture-image" }), |
| 94 | writeClipboard: async (text) => writes.push(text), |
| 95 | nativeAvailable: () => native, |
| 96 | getKeycode: (e) => e.code, |
| 97 | getKeysym: (e) => |
| 98 | ({ Shift: e.code === "ShiftRight" ? 0xffe2 : 0xffe1, Control: 0xffe3, Alt: 0xffe9, Meta: 0xffeb, Enter: 0xff0d })[e.key] ?? |
| 99 | (e.key.length === 1 ? e.key.codePointAt(0) : null), |
| 100 | Keysyms: { |
| 101 | lookup: (codepoint) => |
| 102 | codepoint <= 255 ? codepoint : 0x01000000 | codepoint, |
| 103 | }, |
| 104 | }; |
| 105 | const install = vm.runInNewContext(source + "\ninstallRemoteInput;", context); |
| 106 | const container = Object.assign(new EventTarget(), { |
| 107 | querySelector: () => null, |
| 108 | contains: (element) => element === canvas || element === target, |
| 109 | append() {}, |
| 110 | getBoundingClientRect: () => ({ left: 0, top: 0 }), |
| 111 | }); |
| 112 | const controller = install( |
| 113 | rfb, |
| 114 | container, |
| 115 | { |
| 116 | mode: () => mode, |
| 117 | guestPlatform: () => guestPlatform, |
| 118 | systemClipboard: () => systemClipboard, |
| 119 | preparePaste: options.preparePaste, |
| 120 | copyText: options.copyText, |
| 121 | onStatus: (t) => statuses.push(t), |
| 122 | onFallback() {}, |
| 123 | isActive: () => true, |
| 124 | setCopyContext: async (context, valid) => { if (valid()) copyContexts.push(context); }, |
| 125 | insertText: async (text) => { |
| 126 | texts.push(text); |
| 127 | }, |
| 128 | insertComposition: browserComposition ? async (id, value) => { |
| 129 | if (id !== currentTabId) throw new Error("Input target changed"); |
| 130 | compositions.push({ id, ...value }); |
| 131 | } : null, |
| 132 | tabId: () => currentTabId, |
| 133 | caretRect: browserComposition ? options.caretRect || (async () => ({ |
| 134 | available: true, context: "a".repeat(32), rect: [240, 120, 0, 24], |
| 135 | })) : null, |
| 136 | pasteContent: async (content) => { |
| 137 | const value = await content; |
| 138 | if (rfb.target) pastes.push(value.png || value.text); |
| 139 | }, |
| 140 | }, |
| 141 | ); |
| 142 | const key = (type, key, code, extra = {}) => { |
| 143 | const event = new Event(type, { cancelable: true }); |
| 144 | Object.assign(event, { |
| 145 | key, |
| 146 | code, |
| 147 | metaKey: false, |
| 148 | ctrlKey: false, |
| 149 | shiftKey: false, |
| 150 | altKey: false, |
| 151 | ...extra, |
| 152 | }); |
| 153 | doc.dispatchEvent(event); |
| 154 | return event; |
| 155 | }; |
| 156 | return { |
| 157 | doc, |
| 158 | win, |
| 159 | rfb, |
| 160 | keys, |
| 161 | pastes, |
| 162 | texts, |
| 163 | compositions, |
| 164 | copyContexts, |
| 165 | writes, |
| 166 | statuses, |
| 167 | armCopy: () => { |
| 168 | const event = new Event("pointerdown"); |
| 169 | Object.defineProperty(event, "isTrusted", { value: true }); |
| 170 | Object.assign(event, { button: 0, clientX: 20, clientY: 20 }); |
| 171 | container.dispatchEvent(event); |
| 172 | return copyContexts.at(-1); |
| 173 | }, |
| 174 | controller, |
| 175 | key, |
| 176 | local, |
| 177 | target, |
| 178 | setMode: (value) => { |
| 179 | mode = value; |
| 180 | }, |
| 181 | setGuestPlatform: value => { guestPlatform = value; }, |
| 182 | setSystemClipboard: value => { systemClipboard = value; }, |
| 183 | setTabId: (value) => { |
| 184 | currentTabId = value; |
| 185 | }, |
| 186 | reads: () => reads, |
| 187 | }; |
| 188 | } |
| 189 | for (const guest of ["linux", "windows", "darwin"]) { |
| 190 | test(`${guest}: Shift+Enter restores a modifier missed during focus changes`, () => { |
| 191 | const f = fixture(); |
| 192 | f.setGuestPlatform(guest); |
| 193 | try { |
| 194 | assert.equal(f.key("keydown", "Enter", "Enter", { shiftKey: true }).defaultPrevented, true); |
| 195 | f.key("keyup", "Enter", "Enter", { shiftKey: true }); |
| 196 | assert.deepEqual(f.keys, [ |
| 197 | [0xffe1, "ShiftLeft", true], [0xff0d, "Enter", true], |
| 198 | [0xff0d, "Enter", false], [0xffe1, "ShiftLeft", false], |
| 199 | ]); |
| 200 | f.keys.length = 0; |
| 201 | f.key("keydown", "Enter", "Enter", { shiftKey: true, isComposing: true }); |
| 202 | assert.deepEqual(f.keys, [], "IME confirmation must not insert a newline"); |
| 203 | } finally { f.controller.dispose(); } |
| 204 | }); |
| 205 | for (const code of ["ShiftLeft", "ShiftRight"]) { |
| 206 | test(`${guest}: Shift+Enter keeps held ${code} until its release`, () => { |
| 207 | const f = fixture(); |
| 208 | f.setGuestPlatform(guest); |
| 209 | try { |
| 210 | const sym = code === "ShiftRight" ? 0xffe2 : 0xffe1; |
| 211 | f.key("keydown", "Shift", code, { shiftKey: true }); |
| 212 | f.key("keydown", "Enter", "Enter", { shiftKey: true }); |
| 213 | f.key("keyup", "Enter", "Enter", { shiftKey: true }); |
| 214 | assert.ok(!f.keys.some(([key, , down]) => key === sym && !down)); |
| 215 | f.key("keyup", "Shift", code); |
| 216 | assert.deepEqual(f.keys.at(-1), [sym, code, false]); |
| 217 | f.keys.length = 0; |
| 218 | f.win.dispatchEvent(new Event("blur")); |
| 219 | assert.deepEqual(f.keys, [], "released Shift must not remain held remotely"); |
| 220 | } finally { f.controller.dispose(); } |
| 221 | }); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | test("Mac find and redo preserve the Linux Ctrl/Shift chord", () => { |
| 226 | const f = fixture(); |
| 227 | try { |
| 228 | assert.equal( |
| 229 | f.key("keydown", "f", "KeyF", { metaKey: true }).defaultPrevented, |
| 230 | true, |
| 231 | ); |
| 232 | assert.deepEqual( |
| 233 | f.keys.map((k) => [k[0], k[2]]), |
| 234 | [ |
| 235 | [0xffe3, true], |
| 236 | [102, true], |
| 237 | [102, false], |
| 238 | [0xffe3, false], |
| 239 | ], |
| 240 | ); |
| 241 | f.keys.length = 0; |
| 242 | f.key("keydown", "\0", "KeyZ", { metaKey: true, shiftKey: true }); |
| 243 | assert.deepEqual( |
| 244 | f.keys.map((k) => [k[0], k[2]]), |
| 245 | [ |
| 246 | [0xffe3, true], |
| 247 | [0xffe1, true], |
| 248 | [90, true], |
| 249 | [90, false], |
| 250 | [0xffe1, false], |
| 251 | [0xffe3, false], |
| 252 | ], |
| 253 | ); |
| 254 | } finally { |
| 255 | f.controller.dispose(); |
| 256 | } |
| 257 | }); |
| 258 | |
| 259 | test("Mac guests use Command for edit actions and preserve physical Control", () => { |
| 260 | const f = fixture(); |
| 261 | f.setGuestPlatform("darwin"); |
| 262 | try { |
| 263 | f.key("keydown", "f", "KeyF", { metaKey: true }); |
| 264 | assert.deepEqual(f.keys.map(([symbol, , down]) => [symbol, down]), [[0xffeb, true], [102, true], [102, false], [0xffeb, false]]); |
| 265 | f.keys.length = 0; |
| 266 | f.controller.edit("selectAll"); |
| 267 | assert.deepEqual(f.keys.map(([symbol, , down]) => [symbol, down]), [[0xffeb, true], [97, true], [97, false], [0xffeb, false]]); |
| 268 | f.keys.length = 0; |
| 269 | f.key("keydown", "Control", "ControlLeft", { ctrlKey: true }); |
| 270 | f.key("keydown", "a", "KeyA", { ctrlKey: true }); |
| 271 | f.key("keyup", "a", "KeyA", { ctrlKey: true }); |
| 272 | f.key("keyup", "Control", "ControlLeft"); |
| 273 | assert.deepEqual(f.keys.map(([symbol, , down]) => [symbol, down]), [[0xffe3, true], [97, true], [97, false], [0xffe3, false]]); |
| 274 | f.keys.length = 0; |
| 275 | f.key("keydown", "c", "KeyC", { ctrlKey: true }); |
| 276 | assert.deepEqual(f.keys.map(([symbol, , down]) => [symbol, down]), [[99, true]]); |
| 277 | } finally { f.controller.dispose(); } |
| 278 | }); |
| 279 | |
| 280 | test("a private browser clipboard uses its surface path while OS sharing is active", async () => { |
| 281 | const order = []; |
| 282 | const f = fixture(true, false, { |
| 283 | preparePaste: () => { order.push("prepare"); return { dispose() { order.push("dispose"); } }; }, |
| 284 | onRead: () => order.push("read"), |
| 285 | }); |
| 286 | f.setGuestPlatform("darwin"); |
| 287 | f.setSystemClipboard(false); |
| 288 | f.win.cageSharedClipboardActive = true; |
| 289 | try { |
| 290 | await f.controller.readAndPaste(); |
| 291 | assert.equal(f.reads(), 1); |
| 292 | assert.deepEqual(order, ["prepare", "read", "dispose"]); |
| 293 | f.doc.visibilityState = "visible"; |
| 294 | const context = f.armCopy(); |
| 295 | await f.controller.receiveCopy({ id: "tab-1", text: "private browser copy", context }); |
| 296 | assert.deepEqual(f.writes, ["private browser copy"]); |
| 297 | } finally { f.controller.dispose(); } |
| 298 | }); |
| 299 | |
| 300 | test("shared clipboard input skips fallback copy setup and resumes it after disconnect", async () => { |
| 301 | const nativeCalls = []; |
| 302 | const f = fixture(true, false, { |
| 303 | native: async operation => { nativeCalls.push(operation); return 1; }, |
| 304 | }); |
| 305 | try { |
| 306 | const beforeSharing = f.armCopy(); |
| 307 | assert.ok(beforeSharing); |
| 308 | f.win.cageSharedClipboardActive = true; |
| 309 | nativeCalls.length = 0; |
| 310 | f.armCopy(); |
| 311 | for (const [key, code] of [[" ", "Space"], ["Enter", "Enter"], ["Tab", "Tab"]]) { |
| 312 | f.key("keydown", key, code, { cageNativeInput: true }); |
| 313 | } |
| 314 | assert.deepEqual(nativeCalls, []); |
| 315 | assert.deepEqual(f.copyContexts, [beforeSharing]); |
| 316 | f.win.cageSharedClipboardActive = false; |
| 317 | await f.controller.receiveCopy({ id: "tab-1", text: "obsolete", context: beforeSharing }); |
| 318 | assert.deepEqual(nativeCalls, []); |
| 319 | assert.notEqual(f.armCopy(), beforeSharing); |
| 320 | assert.deepEqual(nativeCalls, ["clipboardRevision"]); |
| 321 | } finally { f.controller.dispose(); } |
| 322 | }); |
| 323 | |
| 324 | test("a right Command shortcut never leaves an extra left Command pressed", () => { |
| 325 | const f = fixture(); |
| 326 | f.setGuestPlatform("macos"); |
| 327 | try { |
| 328 | f.key("keydown", "Meta", "MetaRight", { metaKey: true }); |
| 329 | f.key("keydown", "f", "KeyF", { metaKey: true }); |
| 330 | f.key("keyup", "f", "KeyF", { metaKey: true }); |
| 331 | f.key("keyup", "Meta", "MetaRight"); |
| 332 | assert.equal(f.keys.some(([, code]) => code === "MetaLeft"), false); |
| 333 | assert.deepEqual(f.keys.at(-1).slice(1), ["MetaRight", false]); |
| 334 | } finally { f.controller.dispose(); } |
| 335 | }); |
| 336 | |
| 337 | function inputEvent(f, type, properties = {}) { |
| 338 | const event = new Event(type, { cancelable: true }); |
| 339 | Object.assign(event, properties); |
| 340 | f.target.dispatchEvent(event); |
| 341 | return event; |
| 342 | } |
| 343 | |
| 344 | test("host IME receives composition keys and sends only committed Unicode in order", () => { |
| 345 | const f = fixture(); |
| 346 | try { |
| 347 | f.rfb.focus(); |
| 348 | assert.equal(f.doc.activeElement, f.target); |
| 349 | assert.equal( |
| 350 | f.key("keydown", "Process", "KeyN", { keyCode: 229 }).defaultPrevented, |
| 351 | false, |
| 352 | ); |
| 353 | inputEvent(f, "compositionstart"); |
| 354 | f.target.value = "nihao"; |
| 355 | inputEvent(f, "input", { |
| 356 | data: "nihao", |
| 357 | isComposing: true, |
| 358 | inputType: "insertCompositionText", |
| 359 | }); |
| 360 | assert.equal( |
| 361 | f.key("keydown", " ", "Space", { isComposing: true }).defaultPrevented, |
| 362 | false, |
| 363 | ); |
| 364 | assert.deepEqual(f.keys, []); |
| 365 | inputEvent(f, "compositionend", { data: "你好🙂" }); |
| 366 | |
| 367 | f.target.value = "你好🙂"; |
| 368 | inputEvent(f, "input", { |
| 369 | data: "你好🙂", |
| 370 | inputType: "insertFromComposition", |
| 371 | }); |
| 372 | assert.deepEqual(f.keys, []); |
| 373 | assert.equal(f.target.value, ""); |
| 374 | assert.deepEqual(f.texts, ["你好🙂"]); |
| 375 | assert.equal(f.reads(), 0); |
| 376 | inputEvent(f, "compositionstart"); |
| 377 | inputEvent(f, "compositionend", { data: "你好" }); |
| 378 | assert.equal( |
| 379 | f.texts.length, |
| 380 | 2, |
| 381 | "identical consecutive commits are not deduplicated", |
| 382 | ); |
| 383 | } finally { |
| 384 | f.controller.dispose(); |
| 385 | } |
| 386 | assert.equal(f.rfb.target, null); |
| 387 | }); |
| 388 | |
| 389 | test("browser composition is forwarded as preedit, then committed separately", async () => { |
| 390 | const f = fixture(false, true); |
| 391 | await new Promise((resolve) => setTimeout(resolve, 5)); |
| 392 | try { |
| 393 | f.rfb.focus(); |
| 394 | inputEvent(f, "compositionstart"); |
| 395 | f.target.value = "nihao"; |
| 396 | inputEvent(f, "compositionupdate", { data: "nihao" }); |
| 397 | inputEvent(f, "input", { data: "nihao", inputType: "insertCompositionText", isComposing: true }); |
| 398 | await Promise.resolve(); |
| 399 | inputEvent(f, "compositionend", { data: "你好" }); |
| 400 | await new Promise((resolve) => setImmediate(resolve)); |
| 401 | assert.deepEqual(f.texts, []); |
| 402 | assert.equal(f.compositions[0].mode, "begin"); |
| 403 | assert.equal(new Set(f.compositions.map(value => value.session)).size, 1); |
| 404 | assert.deepEqual(f.compositions.map(value => value.sequence), [0, 1, 2]); |
| 405 | assert.deepEqual(JSON.parse(JSON.stringify(f.compositions.slice(1).map(({session, sequence, ...value}) => value))), [ |
| 406 | { id: "tab-1", mode: "update", text: "nihao", selectionStart: 5, selectionEnd: 5 }, |
| 407 | { id: "tab-1", mode: "commit", text: "你好", selectionStart: 0, selectionEnd: 0 }, |
| 408 | ]); |
| 409 | } finally { |
| 410 | f.controller.dispose(); |
| 411 | } |
| 412 | }); |
| 413 | |
| 414 | |
| 415 | function fakeClock() { |
| 416 | let now = 1_000_000, sequence = 0; |
| 417 | const timers = new Map(); |
| 418 | return { |
| 419 | Date: { now: () => now }, |
| 420 | setTimeout: (run, delay = 0) => { timers.set(++sequence, { at: now + delay, run }); return sequence; }, |
| 421 | clearTimeout: (id) => { timers.delete(id); }, |
| 422 | async advance(duration) { |
| 423 | const end = now + duration; |
| 424 | for (;;) { |
| 425 | const due = [...timers].filter(([, timer]) => timer.at <= end).sort((a, b) => a[1].at - b[1].at || a[0] - b[0])[0]; |
| 426 | if (!due) break; |
| 427 | timers.delete(due[0]); |
| 428 | now = due[1].at; |
| 429 | due[1].run(); |
| 430 | for (let turn = 0; turn < 8; turn++) await Promise.resolve(); |
| 431 | } |
| 432 | now = end; |
| 433 | }, |
| 434 | }; |
| 435 | } |
| 436 | |
| 437 | test("caret readings back off while the viewer is quiet and resume with input", async () => { |
| 438 | const clock = fakeClock(); |
| 439 | let readings = 0; |
| 440 | const f = fixture(false, true, { ...clock, caretRect: async () => { |
| 441 | readings++; |
| 442 | return { available: true, context: "a".repeat(32), rect: [240, 120, 0, 24] }; |
| 443 | } }); |
| 444 | try { |
| 445 | f.rfb.focus(); |
| 446 | await clock.advance(2900); |
| 447 | assert.equal(readings, 12); |
| 448 | readings = 0; |
| 449 | |
| 450 | await clock.advance(60000); |
| 451 | assert.ok(readings >= 30 && readings <= 50, `${readings} readings`); |
| 452 | readings = 0; |
| 453 | await clock.advance(1900); |
| 454 | assert.equal(readings, 1); |
| 455 | |
| 456 | f.key("keydown", "a", "KeyA"); |
| 457 | readings = 0; |
| 458 | await clock.advance(250); |
| 459 | assert.equal(readings, 1); |
| 460 | await clock.advance(1000); |
| 461 | assert.equal(readings, 5); |
| 462 | } finally { |
| 463 | f.controller.dispose(); |
| 464 | } |
| 465 | }); |
| 466 | |
| 467 | test("a composition begins from the last reading of a quiet viewer", async () => { |
| 468 | const clock = fakeClock(); |
| 469 | const f = fixture(false, true, clock); |
| 470 | try { |
| 471 | f.rfb.focus(); |
| 472 | await clock.advance(60000); |
| 473 | |
| 474 | await clock.advance(1900); |
| 475 | inputEvent(f, "compositionstart"); |
| 476 | assert.equal(f.compositions[0]?.mode, "begin"); |
| 477 | assert.equal(f.compositions[0].context, "a".repeat(32)); |
| 478 | } finally { |
| 479 | f.controller.dispose(); |
| 480 | } |
| 481 | }); |
| 482 | |
| 483 | test("queued composition stays bound to its original browser tab", async () => { |
| 484 | const f = fixture(false, true); |
| 485 | await new Promise((resolve) => setTimeout(resolve, 5)); |
| 486 | f.rfb.focus(); |
| 487 | inputEvent(f, "compositionstart"); |
| 488 | f.target.value = "nihao"; |
| 489 | inputEvent(f, "input", { data: "nihao", inputType: "insertCompositionText", isComposing: true }); |
| 490 | f.setTabId("tab-2"); |
| 491 | inputEvent(f, "compositionend", { data: "你好" }); |
| 492 | await new Promise((resolve) => setImmediate(resolve)); |
| 493 | assert.deepEqual(f.compositions.map(value => value.mode), ["begin", "update"]); |
| 494 | assert.ok(f.statuses.includes("Input target changed")); |
| 495 | f.controller.dispose(); |
| 496 | }); |
| 497 | |
| 498 | test("cancelled and disconnected compositions do not insert text", () => { |
| 499 | const f = fixture(); |
| 500 | f.rfb.focus(); |
| 501 | inputEvent(f, "compositionstart"); |
| 502 | inputEvent(f, "compositionend", { data: "" }); |
| 503 | assert.deepEqual(f.keys, []); |
| 504 | inputEvent(f, "compositionstart"); |
| 505 | f.controller.dispose(); |
| 506 | inputEvent(f, "compositionend", { data: "迟到的文本" }); |
| 507 | assert.deepEqual(f.keys, []); |
| 508 | assert.deepEqual(f.texts, []); |
| 509 | }); |
| 510 | |
| 511 | test("text input without a composition supports emoji and clears the host buffer", () => { |
| 512 | const f = fixture(); |
| 513 | try { |
| 514 | f.rfb.focus(); |
| 515 | f.target.value = "🙂"; |
| 516 | inputEvent(f, "input", { data: "🙂", inputType: "insertText" }); |
| 517 | assert.deepEqual(f.keys, []); |
| 518 | assert.deepEqual(f.texts, ["🙂"]); |
| 519 | assert.equal(f.target.value, ""); |
| 520 | } finally { |
| 521 | f.controller.dispose(); |
| 522 | } |
| 523 | }); |
| 524 | |
| 525 | test("fullwidth punctuation without composition uses text, while host punctuation keeps its default action", () => { |
| 526 | const f = fixture(); |
| 527 | try { |
| 528 | f.rfb.focus(); |
| 529 | for (const character of ["?", "?", ",", "。", "!", "🙂"]) { |
| 530 | assert.equal(f.key("keydown", character, "Slash", { shiftKey: true }).defaultPrevented, true); |
| 531 | f.key("keyup", character, "Slash"); |
| 532 | } |
| 533 | assert.deepEqual(f.texts, ["?", "?", ",", "。", "!", "🙂"]); |
| 534 | assert.deepEqual(f.keys, []); |
| 535 | assert.equal(f.key("keydown", "?", "Slash", { shiftKey: true }).defaultPrevented, false); |
| 536 | f.target.value = "?"; |
| 537 | inputEvent(f, "input", { data: "?", inputType: "insertText" }); |
| 538 | assert.equal(f.texts.at(-1), "?"); |
| 539 | assert.equal(f.key("keydown", "?", "Slash", { ctrlKey: true }).defaultPrevented, false); |
| 540 | } finally { f.controller.dispose(); } |
| 541 | }); |
| 542 | test("local fields and native keyboard mode do not acquire mapped shortcuts", () => { |
| 543 | const f = fixture(); |
| 544 | try { |
| 545 | f.doc.activeElement = f.local; |
| 546 | assert.equal( |
| 547 | f.key("keydown", "f", "KeyF", { metaKey: true }).defaultPrevented, |
| 548 | false, |
| 549 | ); |
| 550 | assert.equal(f.keys.length, 0); |
| 551 | f.rfb.focus(); |
| 552 | f.setMode("native"); |
| 553 | f.key("keydown", "f", "KeyF", { metaKey: true }); |
| 554 | assert.equal(f.keys.length, 0); |
| 555 | } finally { |
| 556 | f.controller.dispose(); |
| 557 | } |
| 558 | }); |
| 559 | test("background clipboard notifications never write to the host", async () => { |
| 560 | const f = fixture(); |
| 561 | try { |
| 562 | const clipboard = new Event("clipboard"); |
| 563 | clipboard.detail = { text: "guest text" }; |
| 564 | f.rfb.dispatchEvent(clipboard); |
| 565 | await Promise.resolve(); |
| 566 | assert.equal(f.reads(), 0); |
| 567 | assert.deepEqual(f.writes, []); |
| 568 | f.key("keydown", "c", "KeyC", { metaKey: true }); |
| 569 | f.rfb.dispatchEvent(clipboard); |
| 570 | await new Promise(resolve => setImmediate(resolve)); |
| 571 | assert.deepEqual(f.writes, ["guest text"]); |
| 572 | } finally { |
| 573 | f.controller.dispose(); |
| 574 | } |
| 575 | }); |
| 576 | test("native menu paste accepts every rapid gesture, without debouncing", async () => { |
| 577 | const f = fixture(true); |
| 578 | try { |
| 579 | for (let i = 0; i < 10; i++) |
| 580 | f.key("keydown", "v", "KeyV", { metaKey: true }); |
| 581 | await new Promise((resolve) => setImmediate(resolve)); |
| 582 | assert.equal(f.reads(), 10); |
| 583 | assert.equal(f.pastes.length, 10); |
| 584 | assert.ok(f.pastes.every((t) => t === "中文🙂")); |
| 585 | } finally { |
| 586 | f.controller.dispose(); |
| 587 | } |
| 588 | }); |
| 589 | test("mapped modifier is released even after focus is lost", () => { |
| 590 | const f = fixture(); |
| 591 | try { |
| 592 | f.key("keydown", "Shift", "ShiftLeft", { metaKey: true, shiftKey: true }); |
| 593 | f.doc.activeElement = f.local; |
| 594 | f.win.dispatchEvent(new Event("blur")); |
| 595 | assert.deepEqual(f.keys, [ |
| 596 | [0xffe1, "ShiftLeft", true], |
| 597 | [0xffe1, "ShiftLeft", false], |
| 598 | ]); |
| 599 | } finally { |
| 600 | f.controller.dispose(); |
| 601 | } |
| 602 | }); |
| 603 | |
| 604 | test("a clipboard read finishing after disconnect is not pasted into a stale connection", async () => { |
| 605 | const f = fixture(true); |
| 606 | f.controller.readAndPaste(); |
| 607 | f.controller.dispose(); |
| 608 | await new Promise((resolve) => setImmediate(resolve)); |
| 609 | assert.equal(f.pastes.length, 0); |
| 610 | }); |
| 611 | |
| 612 | test("verified page copies only write to the host from the focused live tab", async () => { |
| 613 | const f = fixture(); |
| 614 | const context = f.armCopy(); |
| 615 | const copy = (id = "tab-1", text = "实际复制的文字🙂") => f.controller.receiveCopy({ id, text, context }); |
| 616 | try { |
| 617 | await copy(); |
| 618 | assert.deepEqual(f.writes, ["实际复制的文字🙂"]); |
| 619 | await copy("other-tab"); |
| 620 | f.doc.hasFocus = () => false; |
| 621 | await copy(); |
| 622 | f.doc.hasFocus = () => true; |
| 623 | f.doc.visibilityState = "hidden"; |
| 624 | await copy(); |
| 625 | f.doc.visibilityState = "visible"; |
| 626 | f.doc.activeElement = f.local; |
| 627 | await copy(); |
| 628 | f.doc.activeElement = f.target; |
| 629 | await copy("tab-1", "x".repeat(2 * 1024 * 1024 + 1)); |
| 630 | assert.equal(f.writes.length, 1); |
| 631 | f.controller.dispose(); |
| 632 | await copy(); |
| 633 | assert.equal(f.writes.length, 1); |
| 634 | } finally { f.controller.dispose(); } |
| 635 | }); |
| 636 | |
| 637 | test("copy results from before a focus or tab round-trip cannot write after returning", async () => { |
| 638 | const f = fixture(); |
| 639 | try { |
| 640 | const context = f.armCopy(); |
| 641 | const oldCopy = { id: "tab-1", text: "old pending copy", context }; |
| 642 | f.win.dispatchEvent(new Event("blur")); |
| 643 | const next = f.armCopy(); |
| 644 | assert.notEqual(context, next); |
| 645 | await f.controller.receiveCopy(oldCopy); |
| 646 | f.setTabId("tab-2"); |
| 647 | f.controller.cancelCopy(); |
| 648 | f.setTabId("tab-1"); |
| 649 | const latest = f.armCopy(); |
| 650 | await f.controller.receiveCopy({ ...oldCopy, context: next }); |
| 651 | assert.equal(f.writes.length, 0); |
| 652 | await f.controller.receiveCopy({ ...oldCopy, text: "new copy", context: latest }); |
| 653 | assert.deepEqual(f.writes, ["new copy"]); |
| 654 | } finally { f.controller.dispose(); } |
| 655 | }); |
| 656 | |
| 657 | test("native copied PNG uses the host revision captured by its user gesture", async () => { |
| 658 | const calls = []; |
| 659 | let revision = 7; |
| 660 | const f = fixture(true, false, { |
| 661 | api: async (path, body) => { calls.push([path, body.id]); revision = 8; return { png: "aW1hZ2U=" }; }, |
| 662 | native: async (operation, value) => { |
| 663 | if (operation === "clipboardRevision") return revision; |
| 664 | calls.push([operation, value.revision]); |
| 665 | return { superseded: value.revision !== revision }; |
| 666 | }, |
| 667 | }); |
| 668 | try { |
| 669 | const context = f.armCopy(); |
| 670 | await f.controller.receiveCopy({ id: "tab-1", context, document: "page-a", clipboard: { id: "opaque-id", mime: "image/png", size: 5 } }); |
| 671 | assert.deepEqual(calls, [["/browser/clipboard", "opaque-id"], ["clipboardSyncWrite", 7]]); |
| 672 | assert.ok(f.statuses.at(-1).includes("剪贴板已更新")); |
| 673 | } finally { f.controller.dispose(); } |
| 674 | }); |
| 675 | |
| 676 | test("a PNG fetched after a focus change cannot write the host clipboard", async () => { |
| 677 | let finish; |
| 678 | const f = fixture(true, false, { api: () => new Promise(resolve => { finish = resolve; }) }); |
| 679 | try { |
| 680 | const context = f.armCopy(); |
| 681 | const pending = f.controller.receiveCopy({ id: "tab-1", context, document: "page-a", clipboard: { id: "opaque-id", mime: "image/png", size: 5 } }); |
| 682 | await new Promise(resolve => setImmediate(resolve)); |
| 683 | f.win.dispatchEvent(new Event("blur")); |
| 684 | finish({ png: "aW1hZ2U=" }); |
| 685 | await pending; |
| 686 | assert.deepEqual(f.writes, []); |
| 687 | } finally { f.controller.dispose(); } |
| 688 | }); |
| 689 | |
| 690 | test("a private browser cut uses its callback without a duplicate key chord", async () => { |
| 691 | const actions = []; |
| 692 | const f = fixture(false, false, { |
| 693 | copyText: async (_sendCopy, valid, action) => { assert.equal(valid(), true); actions.push(action.key); return { text: "cut result" }; }, |
| 694 | }); |
| 695 | f.setGuestPlatform("darwin"); |
| 696 | f.setSystemClipboard(false); |
| 697 | try { |
| 698 | await f.controller.copy("x"); |
| 699 | assert.deepEqual(actions, ["x"]); |
| 700 | assert.deepEqual(f.keys, []); |
| 701 | assert.deepEqual(f.writes, ["cut result"]); |
| 702 | } finally { f.controller.dispose(); } |
| 703 | }); |
| 704 | |
| 705 | test("explicit desktop copy preserves the queued native copy keys", async () => { |
| 706 | const f = fixture(false, false, { copyText: async sendCopy => { await sendCopy(); return { text: "fresh desktop text" }; } }); |
| 707 | f.setGuestPlatform("darwin"); |
| 708 | try { |
| 709 | await f.controller.copy(); |
| 710 | assert.deepEqual(f.keys.map(([symbol, , down]) => [symbol, down]), [[0xffeb, true], [99, true], [99, false], [0xffeb, false]]); |
| 711 | assert.deepEqual(f.writes, ["fresh desktop text"]); |
| 712 | } finally { f.controller.dispose(); } |
| 713 | }); |
| 714 | |
| 715 | test("a late browser capability can switch copying from native events to its private adapter", async () => { |
| 716 | let available = false; |
| 717 | const f = fixture(false, false, { copyText: () => available ? Promise.resolve({ text: "private copy" }) : undefined }); |
| 718 | try { |
| 719 | await f.controller.copy(); |
| 720 | assert.equal(f.keys.length, 4); |
| 721 | const event = new Event("clipboard"); |
| 722 | event.detail = { text: "native event" }; |
| 723 | f.rfb.dispatchEvent(event); |
| 724 | await new Promise(resolve => setImmediate(resolve)); |
| 725 | assert.deepEqual(f.writes, ["native event"]); |
| 726 | available = true; |
| 727 | f.keys.length = 0; |
| 728 | await f.controller.copy(); |
| 729 | assert.deepEqual(f.keys, []); |
| 730 | assert.deepEqual(f.writes, ["native event", "private copy"]); |
| 731 | } finally { f.controller.dispose(); } |
| 732 | }); |
| 733 | |
| 734 | test("copied text supports two MiB in UTF-8 and rejects larger content", async () => { |
| 735 | const f = fixture(); |
| 736 | try { |
| 737 | const context = f.armCopy(); |
| 738 | const text = "界".repeat(Math.floor(2 * 1024 * 1024 / 3)); |
| 739 | await f.controller.receiveCopy({ id: "tab-1", context, text }); |
| 740 | assert.deepEqual(f.writes, [text]); |
| 741 | await f.controller.receiveCopy({ id: "tab-1", context, text: text + "界" }); |
| 742 | assert.equal(f.writes.length, 1); |
| 743 | } finally { f.controller.dispose(); } |
| 744 | }); |
| 745 | |
| 746 | test("clicking between split panes rejects earlier copies after returning", async () => { |
| 747 | const f = fixture(); |
| 748 | try { |
| 749 | const leftContext = f.armCopy(); |
| 750 | f.setTabId("tab-2"); |
| 751 | const rightContext = f.armCopy(); |
| 752 | f.setTabId("tab-1"); |
| 753 | const currentContext = f.armCopy(); |
| 754 | |
| 755 | |
| 756 | await f.controller.receiveCopy({ id: "tab-1", text: "old left copy", context: leftContext }); |
| 757 | await f.controller.receiveCopy({ id: "tab-2", text: "old right copy", context: rightContext }); |
| 758 | assert.deepEqual(f.writes, []); |
| 759 | await f.controller.receiveCopy({ id: "tab-1", text: "current left copy", context: currentContext }); |
| 760 | assert.deepEqual(f.writes, ["current left copy"]); |
| 761 | } finally { f.controller.dispose(); } |
| 762 | }); |
| 763 | |
| 764 | test("an unrelated RFB clipboard update never authorizes a host write", async () => { |
| 765 | const f = fixture(); |
| 766 | try { |
| 767 | const event = new Event("clipboard"); |
| 768 | event.detail = { text: "unrelated guest application" }; |
| 769 | f.rfb.dispatchEvent(event); |
| 770 | await new Promise(resolve => setImmediate(resolve)); |
| 771 | assert.equal(f.writes.length, 0); |
| 772 | } finally { f.controller.dispose(); } |
| 773 | }); |
| 774 | |
| 775 | test("malformed native Command letters retain clipboard and redo semantics", async () => { |
| 776 | const f = fixture(true); |
| 777 | try { |
| 778 | f.key("keydown", "\u0016", "KeyV", { metaKey: true }); |
| 779 | await new Promise((resolve) => setImmediate(resolve)); |
| 780 | assert.equal(f.reads(), 1); |
| 781 | assert.equal(f.pastes.length, 1); |
| 782 | assert.equal(f.keys.length, 0); |
| 783 | for (const letter of ["\ufffd", "\uf700", "Ω"]) { |
| 784 | f.keys.length = 0; |
| 785 | f.key("keydown", letter, "KeyZ", { metaKey: true, shiftKey: true }); |
| 786 | assert.deepEqual( |
| 787 | f.keys.map((k) => [k[0], k[2]]), |
| 788 | [ |
| 789 | [0xffe3, true], |
| 790 | [0xffe1, true], |
| 791 | [90, true], |
| 792 | [90, false], |
| 793 | [0xffe1, false], |
| 794 | [0xffe3, false], |
| 795 | ], |
| 796 | ); |
| 797 | } |
| 798 | } finally { |
| 799 | f.controller.dispose(); |
| 800 | } |
| 801 | }); |