Skip to content

Commit eef1a3f

Browse files
authored
test(ios): stabilize text clearing and snapshot budget regressions (#3802)
* test(ios): make dropped-select-all coverage independent of shortcut recovery * test(ios): isolate capture-budget regression from live prerequisites
1 parent 3d91901 commit eef1a3f

3 files changed

Lines changed: 79 additions & 81 deletions

File tree

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
import XCTest
33
import AgentDeviceSnapshotPresentation
44

5-
/// An element's attributes as XCTest hands them to a query predicate.
5+
/// Snapshot attributes shared by query-predicate and capture-plan fixtures.
66
@MainActor
7-
private final class ElementAttributesFixture: NSObject, XCUIElementSnapshot {
7+
final class ElementAttributesFixture: NSObject, XCUIElementSnapshot {
88
let elementType: XCUIElement.ElementType
99
let frame: CGRect
1010
let label: String
11+
let children: [XCUIElementSnapshot]
1112

12-
init(_ elementType: XCUIElement.ElementType, _ frame: CGRect, label: String = "") {
13+
init(_ elementType: XCUIElement.ElementType, _ frame: CGRect, label: String = "", children: [XCUIElementSnapshot] = []) {
1314
self.elementType = elementType
1415
self.frame = frame
1516
self.label = label
17+
self.children = children
1618
}
1719

1820
var identifier: String { "" }
@@ -26,7 +28,6 @@ private final class ElementAttributesFixture: NSObject, XCUIElementSnapshot {
2628
#if !os(macOS)
2729
var hasFocus: Bool { false }
2830
#endif
29-
var children: [XCUIElementSnapshot] { [] }
3031
var dictionaryRepresentation: [XCUIElement.AttributeName: Any] { [:] }
3132
}
3233

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

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -70,37 +70,35 @@ private final class RunnerSlowSweepQueryStub: NSObject {
7070
}
7171
}
7272

73-
/// Per-run state for `RunnerSlowRootSnapshotStub`: the swizzled IMP cannot capture the original
74-
/// implementation or a test-local flag.
73+
/// The root-read delay exhausts the collapsed-tab query's processing slice. All other facts are
74+
/// fixed, so XCTest's viewport, orientation, keyboard, and large-tree XPCs cannot consume the slice.
7575
private enum RunnerSlowRootSnapshotGate {
76-
/// Longer than half the plan deadline the test passes, so whatever slice is left after the root
77-
/// read is shorter than the read itself, while the real read still has most of `delay` to finish.
7876
static let delay: TimeInterval = 3
79-
static var original: IMP?
8077
static var delayed = false
78+
static var root: XCUIElementSnapshot?
8179
}
8280

83-
/// Stands in for `-[XCUIElement snapshotWithError:]` so the capture's root read, the plan's first
84-
/// tree snapshot, takes at least `RunnerSlowRootSnapshotGate.delay` and then reads the real tree.
8581
private final class RunnerSlowRootSnapshotStub: NSObject {
8682
@objc(snapshotWithError:)
8783
func snapshot() throws -> XCUIElementSnapshot {
88-
if !RunnerSlowRootSnapshotGate.delayed {
89-
RunnerSlowRootSnapshotGate.delayed = true
90-
Thread.sleep(forTimeInterval: RunnerSlowRootSnapshotGate.delay)
84+
RunnerSlowRootSnapshotGate.delayed = true
85+
Thread.sleep(forTimeInterval: RunnerSlowRootSnapshotGate.delay)
86+
guard let root = RunnerSlowRootSnapshotGate.root else {
87+
throw NSError(domain: "AgentDeviceRunner.tests", code: 2)
9188
}
92-
typealias Read = @convention(c) (
93-
AnyObject, Selector, AutoreleasingUnsafeMutablePointer<NSError?>?
94-
) -> XCUIElementSnapshot?
95-
let read = unsafeBitCast(RunnerSlowRootSnapshotGate.original, to: Read.self)
96-
var error: NSError?
97-
guard let snapshot = read(self, #selector(XCUIElement.snapshot), &error) else {
98-
throw error ?? NSError(domain: "AgentDeviceRunner.tests", code: 2)
99-
}
100-
return snapshot
89+
return root
10190
}
10291
}
10392

93+
/// XCTest getters used by the plan's prerequisites and query-sweep fallback. The method swaps are
94+
/// process-wide and restored after this serial XCTest case, as the blocking snapshot stub is above.
95+
private final class RunnerSnapshotFactsStub: NSObject {
96+
@objc var frame: CGRect { CGRect(x: 0, y: 0, width: 390, height: 844) }
97+
@objc var exists: Bool { false }
98+
@objc var allElementsBoundByIndex: [XCUIElement] { [] }
99+
@objc class func interfaceOrientation(forApplication application: AnyObject) -> Int { 1 }
100+
}
101+
104102
extension RunnerTests {
105103
/// The Bluesky feed shape: the tree XPC grinds past its slice. The plan must recover through
106104
/// private AX without queueing the query sweep behind the abandoned XPC, and a fresh process's
@@ -476,48 +474,57 @@ extension RunnerTests {
476474
XCTAssertEqual(quality.reasonCode, "budget")
477475
}
478476

479-
/// A childless tab bar makes the tree tier run the collapsed tab query, and a slow root read leaves too
480-
/// little of the processing slice for it to start. The plan must treat that tree as a tier that
481-
/// ran out of time and move on, not accept the truncated tree as a healthy capture.
477+
/// A childless tab bar needs a query whose estimated cost exceeds the remaining processing slice.
478+
/// The plan must reject that incomplete tree even though its visible buttons otherwise look healthy.
482479
func testPlanMovesPastATreeTierWhoseCollapsedTabQueryCannotStart() throws {
483-
guard
484-
let snapshotMethod = class_getInstanceMethod(
485-
XCUIApplication.self,
486-
#selector(XCUIElement.snapshot)
487-
),
488-
let stubMethod = class_getInstanceMethod(
489-
RunnerSlowRootSnapshotStub.self,
490-
#selector(RunnerSlowRootSnapshotStub.snapshot)
491-
)
492-
else {
493-
XCTFail("unable to install the slow root snapshot stub")
494-
return
480+
let swaps: [(Method?, Method?)] = [
481+
(class_getInstanceMethod(XCUIApplication.self, #selector(XCUIElement.snapshot)),
482+
class_getInstanceMethod(RunnerSlowRootSnapshotStub.self, #selector(RunnerSlowRootSnapshotStub.snapshot))),
483+
(class_getInstanceMethod(XCUIElement.self, #selector(getter: XCUIElement.frame)),
484+
class_getInstanceMethod(RunnerSnapshotFactsStub.self, #selector(getter: RunnerSnapshotFactsStub.frame))),
485+
(class_getInstanceMethod(XCUIElement.self, #selector(getter: XCUIElement.exists)),
486+
class_getInstanceMethod(RunnerSnapshotFactsStub.self, #selector(getter: RunnerSnapshotFactsStub.exists))),
487+
(class_getInstanceMethod(XCUIElementQuery.self, #selector(getter: XCUIElementQuery.allElementsBoundByIndex)),
488+
class_getInstanceMethod(RunnerSnapshotFactsStub.self, #selector(getter: RunnerSnapshotFactsStub.allElementsBoundByIndex))),
489+
(class_getClassMethod(RunnerSynthesizedGesture.self, #selector(RunnerSynthesizedGesture.interfaceOrientation(forApplication:))),
490+
class_getClassMethod(RunnerSnapshotFactsStub.self, #selector(RunnerSnapshotFactsStub.interfaceOrientation(forApplication:)))),
491+
]
492+
var originals: [(Method, IMP)] = []
493+
let previousPrivateAX = privateAXAcquisitionOverrideForTesting
494+
defer {
495+
for (method, original) in originals.reversed() { method_setImplementation(method, original) }
496+
RunnerSlowRootSnapshotGate.root = nil
497+
privateAXAcquisitionOverrideForTesting = previousPrivateAX
498+
clearSnapshotXCTestChannelPenalty(reason: "test-cleanup")
499+
MainActor.assumeIsolated { invalidateCachedTarget(reason: "unit_test_cleanup") }
500+
}
501+
for (method, stub) in swaps {
502+
let method = try XCTUnwrap(method)
503+
let stub = try XCTUnwrap(stub)
504+
originals.append((method, method_setImplementation(method, method_getImplementation(stub))))
495505
}
496-
app.launchArguments = ["--agent-device-crowded-screen", "--agent-device-collapsed-tab-bar"]
497-
app.launch()
498-
XCTAssertTrue(app.staticTexts["agent-device-crowded-row-499"].waitForExistence(timeout: appExistenceTimeout))
499506
let captureTarget = MainActor.assumeIsolated {
507+
let windowFrame = CGRect(x: 0, y: 0, width: 390, height: 844)
508+
let buttons: [XCUIElementSnapshot] = (0..<13).map { index in
509+
ElementAttributesFixture(
510+
.button,
511+
CGRect(x: 20, y: 20 + index * 45, width: 100, height: 30),
512+
label: "Row \(index)"
513+
)
514+
}
515+
let tabBar = ElementAttributesFixture(.tabBar, CGRect(x: 0, y: 790, width: 390, height: 50))
516+
RunnerSlowRootSnapshotGate.root = ElementAttributesFixture(
517+
.application, windowFrame, label: "Fixture",
518+
children: [ElementAttributesFixture(.window, windowFrame, children: buttons + [tabBar])]
519+
)
500520
mainOwned.app = app
501521
mainOwned.bundleId = "com.callstack.agentdevice.runner.collapsed-tab-query-test"
502522
return takeSnapshotCaptureTarget(app: app)
503523
}
524+
privateAXAcquisitionOverrideForTesting = { nil }
504525
snapshotXCTestPenaltyWarmupExemption.isPending = false
505526
clearSnapshotXCTestChannelPenalty(reason: "test-setup")
506527
RunnerSlowRootSnapshotGate.delayed = false
507-
RunnerSlowRootSnapshotGate.original = method_setImplementation(
508-
snapshotMethod,
509-
method_getImplementation(stubMethod)
510-
)
511-
defer {
512-
if let original = RunnerSlowRootSnapshotGate.original {
513-
method_setImplementation(snapshotMethod, original)
514-
}
515-
clearSnapshotXCTestChannelPenalty(reason: "test-cleanup")
516-
MainActor.assumeIsolated {
517-
invalidateCachedTarget(reason: "unit_test_cleanup")
518-
}
519-
app.terminate()
520-
}
521528

522529
let payload = try runDeferredPlanOffMain(
523530
target: captureTarget,

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

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -194,47 +194,37 @@ extension RunnerTests {
194194
XCTAssertEqual(String(describing: textField.value ?? ""), "")
195195
}
196196

197-
/// Drops the select-all of the first `remaining` replacing posts, the way a React Native field in
198-
/// a freshly launched app does, so each of those passes deletes only the last character.
199-
final class SelectAllDroppingSynthesizer: TextEntrySynthesizing {
200-
var remaining: Int
201-
202-
init(dropping count: Int) {
203-
remaining = count
204-
}
205-
197+
/// Drops every select-all while preserving real synthesized delete and text delivery.
198+
struct SelectAllDroppingSynthesizer: TextEntrySynthesizing {
206199
func enterText(
207200
app: XCUIApplication,
208201
text: String,
209202
replacingExistingText: Bool
210203
) -> SynthesizedTextEntryAction {
211-
let dropsSelectAll = replacingExistingText && remaining > 0
212-
if dropsSelectAll { remaining -= 1 }
213-
return PrivateXCTestTextEntrySynthesizer().enterText(
204+
PrivateXCTestTextEntrySynthesizer().enterText(
214205
app: app,
215206
text: text,
216-
replacingExistingText: replacingExistingText && !dropsSelectAll
207+
replacingExistingText: false
217208
)
218209
}
219210
}
220211

221-
/// Two fixed clear passes left "Clie" of "Client" in 3 of 5 fills on a freshly launched React
222-
/// Native app that dropped its select-alls, and the fill typed after it. The clear now repeats
223-
/// until the field reads empty, and types nothing over text it could not remove.
212+
/// A later Command-A can also be dropped by XCTest/iOS, so neither case relies on it succeeding.
213+
/// Three characters require more than the old two clear passes; refusal exceeds the clear budget.
224214
@MainActor
225215
func testSynthesizedReplacementClearsPastDroppedSelectAlls() throws {
226-
let field = try focusSynthesizedReplacementField()
227-
defer { tearDownSynthesizedReplacementField() }
228-
let frame = field.frame
229-
let found = try requireTextInputAtPoint(app: app, x: frame.midX, y: frame.midY)
230-
let cases: [(dropped: Int, failure: TextEntryFailure?)] = [
231-
(2, nil),
232-
(SynthesizedTextPlan.Step.maxClearPassCount, .clearNotObserved),
216+
let cases: [(seed: String, failure: TextEntryFailure?)] = [
217+
("old", nil),
218+
(String(repeating: "x", count: SynthesizedTextPlan.Step.maxClearPassCount + 1), .clearNotObserved),
233219
]
234220

235221
for testCase in cases {
236-
let label = "\(testCase.dropped) select-alls dropped"
237-
let seeded = try replaceSynthesizedFieldText(field, text: "stale", commandId: "seed-\(testCase.dropped)")
222+
let field = try focusSynthesizedReplacementField()
223+
defer { tearDownSynthesizedReplacementField() }
224+
let frame = field.frame
225+
let found = try requireTextInputAtPoint(app: app, x: frame.midX, y: frame.midY)
226+
let label = "\(testCase.seed.count) characters, all select-alls dropped"
227+
let seeded = try replaceSynthesizedFieldText(field, text: testCase.seed, commandId: "seed-\(testCase.seed.count)")
238228
XCTAssertTrue(seeded.ok, String(describing: seeded.error))
239229
let result = typeTextReliably(
240230
app: app,
@@ -248,7 +238,7 @@ extension RunnerTests {
248238
delaySeconds: 0,
249239
repairMode: .replacement,
250240
xCTestChannelPenalized: true,
251-
synthesizer: SelectAllDroppingSynthesizer(dropping: testCase.dropped)
241+
synthesizer: SelectAllDroppingSynthesizer()
252242
)
253243

254244
let value = String(describing: field.value ?? "")

0 commit comments

Comments
 (0)