Skip to content

Commit 5863a69

Browse files
committed
refactor(ios): share snapshot filter predicates
1 parent 0425df2 commit 5863a69

3 files changed

Lines changed: 305 additions & 49 deletions

File tree

‎ios-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+AXSnapshotFallback.swift‎

Lines changed: 101 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -107,34 +107,21 @@ extension RunnerTests {
107107
let typeName = elementTypeName(rawElementType: rawType)
108108
let enabled = privateAXBool(rawNode["enabled"]) ?? true
109109
let visible = isVisibleInViewport(rect, viewport)
110-
let hasContent = !label.isEmpty || !identifier.isEmpty || !value.isEmpty
111-
let isRoot = parentIndex == nil
112-
113-
// Scope selects a subtree, matching regular snapshot semantics: once a node matches,
114-
// every descendant is inside scope and only the normal option filters apply to it.
115-
let scope = options.scope?.trimmingCharacters(in: .whitespacesAndNewlines)
116-
let scopeActive = (scope?.isEmpty == false)
117-
let matchesScope: Bool
118-
if scopeActive, let scope {
119-
let haystack = [label, identifier, value].joined(separator: "\n")
120-
matchesScope = haystack.localizedCaseInsensitiveContains(scope)
121-
} else {
122-
matchesScope = false
123-
}
124-
let nowInsideScope = insideMatchedScope || matchesScope
125-
126-
let include: Bool
127-
if isRoot {
128-
include = true
129-
} else if scopeActive && !nowInsideScope {
130-
include = false
131-
} else if options.interactiveOnly && !visible {
132-
include = false
133-
} else if options.compact {
134-
include = hasContent || privateAXLikelyInteractive(rawElementType: rawType)
135-
} else {
136-
include = true
137-
}
110+
let compactCandidate = privateAXFlatCompactCandidate(rawElementType: rawType)
111+
let filterDecision = flatSnapshotFilterDecision(
112+
FlatSnapshotFilterNode(
113+
isRoot: parentIndex == nil,
114+
label: label,
115+
identifier: identifier,
116+
valueText: value.isEmpty ? nil : value,
117+
visible: visible,
118+
compactCandidate: compactCandidate
119+
),
120+
options: options,
121+
insideMatchedScope: insideMatchedScope
122+
)
123+
let include = filterDecision.include
124+
let nowInsideScope = filterDecision.insideMatchedScope
138125

139126
let currentIndex: Int?
140127
if include {
@@ -150,7 +137,7 @@ extension RunnerTests {
150137
enabled: enabled,
151138
focused: privateAXBool(rawNode["focused"]) == true ? true : nil,
152139
selected: privateAXBool(rawNode["selected"]) == true ? true : nil,
153-
hittable: visible && enabled && privateAXLikelyInteractive(rawElementType: rawType),
140+
hittable: visible && enabled && compactCandidate,
154141
depth: depth,
155142
parentIndex: parentIndex,
156143
hiddenContentAbove: nil,
@@ -178,23 +165,12 @@ extension RunnerTests {
178165
}
179166

180167
private func elementTypeName(rawElementType: Int) -> String {
181-
if let raw = UInt(exactly: rawElementType),
182-
let type = XCUIElement.ElementType(rawValue: raw)
183-
{
168+
if let type = flatSnapshotElementType(rawElementType: rawElementType) {
184169
return elementTypeName(type)
185170
}
186171
return "Element(\(rawElementType))"
187172
}
188173

189-
private func privateAXLikelyInteractive(rawElementType: Int) -> Bool {
190-
guard let raw = UInt(exactly: rawElementType),
191-
let type = XCUIElement.ElementType(rawValue: raw)
192-
else {
193-
return false
194-
}
195-
return interactiveTypes.contains(type) || Self.scrollContainerTypes.contains(type)
196-
}
197-
198174
private func privateAXString(_ value: Any?) -> String {
199175
guard let value else { return "" }
200176
if let string = value as? String {
@@ -268,4 +244,88 @@ extension RunnerTests {
268244
XCTAssertTrue(labels.contains("Post body without the scope text"))
269245
XCTAssertFalse(labels.contains("unrelated sibling"))
270246
}
247+
248+
func testPrivateAXCompactInteractiveFiltersLoginLikeHiddenDrawer() {
249+
let tree: [String: Any] = [
250+
"type": Int(XCUIElement.ElementType.application.rawValue),
251+
"label": "Blue Sky",
252+
"frame": ["x": 0, "y": 0, "width": 390, "height": 844],
253+
"children": [
254+
[
255+
"type": Int(XCUIElement.ElementType.scrollView.rawValue),
256+
"frame": ["x": 0, "y": 0, "width": 390, "height": 844],
257+
"children": [
258+
[
259+
"type": Int(XCUIElement.ElementType.image.rawValue),
260+
"label": "Callstack",
261+
"frame": ["x": 145, "y": 104, "width": 100, "height": 100],
262+
"children": [],
263+
],
264+
[
265+
"type": Int(XCUIElement.ElementType.staticText.rawValue),
266+
"label": "Welcome back",
267+
"frame": ["x": 32, "y": 260, "width": 326, "height": 32],
268+
"children": [],
269+
],
270+
[
271+
"type": Int(XCUIElement.ElementType.textField.rawValue),
272+
"label": "Email",
273+
"identifier": "login.email",
274+
"frame": ["x": 32, "y": 348, "width": 326, "height": 48],
275+
"children": [],
276+
],
277+
[
278+
"type": Int(XCUIElement.ElementType.secureTextField.rawValue),
279+
"label": "Password",
280+
"identifier": "login.password",
281+
"frame": ["x": 32, "y": 412, "width": 326, "height": 48],
282+
"children": [],
283+
],
284+
[
285+
"type": Int(XCUIElement.ElementType.button.rawValue),
286+
"label": "Sign in",
287+
"identifier": "login.submit",
288+
"frame": ["x": 32, "y": 492, "width": 326, "height": 52],
289+
"children": [],
290+
],
291+
[
292+
"type": Int(XCUIElement.ElementType.link.rawValue),
293+
"label": "Forgot password?",
294+
"frame": ["x": 128, "y": 568, "width": 134, "height": 32],
295+
"children": [],
296+
],
297+
[
298+
"type": Int(XCUIElement.ElementType.button.rawValue),
299+
"label": "Admin settings",
300+
"frame": ["x": -260, "y": 184, "width": 220, "height": 44],
301+
"children": [],
302+
],
303+
[
304+
"type": Int(XCUIElement.ElementType.other.rawValue),
305+
"frame": ["x": 16, "y": 184, "width": 220, "height": 44],
306+
"children": [],
307+
],
308+
],
309+
]
310+
],
311+
]
312+
var nodes: [SnapshotNode] = []
313+
appendPrivateAXNode(
314+
tree,
315+
to: &nodes,
316+
options: SnapshotOptions(
317+
interactiveOnly: true, compact: true, depth: nil, scope: nil, raw: false),
318+
viewport: CGRect(x: 0, y: 0, width: 390, height: 844),
319+
depth: 0,
320+
parentIndex: nil,
321+
insideMatchedScope: false
322+
)
323+
324+
let labels = nodes.compactMap { $0.label }
325+
XCTAssertEqual(
326+
labels,
327+
["Blue Sky", "Callstack", "Welcome back", "Email", "Password", "Sign in", "Forgot password?"]
328+
)
329+
XCTAssertFalse(labels.contains("Admin settings"))
330+
}
271331
}
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
import XCTest
2+
3+
struct FlatSnapshotFilterNode {
4+
let isRoot: Bool
5+
let label: String
6+
let identifier: String
7+
let valueText: String?
8+
let visible: Bool
9+
let compactCandidate: Bool
10+
11+
var hasContent: Bool {
12+
return !label.isEmpty || !identifier.isEmpty || valueText != nil
13+
}
14+
15+
func matchesScope(_ scope: String) -> Bool {
16+
let haystack = [label, identifier, valueText ?? ""].joined(separator: "\n")
17+
return haystack.localizedCaseInsensitiveContains(scope)
18+
}
19+
}
20+
21+
struct FlatSnapshotFilterDecision {
22+
let include: Bool
23+
let insideMatchedScope: Bool
24+
}
25+
26+
extension RunnerTests {
27+
func flatSnapshotFilterDecision(
28+
_ node: FlatSnapshotFilterNode,
29+
options: SnapshotOptions,
30+
insideMatchedScope: Bool
31+
) -> FlatSnapshotFilterDecision {
32+
let scope = options.scope?.trimmingCharacters(in: .whitespacesAndNewlines)
33+
let scopeActive = scope?.isEmpty == false
34+
let matchesScope: Bool
35+
if scopeActive, let scope {
36+
matchesScope = node.matchesScope(scope)
37+
} else {
38+
matchesScope = false
39+
}
40+
let nowInsideScope = insideMatchedScope || matchesScope
41+
42+
let include: Bool
43+
if node.isRoot {
44+
include = true
45+
} else if scopeActive && !nowInsideScope {
46+
include = false
47+
} else if options.interactiveOnly && !node.visible {
48+
include = false
49+
} else if options.compact {
50+
include = node.hasContent || node.compactCandidate
51+
} else {
52+
include = true
53+
}
54+
55+
return FlatSnapshotFilterDecision(include: include, insideMatchedScope: nowInsideScope)
56+
}
57+
58+
func querySweepFlatCompactCandidate(
59+
elementType: XCUIElement.ElementType,
60+
hittable: Bool
61+
) -> Bool {
62+
return hittable || interactiveTypes.contains(elementType)
63+
}
64+
65+
func privateAXFlatCompactCandidate(rawElementType: Int) -> Bool {
66+
guard let type = flatSnapshotElementType(rawElementType: rawElementType) else {
67+
return false
68+
}
69+
return interactiveTypes.contains(type) || Self.scrollContainerTypes.contains(type)
70+
}
71+
72+
func flatSnapshotElementType(rawElementType: Int) -> XCUIElement.ElementType? {
73+
guard let raw = UInt(exactly: rawElementType),
74+
let type = XCUIElement.ElementType(rawValue: raw)
75+
else {
76+
return nil
77+
}
78+
return type
79+
}
80+
81+
func testFlatSnapshotFilterDecisionMatrixCoversOptions() {
82+
let visibleContent = FlatSnapshotFilterNode(
83+
isRoot: false,
84+
label: "Welcome back",
85+
identifier: "",
86+
valueText: nil,
87+
visible: true,
88+
compactCandidate: false
89+
)
90+
let hiddenInteractive = FlatSnapshotFilterNode(
91+
isRoot: false,
92+
label: "Hidden menu",
93+
identifier: "",
94+
valueText: nil,
95+
visible: false,
96+
compactCandidate: true
97+
)
98+
let decorative = FlatSnapshotFilterNode(
99+
isRoot: false,
100+
label: "",
101+
identifier: "",
102+
valueText: nil,
103+
visible: true,
104+
compactCandidate: false
105+
)
106+
107+
XCTAssertTrue(
108+
flatSnapshotFilterDecision(
109+
visibleContent,
110+
options: SnapshotOptions(
111+
interactiveOnly: false, compact: true, depth: nil, scope: nil, raw: false),
112+
insideMatchedScope: false
113+
).include
114+
)
115+
XCTAssertFalse(
116+
flatSnapshotFilterDecision(
117+
hiddenInteractive,
118+
options: SnapshotOptions(
119+
interactiveOnly: true, compact: true, depth: nil, scope: nil, raw: false),
120+
insideMatchedScope: false
121+
).include
122+
)
123+
XCTAssertFalse(
124+
flatSnapshotFilterDecision(
125+
decorative,
126+
options: SnapshotOptions(
127+
interactiveOnly: false, compact: true, depth: nil, scope: nil, raw: false),
128+
insideMatchedScope: false
129+
).include
130+
)
131+
XCTAssertTrue(
132+
flatSnapshotFilterDecision(
133+
decorative,
134+
options: SnapshotOptions(
135+
interactiveOnly: false, compact: false, depth: nil, scope: nil, raw: false),
136+
insideMatchedScope: false
137+
).include
138+
)
139+
}
140+
141+
func testFlatSnapshotFilterDecisionCarriesSubtreeScopeState() {
142+
let scopeRoot = FlatSnapshotFilterNode(
143+
isRoot: false,
144+
label: "",
145+
identifier: "homeScreen",
146+
valueText: nil,
147+
visible: true,
148+
compactCandidate: false
149+
)
150+
let unmatchedDescendant = FlatSnapshotFilterNode(
151+
isRoot: false,
152+
label: "Post body without the scope text",
153+
identifier: "",
154+
valueText: nil,
155+
visible: true,
156+
compactCandidate: false
157+
)
158+
let options = SnapshotOptions(
159+
interactiveOnly: false, compact: false, depth: nil, scope: "homeScreen", raw: false)
160+
161+
let rootDecision = flatSnapshotFilterDecision(
162+
scopeRoot,
163+
options: options,
164+
insideMatchedScope: false
165+
)
166+
XCTAssertTrue(rootDecision.include)
167+
XCTAssertTrue(rootDecision.insideMatchedScope)
168+
169+
XCTAssertTrue(
170+
flatSnapshotFilterDecision(
171+
unmatchedDescendant,
172+
options: options,
173+
insideMatchedScope: rootDecision.insideMatchedScope
174+
).include
175+
)
176+
XCTAssertFalse(
177+
flatSnapshotFilterDecision(
178+
unmatchedDescendant,
179+
options: options,
180+
insideMatchedScope: false
181+
).include
182+
)
183+
}
184+
185+
func testFlatSnapshotCompactCandidatesPreserveBackendInputs() {
186+
XCTAssertFalse(
187+
querySweepFlatCompactCandidate(elementType: .scrollView, hittable: false),
188+
"query sweep should not newly admit contentless scroll containers"
189+
)
190+
XCTAssertTrue(
191+
privateAXFlatCompactCandidate(rawElementType: Int(XCUIElement.ElementType.scrollView.rawValue)),
192+
"private AX keeps its existing scroll-container compact candidate behavior"
193+
)
194+
}
195+
}

‎ios-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+Snapshot.swift‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,19 +1113,20 @@ extension RunnerTests {
11131113
let label = element.label.trimmingCharacters(in: .whitespacesAndNewlines)
11141114
let identifier = element.identifier.trimmingCharacters(in: .whitespacesAndNewlines)
11151115
let valueText = snapshotValueText(element)
1116-
let hasContent = !label.isEmpty || !identifier.isEmpty || valueText != nil
11171116
let elementType = element.elementType
11181117
let enabled = element.isEnabled
11191118
let hittable = visible && enabled && element.isHittable
1120-
if options.compact && !hasContent && !hittable && !interactiveTypes.contains(elementType) {
1119+
let filterNode = FlatSnapshotFilterNode(
1120+
isRoot: false,
1121+
label: label,
1122+
identifier: identifier,
1123+
valueText: valueText,
1124+
visible: visible,
1125+
compactCandidate: querySweepFlatCompactCandidate(elementType: elementType, hittable: hittable)
1126+
)
1127+
if !flatSnapshotFilterDecision(filterNode, options: options, insideMatchedScope: false).include {
11211128
return
11221129
}
1123-
if let scope = options.scope?.trimmingCharacters(in: .whitespacesAndNewlines), !scope.isEmpty {
1124-
let haystack = [label, identifier, valueText ?? ""].joined(separator: "\n")
1125-
if !haystack.localizedCaseInsensitiveContains(scope) {
1126-
return
1127-
}
1128-
}
11291130

11301131
node = SnapshotNode(
11311132
index: index,

0 commit comments

Comments
 (0)