@@ -19,22 +19,101 @@ extension RunnerTests {
1919
2020 @ MainActor
2121 func testInAppBackVerifiesSoleScreenWhenXCTestCannotReadFramesOrEnumerate ( ) throws {
22- app . launchArguments = [ " --agent-device-back-regression " ]
22+ let status = try launchBackFixture ( [ " --agent-device-back-regression " ] )
23+ defer { endBackFixture ( ) }
24+
25+ let outcome = try withRejectedAppTree ( framesReadable : false ) { tapInAppBackControl ( app : app ) }
26+
27+ guard showsAppsOnlyFullScreen else {
28+ guard case . unavailable = outcome else {
29+ return XCTFail ( " without a window or a sole screen there is no route to tap " )
30+ }
31+ return
32+ }
33+ guard case . performed = outcome else {
34+ return XCTFail ( " a window-less back must synthesize and verify against the sole display " )
35+ }
36+ XCTAssertEqual ( status . label , " Back performed " )
37+ }
38+
39+ /// The sole-screen route compares real captures: a tap that lands on nothing leaves the screen
40+ /// unchanged, so back is unavailable rather than performed.
41+ @ MainActor
42+ func testInAppBackOnTheSoleScreenReportsATapThatChangedNothingAsUnavailable ( ) throws {
43+ _ = try launchBackFixture ( [ ] )
44+ defer { endBackFixture ( ) }
45+
46+ let outcome = try withRejectedAppTree ( framesReadable : false ) { tapInAppBackControl ( app : app ) }
47+
48+ guard case . unavailable = outcome else {
49+ return XCTFail ( " a top-leading tap on an app without a back control must not read as performed " )
50+ }
51+ }
52+
53+ /// Only an iPhone with one screen can skip the windows query after the tree rejected one; anywhere
54+ /// else the windows query is still asked, and its window carries the tap.
55+ @ MainActor
56+ func testInAppBackStillAsksForWindowsWhereNoSoleScreenAnswers ( ) throws {
57+ let status = try launchBackFixture ( [ " --agent-device-back-regression " ] )
58+ defer { endBackFixture ( ) }
59+
60+ let outcome = try withRejectedAppTree ( framesReadable : true , rejections : 1 ) {
61+ tapInAppBackControl ( app : app )
62+ }
63+
64+ guard case . performed = outcome else {
65+ return XCTFail ( " back must reach the fixture's top-leading control through a window or the sole screen " )
66+ }
67+ XCTAssertEqual ( status . label , " Back performed " )
68+ }
69+
70+ private var showsAppsOnlyFullScreen : Bool {
71+ UIDevice . current . userInterfaceIdiom == . phone && XCUIScreen . screens . count == 1
72+ }
73+
74+ /// Launches the runner app; with `--agent-device-back-regression` it shows a top-leading back
75+ /// control whose press sets the returned status to "Back performed", and without it nothing there.
76+ @ MainActor
77+ private func launchBackFixture ( _ arguments : [ String ] ) throws -> XCUIElement {
78+ app . launchArguments = arguments
2379 app . launch ( )
2480 mainOwned . app = app
2581 mainOwned . bundleId = " com.callstack.agentdevice.runner "
2682 clearSnapshotXCTestChannelPenalty ( reason : " navigation-test " )
27- defer {
28- invalidateCachedTarget ( reason : " unit_test_cleanup " )
29- app . terminate ( )
30- }
31- let status = app . staticTexts [ " agent-device-back-status " ]
83+ let status = arguments . contains ( " --agent-device-back-regression " )
84+ ? app . staticTexts [ " agent-device-back-status " ]
85+ : app . staticTexts . element ( matching : NSPredicate ( format : " label == %@ " , " Agent Device Runner " ) )
3286 XCTAssertTrue ( status . waitForFixtureExistence ( timeout : appExistenceTimeout ) )
87+ return status
88+ }
89+
90+ @ MainActor
91+ private func endBackFixture ( ) {
92+ invalidateCachedTarget ( reason : " unit_test_cleanup " )
93+ app . terminate ( )
94+ }
95+
96+ /// Stands in for XCTest on a tree it cannot serialize while `body` runs: the first `rejections`
97+ /// queries record the rejection the runner suppresses and find nothing, and unless
98+ /// `framesReadable`, every element frame reads empty.
99+ @ MainActor
100+ private func withRejectedAppTree < T > (
101+ framesReadable : Bool ,
102+ rejections : Int = . max ,
103+ _ body : ( ) -> T
104+ ) throws -> T {
33105 var restorations : [ ( ) -> Void ] = [ ]
34106 defer { restorations . reversed ( ) . forEach { $0 ( ) } }
35- let query = try XCTUnwrap ( class_getInstanceMethod ( XCUIElementQuery . self , NSSelectorFromString ( " allElementsBoundByIndex " ) ) )
107+ let selector = NSSelectorFromString ( " allElementsBoundByIndex " )
108+ let query = try XCTUnwrap ( class_getInstanceMethod ( XCUIElementQuery . self , selector ) )
36109 let originalQuery = method_getImplementation ( query )
37- let rejectedQuery = imp_implementationWithBlock ( { ( _ : AnyObject ) -> NSArray in
110+ var remainingRejections = rejections
111+ let rejectedQuery = imp_implementationWithBlock ( { ( target : AnyObject ) -> NSArray in
112+ guard remainingRejections > 0 else {
113+ typealias Query = @convention ( c ) ( AnyObject , Selector ) -> NSArray
114+ return unsafeBitCast ( originalQuery , to : Query . self ) ( target , selector )
115+ }
116+ remainingRejections -= 1
38117 self . record ( XCTIssue ( type : . assertionFailure , compactDescription : " Failed to resolve query: kAXErrorIllegalArgument " ) )
39118 return [ ]
40119 } as @convention ( block ) ( AnyObject ) -> NSArray )
@@ -43,22 +122,17 @@ extension RunnerTests {
43122 method_setImplementation ( query , originalQuery )
44123 imp_removeBlock ( rejectedQuery )
45124 }
46- let frame = try XCTUnwrap ( class_getInstanceMethod ( XCUIElement . self , NSSelectorFromString ( " frame " ) ) )
47- let originalFrame = method_getImplementation ( frame )
48- let unavailableFrame = imp_implementationWithBlock ( { ( _ : AnyObject ) -> CGRect in . zero } as @convention ( block ) ( AnyObject ) -> CGRect )
49- method_setImplementation ( frame , unavailableFrame )
50- restorations . append {
51- method_setImplementation ( frame , originalFrame )
52- imp_removeBlock ( unavailableFrame )
53- }
54-
55- let outcome = tapInAppBackControl ( app : app )
56- restorations . reversed ( ) . forEach { $0 ( ) }
57- restorations . removeAll ( )
58- guard case . performed = outcome else {
59- return XCTFail ( " a window-less back must synthesize and verify against the sole display " )
125+ if ! framesReadable {
126+ let frame = try XCTUnwrap ( class_getInstanceMethod ( XCUIElement . self , NSSelectorFromString ( " frame " ) ) )
127+ let originalFrame = method_getImplementation ( frame )
128+ let unavailableFrame = imp_implementationWithBlock ( { ( _ : AnyObject ) -> CGRect in . zero } as @convention ( block ) ( AnyObject ) -> CGRect )
129+ method_setImplementation ( frame , unavailableFrame )
130+ restorations . append {
131+ method_setImplementation ( frame , originalFrame )
132+ imp_removeBlock ( unavailableFrame )
133+ }
60134 }
61- XCTAssertEqual ( status . label , " Back performed " )
135+ return body ( )
62136 }
63137
64138 @ MainActor
@@ -106,22 +180,14 @@ extension RunnerTests {
106180 }
107181#endif
108182
183+ /// A 402x874 iPhone's navigation bar spans y 62-116 and its back control 62-106.
109184 func testTopLeadingNavigationFallbackPointTargetsHeaderControlBand ( ) throws {
110185 let point = try XCTUnwrap (
111186 Self . topLeadingNavigationFallbackPoint (
112- in : CGRect ( x : 0 , y : 0 , width : 430 , height : 932 )
187+ in : CGRect ( x : 0 , y : 0 , width : 402 , height : 874 )
113188 )
114189 )
115190
116- XCTAssertEqual ( point . x , 34.4 , accuracy : 0.01 )
117- XCTAssertEqual ( point . y , 132 , accuracy : 0.01 )
118- }
119-
120- func testSoleScreenNavigationFallbackPointTargetsCompactHeader ( ) throws {
121- let point = try XCTUnwrap ( Self . topLeadingNavigationFallbackPoint (
122- in : CGRect ( x : 0 , y : 0 , width : 402 , height : 874 ) ,
123- windowResolved : false
124- ) )
125191 XCTAssertEqual ( point . x , 32.16 , accuracy : 0.01 )
126192 XCTAssertEqual ( point . y , 87.4 , accuracy : 0.01 )
127193 }
0 commit comments