Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ extension RunnerTests {
)
}

var seen = Set<String>()
var regularTree = RegularSnapshotTree(policy: .platformDefault)
var stack: [SnapshotTraversalEntry] = []
if Self.canDescendAtRawDepth(0, hint: hint) {
stack = rootChildren.map {
Expand All @@ -238,29 +238,15 @@ extension RunnerTests {
index: nodes.count,
parentIndex: entry.parentIndex
)
let key = Self.snapshotTraversalIdentity(
elementType: snapshot.elementType,
label: evaluation.label,
identifier: evaluation.identifier,
frame: snapshot.frame
)
let isDuplicate = seen.contains(key)
if !isDuplicate {
seen.insert(key)
}

// A repeated node collapses into its parent: its children re-parent onto `entry.parentIndex`,
// so identical rows share one addressable owner.
let currentIndex = isDuplicate ? entry.parentIndex : nodes.count
let included = regularTree.append(node, elementType: String(describing: snapshot.elementType), to: &nodes)
let currentIndex = included ? node.index : entry.parentIndex
for child in snapshot.children.reversed() {
stack.append(
SnapshotTraversalEntry(snapshot: child, depth: depth + 1, parentIndex: currentIndex)
)
}

if isDuplicate { continue }

nodes.append(node)
if !included { continue }
if nodes.count > Self.regularSnapshotMaxNodes {
throw regularSnapshotTooLargeFailure(nodeCount: nodes.count)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,19 +303,6 @@ extension RunnerTests {
#endif
}

static func snapshotTraversalIdentity(
elementType: XCUIElement.ElementType,
label: String,
identifier: String,
frame: CGRect
) -> String {
#if os(iOS)
"\(elementType)-\(label)-\(identifier)-\(frame.origin.x)-\(frame.origin.y)-\(frame.width)-\(frame.height)"
#else
return "\(elementType)-\(label)-\(identifier)-\(frame.origin.x)-\(frame.origin.y)"
#endif
}

private func aggregatedLabel(for snapshot: XCUIElementSnapshot, depth: Int = 0) -> String? {
if depth > 4 { return nil }
let text = snapshot.label.trimmingCharacters(in: .whitespacesAndNewlines)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import AgentDeviceSnapshotPresentation
import XCTest

extension RunnerTests {
#if AGENT_DEVICE_RUNNER_UNIT_TESTS
func testSnapshotTraversalIdentityPreservesSameOriginNodesWithDifferentBounds() {
let wrapper = Self.snapshotTraversalIdentity(
elementType: .other,
let wrapper = RegularSnapshotTree.Policy.platformDefault.identity(
elementType: "Other",
label: "Article",
identifier: "",
frame: CGRect(x: 0, y: 97, width: 393, height: 48)
)
let leaf = Self.snapshotTraversalIdentity(
elementType: .other,
let leaf = RegularSnapshotTree.Policy.platformDefault.identity(
elementType: "Other",
label: "Article",
identifier: "",
frame: CGRect(x: 0, y: 97, width: 131, height: 48)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import CoreGraphics

/// Duplicate handling for the runner's regular XCTest tree walk.
public struct RegularSnapshotTree {
public enum Policy: String, Decodable {
case fullBounds
case originOnly
case tvOS

public static var platformDefault: Self {
#if os(iOS)
return .fullBounds
#elseif os(tvOS)
return .tvOS
#else
return .originOnly
#endif
}

public func identity(elementType: String, label: String, identifier: String, frame: CGRect) -> String {
let originIdentity = "\(elementType)-\(label)-\(identifier)-\(frame.origin.x)-\(frame.origin.y)"
if self == .fullBounds {
return "\(originIdentity)-\(frame.width)-\(frame.height)"
}
return originIdentity
}
}

private let policy: Policy
private var firstIndexByIdentity: [String: Int] = [:]

public init(policy: Policy) {
self.policy = policy
}

/// Appends an admitted node. Children of a rejected duplicate retain the duplicate's parent.
public mutating func append(_ node: RawAXNode, elementType: String, to nodes: inout [RawAXNode]) -> Bool {
let identity = policy.identity(
elementType: elementType,
label: node.label ?? "",
identifier: node.identifier ?? "",
frame: node.rect.cgRect
)
if let firstIndex = firstIndexByIdentity[identity] {
if policy == .tvOS,
node.focused == true,
node.type == "Other",
(node.label != nil || node.identifier != nil),
node.parentIndex == firstIndex,
nodes[firstIndex].label == node.label,
nodes[firstIndex].identifier == node.identifier
{
nodes[firstIndex].focused = true
}
return false
}
firstIndexByIdentity[identity] = node.index
nodes.append(node)
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public struct RawAXNode: Equatable {
public let placeholder: String?
public var rect: SnapshotRect
public let enabled: Bool
public let focused: Bool?
public internal(set) var focused: Bool?
public let selected: Bool?
/// Geometric actionability; `nil` when the capture has no viewport box to decide it against.
public var hittable: Bool?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ private struct ConformanceInput: Decodable {
let depth: Int?
let scope: String?
let foldPolicy: String
let regularTreePolicy: RegularSnapshotTree.Policy?
let viewport: SnapshotRect
let nodes: [Node]
}
Expand Down Expand Up @@ -54,33 +55,54 @@ private struct BatchOutput: Encodable {

private func acquisition(for input: ConformanceInput) -> SnapshotAcquisition {
let inputOptions = options(for: input)
let rawNodes = input.nodes.map { node in
RawAXNode(
index: node.index,
type: node.type,
label: node.label,
identifier: node.identifier,
value: node.value,
rect: node.rect,
enabled: node.enabled,
focused: node.focused,
selected: node.selected,
hittable: node.hittable,
depth: node.depth,
parentIndex: node.parentIndex,
hiddenContentAbove: node.hiddenContentAbove,
hiddenContentBelow: node.hiddenContentBelow
)
}
let hint = SnapshotPresentation.captureHint(for: inputOptions)
let nodes = hint.isRaw ? rawNodes : input.regularTreePolicy.map { regularTreeNodes(rawNodes, policy: $0) } ?? rawNodes
return SnapshotAcquisition(
hint: SnapshotPresentation.captureHint(for: inputOptions),
nodes: input.nodes.map { node in
RawAXNode(
index: node.index,
type: node.type,
label: node.label,
identifier: node.identifier,
value: node.value,
rect: node.rect,
enabled: node.enabled,
focused: node.focused,
selected: node.selected,
hittable: node.hittable,
depth: node.depth,
parentIndex: node.parentIndex,
hiddenContentAbove: node.hiddenContentAbove,
hiddenContentBelow: node.hiddenContentBelow
)
},
hint: hint,
nodes: nodes,
truncated: false,
effectiveDepth: nil,
// The host engine refuses to fold without a viewport, so the differential always reports one.
viewport: .reported(box: input.viewport.cgRect)
)
}

private func regularTreeNodes(_ rawNodes: [RawAXNode], policy: RegularSnapshotTree.Policy) -> [RawAXNode] {
var tree = RegularSnapshotTree(policy: policy)
var retained: [RawAXNode] = []
var childParents: [Int: Int] = [:]
for raw in rawNodes {
let parentIndex = raw.parentIndex.flatMap { childParents[$0] }
let node = raw.reindexed(index: retained.count, depth: raw.depth, parentIndex: parentIndex)
if raw.parentIndex == nil {
retained.append(node)
childParents[raw.index] = node.index
} else {
let included = tree.append(node, elementType: node.type, to: &retained)
childParents[raw.index] = included ? node.index : parentIndex
}
}
return retained
}

private func options(for input: ConformanceInput) -> PresentationOptions {
PresentationOptions(
interactiveOnly: input.interactiveOnly,
Expand Down
104 changes: 104 additions & 0 deletions contracts/fixtures/ios-snapshot-engine-conformance.json
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,110 @@
}
]
}
},
{
"name": "focus-scaled cell content survives the plain-viewport fold",
"swift": true,
"projection": "regular",
"interactiveOnly": false,
"depth": null,
"scope": null,
"foldPolicy": "plain-viewport",
"truncated": false,
"residue": [],
"nodes": [
{
"index": 0,
"type": "Application",
"label": "App",
"rect": { "x": 0, "y": 0, "width": 320, "height": 240 },
"enabled": true,
"hittable": false,
"depth": 0
},
{
"index": 1,
"type": "CollectionView",
"label": "Row",
"rect": { "x": 0, "y": 80, "width": 320, "height": 100 },
"enabled": true,
"hittable": false,
"depth": 1,
"parentIndex": 0
},
{
"index": 2,
"type": "Cell",
"label": "Focused tile",
"rect": { "x": 40, "y": 80, "width": 80, "height": 100 },
"enabled": true,
"focused": true,
"hittable": true,
"depth": 2,
"parentIndex": 1
},
{
"index": 3,
"type": "StaticText",
"label": "Focus-scaled content",
"rect": { "x": 38, "y": 77.5, "width": 84, "height": 105 },
"enabled": true,
"hittable": false,
"depth": 3,
"parentIndex": 2
}
],
"expected": {
"outcome": "success",
"truncated": false,
"residue": [],
"nodes": [
{
"index": 0,
"type": "Application",
"label": "App",
"rect": { "x": 0, "y": 0, "width": 320, "height": 240 },
"depth": 0,
"parentIndex": null,
"hittable": false,
"hiddenContentAbove": false,
"hiddenContentBelow": false
},
{
"index": 1,
"type": "CollectionView",
"label": "Row",
"rect": { "x": 0, "y": 80, "width": 320, "height": 100 },
"depth": 1,
"parentIndex": 0,
"hittable": false,
"hiddenContentAbove": false,
"hiddenContentBelow": false
},
{
"index": 2,
"type": "Cell",
"label": "Focused tile",
"rect": { "x": 40, "y": 80, "width": 80, "height": 100 },
"depth": 2,
"parentIndex": 1,
"hittable": true,
"hiddenContentAbove": false,
"hiddenContentBelow": false
},
{
"index": 3,
"type": "StaticText",
"label": "Focus-scaled content",
"rect": { "x": 38, "y": 77.5, "width": 84, "height": 105 },
"depth": 3,
"parentIndex": 2,
"hittable": false,
"hiddenContentAbove": false,
"hiddenContentBelow": false
}
]
}
}
]
}
1 change: 1 addition & 0 deletions docs/adr/0004-ios-snapshot-backend-strategy.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Before a Simulator capture uses the bridge, it proves host absence as ADR 0038 "
If the proof fails or a host is present, use the runner for that capture without changing the app-generation circuit.

Decision history: #3801 permits acquisition overflow from system alerts at large Dynamic Type sizes. Presentation clips it with one `regular-geometry-clipped` quality note instead of failing the capture.
Decision history: #4654 states that the fold policy is producer evidence. The Apple runner compiles its policy per runtime — the projection cursor on iPhone/iPad, plain viewport intersection on tvOS and visionOS — and the host revalidation of an already presented payload reuses that policy instead of its own default, because the tvOS focus effect scales a focused cell past its row by design.

## Rationale

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type DifferentialCase = Readonly<{
scope: string | null;
foldPolicy: 'cursor-projected' | 'plain-viewport';
viewport: Rect;
regularTreePolicy?: 'fullBounds' | 'originOnly' | 'tvOS';
nodes: readonly RawSnapshotNode[];
requiredLabels?: readonly string[];
absentLabels?: readonly string[];
Expand Down Expand Up @@ -82,7 +83,7 @@ export function compareDifferentialCases(
/* c8 ignore stop */

/* c8 ignore start */
function runSwiftCases(cases: readonly DifferentialCase[]): DifferentialOutcome[] {
export function runSwiftCases(cases: readonly DifferentialCase[]): DifferentialOutcome[] {
const stdout = execFileSync(swiftConformanceExecutable(), [], {
cwd: REPO_ROOT,
encoding: 'utf8',
Expand Down
1 change: 1 addition & 0 deletions packages/capture-kit/src/ios-snapshot-engine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export { presentIosSnapshot, publishIosSnapshot } from './engine.ts';
export { presentIosRunnerSnapshot } from './runner-presentation.ts';
export { buildIosInteractiveSnapshotPresentation } from './semantic-index.ts';
export { IosSnapshotEngineError, toIosSnapshotEngineErrorDetails } from './types.ts';
export type { IosSnapshotFoldPolicy } from './types.ts';
export type { SnapshotTreeRuleContext } from './tree.ts';
10 changes: 10 additions & 0 deletions packages/platform-apple/src/__tests__/device-fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ export const IOS_DEVICE: DeviceInfo = {
booted: true,
};

export const TVOS_SIMULATOR: DeviceInfo = {
platform: 'apple',
id: 'tv-sim-1',
name: 'Apple TV 4K',
kind: 'simulator',
target: 'tv',
appleOs: 'tvos',
booted: true,
};

export const MACOS_DEVICE: DeviceInfo = {
platform: 'apple',
id: 'host-macos-local',
Expand Down
Loading
Loading