@@ -3,7 +3,11 @@ import XCTest
33
44extension RunnerTests {
55 static let backSwipeVerificationDelay : TimeInterval = 0.25
6- static let backSwipeSettleSamples = 4
6+ /// Three agreeing samples span two verification delays, at least the gap from the last before
7+ /// sample to the after sample, so a screen that changes on its own more slowly than one delay
8+ /// cannot pass for settled and then read as the swipe's effect.
9+ static let backSwipeStableSamples = 3
10+ static let backSwipeSettleSampleBudget = 6
711
812 /// What one `back` concluded. `.refused` dispatched nothing. `.unconfirmed` ran the back gesture,
913 /// or may have, without proof that it navigated back.
@@ -13,6 +17,25 @@ extension RunnerTests {
1317 case unconfirmed ( ErrorPayload )
1418 }
1519
20+ /// The back swipe's endpoints and duration across `surface`. Mirrors the `right-edge` fling preset
21+ /// (`buildSwipePresetGesturePlan` and `clampGestureCoordinate` in packages/contracts/src/scroll-gesture.ts):
22+ /// 1% to 85% of the width at half the height, truncated, then held 8 pt inside the edges.
23+ /// contracts/fixtures/back-swipe-geometry.json holds both sides to the same table.
24+ static let backSwipeDurationMs = 100.0
25+
26+ static func backSwipeEndpoints ( in surface : CGRect ) -> ( start : CGPoint , end : CGPoint ) {
27+ func axis ( _ size : CGFloat , percent : CGFloat ) -> CGFloat {
28+ let margin : CGFloat = 8
29+ let upper = max ( margin , ( size - margin ) . rounded ( ) )
30+ return min ( upper , max ( margin , ( size * percent / 100 ) . rounded ( . towardZero ) ) )
31+ }
32+ let y = surface . minY + axis ( surface . height , percent : 50 )
33+ return (
34+ CGPoint ( x : surface . minX + axis ( surface . width , percent : 1 ) , y : y ) ,
35+ CGPoint ( x : surface . minX + axis ( surface . width , percent : 85 ) , y : y )
36+ )
37+ }
38+
1639 /// The three answers a before/after visual comparison can give. `unobserved` carries the typed
1740 /// refusal that made the pair uncomparable: a display that refuses to be sampled proves nothing
1841 /// either way, and reading it as "unchanged" would claim the swipe did nothing.
@@ -48,7 +71,7 @@ extension RunnerTests {
4871#elseif os( tvOS )
4972 _ = pressTvRemote ( . menu )
5073 return . performed
51- #else
74+ #elseif os ( iOS )
5275 guard let context = try synthesizedCoordinateContext ( app : app ) else {
5376 return . refused ( Self . backScreenRefusal ( . unresolvedWindow ) )
5477 }
@@ -77,6 +100,15 @@ extension RunnerTests {
77100 )
78101 NSLog ( " AGENT_DEVICE_RUNNER_BACK_SWIPE_VERIFICATION changed=%@ " , observation . logToken )
79102 return Self . backSwipeOutcome ( observation : observation )
103+ #else
104+ // visionOS has no edge swipe that pops a navigation stack.
105+ return . refused (
106+ ErrorPayload (
107+ code : RunnerWireErrorCode . backSwipeNotDispatched ,
108+ message : " back has no gesture on this platform, so nothing was sent. " ,
109+ hint : " Press the visible back control by its ref. "
110+ )
111+ )
80112#endif
81113 }
82114
@@ -88,8 +120,13 @@ extension RunnerTests {
88120 return nil
89121 case . notSent ( let message , let hint ) :
90122 return . refused ( ErrorPayload ( code : RunnerWireErrorCode . backSwipeNotDispatched , message : message , hint : hint ) )
91- case . interrupted ( let message , let hint ) :
92- return . unconfirmed ( ErrorPayload ( code : " UNSUPPORTED_OPERATION " , message : message , hint : hint ) )
123+ case . interrupted ( let message , _) :
124+ return . unconfirmed (
125+ backSwipeUnverified (
126+ because : " it was cut short after it touched down ( \( message ) ) " ,
127+ hint : " Check the screen with snapshot before you retry; the swipe may already have navigated. "
128+ )
129+ )
93130 }
94131 }
95132#endif
@@ -103,27 +140,28 @@ extension RunnerTests {
103140 )
104141 }
105142
106- /// Samples until two frames a verification delay apart agree, so a screen still changing on its
107- /// own, such as one still rendering after a launch, is not read as the swipe's effect. `settled`
108- /// is false when the samples ran out first.
143+ /// Samples a verification delay apart until `backSwipeStableSamples` in a row agree with the
144+ /// first of them. `settled` is false when the budget ran out first.
109145 private func settledBackVisualState (
110146 app : XCUIApplication ,
111147 context : SynthesizedCoordinateContext
112148 ) -> Result < ( sample : Data , settled : Bool ) , RunnerAppScreenCaptureFailure > {
113- var previous : Data
114- switch captureBackVisualState ( app : app , context : context ) {
115- case . success ( let sample ) : previous = sample
116- case . failure ( let refusal ) : return . failure ( refusal )
117- }
118- for _ in 0 ..< Self . backSwipeSettleSamples {
119- sleepFor ( Self . backSwipeVerificationDelay )
149+ var stableRun : [ Data ] = [ ]
150+ for index in 0 ..< Self . backSwipeSettleSampleBudget {
151+ if index > 0 { sleepFor ( Self . backSwipeVerificationDelay ) }
120152 switch captureBackVisualState ( app : app , context : context ) {
121- case . success ( let next ) where ! Self . visualSamplesDiffer ( next , previous ) : return . success ( ( next , true ) )
122- case . success ( let next ) : previous = next
123- case . failure ( let refusal ) : return . failure ( refusal )
153+ case . failure ( let refusal ) :
154+ return . failure ( refusal )
155+ case . success ( let sample ) :
156+ if let first = stableRun . first , Self . visualSamplesDiffer ( first , sample ) {
157+ stableRun = [ sample ]
158+ } else {
159+ stableRun . append ( sample )
160+ }
161+ if stableRun . count >= Self . backSwipeStableSamples { return . success ( ( sample , true ) ) }
124162 }
125163 }
126- return . success ( ( previous , false ) )
164+ return . success ( ( stableRun [ stableRun . count - 1 ] , false ) )
127165 }
128166
129167 /// The frame the swipe compares, as a coarse grayscale grid when the display answered and the
@@ -139,7 +177,11 @@ extension RunnerTests {
139177 from : context . resolvedWindow == nil && requestLitPanel == nil
140178 ? captureSoleAppScreen ( app : app )
141179 : captureResolvedAppScreen ( app : app , litPanel : requestLitPanel ) ,
142- encoding : { runnerCGImage ( from : $0 . image ) . flatMap ( Self . visualGrid ) }
180+ encoding : { screen in
181+ // The status bar changes on its own (the clock, signal, a recording indicator).
182+ let statusBar = screenTopSafeAreaInset ( ) * CGFloat ( screen . pixelsPerPoint )
183+ return runnerCGImage ( from : screen . image ) . flatMap { Self . visualGrid ( of : $0 , excludingTop : statusBar ) }
184+ }
143185 )
144186#else
145187 return . failure ( . unresolvedScreen )
@@ -163,9 +205,26 @@ extension RunnerTests {
163205 static let visualCellTolerance = 24
164206 static let visualChangedCellFraction = 0.01
165207
166- /// The screen drawn into a small grayscale grid. A back navigation redraws most of it, while a
167- /// spinner, a clock tick, or a material shimmer moves few cells or moves them by little.
168- static func visualGrid ( of image : CGImage ) -> Data ? {
208+ #if os( iOS )
209+ /// The screen's top inset, read from the runner's own window scenes: the status-bar band is a
210+ /// screen-level fact, so the runner's full-screen host window reports the one the app sees.
211+ private func screenTopSafeAreaInset ( ) -> CGFloat {
212+ UIApplication . shared . connectedScenes
213+ . compactMap { $0 as? UIWindowScene }
214+ . flatMap ( \. windows )
215+ . map ( \. safeAreaInsets . top )
216+ . max ( ) ?? 0
217+ }
218+ #endif
219+
220+ /// The screen below its top `excludingTop` pixels, drawn into a small grayscale grid. A back
221+ /// navigation redraws most of it, while a spinner, a caret, or a material shimmer moves few
222+ /// cells or moves them by little.
223+ static func visualGrid ( of fullImage : CGImage , excludingTop topPixels : CGFloat = 0 ) -> Data ? {
224+ let top = min ( max ( topPixels , 0 ) , CGFloat ( fullImage . height - 1 ) )
225+ guard let image = fullImage . cropping (
226+ to : CGRect ( x : 0 , y : top , width : CGFloat ( fullImage . width ) , height : CGFloat ( fullImage . height ) - top )
227+ ) else { return nil }
169228 let ( width , height ) = visualGridSize
170229 var cells = [ UInt8 ] ( repeating : 0 , count : width * height )
171230 let drawn = cells . withUnsafeMutableBytes { buffer -> Bool in
0 commit comments