From 65c5e8cf8a27478d8fedda2578fa4ca393717980 Mon Sep 17 00:00:00 2001 From: Bart van Helvert Date: Fri, 6 Nov 2026 22:27:33 +0100 Subject: [PATCH] test(ui-test): hand a page its undo keys on Windows The driver posts key events instead of typing them, and they carry no scan code. On Windows the off-screen browser makes a page's key from the scan code alone, so every key reached the counter editor's page as "Unidentified" (code Escape, keyCode 0) and the page never saw an undo. On Windows the test now dispatches the undo and redo keydowns in the page itself. Elsewhere it still presses the keys. A real keyboard gives the scan code, so this is not a product bug. Refs #87 --- .../runtime/SplitModeCustomEditorTest.kt | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/ui-test/src/test/kotlin/me/helvert/codebridge/uitest/runtime/SplitModeCustomEditorTest.kt b/src/ui-test/src/test/kotlin/me/helvert/codebridge/uitest/runtime/SplitModeCustomEditorTest.kt index 4f75208f..1878d3b6 100644 --- a/src/ui-test/src/test/kotlin/me/helvert/codebridge/uitest/runtime/SplitModeCustomEditorTest.kt +++ b/src/ui-test/src/test/kotlin/me/helvert/codebridge/uitest/runtime/SplitModeCustomEditorTest.kt @@ -254,12 +254,12 @@ class SplitModeCustomEditorTest { // undone: the editor is modified once the backend has it. awaitProbeValue("the workbench holding the extension's edits", { selectedEditorModified() }) { it == true } browser.findElement("//p[@id='value']").click() - frame.keyboard { hotKey(KeyEvent.VK_CONTROL, KeyEvent.VK_Z) } + pageUndoKey(frame, browser, redo = false) awaitProbeValue("the extension asked to undo", ::state) { it?.contains(""""undone":1""") == true } count(1) - frame.keyboard { hotKey(KeyEvent.VK_CONTROL, KeyEvent.VK_SHIFT, KeyEvent.VK_Z) } + pageUndoKey(frame, browser, redo = true) count(2) - frame.keyboard { hotKey(KeyEvent.VK_CONTROL, KeyEvent.VK_Z) } + pageUndoKey(frame, browser, redo = false) count(1) awaitProbeValue("the extension asked to undo twice and redo once", ::state) { it?.contains(""""undone":2""") == true && it.contains(""""redone":1""") @@ -278,6 +278,26 @@ class SplitModeCustomEditorTest { } } + /** + * Ctrl+Z, or Ctrl+Shift+Z to [redo], pressed in [browser]'s page. + * + * On Windows the page is handed the key itself: the driver posts key events rather than typing + * them, and those carry no scan code, which is what the off-screen browser there makes a page's + * key from. Each reaches the page as an unidentified key, so the page never sees an undo. + */ + private fun pageUndoKey(frame: IdeaFrameUI, browser: JCefUI, redo: Boolean) { + if (!System.getProperty("os.name").startsWith("Windows")) { + frame.keyboard { + if (redo) hotKey(KeyEvent.VK_CONTROL, KeyEvent.VK_SHIFT, KeyEvent.VK_Z) else hotKey(KeyEvent.VK_CONTROL, KeyEvent.VK_Z) + } + return + } + browser.jcefWorker.runJs( + "(document.activeElement || document.body).dispatchEvent(new KeyboardEvent('keydown', " + + "{key: '${if (redo) "Z" else "z"}', code: 'KeyZ', ctrlKey: true, shiftKey: $redo, bubbles: true, cancelable: true}))" + ) + } + /** Whether the client's selected editor has changes, which a custom editor's are the backend's; null while it cannot say. */ private fun WorkbenchProbe.selectedEditorModified(): Boolean? = runCatching { driver.service(FileEditorManagerRef::class, driver.singleProject()).getSelectedEditor()?.isModified()