@@ -177,17 +177,38 @@ extension RunnerTests {
177177
178178 private func captureNavigationFallbackVisualState ( app : XCUIApplication ) -> NavigationVisualSample {
179179#if os( iOS )
180- // A visual check is optional evidence, and the check is about THIS app's screen after the tap.
181- // It uses the app's own resolved screen, never the system-surface fallback: sampling SpringBoard's
182- // home screen twice would read as "unchanged" for an app that resolved no window, laundering a
183- // wrong-process sample into the same false "no back control" verdict a `nil` sample produces (#2728).
184- switch captureResolvedAppScreen ( app : app ) {
180+ // A visual check is optional evidence, and the check is about THIS app's screen after the tap. It
181+ // observes the app's own resolved screen with a `.never` system-surface fallback: sampling
182+ // SpringBoard's home screen twice would read as "unchanged" for an app that resolved no window,
183+ // laundering a wrong-process sample into the same false "no back control" verdict a `nil` sample
184+ // produces (#2728). The fallback is handed no system-surface resolver to reach for.
185+ let outcome = selectScreenCapture (
186+ resolving : { captureResolvedAppScreen ( app : app ) } ,
187+ fallback : . never
188+ )
189+ return Self . navigationVisualSample ( from : outcome , encoding : { runnerPngData ( for : $0 . image ) } )
190+ #else
191+ return NavigationVisualSample ( data : nil )
192+ #endif
193+ }
194+
195+ /// Turns a capture answer into a navigation sample: the encoded frame when the display answered,
196+ /// and the typed reason it refused otherwise. A resolved display whose image would not encode is
197+ /// named as the capture failure it is, not as an unnamed no-sample that would default to "no
198+ /// display resolved" (#2728). Kept apart from the query so the mapping itself is testable.
199+ static func navigationVisualSample (
200+ from outcome : Result < CapturedAppScreen , RunnerAppScreenCaptureFailure > ,
201+ encoding : ( CapturedAppScreen ) -> Data ?
202+ ) -> NavigationVisualSample {
203+ switch outcome {
185204 case . success ( let captured ) :
186- guard let png = runnerPngData ( for : captured . image ) else {
187- // The display resolved but the image would not encode: name it as the capture failure it is,
188- // not as an unnamed no-sample that would default to "no display resolved" (#2728).
205+ guard let png = encoding ( captured ) else {
189206 let refusal = RunnerAppScreenCaptureFailure . unrenderableImage
190- return NavigationVisualSample ( data : nil , refusalCode : refusal . rawValue , refusalHint : refusal . hint )
207+ return NavigationVisualSample (
208+ data : nil ,
209+ refusalCode : refusal . rawValue ,
210+ refusalHint : refusal . hint
211+ )
191212 }
192213 return NavigationVisualSample ( data : png )
193214 case . failure ( let failure ) :
@@ -197,9 +218,6 @@ extension RunnerTests {
197218 refusalHint : failure . hint
198219 )
199220 }
200- #else
201- return NavigationVisualSample ( data : nil )
202- #endif
203221 }
204222
205223 private func verifyNavigationFallbackOutcome (
@@ -217,6 +235,16 @@ extension RunnerTests {
217235 after . data ? . count ?? - 1 ,
218236 observation . logToken
219237 )
238+ return Self . inAppBackOutcome ( observation : observation , before : before , after : after )
239+ }
240+
241+ /// What an observation of the fallback's before/after samples concludes. Kept apart from the sleep
242+ /// and the capture so the three-way decision is testable without a running app.
243+ static func inAppBackOutcome (
244+ observation : NavigationVisualObservation ,
245+ before : NavigationVisualSample ,
246+ after : NavigationVisualSample
247+ ) -> InAppBackOutcome {
220248 switch observation {
221249 case . changed :
222250 return . performed
@@ -363,5 +391,66 @@ extension RunnerTests {
363391 XCTAssertEqual ( unnamed . code , " APP_SCREEN_UNRESOLVED " )
364392 XCTAssertTrue ( unnamed . message . contains ( " unknown outcome " ) )
365393 }
394+
395+ func testVerifyNavigationFallbackOutcomeReportsUnresolvedWindowWithoutSystemSurface ( ) {
396+ // The in-app `back` fallback ran its tap but the app resolved no window. The conclusion must name
397+ // that refusal as an unknown outcome, and the fallback observes the app only: it selects through a
398+ // `.never` system-surface fallback, so it is handed no SpringBoard resolver to sample. Asking the
399+ // home screen twice would read as "unchanged" and launder a wrong-process capture into "no back
400+ // control exists" (#2728). Driving the real selection + mapping + decision here is what turns the
401+ // changed lines into tested behavior.
402+ let outcome = selectScreenCapture (
403+ resolving : { . failure ( . unresolvedWindow ) } ,
404+ fallback : . never
405+ )
406+
407+ let sample = Self . navigationVisualSample ( from : outcome , encoding : { _ in Data ( [ 1 , 2 , 3 ] ) } )
408+ XCTAssertNil ( sample . data )
409+ XCTAssertEqual ( sample . refusalCode , " APP_SCREEN_WINDOW_UNRESOLVED " )
410+
411+ let observation = Self . navigationVisualObservation ( before : sample . data , after : sample . data )
412+ XCTAssertEqual ( observation , . unobserved )
413+
414+ switch Self . inAppBackOutcome ( observation : observation , before : sample , after : sample ) {
415+ case . unverified ( let payload ) :
416+ XCTAssertEqual ( payload . code , " APP_SCREEN_WINDOW_UNRESOLVED " )
417+ case . performed , . unavailable :
418+ XCTFail ( " a refused capture must report an unknown outcome, not 'no back control' " )
419+ }
420+ }
421+
422+ func testNavigationVisualSampleDistinguishesEncodedFrameFromRefusal ( ) {
423+ // The same capture entry point yields three different samples, and only the refusal ones may carry
424+ // a code: an encoded frame is evidence, a resolved-but-unencodable image and a refusal are not
425+ // (#2728). Reverting the mapping to a plain no-sample loses the reason a host keys on.
426+ let captured = CapturedAppScreen (
427+ image : RunnerImage ( ) ,
428+ displayID : 3 ,
429+ pixelWidth : 12 ,
430+ pixelHeight : 24 ,
431+ pixelsPerPoint : 3
432+ )
433+
434+ let encoded = Self . navigationVisualSample (
435+ from : . success ( captured ) ,
436+ encoding : { _ in Data ( [ 7 , 7 ] ) }
437+ )
438+ XCTAssertEqual ( encoded . data , Data ( [ 7 , 7 ] ) )
439+ XCTAssertNil ( encoded . refusalCode )
440+
441+ let unencodable = Self . navigationVisualSample (
442+ from : . success ( captured ) ,
443+ encoding : { _ in nil }
444+ )
445+ XCTAssertNil ( unencodable . data )
446+ XCTAssertEqual ( unencodable . refusalCode , " APP_SCREEN_CAPTURE_UNRENDERABLE " )
447+
448+ let refused = Self . navigationVisualSample (
449+ from : . failure ( . unresolvedScreen ) ,
450+ encoding : { _ in Data ( [ 7 , 7 ] ) }
451+ )
452+ XCTAssertNil ( refused . data )
453+ XCTAssertEqual ( refused . refusalCode , " APP_SCREEN_UNRESOLVED " )
454+ }
366455#endif
367456}
0 commit comments