Skip to content

Commit c237027

Browse files
authored
fix: guard alert deadline before native tap synthesis (#3113)
* fix: guard alert deadline immediately before tap synthesis * fix: make alert synthesis refusal mapping explicit * fix: restore bounded alert post-event settling * fix: share deadline-aware alert tap synthesis
1 parent 35b7b41 commit c237027

9 files changed

Lines changed: 298 additions & 40 deletions

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerSynthesizedGesture.h‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ NS_ASSUME_NONNULL_BEGIN
55
FOUNDATION_EXPORT NSInteger RunnerControlledScrollFrameCount(double durationMs);
66
FOUNDATION_EXPORT double RunnerControlledScrollProgress(double t);
77

8+
typedef NS_ENUM(NSInteger, RunnerTapSynthesisStatus) {
9+
RunnerTapSynthesisStatusSucceeded,
10+
RunnerTapSynthesisStatusDeadlineExceeded,
11+
RunnerTapSynthesisStatusFailed,
12+
};
13+
814
@interface RunnerSynthesizedGesture : NSObject
915

1016
// `resolvedWindow` is the app window the caller already resolved for the gesture's reference
@@ -26,10 +32,12 @@ FOUNDATION_EXPORT double RunnerControlledScrollProgress(double t);
2632
y2:(double)y2
2733
durationMs:(double)durationMs;
2834

29-
+ (NSString * _Nullable)synthesizeTapWithApplication:(id)application
35+
+ (RunnerTapSynthesisStatus)synthesizeTapWithApplication:(id)application
3036
resolvedWindow:(id _Nullable)resolvedWindow
3137
x:(double)x
32-
y:(double)y;
38+
y:(double)y
39+
deadline:(NSDate * _Nullable)deadline
40+
errorMessage:(NSString * _Nullable * _Nullable)errorMessage;
3341

3442
// Each pointer is an ordered array of { x, y, offsetMs } samples. The first sample
3543
// starts contact; subsequent samples move it; all pointers lift at their final offset.

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerSynthesizedGesture.m‎

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,13 @@ static id RunnerSampledDragPointerPath(
7575
NSString *recordName,
7676
RunnerDragPointerPathFactory pathFactory
7777
);
78-
static NSString * _Nullable RunnerTrySynthesizeTap(id application, id _Nullable resolvedWindow, CGPoint point);
78+
static RunnerTapSynthesisStatus RunnerTrySynthesizeTap(
79+
id application,
80+
id _Nullable resolvedWindow,
81+
CGPoint point,
82+
NSDate * _Nullable deadline,
83+
NSString * _Nullable * _Nullable errorMessage
84+
);
7985
// XCTest's proven swipe profile reaches the endpoint in 100 ms, then holds for the planned
8086
// fling duration. Fast movement is what lets UIKit distinguish a fling from a timed pan.
8187
static const NSTimeInterval RunnerSwipeMovementDurationSeconds = 0.1;
@@ -132,14 +138,20 @@ + (NSString * _Nullable)synthesizeControlledScrollWithApplication:(id)applicatio
132138
}
133139
}
134140

135-
+ (NSString * _Nullable)synthesizeTapWithApplication:(id)application
141+
+ (RunnerTapSynthesisStatus)synthesizeTapWithApplication:(id)application
136142
resolvedWindow:(id _Nullable)resolvedWindow
137143
x:(double)x
138-
y:(double)y {
144+
y:(double)y
145+
deadline:(NSDate * _Nullable)deadline
146+
errorMessage:(NSString * _Nullable * _Nullable)errorMessage {
147+
if (errorMessage != NULL) *errorMessage = nil;
139148
@try {
140-
return RunnerTrySynthesizeTap(application, resolvedWindow, CGPointMake(x, y));
149+
return RunnerTrySynthesizeTap(application, resolvedWindow, CGPointMake(x, y), deadline, errorMessage);
141150
} @catch (NSException *exception) {
142-
return RunnerFormatXCTestException(exception, @"private XCTest event synthesis failed");
151+
if (errorMessage != NULL) {
152+
*errorMessage = RunnerFormatXCTestException(exception, @"private XCTest event synthesis failed");
153+
}
154+
return RunnerTapSynthesisStatusFailed;
143155
}
144156
}
145157

@@ -227,7 +239,13 @@ + (NSInteger)interfaceOrientationForApplication:(id)application {
227239
return RunnerSynthesizeEventRecord(&bridge, record);
228240
}
229241

230-
static NSString * _Nullable RunnerTrySynthesizeTap(id application, id _Nullable resolvedWindow, CGPoint point) {
242+
static RunnerTapSynthesisStatus RunnerTrySynthesizeTap(
243+
id application,
244+
id _Nullable resolvedWindow,
245+
CGPoint point,
246+
NSDate * _Nullable deadline,
247+
NSString * _Nullable * _Nullable errorMessage
248+
) {
231249
RunnerGestureEventBridge bridge;
232250
id record = nil;
233251
NSString *error = RunnerCreateEventRecord(
@@ -237,14 +255,23 @@ + (NSInteger)interfaceOrientationForApplication:(id)application {
237255
&bridge,
238256
&record
239257
);
240-
if (error != nil) return error;
258+
if (error != nil) {
259+
if (errorMessage != NULL) *errorMessage = error;
260+
return RunnerTapSynthesisStatusFailed;
261+
}
241262

242263
id path = RunnerTapPointerPath(&bridge, point);
243264
if (path == nil) {
244-
return @"private XCTest event synthesis failed: could not create pointer path";
265+
if (errorMessage != NULL) *errorMessage = @"private XCTest event synthesis failed: could not create pointer path";
266+
return RunnerTapSynthesisStatusFailed;
245267
}
246268
((RunnerMsgSendAddPath)objc_msgSend)(record, bridge.core.addPathSelector, path);
247-
return RunnerSynthesizeEventRecord(&bridge, record);
269+
if (deadline != nil && deadline.timeIntervalSinceNow <= 0) {
270+
return RunnerTapSynthesisStatusDeadlineExceeded;
271+
}
272+
error = RunnerSynthesizeEventRecord(&bridge, record);
273+
if (errorMessage != NULL) *errorMessage = error;
274+
return error == nil ? RunnerTapSynthesisStatusSucceeded : RunnerTapSynthesisStatusFailed;
248275
}
249276

250277
static NSString * _Nullable RunnerResolveGestureEventBridge(

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,40 @@ extension RunnerTests {
138138
#if !os(tvOS)
139139
guard !frame.isEmpty else { return }
140140
#endif
141+
#if os(iOS)
142+
let postEventWaitSelector = NSSelectorFromString("_waitForQuiescenceAsPreEvent:")
143+
guard alert.ownerApp.responds(to: postEventWaitSelector) else {
144+
outcome = .unsupported(
145+
message: "alert activation could not wait for post-event quiescence",
146+
hint: "Inspect the current alert before deciding whether to act again."
147+
)
148+
return
149+
}
150+
switch synthesizedTapAt(app: alert.ownerApp, x: frame.midX, y: frame.midY, deadline: deadline) {
151+
case .performed?:
152+
typealias WaitForQuiescence = @convention(c) (NSObject, Selector, Bool) -> Void
153+
let waitForQuiescence = unsafeBitCast(
154+
alert.ownerApp.method(for: postEventWaitSelector),
155+
to: WaitForQuiescence.self
156+
)
157+
waitForQuiescence(alert.ownerApp, postEventWaitSelector, false)
158+
outcome = .performed
159+
case .unsupported(let message, _)?:
160+
outcome = .unsupported(
161+
message: message,
162+
hint: "Inspect the current alert before deciding whether to act again."
163+
)
164+
case nil:
165+
break
166+
}
167+
#else
141168
outcome = activateElement(
142169
app: alert.ownerApp,
143170
element: button,
144171
action: "alert \(action)",
145172
resolvedFrame: frame
146173
)
174+
#endif
147175
}
148176
}
149177
return outcome

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,14 @@ extension RunnerTests {
143143
_ app: XCUIApplication,
144144
kind: SynthesizedGesturePolicyKind,
145145
context: SynthesizedCoordinateContext?,
146-
synthesize: () -> RunnerInteractionOutcome
146+
synthesize: () -> RunnerInteractionOutcome?
147147
) -> SynthesizedGestureAttempt {
148-
let (timing, outcome) = performGesture(app, idleTimeout: false, synthesize)
148+
var outcome: RunnerInteractionOutcome?
149+
let timing = measureGesture { outcome = synthesize() }
150+
guard let outcome else {
151+
logSynthesizedGesturePolicyDecision(kind: kind, context: context, fallbackAttempted: false)
152+
return .refused(timing: timing, message: "synthesized gesture deadline expired before dispatch", hint: nil)
153+
}
149154
guard case .unsupported(let message, let hint) = outcome else {
150155
logSynthesizedGesturePolicyDecision(kind: kind, context: context, fallbackAttempted: false)
151156
return .performed(timing: timing)

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ extension RunnerTests {
9595
app: XCUIApplication,
9696
x: Double,
9797
y: Double,
98-
context: SynthesizedCoordinateContext? = nil
99-
) -> RunnerInteractionOutcome {
98+
context: SynthesizedCoordinateContext? = nil,
99+
deadline: Date? = nil
100+
) -> RunnerInteractionOutcome? {
100101
#if os(iOS)
101102
guard x.isFinite, y.isFinite else {
102103
return .unsupported(
@@ -126,18 +127,31 @@ extension RunnerTests {
126127
referenceFrame: context.referenceFrame,
127128
orientation: orientation
128129
)
129-
if let message = RunnerSynthesizedGesture.synthesizeTap(
130+
var message: NSString?
131+
let status = RunnerSynthesizedGesture.synthesizeTap(
130132
withApplication: app,
131133
resolvedWindow: context.resolvedWindow,
132134
x: Double(point.x),
133-
y: Double(point.y)
134-
) {
135+
y: Double(point.y),
136+
deadline: deadline,
137+
errorMessage: &message
138+
)
139+
switch status {
140+
case .succeeded:
141+
return .performed
142+
case .deadlineExceeded:
143+
return nil
144+
case .failed:
135145
return .unsupported(
136-
message: message,
146+
message: message as String? ?? "private XCTest event synthesis failed",
137147
hint: "Falling back to XCTest coordinate tap may be slower and can still need a healthy accessibility tree."
138148
)
149+
@unknown default:
150+
return .unsupported(
151+
message: "private XCTest event synthesis returned an unknown status",
152+
hint: "Inspect the current app before deciding whether to act again."
153+
)
139154
}
140-
return .performed
141155
#elseif os(tvOS)
142156
return .unsupported(
143157
message: "coordinate tap is not supported on tvOS; move focus with swipe or scroll, then select the focused element",

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

Lines changed: 92 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
import XCTest
22

3+
#if AGENT_DEVICE_RUNNER_UNIT_TESTS && os(iOS)
4+
import ObjectiveC.runtime
5+
6+
private final class AlertTapDeadlineStub: NSObject {
7+
static var status: RunnerTapSynthesisStatus?
8+
9+
@objc(synthesizeTapWithApplication:resolvedWindow:x:y:deadline:errorMessage:)
10+
class func synthesizeTap(
11+
application: XCUIApplication,
12+
resolvedWindow: Any?,
13+
x: Double,
14+
y: Double,
15+
deadline: NSDate?,
16+
errorMessage: AutoreleasingUnsafeMutablePointer<NSString?>?
17+
) -> RunnerTapSynthesisStatus {
18+
status ?? (deadline == nil ? .succeeded : .deadlineExceeded)
19+
}
20+
}
21+
#endif
22+
323
extension RunnerTests {
424
#if AGENT_DEVICE_RUNNER_UNIT_TESTS && os(iOS)
525
@MainActor
@@ -82,6 +102,42 @@ extension RunnerTests {
82102
XCTAssertEqual(app.staticTexts["agent-device-alert-actions"].label, "First actions: 0; replacement actions: 0")
83103
}
84104

105+
@MainActor
106+
func testAlertDeadlineDuringSynthesisPreparationLeavesTheOriginalUntouched() throws {
107+
app.launchArguments = ["--agent-device-alert-replacement-regression"]
108+
app.launch()
109+
defer {
110+
invalidateCachedTarget(reason: "unit_test_cleanup")
111+
app.terminate()
112+
}
113+
XCTAssertTrue(app.alerts.firstMatch.waitForExistence(timeout: appExistenceTimeout))
114+
let alert = try resolveAlertBeforeTheCommand()
115+
let button = try XCTUnwrap(alert.buttons.first { $0.label == "OK" })
116+
let frame = button.frame
117+
let selector = NSSelectorFromString("synthesizeTapWithApplication:resolvedWindow:x:y:deadline:errorMessage:")
118+
let method = try XCTUnwrap(class_getClassMethod(RunnerSynthesizedGesture.self, selector))
119+
let stub = try XCTUnwrap(class_getClassMethod(AlertTapDeadlineStub.self, selector))
120+
let original = method_getImplementation(method)
121+
method_setImplementation(method, method_getImplementation(stub))
122+
defer {
123+
AlertTapDeadlineStub.status = nil
124+
method_setImplementation(method, original)
125+
}
126+
127+
let outcome = activateAlertButton(alert, button: button, action: "accept", frame: frame, deadline: .distantFuture)
128+
129+
XCTAssertNil(outcome)
130+
XCTAssertTrue(app.alerts.firstMatch.exists)
131+
XCTAssertEqual(app.staticTexts["agent-device-alert-actions"].label, "First actions: 0; replacement actions: 0")
132+
133+
AlertTapDeadlineStub.status = RunnerTapSynthesisStatus(rawValue: 999)
134+
let unknown = activateAlertButton(alert, button: button, action: "accept", frame: frame, deadline: .distantFuture)
135+
guard case .unsupported? = unknown else {
136+
return XCTFail("an unknown native status must report failure rather than deadline expiry")
137+
}
138+
XCTAssertEqual(app.staticTexts["agent-device-alert-actions"].label, "First actions: 0; replacement actions: 0")
139+
}
140+
85141
@MainActor
86142
func testAlertActivationIgnoresAnAppThatNeverSettlesBeforeTheDeadline() throws {
87143
app.launchArguments = [
@@ -109,42 +165,63 @@ extension RunnerTests {
109165
}
110166

111167
@MainActor
112-
func testAlertActivationDoesNotWaitOutANotificationBanner() throws {
168+
func testAlertActivationDoesNotConsultInterruptionHandlingWithANotificationBanner() throws {
169+
try withNotificationBannerAlert { alert in
170+
var consultedInterruptions: [String] = []
171+
let monitor = addUIInterruptionMonitor(withDescription: "alert activation banner") { element in
172+
consultedInterruptions.append(element.identifier)
173+
return false
174+
}
175+
defer { removeUIInterruptionMonitor(monitor) }
176+
177+
_ = handleAlert(alert, action: "accept", deadline: Date().addingTimeInterval(RunnerTests.alertActivationDeadline))
178+
179+
XCTAssertEqual(consultedInterruptions, [], "alert activation consulted XCTest's interruption handling")
180+
}
181+
}
182+
183+
@MainActor
184+
func testAlertActivationConfirmsBeforeDeadlineWithANotificationBanner() throws {
185+
try withNotificationBannerAlert { alert in
186+
let deadline = Date().addingTimeInterval(RunnerTests.alertActivationDeadline)
187+
let response = handleAlert(alert, action: "accept", deadline: deadline)
188+
189+
XCTAssertTrue(response.ok, String(describing: response.error))
190+
XCTAssertLessThan(Date(), deadline)
191+
XCTAssertEqual(app.staticTexts["agent-device-alert-actions"].label, "First actions: 1; replacement actions: 0")
192+
}
193+
}
194+
195+
@MainActor
196+
private func withNotificationBannerAlert(_ assertions: (RunnerAlert) throws -> Void) throws {
113197
app.launchArguments = ["--agent-device-alert-replacement-regression", "--agent-device-alert-banner"]
114198
app.launch()
115199
let banner = XCUIApplication(bundleIdentifier: "com.apple.springboard")
116200
.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-
}
122201
defer {
123-
removeUIInterruptionMonitor(monitor)
124202
invalidateCachedTarget(reason: "unit_test_cleanup")
125203
app.terminate()
126204
_ = banner.waitForNonExistence(timeout: 15)
127205
}
128-
acceptNotificationAuthorizationUntilAlertAppears()
206+
try acceptNotificationAuthorizationUntilAlertAppears()
129207
XCTAssertTrue(banner.waitForExistence(timeout: appExistenceTimeout), "the fixture keeps a banner up")
130208
let alert = try resolveAlertBeforeTheCommand()
131209

132-
let response = handleAlert(alert, action: "accept", deadline: Date().addingTimeInterval(RunnerTests.alertBannerActivationDeadline))
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")
210+
try assertions(alert)
137211
}
138212

139213
/// The banner fixture presents its alert only once this app may post notifications; a fresh
140214
/// simulator asks first, through SpringBoard.
141-
private func acceptNotificationAuthorizationUntilAlertAppears() {
215+
@MainActor
216+
private func acceptNotificationAuthorizationUntilAlertAppears() throws {
142217
let allow = XCUIApplication(bundleIdentifier: "com.apple.springboard").alerts.buttons["Allow"]
143218
let fixtureAlert = app.alerts.firstMatch
144219
let deadline = Date().addingTimeInterval(appExistenceTimeout)
145220
while Date() < deadline, !fixtureAlert.exists {
146221
if allow.exists {
147-
allow.tap()
222+
let authorization = try XCTUnwrap(resolveAlert(app: app, deadline: deadline))
223+
let response = handleAlert(authorization, action: "accept", deadline: deadline)
224+
XCTAssertTrue(response.ok, String(describing: response.error))
148225
} else {
149226
Thread.sleep(forTimeInterval: 0.25)
150227
}
@@ -163,7 +240,6 @@ extension RunnerTests {
163240
/// the deadline itself pays it in full. It buys the dozen reads after resolution 2.5 s each, above the
164241
/// 1.7 s a read cost on the worst hosted nights traced (#2708).
165242
static let alertActivationDeadline: TimeInterval = 30
166-
static let alertBannerActivationDeadline: TimeInterval = 90
167243

168244
@MainActor
169245
private func resolveAlertBeforeTheCommand() throws -> RunnerAlert {

0 commit comments

Comments
 (0)