Skip to content

Commit e446500

Browse files
committed
fix(ios): keep alert activation out of XCTest's interruption handling
Before every event XCTest looks for SpringBoard banners and alerts over the target and runs its interruption handler on them. For a notification banner the default handler waits up to 15 s for it to leave, so an `alert accept` issued while a banner was visible tapped after the command's deadline and answered ALERT_DEADLINE_EXCEEDED for a button it had in fact pressed (#2546's late tap, from the banner side). Four iOS CI failures of the alert replacement tests carry this trace, including main job 106127169259: banner wait from t=7.98 s, tap at t=15.12 s on a ~15 s deadline. `withBoundedInteractionIdleTimeoutIfSupported(.preEventSkipped)` drops only the idle wait. Alert activation now also sets XCUIApplication's `doesNotHandleUIInterruptions` on the alert's owner app for the one tap. It keeps the XCTest coordinate tap and its post-event settle, which alert verification relies on; the synthesized-event path would have dropped that settle too. The runner host app gains a `--agent-device-alert-banner` fixture that keeps a notification banner over its alert until the alert is answered. The new test fails on the old dispatch with the CI signature (interruption monitors consulted, ALERT_DEADLINE_EXCEEDED after the button was activated) and passes with the opt-out.
1 parent c3e3451 commit e446500

5 files changed

Lines changed: 144 additions & 7 deletions

File tree

‎.github/workflows/ios.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ jobs:
193193
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testAlertDeadlineBeforeActivationLeavesTheOriginalUntouched \
194194
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testAlertHittableProbeCompletingAfterDeadlineLeavesTheOriginalUntouched \
195195
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testAlertActivationIgnoresAnAppThatNeverSettlesBeforeTheDeadline \
196+
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testAlertActivationDoesNotWaitOutANotificationBanner \
196197
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testSystemModalProbeSliceSharesAndClampsToPlanDeadline \
197198
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testDispatchRecoverySkipsBookkeepingWhileXCTestChannelOccupied \
198199
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testBoundedSystemModalProbeTimeoutRecoversThenReleasesOnDrain \

‎CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
CI it spent its whole 10 s budget there (`wait_capture_stalled` with `captures: 1`). The probe now
1111
joins the running discovery, bounded by the discovery's own deadline, and then observes the launch
1212
as before. A resolution failure other than a pending discovery still ends the probe at once.
13+
- Fixed (ios): `alert accept` and `alert dismiss` no longer land their tap after the command's
14+
deadline when a notification banner is on screen. XCTest checks for SpringBoard banners and alerts
15+
before every event and its default handler waited up to 15 s for a banner to leave, so the command
16+
answered `ALERT_DEADLINE_EXCEEDED` ("The button was activated once") for a tap that arrived after
17+
the caller was told it had failed (#2546's late tap, from the banner side). Alert activation now
18+
opts out of XCTest's interruption handling, which also stops that handler from pressing a button of
19+
its own choosing on an unrelated system alert while the command answers the alert it resolved.
1320
- Fixed (ios): a local Simulator snapshot taken through the host AX bridge once again publishes the
1421
geometric `hittable` fact, so `is hittable` and a `hittable:` selector resolve the same controls on
1522
the bridge and the XCTest runner. The snapshot capability table has declared `hittable =

‎apple/runner/AgentDeviceRunner/AgentDeviceRunner/AgentDeviceRunnerApp.m‎

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,24 @@ int main(int argc, const char *argv[]) {
5656

5757
#else
5858
#import <UIKit/UIKit.h>
59+
#if TARGET_OS_IOS
60+
#import <UserNotifications/UserNotifications.h>
61+
#endif
5962

6063
@interface AgentDeviceRunnerViewController : UIViewController
6164
@property(nonatomic, strong) UILabel *alertActionStatus;
6265
@property(nonatomic, assign) NSUInteger firstAlertActions;
6366
@property(nonatomic, assign) NSUInteger replacementAlertActions;
6467
@property(nonatomic, assign) BOOL alertFixtureStarted;
6568
@property(nonatomic, strong) NSTimer *alertActivationBusyBackstop;
69+
@property(nonatomic, strong) NSTimer *alertBannerRepost;
6670
@end
6771

72+
#if TARGET_OS_IOS
73+
@interface AgentDeviceRunnerViewController () <UNUserNotificationCenterDelegate>
74+
@end
75+
#endif
76+
6877
@implementation AgentDeviceRunnerViewController
6978

7079
#if TARGET_OS_IOS
@@ -101,6 +110,57 @@ - (void)stopAlertActivationBusy {
101110
self.alertActivationBusyBackstop = nil;
102111
}
103112

113+
// A banner from this app, shown over its own alert and re-posted before the previous one expires so
114+
// one is on screen for as long as the first alert is unanswered. XCTest treats such a banner as an
115+
// interruption of every event aimed at the app (#2546's late tap, from the banner side).
116+
- (void)startAlertBannerThen:(dispatch_block_t)presentAlert {
117+
UNUserNotificationCenter *center = UNUserNotificationCenter.currentNotificationCenter;
118+
center.delegate = self;
119+
[center requestAuthorizationWithOptions:UNAuthorizationOptionAlert
120+
completionHandler:^(BOOL granted, NSError *error) {
121+
(void)error;
122+
if (!granted) {
123+
return;
124+
}
125+
dispatch_async(dispatch_get_main_queue(), ^{
126+
presentAlert();
127+
[self postAlertBanner];
128+
self.alertBannerRepost = [NSTimer scheduledTimerWithTimeInterval:2.0
129+
target:self
130+
selector:@selector(postAlertBanner)
131+
userInfo:nil
132+
repeats:YES];
133+
});
134+
}];
135+
}
136+
137+
- (void)postAlertBanner {
138+
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
139+
content.title = @"Agent Device banner";
140+
content.body = @"Shown over the alert fixture";
141+
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:NSUUID.UUID.UUIDString
142+
content:content
143+
trigger:nil];
144+
[UNUserNotificationCenter.currentNotificationCenter addNotificationRequest:request withCompletionHandler:nil];
145+
}
146+
147+
- (void)stopAlertBanner {
148+
if (self.alertBannerRepost == nil) {
149+
return;
150+
}
151+
[self.alertBannerRepost invalidate];
152+
self.alertBannerRepost = nil;
153+
[UNUserNotificationCenter.currentNotificationCenter removeAllDeliveredNotifications];
154+
}
155+
156+
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
157+
willPresentNotification:(UNNotification *)notification
158+
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
159+
(void)center;
160+
(void)notification;
161+
completionHandler(UNNotificationPresentationOptionBanner);
162+
}
163+
104164

105165
- (void)updateAlertActionStatus {
106166
self.alertActionStatus.text = [NSString stringWithFormat:@"First actions: %lu; replacement actions: %lu",
@@ -124,6 +184,7 @@ - (void)presentAlertFixtureReplacement:(BOOL)replacement {
124184
[alert addAction:[UIAlertAction actionWithTitle:buttonTitle style:style handler:^(UIAlertAction *action) {
125185
(void)action;
126186
[self stopAlertActivationBusy];
187+
[self stopAlertBanner];
127188
if (replacement) {
128189
self.replacementAlertActions += 1;
129190
} else {
@@ -145,9 +206,16 @@ - (void)viewDidAppear:(BOOL)animated {
145206
if (!self.alertFixtureStarted &&
146207
[NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-alert-replacement-regression"]) {
147208
self.alertFixtureStarted = YES;
148-
[self presentAlertFixtureReplacement:NO];
149-
if ([NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-alert-activation-busy"]) {
150-
[self startAlertActivationBusy];
209+
dispatch_block_t presentAlert = ^{
210+
[self presentAlertFixtureReplacement:NO];
211+
if ([NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-alert-activation-busy"]) {
212+
[self startAlertActivationBusy];
213+
}
214+
};
215+
if ([NSProcessInfo.processInfo.arguments containsObject:@"--agent-device-alert-banner"]) {
216+
[self startAlertBannerThen:presentAlert];
217+
} else {
218+
presentAlert();
151219
}
152220
}
153221
}

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,17 @@ extension RunnerTests {
7676
buttonFrame.origin.x, buttonFrame.origin.y, buttonFrame.size.width, buttonFrame.size.height,
7777
buttonFrame.midX, buttonFrame.midY
7878
)
79-
// The hittable read above is this activation's readiness gate, so XCTest's pre-synthesis wait
80-
// adds nothing and can cost more than the command has: the tap would land after the deadline
79+
// The hittable read above is this activation's readiness gate, so XCTest's pre-synthesis waits
80+
// add nothing and can cost more than the command has: the tap would land after the deadline
8181
// expired and the alert would be answered by a button the caller was told nothing about
8282
// (#2546). The post-tap settle stays, because the verification below reads the alert this tap
8383
// replaces; an alert that dismisses and presents an identical replacement passes through a
8484
// window with no alert, and a first read landing there reports a dismissal nothing proved.
8585
var outcome = RunnerInteractionOutcome.performed
86-
withBoundedInteractionIdleTimeoutIfSupported(alert.ownerApp, waits: .preEventSkipped) {
87-
outcome = activateElement(app: alert.ownerApp, element: button, action: "alert \(action)")
86+
withUIInterruptionHandlingDisabledIfSupported(alert.ownerApp) {
87+
withBoundedInteractionIdleTimeoutIfSupported(alert.ownerApp, waits: .preEventSkipped) {
88+
outcome = activateElement(app: alert.ownerApp, element: button, action: "alert \(action)")
89+
}
8890
}
8991
if let response = unsupportedResponse(for: outcome) {
9092
return response
@@ -114,6 +116,22 @@ extension RunnerTests {
114116
)
115117
}
116118

119+
/// Before each event XCTest looks for SpringBoard elements over the target and hands them to its
120+
/// interruption handler, which waits up to 15 s for a notification banner to leave and taps a
121+
/// button of its own choosing on any other alert. An alert command answers exactly the alert it
122+
/// resolved, with the button it chose, before its deadline, so it opts out of both.
123+
private func withUIInterruptionHandlingDisabledIfSupported(_ target: XCUIApplication, operation: () -> Void) {
124+
let key = "doesNotHandleUIInterruptions"
125+
guard target.responds(to: NSSelectorFromString("setDoesNotHandleUIInterruptions:")) else {
126+
operation()
127+
return
128+
}
129+
let previous = target.value(forKey: key) as? NSNumber
130+
target.setValue(true, forKey: key)
131+
defer { target.setValue(previous?.boolValue ?? false, forKey: key) }
132+
operation()
133+
}
134+
117135
private func runnerAlert(_ modal: ResolvedBlockingSystemModal) -> RunnerAlert? {
118136
let buttons = modal.actions.filter { isEnabledElement($0) }
119137
guard !buttons.isEmpty else {

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,49 @@ extension RunnerTests {
109109
}
110110
}
111111

112+
func testAlertActivationDoesNotWaitOutANotificationBanner() throws {
113+
app.launchArguments = ["--agent-device-alert-replacement-regression", "--agent-device-alert-banner"]
114+
app.launch()
115+
let banner = XCUIApplication(bundleIdentifier: "com.apple.springboard")
116+
.descendants(matching: .any)["NotificationShortLookView"]
117+
var consultedInterruptions: [String] = []
118+
let monitor = addUIInterruptionMonitor(withDescription: "alert activation banner") { element in
119+
consultedInterruptions.append(element.identifier)
120+
return false
121+
}
122+
defer {
123+
removeUIInterruptionMonitor(monitor)
124+
invalidateCachedTarget(reason: "unit_test_cleanup")
125+
app.terminate()
126+
_ = banner.waitForNonExistence(timeout: 15)
127+
}
128+
acceptNotificationAuthorizationUntilAlertAppears()
129+
XCTAssertTrue(banner.waitForExistence(timeout: appExistenceTimeout), "the fixture keeps a banner up")
130+
let alert = try XCTUnwrap(resolveAlert(app: app, deadline: Date().addingTimeInterval(30)))
131+
132+
let response = handleAlert(alert, action: "accept", deadline: Date().addingTimeInterval(30))
133+
134+
XCTAssertEqual(consultedInterruptions, [], "alert activation waited on XCTest's interruption handling")
135+
XCTAssertTrue(response.ok, String(describing: response.error))
136+
XCTAssertEqual(app.staticTexts["agent-device-alert-actions"].label, "First actions: 1; replacement actions: 0")
137+
}
138+
139+
/// The banner fixture presents its alert only once this app may post notifications; a fresh
140+
/// simulator asks first, through SpringBoard.
141+
private func acceptNotificationAuthorizationUntilAlertAppears() {
142+
let allow = XCUIApplication(bundleIdentifier: "com.apple.springboard").alerts.buttons["Allow"]
143+
let fixtureAlert = app.alerts.firstMatch
144+
let deadline = Date().addingTimeInterval(appExistenceTimeout)
145+
while Date() < deadline, !fixtureAlert.exists {
146+
if allow.exists {
147+
allow.tap()
148+
} else {
149+
Thread.sleep(forTimeInterval: 0.25)
150+
}
151+
}
152+
XCTAssertTrue(fixtureAlert.exists, "the banner fixture needs notification authorization before it presents its alert")
153+
}
154+
112155
/// The accessibility round trips `resolveAlert` spends before a button is chosen, decomposed from
113156
/// the implementation rather than counted off one trace: the blocking-modal probe scans
114157
/// SpringBoard's alert and sheet lists and re-reads the candidate it settles on; the app's own

0 commit comments

Comments
 (0)