Skip to content

Commit cc3b99b

Browse files
fix(ios): bound the idle wait of keyboard dismiss and keyboard enter (#4454)
* fix(ios): bound the idle wait of keyboard dismiss and keyboard enter `keyboard dismiss` and `keyboard enter` tapped the keyboard's control with a plain XCUIElement.tap() outside withBoundedInteractionIdleTimeoutIfSupported, which `type`, `swipe` and taps already use. XCTest then waited for the app to idle with its own timeout. An app that keeps animating (Safari right after an iPhone Duo unfold) held the runner main thread past its 30 s watchdog: the command failed with "main thread execution timed out" and the next commands were refused until XCTest gave up. Both commands now run inside the bounded wait with both quiescence waits skipped. dismissKeyboard still waits for the keyboard to go and the screen to settle on its own. The host app's --agent-device-keyboard-busy fixture keeps a UIView animation in flight while its field has focus, until a backstop, and records whether its Done accessory and its return key landed while the app was still busy. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(ios): require busy keyboard coverage and settle Return * docs(ios): restore Simulator keyboard preference after tests --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 9e4b5a4 commit cc3b99b

4 files changed

Lines changed: 192 additions & 2 deletions

File tree

‎apple/runner/AgentDeviceRunner/AgentDeviceRunner/AgentDeviceRunnerAppFixtures.m‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ @interface AgentDeviceRunnerFixtureViewController () <UNUserNotificationCenterDe
7575
@property(nonatomic, assign) BOOL alertFixtureStarted;
7676
@property(nonatomic, strong) NSTimer *alertActivationBusyBackstop;
7777
@property(nonatomic, strong) NSTimer *alertBannerRepost;
78+
@property(nonatomic, weak) UITextField *keyboardBusyField;
79+
@property(nonatomic, strong) UILabel *keyboardBusyAnswer;
80+
@property(nonatomic, strong) NSTimer *keyboardBusyBackstop;
7881
@end
7982

8083
// A one-time-code field that announces how many digits it holds instead of the digits, the way
@@ -251,6 +254,57 @@ - (void)stopAlertActivationBusy {
251254
self.alertActivationBusyBackstop = nil;
252255
}
253256

257+
// The keyboard counterpart of the busy alert: the field starts the same never-ending UIView animation
258+
// when it takes focus, so XCTest sees work in flight for as long as the keyboard is up. Its Done
259+
// accessory and its return key record whether they landed while the app was still busy. The backstop
260+
// passed after `--agent-device-keyboard-busy` ends the busy state, so a command that waits for the app
261+
// to idle before it taps still finishes, and records "after busy" instead of outlasting the test.
262+
static NSTimeInterval AgentDeviceKeyboardBusyWindow(void) {
263+
NSArray<NSString *> *arguments = NSProcessInfo.processInfo.arguments;
264+
NSUInteger flag = [arguments indexOfObject:@"--agent-device-keyboard-busy"];
265+
return flag + 1 < arguments.count ? arguments[flag + 1].doubleValue : 0;
266+
}
267+
268+
- (void)startKeyboardBusy {
269+
if (self.keyboardBusyBackstop != nil) {
270+
return;
271+
}
272+
self.keyboardBusyBackstop = [NSTimer scheduledTimerWithTimeInterval:AgentDeviceKeyboardBusyWindow()
273+
target:self
274+
selector:@selector(stopKeyboardBusy)
275+
userInfo:nil
276+
repeats:NO];
277+
[UIView animateWithDuration:0.4
278+
delay:0
279+
options:(UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse)
280+
animations:^{
281+
self.keyboardBusyAnswer.transform = CGAffineTransformMakeTranslation(0, 8);
282+
}
283+
completion:nil];
284+
}
285+
286+
- (void)stopKeyboardBusy {
287+
[self.keyboardBusyAnswer.layer removeAllAnimations];
288+
self.keyboardBusyAnswer.transform = CGAffineTransformIdentity;
289+
[self.keyboardBusyBackstop invalidate];
290+
self.keyboardBusyBackstop = nil;
291+
}
292+
293+
- (void)recordKeyboardBusyAnswer:(NSString *)control {
294+
self.keyboardBusyAnswer.text = [NSString
295+
stringWithFormat:@"%@ %@", control, self.keyboardBusyBackstop != nil ? @"while busy" : @"after busy"];
296+
[self stopKeyboardBusy];
297+
[self.keyboardBusyField resignFirstResponder];
298+
}
299+
300+
- (void)keyboardBusyDonePressed {
301+
[self recordKeyboardBusyAnswer:@"Done"];
302+
}
303+
304+
- (void)keyboardBusyReturnPressed {
305+
[self recordKeyboardBusyAnswer:@"Return"];
306+
}
307+
254308
// A banner from this app, shown over its own alert and re-posted before the previous one expires so
255309
// one is on screen for as long as the first alert is unanswered. XCTest treats such a banner as an
256310
// interruption of every event aimed at the app (#2546's late tap, from the banner side).
@@ -652,6 +706,43 @@ - (void)viewDidLoad {
652706
self.alertActivationBusyAnswer = busyAnswer;
653707
}
654708

709+
if ([NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-keyboard-busy"]) {
710+
UITextField *field = [[UITextField alloc] init];
711+
field.accessibilityIdentifier = @"agent-device-keyboard-busy-input";
712+
field.placeholder = @"Busy while editing";
713+
field.borderStyle = UITextBorderStyleRoundedRect;
714+
UIToolbar *accessory = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
715+
accessory.items = @[
716+
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
717+
[[UIBarButtonItem alloc] initWithTitle:@"Done"
718+
style:UIBarButtonItemStyleDone
719+
target:self
720+
action:@selector(keyboardBusyDonePressed)],
721+
];
722+
field.inputAccessoryView = accessory;
723+
[field addTarget:self action:@selector(startKeyboardBusy) forControlEvents:UIControlEventEditingDidBegin];
724+
[field addTarget:self
725+
action:@selector(keyboardBusyReturnPressed)
726+
forControlEvents:UIControlEventEditingDidEndOnExit];
727+
field.translatesAutoresizingMaskIntoConstraints = NO;
728+
[self.view addSubview:field];
729+
UILabel *answer = [[UILabel alloc] init];
730+
answer.text = @"Unanswered";
731+
answer.accessibilityIdentifier = @"agent-device-keyboard-busy-answer";
732+
answer.translatesAutoresizingMaskIntoConstraints = NO;
733+
[self.view addSubview:answer];
734+
[NSLayoutConstraint activateConstraints:@[
735+
[field.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
736+
[field.topAnchor constraintEqualToAnchor:label.bottomAnchor constant:24],
737+
[field.widthAnchor constraintEqualToConstant:240],
738+
[field.heightAnchor constraintEqualToConstant:44],
739+
[answer.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
740+
[answer.topAnchor constraintEqualToAnchor:field.bottomAnchor constant:12],
741+
]];
742+
self.keyboardBusyField = field;
743+
self.keyboardBusyAnswer = answer;
744+
}
745+
655746
if ([NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-native-states"]) {
656747
for (NSUInteger index = 0; index < 3; index++) {
657748
UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(40, 120 + index * 60, 260, 44)];

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+CommandExecution.swift‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,13 @@ extension RunnerTests {
587587
}
588588
return Response(ok: true, data: DataPayload(message: command.command.rawValue))
589589
case .keyboardDismiss:
590-
let result = dismissKeyboard(app: activeApp)
590+
var dismissal: (wasVisible: Bool, dismissed: Bool, visible: Bool, mechanism: RunnerKeyboardDismissMechanism?)?
591+
withBoundedInteractionIdleTimeoutIfSupported(activeApp, waits: .bothSkipped) {
592+
dismissal = dismissKeyboard(app: activeApp)
593+
}
594+
guard let result = dismissal else {
595+
return Response(ok: false, error: ErrorPayload(message: "keyboardDismiss produced no result"))
596+
}
591597
if result.wasVisible && !result.dismissed {
592598
return Response(
593599
ok: false,
@@ -610,7 +616,13 @@ extension RunnerTests {
610616
)
611617
)
612618
case .keyboardReturn:
613-
let result = pressKeyboardReturn(app: activeApp)
619+
var press: (wasVisible: Bool, pressed: Bool, visible: Bool)?
620+
withBoundedInteractionIdleTimeoutIfSupported(activeApp, waits: .preEventSkipped) {
621+
press = pressKeyboardReturn(app: activeApp)
622+
}
623+
guard let result = press else {
624+
return Response(ok: false, error: ErrorPayload(message: "keyboardReturn produced no result"))
625+
}
614626
if !result.pressed {
615627
return Response(
616628
ok: false,

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+KeyboardTests.swift‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,68 @@ extension RunnerTests {
327327
private func textEntryInputCount() -> Int {
328328
app.textFields.count + app.secureTextFields.count + app.searchFields.count + app.textViews.count
329329
}
330+
331+
// `keyboard dismiss` and `keyboard enter` tap a keyboard control. An app that never stops animating
332+
// made that tap wait for XCTest's idle timeout, past the runner's main-thread watchdog, so the command
333+
// failed with "main thread execution timed out" and the runner refused the next commands. The fixture
334+
// keeps an animation in flight while its field has focus, until its backstop, and records whether
335+
// each control landed while the app was still busy.
336+
static let keyboardBusyBackstop: TimeInterval = 20
337+
338+
@MainActor
339+
func testKeyboardDismissTapsItsDismissKeyWhileTheAppIsBusy() throws {
340+
try withBusyKeyboardField {
341+
let command = try runnerCommandFixture(#"{"command":"keyboardDismiss","commandId":"dismiss-busy"}"#)
342+
let response = try execute(command: command)
343+
344+
XCTAssertTrue(response.ok, String(describing: response.error))
345+
let answer = app.staticTexts["agent-device-keyboard-busy-answer"].label
346+
XCTAssertEqual(answer, "Done while busy")
347+
NSLog("AGENT_DEVICE_KEYBOARD_BUSY command=dismiss response.ok=%@ answer=%@", response.ok ? "true" : "false", answer)
348+
}
349+
}
350+
351+
@MainActor
352+
func testKeyboardEnterPressesReturnWhileTheAppIsBusy() throws {
353+
try withBusyKeyboardField {
354+
let command = try runnerCommandFixture(#"{"command":"keyboardReturn","commandId":"return-busy"}"#)
355+
let response = try execute(command: command)
356+
357+
XCTAssertTrue(response.ok, String(describing: response.error))
358+
XCTAssertEqual(response.data?.wasVisible, true)
359+
XCTAssertEqual(response.data?.visible, false)
360+
let answer = app.staticTexts["agent-device-keyboard-busy-answer"].label
361+
XCTAssertEqual(answer, "Return while busy")
362+
NSLog("AGENT_DEVICE_KEYBOARD_BUSY command=enter response.ok=%@ visible=%@ answer=%@", response.ok ? "true" : "false", String(describing: response.data?.visible), answer)
363+
}
364+
}
365+
366+
@MainActor
367+
private func withBusyKeyboardField(_ body: () throws -> Void) throws {
368+
app.launchArguments = ["--agent-device-keyboard-busy", String(Self.keyboardBusyBackstop)]
369+
app.launch()
370+
defer {
371+
invalidateCachedTarget(reason: "unit_test_cleanup")
372+
app.terminate()
373+
}
374+
let field = app.textFields["agent-device-keyboard-busy-input"]
375+
XCTAssertTrue(field.waitForFixtureExistence(timeout: appExistenceTimeout))
376+
mainOwned.app = app
377+
mainOwned.bundleId = "com.callstack.agentdevice.runner"
378+
mainOwned.processIdentifier = try XCTUnwrap(Self.processIdentifier(of: app))
379+
// Focus starts the busy animation, so this tap must not wait for the app to idle either.
380+
withBoundedInteractionIdleTimeoutIfSupported(app, waits: .bothSkipped) { field.tap() }
381+
let keyboardDeadline = Date().addingTimeInterval(5)
382+
while !isKeyboardVisible(app: app) && Date() < keyboardDeadline {
383+
sleepFor(0.2)
384+
}
385+
XCTAssertTrue(
386+
isKeyboardVisible(app: app),
387+
"Enable the simulator software keyboard before running the busy keyboard tests; see apple/runner/README.md"
388+
)
389+
XCTAssertEqual(app.staticTexts["agent-device-keyboard-busy-answer"].label, "Unanswered")
390+
try body()
391+
}
330392
#endif
331393
}
332394
#endif

‎apple/runner/README.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,31 @@ Protocol and maintenance references:
5656
- `UnitTests/RunnerTests+<Source>Tests.swift`: the `AGENT_DEVICE_RUNNER_UNIT_TESTS` tests for each
5757
source file. The packaged runner source omits this directory.
5858

59+
### Busy keyboard regression tests
60+
61+
Run the two `testKeyboard*WhileTheAppIsBusy` tests with the simulator software keyboard enabled.
62+
Before you boot a throwaway simulator, save the current hardware-keyboard preference:
63+
64+
```sh
65+
keyboard_preference=$(defaults read com.apple.iphonesimulator ConnectHardwareKeyboard 2>/dev/null) || keyboard_preference=absent
66+
defaults write com.apple.iphonesimulator ConnectHardwareKeyboard -bool NO
67+
```
68+
69+
After the tests, restore the preference in the same shell, including when the tests fail.
70+
If the key was absent, delete it:
71+
72+
```sh
73+
if [ "$keyboard_preference" = absent ]; then
74+
defaults delete com.apple.iphonesimulator ConnectHardwareKeyboard
75+
else
76+
defaults write com.apple.iphonesimulator ConnectHardwareKeyboard -bool "$keyboard_preference"
77+
fi
78+
```
79+
80+
If the software keyboard does not appear, the tests fail instead of skipping.
81+
The tests assert command success and the fixture's `Done while busy` or `Return while busy` label.
82+
The Return test also asserts the keyboard visibility reported after submission.
83+
5984
## Snapshot Strategy
6085

6186
iOS snapshots have two explicit public capture modes:

0 commit comments

Comments
 (0)