Skip to content

Commit 521e511

Browse files
thymikeeclaude
andcommitted
perf(ios): take a synthesized press series' touch frame from its context
A press series read the app frame and the windows query for its first step's touch frame before any step ran. On a tree XCTest cannot serialize each of those reads costs about 2 s of XCTest retries, and now that held presses run as synthesized sequence steps that cost came before every hold. A sequence with a synthesized context reads the frame from that context, as the single longPress command does. The long-press test now also pins zero frame reads and the held duration of each synthesized hold. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8448421 commit 521e511

4 files changed

Lines changed: 40 additions & 18 deletions

File tree

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,7 @@ extension RunnerTests {
235235
synthesizedLongPressAt(app: activeApp, x: x, y: y, durationMs: durationMs, context: context)
236236
}) {
237237
case .performed(let timing):
238-
let touchFrame = context.map {
239-
TouchVisualizationFrame(
240-
x: Double($0.referenceFrame.minX) + x,
241-
y: Double($0.referenceFrame.minY) + y,
242-
referenceWidth: Double($0.referenceFrame.width),
243-
referenceHeight: Double($0.referenceFrame.height)
244-
)
245-
}
238+
let touchFrame = context.map { touchVisualizationFrame(x: x, y: y, context: $0) }
246239
return gestureResponse(message: "long pressed", timing: timing, frame: .touch(touchFrame))
247240
case .xctestFallback(let message, let hint):
248241
fallback = GestureFallback(strategy: "xctest-coordinate-long-press", message: message, hint: hint)

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,16 @@ extension RunnerTests {
3535
}
3636
}
3737

38-
// Touch frame resolves from the first step's coords so recording-gestures works unchanged.
38+
let synthesizedContext = synthesizedSequenceCoordinateContext(steps: steps, app: activeApp)
39+
40+
// Touch frame resolves from the first step's coords so recording-gestures works unchanged. A
41+
// synthesized context already names the frame, so the app tree is not read for it again.
3942
let firstStep = steps[0]
4043
let firstFrame = (firstStep.x != nil && firstStep.y != nil)
41-
? resolvedTouchVisualizationFrame(app: activeApp, x: firstStep.x!, y: firstStep.y!)
44+
? synthesizedContext.map { touchVisualizationFrame(x: firstStep.x!, y: firstStep.y!, context: $0) }
45+
?? resolvedTouchVisualizationFrame(app: activeApp, x: firstStep.x!, y: firstStep.y!)
4246
: nil
4347

44-
let synthesizedContext = synthesizedSequenceCoordinateContext(steps: steps, app: activeApp)
45-
4648
let execution = assembleSequenceExecution(steps: steps) { _, step in
4749
performSequenceStep(step, activeApp: activeApp, synthesizedContext: synthesizedContext)
4850
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,21 @@ extension RunnerTests {
296296
)
297297
}
298298

299+
/// The touch visualization frame of a synthesized gesture, read from its context instead of the
300+
/// app tree.
301+
func touchVisualizationFrame(
302+
x: Double,
303+
y: Double,
304+
context: SynthesizedCoordinateContext
305+
) -> TouchVisualizationFrame {
306+
TouchVisualizationFrame(
307+
x: Double(context.referenceFrame.minX) + x,
308+
y: Double(context.referenceFrame.minY) + y,
309+
referenceWidth: Double(context.referenceFrame.width),
310+
referenceHeight: Double(context.referenceFrame.height)
311+
)
312+
}
313+
299314
func resolvedDragVisualizationFrame(
300315
app: XCUIApplication,
301316
x: Double,

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ import ObjectiveC.runtime
88
/// depth 62 or more): the windows query finds no window and every element frame, the application's
99
/// included, reads as null.
1010
private final class RunnerUnserializableTreeStub: NSObject {
11+
/// Each element frame read costs XCTest's retries on such a tree, about 2 s each.
12+
static var frameReads = 0
1113
@objc var allElementsBoundByIndex: [XCUIElement] { [] }
12-
@objc var frame: CGRect { .null }
14+
@objc var frame: CGRect {
15+
Self.frameReads += 1
16+
return .null
17+
}
1318
}
1419

1520
/// Counts XCTest coordinate presses without performing them.
@@ -32,13 +37,12 @@ extension RunnerTests {
3237
(class_getInstanceMethod(XCUICoordinate.self, #selector(XCUICoordinate.press(forDuration:))),
3338
class_getInstanceMethod(RunnerCoordinatePressCounter.self, #selector(RunnerCoordinatePressCounter.press(forDuration:)))),
3439
]
35-
var originals: [(Method, IMP)] = []
36-
for (target, stub) in swaps {
37-
let target = try XCTUnwrap(target)
38-
let stub = try XCTUnwrap(stub)
39-
originals.append((target, method_setImplementation(target, method_getImplementation(stub))))
40+
let resolved = try swaps.map { (try XCTUnwrap($0.0), try XCTUnwrap($0.1)) }
41+
let originals = resolved.map { target, stub in
42+
(target, method_setImplementation(target, method_getImplementation(stub)))
4043
}
4144
RunnerCoordinatePressCounter.presses = 0
45+
RunnerUnserializableTreeStub.frameReads = 0
4246
return {
4347
for (method, original) in originals.reversed() { method_setImplementation(method, original) }
4448
}
@@ -81,6 +85,14 @@ extension RunnerTests {
8185
XCTAssertNil(longPressResponse.data?.gestureFallback)
8286
XCTAssertEqual(pressSeriesResponse.data?.completedSteps, 2)
8387
XCTAssertEqual(RunnerCoordinatePressCounter.presses, 0)
88+
XCTAssertEqual(RunnerUnserializableTreeStub.frameReads, 0)
89+
let steps = pressSeriesResponse.data?.sequenceResults ?? []
90+
XCTAssertEqual(steps.count, 2)
91+
let holds = [(longPressResponse.data?.gestureStartUptimeMs, longPressResponse.data?.gestureEndUptimeMs)]
92+
+ steps.map { ($0.gestureStartUptimeMs, $0.gestureEndUptimeMs) }
93+
for (start, end) in holds {
94+
XCTAssertGreaterThanOrEqual(try XCTUnwrap(end) - XCTUnwrap(start), 600)
95+
}
8496
} else {
8597
XCTAssertEqual(longPressCoordinatePresses, 1)
8698
XCTAssertEqual(longPressResponse.data?.gestureFallback, "xctest-coordinate-long-press")

0 commit comments

Comments
 (0)