11import 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+
323extension 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