test(cua-macos): verify Paste dispatch against partial AppKit menus

This commit is contained in:
creatixchu committed 2026-11-12 17:14:42 +08:00
1 parent 032c4eac89
commit 06ae2c32fc
2 files changed
+39 -5

No files matched your search

@@ -1,6 +1,14 @@
// A foreground-sensitive Paste receiver records AX command delivery independently of clipboard consumption.
import AppKit
private final class PartialMenu: NSMenu {
var gapFirst = false
override func accessibilityChildren() -> [Any]? {
let items = super.accessibilityChildren() ?? []
return gapFirst ? [NSNull()] + items : items + [NSNull()]
}
}
final class PasteFixture: NSObject, NSApplicationDelegate, NSTextViewDelegate {
let statePath: String
let label: String
@@ -11,11 +19,22 @@ final class PasteFixture: NSObject, NSApplicationDelegate, NSTextViewDelegate {
func applicationDidFinishLaunching(_ notification: Notification) {
let menu = NSMenu()
let editItem = NSMenuItem()
let edit = NSMenu(title: "Edit")
let mode = CommandLine.arguments.dropFirst(3).first
let edit: NSMenu
if let mode {
let partial = PartialMenu(title: "Edit")
partial.gapFirst = mode == "before"
edit = partial
} else { edit = NSMenu(title: "Edit") }
edit.autoenablesItems = false
let paste = NSMenuItem(title: "Paste", action: #selector(pasteText(_:)), keyEquivalent: "v")
paste.target = self
edit.addItem(paste)
if mode == "duplicate" {
let duplicate = NSMenuItem(title: "Second Paste", action: #selector(pasteText(_:)), keyEquivalent: "v")
duplicate.target = self
edit.addItem(duplicate)
}
editItem.submenu = edit
menu.addItem(editItem)
NSApp.mainMenu = menu
@@ -49,9 +49,9 @@ def main():
helper.call("activate", {"appHandle": blocker["appHandle"]})
wait_state(root / "target.json", lambda state: not state["active"] and state["textFocused"])
started = time.monotonic()
error = helper.call("paste", params, "PASTE_NOT_CONSUMED")
unread = helper.call("paste", params, "PASTE_NOT_CONSUMED")
elapsed = time.monotonic() - started
assert "foreground" in error["message"] and "focus" in error["message"], error
assert "foreground" in unread["message"] and "focus" in unread["message"], unread
background = wait_state(root / "target.json", lambda state: state["pasteCalls"] == [False])
assert background["text"] == "" and not background["active"], background
helper.call("activate", owner)
@@ -60,11 +60,26 @@ def main():
assert result == {"dispatched": True}, result
foreground = wait_state(root / "target.json", lambda state: state["text"] == "test")
assert foreground["pasteCalls"] == [False, True] and foreground["active"], foreground
for mode in ["before", "after", "duplicate"]:
state_path = root / f"{mode}.json"
receiver = subprocess.Popen([str(binary), str(state_path), f"DSH partial menu {mode}", mode],
stdout=subprocess.PIPE, text=True)
processes.append(receiver)
assert line(receiver).strip() == "READY"
partial = helper.call("getApp", {"selector": str(receiver.pid)})
target = {"appHandle": partial["appHandle"]}
tree = helper.call("getState", {**target, "maxElements": 2000, "maxDepth": 25, "includeMenus": True})
assert tree["hasUnreadableElements"] and any(e["title"] == "Paste" for e in tree["elements"])
error = helper.call("paste", {**params, **target}, "UNSUPPORTED")
reason = "ambiguous" if mode == "duplicate" else "incomplete"
assert reason in error["message"] and "No input was dispatched. Read fresh state" in error["message"], error
state = json.loads(state_path.read_text())
assert state["text"] == "" and state["pasteCalls"] == [], state
if args.artifacts:
args.artifacts.mkdir(parents=True, exist_ok=True)
(args.artifacts / "results.json").write_text(json.dumps({"background": background,
"foreground": foreground, "backgroundElapsedSeconds": elapsed, "error": error}, indent=2) + "\n")
print(f"PASS: background PASTE_NOT_CONSUMED in {elapsed:.3f}s; foreground paste inserted test")
"foreground": foreground, "backgroundElapsedSeconds": elapsed, "error": unread}, indent=2) + "\n")
print(f"PASS: background PASTE_NOT_CONSUMED in {elapsed:.3f}s; foreground paste inserted test; unreadable and ambiguous menus received zero Paste actions")
finally:
if args.artifacts:
args.artifacts.mkdir(parents=True, exist_ok=True)