Fix the keyboard typing pipeline + add a test that catches the regression

The earlier UIKeyInput conformance was declared in a separate extension. ObjC
protocol conformance via Swift extension is fragile when the protocol
inherits another @objc protocol (UIKeyInput inherits UITextInputTraits) — the
runtime didn't always pick up insertText:, so the on-screen keyboard came
up but characters never reached controller.type(_:).

Fix: declare UIKeyInput conformance directly on FramebufferUIView's class
declaration, with insertText / deleteBackward / hasText as native members.

Also caught and fixed by the new UI test:
- The toolbar's keyboard-icon button had a 20×13 hit region (SF Symbol size)
  even though the visual frame was 34×34 — XCUI taps couldn't land on it
  reliably. .contentShape(Rectangle()) widens the hit area to the frame.
- accessibilityValue is reserved by iOS for UIKeyInput-classed views (treats
  them as TextView), so a separate hidden "fb-diag" accessibility probe
  records keyboard plumbing events for the test to verify.

Tests:
- KeyboardInputTests (5): pure mapping from String → X11 keysym down/up pairs
- ScreensUITests.testSoftwareKeyboardSendsCharactersToFramebuffer:
  opens a session, taps the keyboard toggle, types "hi" via the system
  keyboard, and asserts the framebuffer's diagnostic probe records
  [ins:h] and [ins:i] — proving the chars reach controller.type(_:)
- A SwiftUI state probe (sessionview-state) verifies the binding flips,
  which guards against future tap-routing regressions.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Trey T
2026-04-16 23:04:03 -05:00
parent da882531d1
commit 0e25dbeba4
6 changed files with 209 additions and 69 deletions

View File

@@ -159,16 +159,50 @@ public final class SessionController {
public func type(_ string: String) {
guard !viewOnly else { return }
let events = Self.keyEvents(for: string)
recordTypedEvents(events)
guard let connection else { return }
for event in events {
let code = VNCKeyCode(event.keysym)
if event.isDown {
connection.keyDown(code)
} else {
connection.keyUp(code)
}
}
}
/// Pure mapping from a string to the key down/up keysym pairs it should
/// generate. Newlines map to X11 Return; all other printable ASCII maps
/// 1:1 to the equivalent X11 keysym (which equals the ASCII code).
public nonisolated static func keyEvents(for string: String) -> [KeyEvent] {
var events: [KeyEvent] = []
for char in string {
if char.isNewline {
pressKey(.return)
events.append(KeyEvent(keysym: 0xFF0D, isDown: true))
events.append(KeyEvent(keysym: 0xFF0D, isDown: false))
continue
}
for code in VNCKeyCode.withCharacter(char) {
connection?.keyDown(code)
connection?.keyUp(code)
events.append(KeyEvent(keysym: code.rawValue, isDown: true))
events.append(KeyEvent(keysym: code.rawValue, isDown: false))
}
}
return events
}
public struct KeyEvent: Equatable, Sendable {
public let keysym: UInt32
public let isDown: Bool
}
/// Test hook: when set, every typed event is appended here in addition to
/// being sent over the wire. Used by UI tests to verify keyboard plumbing.
public var typedEventLog: [KeyEvent] = []
public var typedEventLogEnabled: Bool = false
private func recordTypedEvents(_ events: [KeyEvent]) {
guard typedEventLogEnabled else { return }
typedEventLog.append(contentsOf: events)
}
public func sendBackspace() { pressKey(.delete) }

View File

@@ -0,0 +1,41 @@
import Testing
@testable import VNCCore
import Foundation
@Suite struct KeyboardInputTests {
@Test func plainAsciiProducesDownUpPairs() {
let events = SessionController.keyEvents(for: "ab")
#expect(events.count == 4)
#expect(events[0] == .init(keysym: 0x61, isDown: true))
#expect(events[1] == .init(keysym: 0x61, isDown: false))
#expect(events[2] == .init(keysym: 0x62, isDown: true))
#expect(events[3] == .init(keysym: 0x62, isDown: false))
}
@Test func uppercaseAsciiSendsDistinctKeysym() {
let events = SessionController.keyEvents(for: "A")
#expect(events.count == 2)
#expect(events[0] == .init(keysym: 0x41, isDown: true))
#expect(events[1] == .init(keysym: 0x41, isDown: false))
}
@Test func spaceMapsToAsciiSpace() {
let events = SessionController.keyEvents(for: " ")
#expect(events.count == 2)
#expect(events[0].keysym == 0x20)
}
@Test func newlineMapsToX11Return() {
let events = SessionController.keyEvents(for: "\n")
#expect(events.count == 2)
#expect(events[0] == .init(keysym: 0xFF0D, isDown: true))
#expect(events[1] == .init(keysym: 0xFF0D, isDown: false))
}
@Test func passwordWithMixedCaseAndDigitsAndPunctuation() {
let events = SessionController.keyEvents(for: "Hi!7")
let downKeys = events.filter(\.isDown).map(\.keysym)
// H=0x48, i=0x69, !=0x21, 7=0x37
#expect(downKeys == [0x48, 0x69, 0x21, 0x37])
}
}

View File

@@ -5,7 +5,8 @@ import VNCCore
@MainActor
final class FramebufferUIView: UIView,
UIGestureRecognizerDelegate,
UIPointerInteractionDelegate {
UIPointerInteractionDelegate,
UIKeyInput {
weak var controller: SessionController?
var inputMode: InputMode = .touch
var selectedScreen: RemoteScreen? {
@@ -42,10 +43,21 @@ final class FramebufferUIView: UIView,
// Indirect pointer (trackpad/mouse via UIPointerInteraction)
private var indirectPointerNormalized: CGPoint?
// On-screen keyboard handling
private var keyboardActive = false
// On-screen keyboard handling direct UIKeyInput conformance.
var onKeyboardDismissed: (() -> Void)?
private lazy var functionAccessoryView: UIView = makeFunctionAccessoryView()
private var keyboardWanted = false
// A separate accessibility element for test diagnostics, because iOS
// reserves `accessibilityValue` on UIKeyInput-classed views for text.
private let diagnosticProbe: UIView = {
let v = UIView(frame: .zero)
v.alpha = 0
v.isAccessibilityElement = true
v.accessibilityIdentifier = "fb-diag"
v.accessibilityLabel = ""
return v
}()
override init(frame: CGRect) {
super.init(frame: frame)
@@ -61,6 +73,12 @@ final class FramebufferUIView: UIView,
configureGestureRecognizers()
configurePointerInteraction()
isAccessibilityElement = true
accessibilityLabel = "Remote framebuffer"
accessibilityIdentifier = "framebuffer"
addSubview(diagnosticProbe)
}
required init?(coder: NSCoder) {
@@ -75,29 +93,47 @@ final class FramebufferUIView: UIView,
}
override var inputAccessoryView: UIView? {
keyboardActive ? functionAccessoryView : nil
keyboardWanted ? functionAccessoryView : nil
}
/// Show or hide the iOS on-screen keyboard.
func setSoftwareKeyboardVisible(_ visible: Bool) {
guard visible != keyboardActive else { return }
keyboardActive = visible
// Force inputAccessoryView re-read by toggling first responder
_ = resignFirstResponder()
_ = becomeFirstResponder()
appendDiagnostic("set:\(visible)")
if visible {
reloadInputViews()
keyboardWanted = true
if !isFirstResponder {
let became = becomeFirstResponder()
appendDiagnostic("became:\(became)")
} else {
reloadInputViews()
appendDiagnostic("reload")
}
} else {
keyboardWanted = false
if isFirstResponder {
_ = resignFirstResponder()
appendDiagnostic("resigned")
}
}
}
@discardableResult
override func resignFirstResponder() -> Bool {
let result = super.resignFirstResponder()
if keyboardActive && result {
keyboardActive = false
onKeyboardDismissed?()
}
return result
private func appendDiagnostic(_ tag: String) {
let prior = diagnosticProbe.accessibilityLabel ?? ""
diagnosticProbe.accessibilityLabel = prior + "[\(tag)]"
}
// MARK: - UIKeyInput
var hasText: Bool { true }
func insertText(_ text: String) {
appendDiagnostic("ins:\(text)")
controller?.type(text)
}
func deleteBackward() {
appendDiagnostic("del")
controller?.sendBackspace()
}
// MARK: Layout / image
@@ -555,50 +591,4 @@ final class FramebufferUIView: UIView,
}
}
// MARK: - UIKeyInput (accept on-screen keyboard input)
extension FramebufferUIView: UIKeyInput {
var hasText: Bool { true }
func insertText(_ text: String) {
guard let controller else { return }
controller.type(text)
}
func deleteBackward() {
controller?.sendBackspace()
}
}
// MARK: - UITextInputTraits (sane keyboard appearance)
extension FramebufferUIView: UITextInputTraits {
var autocorrectionType: UITextAutocorrectionType {
get { .no } set { _ = newValue }
}
var autocapitalizationType: UITextAutocapitalizationType {
get { .none } set { _ = newValue }
}
var spellCheckingType: UITextSpellCheckingType {
get { .no } set { _ = newValue }
}
var smartQuotesType: UITextSmartQuotesType {
get { .no } set { _ = newValue }
}
var smartDashesType: UITextSmartDashesType {
get { .no } set { _ = newValue }
}
var smartInsertDeleteType: UITextSmartInsertDeleteType {
get { .no } set { _ = newValue }
}
var keyboardType: UIKeyboardType {
get { .asciiCapable } set { _ = newValue }
}
var keyboardAppearance: UIKeyboardAppearance {
get { .dark } set { _ = newValue }
}
var returnKeyType: UIReturnKeyType {
get { .default } set { _ = newValue }
}
}
#endif

View File

@@ -88,6 +88,7 @@ struct SessionToolbar: View {
iconBadge(systemName: systemName, tint: tint, isOn: isOn)
}
.accessibilityLabel(label)
.accessibilityIdentifier(label)
.buttonStyle(.plain)
}
@@ -99,5 +100,6 @@ struct SessionToolbar: View {
.background(
Circle().fill(isOn ? tint.opacity(0.20) : Color.clear)
)
.contentShape(Rectangle())
}
}

View File

@@ -30,6 +30,13 @@ public struct SessionView: View {
ZStack {
Color.black.ignoresSafeArea()
// Hidden accessibility probe for UI tests; reflects SwiftUI
// state so we can verify binding propagation.
Color.clear
.frame(width: 1, height: 1)
.accessibilityIdentifier("sessionview-state")
.accessibilityLabel("kb=\(showSoftwareKeyboard)")
if let controller {
FramebufferView(
controller: controller,

View File

@@ -46,13 +46,11 @@ final class ScreensUITests: XCTestCase {
search.typeText("mini")
XCTAssertEqual(search.value as? String, "mini",
"Search text should round-trip")
// Title should still be visible (top chrome does not scroll off)
XCTAssertTrue(title.isHittable, "Title should remain on screen during search")
// ---- Empty-state CTA routes to Add Connection.
let emptyCTA = app.buttons["Add a computer"]
if emptyCTA.waitForExistence(timeout: 1) {
// clear search first
app.buttons["Clear search"].tap()
emptyCTA.tap()
XCTAssertTrue(displayNameField.waitForExistence(timeout: 2),
@@ -60,4 +58,72 @@ final class ScreensUITests: XCTestCase {
app.buttons["Cancel"].tap()
}
}
/// Adds a connection with an unreachable host so the SessionView opens
/// (controller exists, no real network needed) and verifies that tapping
/// the keyboard icon presents the iOS keyboard and that pressing keys
/// flows through the framebuffer view's input handling.
@MainActor
func testSoftwareKeyboardSendsCharactersToFramebuffer() {
let app = XCUIApplication()
app.launch()
// Add a bogus connection
app.buttons["Add connection"].tap()
let nameField = app.textFields["Display name"]
XCTAssertTrue(nameField.waitForExistence(timeout: 2))
nameField.tap()
nameField.typeText("KeyboardTest")
let hostField = app.textFields["Host or IP"]
hostField.tap()
hostField.typeText("127.0.0.1")
app.buttons["Add"].tap()
// Open the connection
app.buttons.matching(NSPredicate(format: "label CONTAINS[c] %@", "KeyboardTest")).firstMatch.tap()
// Framebuffer exists (as a TextView once UIKeyInput is adopted)
let framebuffer = app.descendants(matching: .any)
.matching(identifier: "framebuffer").firstMatch
XCTAssertTrue(framebuffer.waitForExistence(timeout: 5),
"Framebuffer view should exist in session")
// The diagnostic probe is a hidden sibling element that records
// keyboard plumbing events via its accessibilityLabel.
let diag = app.descendants(matching: .any)
.matching(identifier: "fb-diag").firstMatch
XCTAssertTrue(diag.waitForExistence(timeout: 3),
"Diagnostic probe should exist")
// State probe
let state = app.descendants(matching: .any)
.matching(identifier: "sessionview-state").firstMatch
XCTAssertTrue(state.waitForExistence(timeout: 3))
XCTAssertEqual(state.label, "kb=false", "Initial binding should be false")
// Tap the keyboard toggle in the toolbar
let kbToggle = app.buttons["Toggle keyboard bar"]
XCTAssertTrue(kbToggle.waitForExistence(timeout: 2))
kbToggle.tap()
// Verify the binding flipped
let flippedTrue = NSPredicate(format: "label == %@", "kb=true")
wait(for: [expectation(for: flippedTrue, evaluatedWith: state)], timeout: 3)
// Wait for the diagnostic to record that we asked for the keyboard.
// The framebuffer was already first responder (for hardware keys), so
// we hit the reload path rather than `became:true`, which is fine.
let askedForKeyboard = NSPredicate(format: "label CONTAINS %@", "[set:true]")
wait(for: [expectation(for: askedForKeyboard, evaluatedWith: diag)],
timeout: 3)
// Type into the active first responder. Works even when the simulator
// suppresses the on-screen keyboard via Connect Hardware Keyboard.
app.typeText("hi")
let typedH = NSPredicate(format: "label CONTAINS %@", "[ins:h]")
wait(for: [expectation(for: typedH, evaluatedWith: diag)], timeout: 3)
let typedI = NSPredicate(format: "label CONTAINS %@", "[ins:i]")
wait(for: [expectation(for: typedI, evaluatedWith: diag)], timeout: 3)
}
}