Skip to content

test(ios): detach the simulator's hardware keyboard before checking the keyboard inset - #1847

Closed
houko wants to merge 1 commit into
mainfrom
fix/ios-keyboard-inset-test
Closed

houko wants to merge 1 commit into
mainfrom
fix/ios-keyboard-inset-test

Conversation

@houko

@houko houko commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

Root cause

TestSceneKeyboardInsetFollowsFocus fails on every apple-simulator run with focus in scene 1 gave heights [0 0]. It passed on #1168 because the iPad invocation then held only the four scene tests. bec2c94 added TestTitledKeyCommandsUseUIKit to the same invocation, and it runs first (keycommands_test.go sorts before scene_test.go).

That test presses keys, which on a simulator are IOHID keyboard events enqueued through -[UIApplication _enqueueHIDEvent:]. Once UIKit takes one, the simulator counts a hardware keyboard as connected (UIKeyboardImpl.hardwareKeyboardAttached and isInHardwareKeyboardMode are true, HardwareKeyboardLastSeen is written to com.apple.keyboard.preferences), for as long as the simulator runs and in every app launched after. Focusing a field then shows no software keyboard, so KeyboardHeight stays 0 in both scenes. The engine is right: with a hardware keyboard there is no inset.

Evidence on the Mac Studio (Xcode 27.1, iOS 27.0, iPad Pro 13-inch (M5)), each from a freshly erased simulator:

  • the inset test alone: passes
  • TestTitledKeyCommandsUseUIKit then the inset test, on main: fails with [0 0], same as CI
  • after that, the inset test alone on the same booted simulator also fails, until the state is reset

Setting UIKit's flag (-[UIKeyboardImpl setHardWareKeyboardAttached:NO]) is not enough: UIKit reads the system's state again as a scene activates, and the run still fails.

Fix

A test-only ios.DetachHardwareKeyboardForTest calls GraphicsServices' GSEventSetHardwareKeyboardAttached(false, 0) on simulators, putting the simulator back to the no-hardware-keyboard state it boots with, and does nothing on a physical device. TestSceneKeyboardInsetFollowsFocus calls it before focusing the fields, so it no longer depends on which tests ran before it. The assertion is unchanged.

Verification

On the Mac Studio, freshly erased iPad Pro 13-inch (M5) on iOS 27.0:

  • TestTitledKeyCommandsUseUIKit + TestSceneKeyboardInsetFollowsFocus: fails on main, passes with this change
  • CI's full iPad invocation (TestScenesShowTreesOfTheirOwn|TestSceneDisconnectsWhileTheOtherRuns|TestSceneStatusBarAndOrientationsArePerScene|TestSceneKeyboardInsetFollowsFocus|TestTitledKeyCommandsUseUIKit): 5 passed

Screenshots

None.

…he keyboard inset

TestSceneKeyboardInsetFollowsFocus failed with heights [0 0] on every apple-simulator run since bec2c94 added TestTitledKeyCommandsUseUIKit to the same iPad invocation. That test runs first and presses keys as IOHID keyboard events; once UIKit takes one, the simulator counts a hardware keyboard as connected (UIKeyboardImpl hardwareKeyboardAttached, HardwareKeyboardLastSeen in com.apple.keyboard.preferences) for as long as it runs, so focusing a field shows no software keyboard and the inset stays 0 in both scenes.

The test now tells GraphicsServices no hardware keyboard is attached, as on a newly booted simulator, before it focuses the fields. Setting UIKit's own flag is not enough: UIKit reads the system's state again as a scene activates.
@houko
houko enabled auto-merge (rebase) November 22, 2026 01:45
@houko

houko commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator Author

Reviewed. The root cause is well supported: the failure only appears once TestTitledKeyCommandsUseUIKit runs first in the same simulator, it persists on that booted simulator, and it goes away on a fresh erase. That explains why #1168 passed and why every run since bec2c94 fails. The engine's answer of "no inset with a hardware keyboard" is the right one, so fixing the test's environment rather than the engine is correct.

  • The private GraphicsServices call lives in scene_export_test.go, so it is compiled only into tests and never ships in apps.
  • It is gated on runningOnSimulator() and runs on the main thread.
  • The assertion itself is unchanged.

No changes requested.

@houko

houko commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator Author

Superseded by #1874, which stacks this fix with the other two main fixes (#1797, #1815, #1847) so that one PR can pass every required check.

@houko houko closed this Nov 22, 2026
auto-merge was automatically disabled November 22, 2026 03:13

Pull request was closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant