@@ -5,7 +5,9 @@ import XCTest
55import ObjectiveC . runtime
66
77private final class LitPanelBookingScreen : NSObject {
8- @ objc let displayID : UInt = 3
8+ @ objc let displayID : UInt
9+
10+ init ( _ displayID : UInt = 3 ) { self . displayID = displayID }
911}
1012
1113@ MainActor
@@ -15,6 +17,7 @@ private final class LitPanelBookingProbe: NSObject {
1517 static var windowListReads = 0
1618 static var maxWindowListReads : Int ?
1719 static var window : XCUIElement ?
20+ static var darkWindow : XCUIElement ?
1821 static var coordinate : XCUICoordinate ?
1922 static var windowFrame = CGRect . zero
2023 static var orientation = RunnerInterfaceOrientation . landscapeLeft
@@ -31,8 +34,8 @@ private final class LitPanelBookingProbe: NSObject {
3134 case " allElementsBoundByIndex " :
3235 Self . windowListReads += 1
3336 if let max = Self . maxWindowListReads , Self . windowListReads > max { return [ ] }
34- return [ Self . window ! ]
35- case " screen " : return LitPanelBookingScreen ( )
37+ return [ Self . window ! , Self . darkWindow ] . compactMap { $0 }
38+ case " screen " : return LitPanelBookingScreen ( self === Self . darkWindow ? 1 : 3 )
3639 case " exists " : return true
3740 case " frame " : return NSValue ( cgRect : Self . windowFrame )
3841 default : break
@@ -114,6 +117,80 @@ extension RunnerTests {
114117 }
115118 }
116119
120+ @ MainActor
121+ func testCollapsedTabCandidatesExcludeOverlappingDarkWindowsAndKeepTheCaptureScope ( ) throws {
122+ try withLitPanelBookingProbe { window in
123+ let darkWindow = XCUIApplication ( bundleIdentifier : " com.apple.springboard " ) . windows . firstMatch
124+ LitPanelBookingProbe . darkWindow = darkWindow
125+ LitPanelBookingProbe . windowFrame = CGRect ( x : 0 , y : 0 , width : 669 , height : 951 )
126+ setRequestLitPanel ( from : try runnerCommandFixture (
127+ #"{"command":"snapshot","litPanel":{"displayId":3,"widthPt":669,"heightPt":951}}"#
128+ ) )
129+ XCTAssertNil ( bookRequestLitPanelSurface ( app : app ) )
130+ let container = ElementAttributesFixture ( . tabBar , CGRect ( x : 0 , y : 800 , width : 669 , height : 31 ) )
131+ let context = SnapshotTraversalContext (
132+ queryRoot : app ,
133+ rootSnapshot : ElementAttributesFixture ( . application , LitPanelBookingProbe . windowFrame , children : [
134+ ElementAttributesFixture ( . window , LitPanelBookingProbe . windowFrame , children : [ container ] , displayID : 3 ) ,
135+ ] ) ,
136+ rootReadDuration : 0 ,
137+ viewport : . missing ( reason : . notProvided ) ,
138+ keyboardBand : nil ,
139+ litPanelSurface : try XCTUnwrap ( litPanelState . surface )
140+ )
141+ let lit = ElementAttributesFixture ( . button , CGRect ( x : 30 , y : 800 , width : 100 , height : 30 ) , label : " Lit " )
142+ let second = ElementAttributesFixture ( . button , CGRect ( x : 170 , y : 800 , width : 100 , height : 30 ) , label : " Second " )
143+ let dark = ElementAttributesFixture ( . button , lit . frame , label : " Dark " )
144+ let appQuery = app . descendants ( matching : . any )
145+ let litQuery = window . descendants ( matching : . any )
146+ let darkQuery = darkWindow . descendants ( matching : . any )
147+ var predicates : [ ObjectIdentifier : NSPredicate ] = [ : ]
148+ var restores : [ ( ) -> Void ] = [ ]
149+ defer { restores . reversed ( ) . forEach { $0 ( ) } }
150+ func swap ( _ type : AnyClass , _ name : String , _ block : Any ) throws {
151+ let method = try XCTUnwrap ( class_getInstanceMethod ( type , NSSelectorFromString ( name ) ) )
152+ let replacement = imp_implementationWithBlock ( block )
153+ let original = method_setImplementation ( method , replacement )
154+ restores . append {
155+ method_setImplementation ( method , original )
156+ imp_removeBlock ( replacement )
157+ }
158+ }
159+ try swap ( XCUIElement . self , " descendantsMatchingType: " , { ( root : AnyObject , _ : UInt ) -> XCUIElementQuery in
160+ root === window ? litQuery : root === darkWindow ? darkQuery : appQuery
161+ } as @convention ( block ) ( AnyObject , UInt ) -> XCUIElementQuery )
162+ try swap ( XCUIElementQuery . self , " matchingPredicate: " , { ( query : AnyObject , predicate : NSPredicate ) -> XCUIElementQuery in
163+ predicates [ ObjectIdentifier ( query ) ] = predicate
164+ return query as! XCUIElementQuery
165+ } as @convention ( block ) ( AnyObject , NSPredicate ) -> XCUIElementQuery )
166+ try swap ( XCUIElementQuery . self , " allElementsBoundByIndex " , { ( query : AnyObject ) -> NSArray in
167+ let attributes = query === litQuery ? [ lit , second ] : query === darkQuery ? [ dark ] : [ lit , second , dark ]
168+ let matches = attributes . filter { predicates [ ObjectIdentifier ( query ) ] ? . evaluate ( with : $0 ) == true }
169+ return matches . isEmpty ? [ ] : [ window ]
170+ } as @convention ( block ) ( AnyObject ) -> NSArray )
171+ let candidates = try XCTUnwrap ( try collapsedTabCandidates ( context : context , processingDeadline : . distantFuture ) )
172+ XCTAssertEqual ( candidates . map ( \. label ) , [ " Lit " , " Second " ] , " an overlapping control on a dark window must not enter the capture " )
173+ let hint = CaptureHint ( projection : . regular , depth : nil , regularPresentedDepth : nil , interactiveOnly : false , customActions : false )
174+ let capture = try recursiveTreeSnapshotAcquisition ( context : context , hint : hint , processingDeadline : . distantFuture )
175+ let labels = capture . acquisition . nodes . compactMap ( \. label )
176+ XCTAssertTrue ( labels . contains ( " Lit " ) )
177+ XCTAssertTrue ( labels . contains ( " Second " ) )
178+ XCTAssertFalse ( labels . contains ( " Dark " ) , " the serialized capture must exclude the overlapping dark control " )
179+ endRequestLitPanel ( try XCTUnwrap ( litPanelState . requestID ) )
180+ let pinned = try XCTUnwrap ( try collapsedTabCandidates ( context : context , processingDeadline : . distantFuture ) )
181+ XCTAssertEqual ( pinned . map ( \. label ) , [ " Lit " , " Second " ] , " the capture's display scope survives a cleared current request " )
182+ var unscoped = context
183+ unscoped . litPanelSurface = nil
184+ let all = try XCTUnwrap ( try collapsedTabCandidates ( context : unscoped , processingDeadline : . distantFuture ) )
185+ XCTAssertEqual ( all . map ( \. label ) , [ " Lit " , " Second " , " Dark " ] , " an unscoped capture keeps application-wide candidates " )
186+ litPanelWindowWalkIssueForTesting = true
187+ defer { litPanelWindowWalkIssueForTesting = false }
188+ XCTAssertThrowsError ( try collapsedTabCandidates ( context : context , processingDeadline : . distantFuture ) ) {
189+ XCTAssertEqual ( $0 as? RunnerAppScreenCaptureFailure , . unresolvedScreen )
190+ }
191+ }
192+ }
193+
117194 @ MainActor
118195 func testLitPanelBookingUsesBundleIdentityAcrossProxiesAndRequests ( ) throws {
119196 try withLitPanelBookingProbe { window in
@@ -232,6 +309,7 @@ extension RunnerTests {
232309 var originals : [ ( Method , IMP ) ] = [ ]
233310 defer {
234311 for ( method , original ) in originals . reversed ( ) { method_setImplementation ( method , original ) }
312+ LitPanelBookingProbe . darkWindow = nil
235313 LitPanelBookingProbe . window = nil
236314 LitPanelBookingProbe . coordinate = nil
237315 LitPanelBookingProbe . anchor = nil
@@ -519,6 +597,30 @@ extension RunnerTests {
519597 }
520598 }
521599
600+ @ MainActor
601+ func testCollapsedTabSnapshotReadsProvenLitLiveWindows ( ) throws {
602+ app . launchArguments = [ " --agent-device-collapsed-tab-bar " , " --agent-device-wide-slider " ]
603+ let ( panel , _) = launchAppOnTheLitPanel ( )
604+ defer { tearDownLitPanelApp ( ) }
605+ setRequestLitPanel ( from : try runnerCommandFixture ( #"{"command":"snapshot", \#( panel ) }"# ) )
606+ XCTAssertNil ( bookRequestLitPanelSurface ( app : app ) )
607+ let hint = CaptureHint ( projection : . regular , depth : nil , regularPresentedDepth : nil , interactiveOnly : false , customActions : false )
608+ let context = try XCTUnwrap ( try makeSnapshotTraversalContext (
609+ app : app ,
610+ hint : hint ,
611+ litPanelSurface : try XCTUnwrap ( litPanelState . surface )
612+ ) )
613+ let container = try XCTUnwrap ( Self . snapshotDescendants ( of : context . rootSnapshot ) . first { $0 . elementType == . tabBar } )
614+ XCTAssertTrue ( container . children . isEmpty , " the live fixture must reach collapsed-tab recovery " )
615+ let candidates = try XCTUnwrap ( try collapsedTabCandidates ( context : context , processingDeadline : . distantFuture ) )
616+ XCTAssertTrue ( candidates . contains { $0 . label == " Left " } )
617+ XCTAssertTrue ( candidates . contains { $0 . label == " Right " } )
618+ let capture = try recursiveTreeSnapshotAcquisition ( context : context , hint : hint , processingDeadline : . distantFuture )
619+ let labels = capture . acquisition . nodes . compactMap ( \. label )
620+ XCTAssertTrue ( labels . contains ( " Left " ) )
621+ XCTAssertTrue ( labels . contains ( " Right " ) )
622+ }
623+
522624 /// A refused pin is a request refusal: it never reaches the capture plan, so it does not
523625 /// invalidate the cached target the next command relies on.
524626 @ MainActor
0 commit comments