Skip to content

Commit 463fd7b

Browse files
thymikeeclaude
andauthored
refactor(ios): walk the lit windows once per multi-type element read (#4250)
* refactor(ios): walk the lit windows once per multi-type element read elementQueries takes variadic element types, resolves its roots once and flatMaps over the types in the same order, so a text-input lookup walks the lit-panel window list once instead of once per input type. Co-Authored-By: Claude Opus 5.5 <[email protected]> * refactor(ios): share one query builder between the element query overloads Both variadic elementQueries overloads repeated the same flatMap over roots and types. A private queries(matching:in:) builds them for both. --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 5c36c95 commit 463fd7b

6 files changed

Lines changed: 73 additions & 6 deletions

File tree

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerRequestPanel.swift‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,23 @@ extension RunnerTests {
118118
return windows
119119
}
120120

121-
func elementQueries(app: XCUIApplication, matching type: XCUIElement.ElementType) throws -> [XCUIElementQuery] {
122-
try elementQueries(root: app, matching: type, litDisplayID: requestLitPanel?.displayID)
121+
/// One walk of the roots for every type, so a multi-type read walks the lit windows once.
122+
func elementQueries(app: XCUIApplication, matching types: XCUIElement.ElementType...) throws -> [XCUIElementQuery] {
123+
queries(matching: types, in: try elementSearchRoots(root: app, litDisplayID: requestLitPanel?.displayID))
123124
}
124125

125126
func elementQueries(
126127
root: XCUIElement,
127-
matching type: XCUIElement.ElementType,
128+
matching types: XCUIElement.ElementType...,
128129
litDisplayID: UInt?
129130
) throws -> [XCUIElementQuery] {
130-
try elementSearchRoots(root: root, litDisplayID: litDisplayID).map { $0.descendants(matching: type) }
131+
queries(matching: types, in: try elementSearchRoots(root: root, litDisplayID: litDisplayID))
132+
}
133+
134+
private func queries(
135+
matching types: [XCUIElement.ElementType],
136+
in roots: [XCUIElement]
137+
) -> [XCUIElementQuery] {
138+
types.flatMap { type in roots.map { $0.descendants(matching: type) } }
131139
}
132140
}

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+TextInputProbe.swift‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,7 @@ extension RunnerTests {
179179

180180
/// Resolved before the probe's issue containment, so a lit-panel refusal is not read as no input.
181181
private func textInputQueries(app: XCUIApplication) throws -> [XCUIElementQuery] {
182-
let inputTypes: [XCUIElement.ElementType] = [.textField, .secureTextField, .searchField, .textView]
183-
return try inputTypes.flatMap { try elementQueries(app: app, matching: $0) }
182+
try elementQueries(app: app, matching: .textField, .secureTextField, .searchField, .textView)
184183
}
185184

186185
private func queryTextInputs(

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerRequestPanelTests.swift‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,16 @@ extension RunnerTests {
575575
try assertions(response)
576576
}
577577

578+
/// Runs `body` with the app's window probed as the only window on lit display 3 at `frame`,
579+
/// handing it the count of window-list reads.
580+
@MainActor
581+
func withLitPanelWindow(frame: CGRect, _ body: (_ windowListReads: () -> Int) throws -> Void) throws {
582+
try withLitPanelBookingProbe { _ in
583+
LitPanelBookingProbe.windowFrame = frame
584+
try body { LitPanelBookingProbe.windowListReads }
585+
}
586+
}
587+
578588
@MainActor
579589
private func withLitPanelBookingProbe(composed: Bool = false, _ body: (XCUIElement) throws -> Void) throws {
580590
app.launch()

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+KeyboardTests.swift‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ import AgentDeviceSnapshotPresentation
44
#if AGENT_DEVICE_RUNNER_UNIT_TESTS
55
extension RunnerTests {
66
#if os(iOS)
7+
/// Keyboard reads stay application-wide on a lit-panel request, so a failed lit-window walk
8+
/// cannot turn `keyboard dismiss` into a refusal.
9+
@MainActor
10+
func testKeyboardDismissIgnoresAFailedLitPanelWindowWalk() throws {
11+
try withLitPanelWindow(frame: CGRect(x: 0, y: 0, width: 669, height: 951)) { _ in
12+
litPanelWindowWalkIssueForTesting = true
13+
defer { litPanelWindowWalkIssueForTesting = false }
14+
let command = try runnerCommandFixture(#"{"command":"keyboardDismiss","litPanel":{"displayId":3,"widthPt":669,"heightPt":951}}"#)
15+
setRequestLitPanel(from: command)
16+
let response = try executeOnMainPrepared(command: command, activeApp: app)
17+
XCTAssertTrue(response.ok, String(describing: response.error))
18+
}
19+
}
20+
721
@MainActor
822
func testCapturedKeyboardBandIncludesTheInputAssistantOutsideTheKeyboard() {
923
let keyboard = ElementAttributesFixture(.keyboard, CGRect(x: 0, y: 600, width: 390, height: 244))

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+LifecycleTests.swift‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ private struct AlertCommandTraitsFixture: Decodable {
1212
}
1313

1414
extension RunnerTests {
15+
#if os(iOS)
16+
/// A frame sample resolves the lit panel without booking it, so it never replaces the request's booking.
17+
@MainActor
18+
func testFrameSamplingResolvesTheLitPanelWithoutBookingIt() throws {
19+
try withLitPanelWindow(frame: CGRect(x: 0, y: 0, width: 669, height: 951)) { _ in
20+
setRequestLitPanel(from: try runnerCommandFixture(#"{"command":"snapshot","litPanel":{"displayId":3,"widthPt":669,"heightPt":951}}"#))
21+
_ = captureRunnerFrame(app: app)
22+
XCTAssertNil(litPanelState.surface)
23+
}
24+
}
25+
#endif
26+
1527
func testAlertRetryFactMatchesTheSharedGoldenTable() throws {
1628
let fixtureURL = URL(fileURLWithPath: #filePath)
1729
.deletingLastPathComponent()

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+TextInputProbeTests.swift‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,30 @@ import XCTest
22

33
extension RunnerTests {
44
#if AGENT_DEVICE_RUNNER_UNIT_TESTS && os(iOS)
5+
/// One text-input lookup walks the lit windows once, however many input types it queries.
6+
@MainActor
7+
func testTextInputProbeWalksTheLitWindowsOnce() throws {
8+
try withLitPanelWindow(frame: CGRect(x: 0, y: 0, width: 669, height: 951)) { windowListReads in
9+
setRequestLitPanel(from: try runnerCommandFixture(
10+
#"{"command":"type","litPanel":{"displayId":3,"widthPt":669,"heightPt":951}}"#
11+
))
12+
_ = try probeTextInputs(app: app, point: CGPoint(x: 10, y: 10), contentTranslation: .none)
13+
XCTAssertEqual(windowListReads(), 1)
14+
}
15+
}
16+
17+
/// A content-vector read on a lit-panel request that nothing booked yet books the request's
18+
/// window and answers its origin, instead of refusing for want of an earlier booking.
19+
@MainActor
20+
func testUnbookedLitPanelContentTranslationBooksTheRequestWindow() throws {
21+
try withLitPanelWindow(frame: CGRect(x: 0, y: 80, width: 669, height: 871)) { _ in
22+
setRequestLitPanel(from: try runnerCommandFixture(#"{"command":"type","litPanel":{"displayId":3,"widthPt":669,"heightPt":951}}"#))
23+
guard case .known(let vector) = liveContentTranslation(app: app) else { return XCTFail("no vector") }
24+
XCTAssertEqual(vector, CGSize(width: 0, height: 80))
25+
XCTAssertNotNil(litPanelState.surface)
26+
}
27+
}
28+
529
func testTextInputProbePreservesEnclosingRunnerWait() {
630
app.launchArguments = ["--agent-device-text-entry-regression"]
731
app.launch()

0 commit comments

Comments
 (0)