Skip to content

Commit 755d542

Browse files
committed
refactor: tighten snapshot occlusion heuristic
1 parent 6104895 commit 755d542

1 file changed

Lines changed: 40 additions & 6 deletions

File tree

ios-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests.swift

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,8 +1318,11 @@ final class RunnerTests: XCTestCase {
13181318
}
13191319

13201320
let (flatSnapshots, snapshotRanges) = flattenedSnapshots(rootSnapshot)
1321-
let rootRange = snapshotRanges[ObjectIdentifier(rootSnapshot)] ?? (0, 0)
1322-
let rootLaterNodes = flatSnapshots.suffix(from: rootRange.1 + 1)
1321+
let rootLaterNodes = laterSnapshots(
1322+
for: rootSnapshot,
1323+
in: flatSnapshots,
1324+
ranges: snapshotRanges
1325+
)
13231326
let rootLabel = aggregatedLabel(for: rootSnapshot) ?? rootSnapshot.label.trimmingCharacters(in: .whitespacesAndNewlines)
13241327
let rootIdentifier = rootSnapshot.identifier.trimmingCharacters(in: .whitespacesAndNewlines)
13251328
let rootValue = snapshotValueText(rootSnapshot)
@@ -1356,8 +1359,11 @@ final class RunnerTests: XCTestCase {
13561359
let label = aggregatedLabel(for: snapshot) ?? snapshot.label.trimmingCharacters(in: .whitespacesAndNewlines)
13571360
let identifier = snapshot.identifier.trimmingCharacters(in: .whitespacesAndNewlines)
13581361
let valueText = snapshotValueText(snapshot)
1359-
let snapshotRange = snapshotRanges[ObjectIdentifier(snapshot)] ?? (0, 0)
1360-
let laterNodes = flatSnapshots.suffix(from: snapshotRange.1 + 1)
1362+
let laterNodes = laterSnapshots(
1363+
for: snapshot,
1364+
in: flatSnapshots,
1365+
ranges: snapshotRanges
1366+
)
13611367
let hittable = computedSnapshotHittable(snapshot, viewport: viewport, laterNodes: laterNodes)
13621368
let hasContent = !label.isEmpty || !identifier.isEmpty || (valueText != nil)
13631369
if !isVisibleInViewport(snapshot.frame, viewport) && !hasContent {
@@ -1442,8 +1448,11 @@ final class RunnerTests: XCTestCase {
14421448
let label = aggregatedLabel(for: snapshot) ?? snapshot.label.trimmingCharacters(in: .whitespacesAndNewlines)
14431449
let identifier = snapshot.identifier.trimmingCharacters(in: .whitespacesAndNewlines)
14441450
let valueText = snapshotValueText(snapshot)
1445-
let snapshotRange = snapshotRanges[ObjectIdentifier(snapshot)] ?? (0, 0)
1446-
let laterNodes = flatSnapshots.suffix(from: snapshotRange.1 + 1)
1451+
let laterNodes = laterSnapshots(
1452+
for: snapshot,
1453+
in: flatSnapshots,
1454+
ranges: snapshotRanges
1455+
)
14471456
let hittable = computedSnapshotHittable(snapshot, viewport: viewport, laterNodes: laterNodes)
14481457
if shouldInclude(
14491458
snapshot: snapshot,
@@ -1739,13 +1748,23 @@ final class RunnerTests: XCTestCase {
17391748
let center = CGPoint(x: frame.midX, y: frame.midY)
17401749
if !viewport.contains(center) { return false }
17411750
for node in laterNodes {
1751+
if !isOccludingType(node.elementType) { continue }
17421752
let nodeFrame = node.frame
17431753
if nodeFrame.isNull || nodeFrame.isEmpty { continue }
17441754
if nodeFrame.contains(center) { return false }
17451755
}
17461756
return true
17471757
}
17481758

1759+
private func isOccludingType(_ type: XCUIElement.ElementType) -> Bool {
1760+
switch type {
1761+
case .application, .window:
1762+
return false
1763+
default:
1764+
return true
1765+
}
1766+
}
1767+
17491768
private func flattenedSnapshots(
17501769
_ root: XCUIElementSnapshot
17511770
) -> ([XCUIElementSnapshot], [ObjectIdentifier: (Int, Int)]) {
@@ -1768,6 +1787,21 @@ final class RunnerTests: XCTestCase {
17681787
return (ordered, ranges)
17691788
}
17701789

1790+
private func laterSnapshots(
1791+
for snapshot: XCUIElementSnapshot,
1792+
in ordered: [XCUIElementSnapshot],
1793+
ranges: [ObjectIdentifier: (Int, Int)]
1794+
) -> ArraySlice<XCUIElementSnapshot> {
1795+
guard let (_, subtreeEnd) = ranges[ObjectIdentifier(snapshot)] else {
1796+
return ordered.suffix(from: ordered.count)
1797+
}
1798+
let nextIndex = subtreeEnd + 1
1799+
if nextIndex >= ordered.count {
1800+
return ordered.suffix(from: ordered.count)
1801+
}
1802+
return ordered.suffix(from: nextIndex)
1803+
}
1804+
17711805
private func snapshotValueText(_ snapshot: XCUIElementSnapshot) -> String? {
17721806
guard let value = snapshot.value else { return nil }
17731807
let text = String(describing: value).trimmingCharacters(in: .whitespacesAndNewlines)

0 commit comments

Comments
 (0)