@@ -6,6 +6,37 @@ import XCTest
66final class AppleQuietControl : XCTestCase {
77 private func now ( ) -> Double { Date ( ) . timeIntervalSince1970 * 1000 }
88
9+ private func waitForBackground ( timeout : TimeInterval , state : @escaping ( ) -> XCUIApplication . State ) -> Bool {
10+ let background = XCTNSPredicateExpectation ( predicate : NSPredicate { _ , _ in
11+ let observed = state ( )
12+ return observed == . runningBackground || observed == . runningBackgroundSuspended
13+ } , object : nil )
14+ // XCTest delivers application-state updates on the main run loop.
15+ // Sleeping on that thread would keep reading the old foreground state.
16+ return XCTWaiter . wait ( for : [ background ] , timeout : timeout ) == . completed
17+ }
18+
19+ func testBackgroundWaitDeliversMainQueueUpdatesWithoutWeakeningStateGate ( ) {
20+ XCTAssertTrue ( Thread . isMainThread )
21+ let accepted : [ XCUIApplication . State ] = [ . runningBackground , . runningBackgroundSuspended ]
22+ for target in accepted {
23+ let delivered = StateDelivery ( )
24+ DispatchQueue . main . async { delivered . state = target }
25+ // Negative control: the previous sleep loop starves this delivery.
26+ let deadline = ProcessInfo . processInfo . systemUptime + 0.05
27+ while delivered . state == . runningForeground && ProcessInfo . processInfo . systemUptime < deadline {
28+ Thread . sleep ( forTimeInterval : 0.005 )
29+ }
30+ XCTAssertEqual ( delivered . state , . runningForeground )
31+ XCTAssertTrue ( waitForBackground ( timeout : 2 ) { delivered . state } )
32+ XCTAssertEqual ( delivered . state , target )
33+ }
34+ let excluded : [ XCUIApplication . State ] = [ . unknown , . notRunning , . runningForeground ]
35+ for rejected in excluded {
36+ XCTAssertFalse ( waitForBackground ( timeout : 0.05 ) { rejected } )
37+ }
38+ }
39+
940 private func post ( _ endpoint : String , _ value : [ String : Any ] ) throws -> [ String : Any ] {
1041 var request = URLRequest ( url : URL ( string : ControlConfig . url + " / " + endpoint ) ! )
1142 request . httpMethod = " POST "
@@ -68,7 +99,9 @@ final class AppleQuietControl: XCTestCase {
6899 " jobId " : job , " status " : " failed " , " controllerSession " : controllerSession
69100 ]
70101 do {
71- guard app . state == . runningForeground else {
102+ let stateBeforeAction = app . state
103+ result [ " stateBeforeAction " ] = stateBeforeAction . rawValue
104+ guard stateBeforeAction == . runningForeground else {
72105 throw NSError ( domain : " quiet.notForeground " , code : 1 )
73106 }
74107 let remaining = command [ " remainingMs " ] as! Double / 1000
@@ -77,13 +110,11 @@ final class AppleQuietControl: XCTestCase {
77110 if useHome {
78111 result [ " homeRequestedMs " ] = now ( )
79112 XCUIDevice . shared . press ( . home )
80- let transitionDeadline = ProcessInfo . processInfo . systemUptime + 15
81- while app . state != . runningBackground && app . state != . runningBackgroundSuspended {
82- guard ProcessInfo . processInfo . systemUptime < transitionDeadline else {
83- throw NSError ( domain : " quiet.homeFailed " , code : 1 )
84- }
85- Thread . sleep ( forTimeInterval : 0.05 ) // Still before quiet.
113+ result [ " homeReturnedMs " ] = now ( )
114+ guard waitForBackground ( timeout : 15 , state : { app . state } ) else {
115+ throw NSError ( domain : " quiet.homeFailed " , code : 1 )
86116 }
117+ result [ " homeAcknowledgedMs " ] = now ( )
87118 }
88119 result [ " stateBeforeQuiet " ] = app . state . rawValue
89120 result [ " stateBeforeQuietName " ] = String ( describing : app . state )
@@ -111,15 +142,32 @@ final class AppleQuietControl: XCTestCase {
111142 result [ " status " ] = " passed "
112143 } catch {
113144 result [ " error " ] = String ( describing : error )
145+ result [ " stateAtFailure " ] = app . state . rawValue
114146 }
115147 result [ " controlClock " ] = [
116148 " requestMs " : requestMs , " replyMs " : replyMs ,
117149 " serverReceivedMs " : command [ " serverReceivedMs " ] ! ,
118150 " serverReplyMs " : command [ " serverReplyMs " ] !
119151 ]
120152 _ = try post ( " control-result " , result )
153+ if useHome && result [ " status " ] as? String != " passed " {
154+ // Publish the primary failure before collecting diagnostics.
155+ let attachment = XCTAttachment ( screenshot : XCUIScreen . main . screenshot ( ) )
156+ attachment . name = " Failed Home transition "
157+ attachment . lifetime = . keepAlways
158+ add ( attachment )
159+ }
121160 XCTAssertEqual ( result [ " status " ] as? String , " passed " , String ( describing : result ) )
122161 }
123162 XCTFail ( " The app did not complete within the controller deadline " )
124163 }
125164}
165+
166+ private final class StateDelivery : @unchecked Sendable {
167+ private let lock = NSLock ( )
168+ private var stored : XCUIApplication . State = . runningForeground
169+ var state : XCUIApplication . State {
170+ get { lock . lock ( ) ; defer { lock . unlock ( ) } ; return stored }
171+ set { lock . lock ( ) ; defer { lock . unlock ( ) } ; stored = newValue }
172+ }
173+ }
0 commit comments