| 1 | import AppKit |
| 2 | import WebKit |
| 3 | |
| 4 | private final class RecordingWebView: WKWebView { |
| 5 | var scripts: [String] = [] |
| 6 | override func evaluateJavaScript(_ javaScriptString: String, completionHandler: (@MainActor @Sendable (Any?, Error?) -> Void)? = nil) { |
| 7 | scripts.append(javaScriptString) |
| 8 | completionHandler?(nil, nil) |
| 9 | } |
| 10 | } |
| 11 | |
| 12 | @main |
| 13 | enum NativeInputTests { |
| 14 | static func main() { |
| 15 | for number in 1...20 { |
| 16 | let name = "F\(number)" |
| 17 | let code = MacKeyboard.keyCodes[name]! |
| 18 | let identity = MacKeyboard.identity(code, characters: String(UnicodeScalar(0xf703 + number)!)) |
| 19 | precondition(identity.key == name && identity.code == name, "function key became private-use text") |
| 20 | } |
| 21 | precondition(MacKeyboard.identity(83, characters: "1").code == "Numpad1") |
| 22 | precondition(MacKeyboard.identity(76, characters: "\r").key == "Enter") |
| 23 | precondition(MacKeyboard.virtualKey(code: "KeyW", key: "z", shortcut: true) == 6, "logical shortcut followed a foreign keyboard layout") |
| 24 | let app = NSApplication.shared |
| 25 | app.setActivationPolicy(.prohibited) |
| 26 | let window = NSWindow(contentRect: NSRect(x: 120, y: 180, width: 900, height: 700), |
| 27 | styleMask: [.titled, .resizable], backing: .buffered, defer: false) |
| 28 | let web = RecordingWebView(frame: window.contentView!.bounds) |
| 29 | window.contentView!.addSubview(web) |
| 30 | let input = CageNativeInput(frame: .zero) |
| 31 | input.configure(web: web) |
| 32 | window.contentView!.addSubview(input) |
| 33 | let owner = UUID().uuidString |
| 34 | func check(_ x: Double, _ y: Double, _ height: Double) -> NSRect { |
| 35 | precondition(input.handle(["operation":"activate", "owner":owner, |
| 36 | "x":x, "y":y, "height":height])) |
| 37 | let screen = input.firstRect(forCharacterRange: NSRange(location: 0, length: 0), actualRange: nil) |
| 38 | let top = window.convertToScreen(web.convert(NSRect(x: x, y: 0, width: 0, height: 0), to: nil)).minY |
| 39 | |
| 40 | precondition(abs(screen.minX - (window.convertToScreen(web.convert(.zero, to:nil)).minX + x)) < 0.5) |
| 41 | precondition(abs(screen.maxY - (top - y)) < 0.5, "candidate caret was vertically mirrored") |
| 42 | precondition(abs(screen.height - height) < 0.5) |
| 43 | return screen |
| 44 | } |
| 45 | precondition(web.isFlipped, "WKWebView coordinate contract changed") |
| 46 | let upper = check(120, 80, 24) |
| 47 | let lower = check(340, 580, 32) |
| 48 | precondition(lower.minY < upper.minY, "moving the caret down must move candidates down") |
| 49 | window.setFrameOrigin(NSPoint(x: 320, y: 280)) |
| 50 | let moved = check(340, 580, 32) |
| 51 | precondition(abs(moved.minX - lower.minX - 200) < 0.5) |
| 52 | precondition(abs(moved.minY - lower.minY - 100) < 0.5) |
| 53 | web.setFrameSize(NSSize(width: 1000, height: 800)) |
| 54 | _ = check(500, 680, 38) |
| 55 | for number in 1...20 { |
| 56 | let code = MacKeyboard.keyCodes["F\(number)"]! |
| 57 | let character = String(UnicodeScalar(0xf703 + number)!) |
| 58 | let event = NSEvent.keyEvent(with: .keyDown, location: .zero, modifierFlags: [], timestamp: 0, |
| 59 | windowNumber: window.windowNumber, context: nil, characters: character, |
| 60 | charactersIgnoringModifiers: character, isARepeat: false, keyCode: code)! |
| 61 | web.scripts.removeAll() |
| 62 | input.keyDown(with: event) |
| 63 | input.keyUp(with: event) |
| 64 | precondition(web.scripts.count == 2 && web.scripts.allSatisfy { $0.contains("\"code\":\"F\(number)\"") }, |
| 65 | "function key was consumed by host text interpretation") |
| 66 | } |
| 67 | precondition(!input.handle(["operation":"deactivate", "owner":UUID().uuidString])) |
| 68 | precondition(input.handle(["operation":"deactivate", "owner":owner])) |
| 69 | print("Native input F1-F20 down/up, coordinates, window movement, resize, and stale owner checks passed") |
| 70 | } |
| 71 | } |