Skip to content

Commit 7e6ca6a

Browse files
committed
fix(ios): one focus reader behind both snapshot producers, tested where the fix is
The private-AX bridge cannot see keyboard focus (the AX server declares no such attribute), so its OR and the extra keypath were code for a state that cannot occur; they are gone, and the bridge is what it was on main. The two reads that carry the fix, snapshotHasFocus and elementHasFocus, now share one focusBool reader (hasKeyboardFocus, else hasFocus, through KVC), and a host-lane test drives that reader from fixtures carrying only keyboard focus, only native focus, neither, and no key at all.
1 parent a49fee9 commit 7e6ca6a

5 files changed

Lines changed: 55 additions & 77 deletions

File tree

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerAXSnapshotBridge.m‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,6 @@ + (NSArray *)snapshotAttributes
369369
@"enabled",
370370
@"selected",
371371
@"hasFocus",
372-
// The AX server has no keyboard-focus attribute (XCTAutomationSupport declares HasNativeFocus
373-
// and FocusedApplications only), so the mapper drops this keypath today and this backend reports
374-
// the focus engine's focus alone; the keypath stays so a mapper that learns it needs no change.
375-
@"hasKeyboardFocus",
376372
@"children",
377373
];
378374
// The AX server expects real accessibility attribute identifiers, not snapshot keypath
@@ -771,11 +767,7 @@ + (nullable NSMutableDictionary *)dictionaryForSnapshot:(id)snapshot
771767
result[@"frame"] = [self frameValueForSnapshot:snapshot];
772768
result[@"enabled"] = [self boolNumberForKey:@"enabled" snapshot:snapshot defaultValue:YES];
773769
result[@"selected"] = [self boolNumberForKey:@"selected" snapshot:snapshot defaultValue:NO];
774-
// Either focus is focus: `hasFocus` is the focus engine's, `hasKeyboardFocus` the text field's
775-
// under a software keyboard (the XCTest producer reads the same pair).
776-
result[@"focused"] = @(
777-
[[self boolNumberForKey:@"hasKeyboardFocus" snapshot:snapshot defaultValue:NO] boolValue]
778-
|| [[self boolNumberForKey:@"hasFocus" snapshot:snapshot defaultValue:NO] boolValue]);
770+
result[@"focused"] = [self boolNumberForKey:@"hasFocus" snapshot:snapshot defaultValue:NO];
779771

780772
NSMutableArray *children = [NSMutableArray array];
781773
if (depth < maxDepth) {

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -416,17 +416,22 @@ extension RunnerTests {
416416
return node
417417
}
418418

419-
// `hasFocus` is the focus engine's answer (tvOS, keyboard navigation); the field a software
420-
// keyboard is typing into holds `hasKeyboardFocus` instead, which the text-entry preflight already
421-
// consults. The snapshot reports either, so the field under the keyboard reads as focused.
422419
private func snapshotHasFocus(_ snapshot: XCUIElementSnapshot) -> Bool {
423-
return snapshotBool(snapshot, forKey: "hasKeyboardFocus") || snapshotBool(snapshot, forKey: "hasFocus")
420+
return focusBool(snapshot as! NSObject)
424421
}
425422

426-
private func snapshotBool(_ snapshot: XCUIElementSnapshot, forKey key: String) -> Bool {
423+
/// Either focus is focus. `hasFocus` is the focus engine's answer (tvOS, keyboard navigation);
424+
/// the field a software keyboard is typing into holds `hasKeyboardFocus` instead, which the
425+
/// text-entry readiness check already consults. Both an element and its snapshot answer the two
426+
/// keys through KVC, so one reader serves every producer; a key the object lacks reads as false.
427+
func focusBool(_ object: NSObject) -> Bool {
428+
return kvcBool(object, forKey: "hasKeyboardFocus") || kvcBool(object, forKey: "hasFocus")
429+
}
430+
431+
private func kvcBool(_ object: NSObject, forKey key: String) -> Bool {
427432
var result = false
428433
_ = RunnerObjCExceptionCatcher.catchException({
429-
if let value = (snapshot as! NSObject).value(forKey: key) as? Bool {
434+
if let value = object.value(forKey: key) as? Bool {
430435
result = value
431436
}
432437
})

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,8 @@ extension RunnerTests {
5151
return TvRemoteButton(rawValue: raw.lowercased())
5252
}
5353

54-
// Either focus is focus, the same pair the snapshot producers read: `hasFocus` is the focus
55-
// engine's answer, `hasKeyboardFocus` the text field's under a software keyboard.
5654
func elementHasFocus(_ element: XCUIElement) -> Bool {
57-
return elementBool(element, forKey: "hasKeyboardFocus") || elementBool(element, forKey: "hasFocus")
58-
}
59-
60-
private func elementBool(_ element: XCUIElement, forKey key: String) -> Bool {
61-
var result = false
62-
_ = RunnerObjCExceptionCatcher.catchException({
63-
if let value = (element as NSObject).value(forKey: key) as? Bool {
64-
result = value
65-
}
66-
})
67-
return result
55+
return focusBool(element as NSObject)
6856
}
6957

7058
func activateElement(app: XCUIApplication, element: XCUIElement, action: String) -> RunnerInteractionOutcome {

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

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -154,35 +154,27 @@ private final class AXFixtureSnapshot: NSObject {
154154
@objc let enabled: NSNumber = true
155155
@objc let selected: NSNumber = false
156156
@objc let hasFocus: NSNumber = false
157-
@objc let hasKeyboardFocus: NSNumber
158157
@objc let children: [AXFixtureSnapshot]
159158
@objc let accessibilityElement: AXFixtureElement?
160159

161-
init(node: AXFixtureNode, children: [AXFixtureSnapshot], element: AXFixtureElement?, keyboardFocus: Bool = false) {
160+
init(node: AXFixtureNode, children: [AXFixtureSnapshot], element: AXFixtureElement?) {
162161
identifier = node.identity
163162
label = node.identity
164-
hasKeyboardFocus = NSNumber(value: keyboardFocus)
165163
self.children = children
166164
accessibilityElement = element
167165
}
168166

169167
/// `levels` node levels rooted at `node`; the deepest returned level loses its live element
170-
/// when the fixture says the frontier vanished. The node named by `keyboardFocusIdentity` holds
171-
/// the software keyboard's focus, the way a text field being typed into does.
172-
static func fragment(
173-
_ node: AXFixtureNode, levels: Int, vanishAtFrontier: Bool, keyboardFocusIdentity: String? = nil
174-
) -> AXFixtureSnapshot {
168+
/// when the fixture says the frontier vanished.
169+
static func fragment(_ node: AXFixtureNode, levels: Int, vanishAtFrontier: Bool) -> AXFixtureSnapshot {
175170
let boundary = levels <= 1
176171
let children = boundary
177172
? []
178-
: node.children.map {
179-
fragment($0, levels: levels - 1, vanishAtFrontier: vanishAtFrontier, keyboardFocusIdentity: keyboardFocusIdentity)
180-
}
173+
: node.children.map { fragment($0, levels: levels - 1, vanishAtFrontier: vanishAtFrontier) }
181174
return AXFixtureSnapshot(
182175
node: node,
183176
children: children,
184-
element: boundary && vanishAtFrontier ? nil : AXFixtureElement(node: node),
185-
keyboardFocus: node.identity == keyboardFocusIdentity)
177+
element: boundary && vanishAtFrontier ? nil : AXFixtureElement(node: node))
186178
}
187179
}
188180

@@ -191,24 +183,19 @@ private final class AXFixtureSnapshot: NSObject {
191183
private final class AXFixtureClient: NSObject {
192184
private let rejectLevelsAbove: Int?
193185
private let vanishAtFrontier: Bool
194-
private let keyboardFocusIdentity: String?
195186
private(set) var requests = 0
196187
private(set) var rejected = 0
197-
/// Every attribute the bridge asked for, across requests: the request contract under test.
198-
private(set) var requestedAttributes: [String] = []
199188

200-
init(rejectLevelsAbove: Int?, vanishAtFrontier: Bool, keyboardFocusIdentity: String? = nil) {
189+
init(rejectLevelsAbove: Int?, vanishAtFrontier: Bool) {
201190
self.rejectLevelsAbove = rejectLevelsAbove
202191
self.vanishAtFrontier = vanishAtFrontier
203-
self.keyboardFocusIdentity = keyboardFocusIdentity
204192
}
205193

206194
@objc(requestSnapshotForElement:attributes:parameters:error:)
207195
func requestSnapshot(
208196
forElement element: Any, attributes: Any, parameters: [String: Any], error: NSErrorPointer
209197
) -> Any? {
210198
requests += 1
211-
requestedAttributes.append(contentsOf: (attributes as? [Any] ?? []).compactMap { $0 as? String })
212199
let levels = (parameters["maxDepth"] as? NSNumber)?.intValue ?? 0
213200
if let limit = rejectLevelsAbove, levels > limit {
214201
rejected += 1
@@ -218,8 +205,7 @@ private final class AXFixtureClient: NSObject {
218205
return nil
219206
}
220207
guard let element = element as? AXFixtureElement else { return nil }
221-
return AXFixtureSnapshot.fragment(
222-
element.node, levels: levels, vanishAtFrontier: vanishAtFrontier, keyboardFocusIdentity: keyboardFocusIdentity)
208+
return AXFixtureSnapshot.fragment(element.node, levels: levels, vanishAtFrontier: vanishAtFrontier)
223209
}
224210
}
225211

@@ -378,34 +364,6 @@ extension RunnerTests {
378364
return observation
379365
}
380366

381-
/// The bridge reports keyboard focus as `focused` when the snapshot carries it, beside the focus
382-
/// engine's focus it asks the AX server for. The server exposes no keyboard-focus attribute, so
383-
/// the request names the native focus only; a snapshot that arrives with keyboard focus set (the
384-
/// XCTest producers' case) still reads as focused through the shared OR.
385-
func testPrivateAXBridgeReportsKeyboardFocusAsFocused() throws {
386-
let rootNode = AXFixtureNode.build(AXRecoveryFixture.Tree(chain: 2, fan: nil))
387-
let client = AXFixtureClient(rejectLevelsAbove: nil, vanishAtFrontier: false, keyboardFocusIdentity: "1")
388-
let response = RunnerAXSnapshotBridge.snapshotTree(
389-
withClient: client,
390-
target: AXFixtureElement(node: rootNode),
391-
maxDepth: 2,
392-
maxNodes: 10,
393-
deepExtensionCallLimit: 0,
394-
customActionLimit: 0,
395-
deadline: .distantFuture)
396-
let root = try XCTUnwrap(response["root"] as? [String: Any])
397-
let field = try XCTUnwrap((root["children"] as? [[String: Any]])?.first)
398-
XCTAssertEqual(root["label"] as? String, "0")
399-
XCTAssertEqual(root["focused"] as? Bool, false, "no focus of either kind is not focused")
400-
XCTAssertEqual(field["label"] as? String, "1")
401-
XCTAssertEqual(field["focused"] as? Bool, true, "keyboard focus alone is focused")
402-
// The AX server answers only what it was asked for, so the request must still name the focus
403-
// engine's focus: the raw keypath when the mapper is absent, `HasNativeFocus` when it maps.
404-
XCTAssertTrue(
405-
client.requestedAttributes.contains { $0.range(of: "focus", options: .caseInsensitive) != nil },
406-
"requested attributes name the focus engine's focus: \(client.requestedAttributes)")
407-
}
408-
409367
/// Every recovery case of the shared fixture, replayed through the real ladder, bridge
410368
/// capture, frontier extension, and completeness verdict, down to the delivered tree.
411369
func testPrivateAXRecoveryMatchesSharedFixture() throws {

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,40 @@ extension RunnerTests {
2121
XCTAssertNil(tvRemoteButton(from: raw))
2222
}
2323
}
24+
25+
/// `focusBool` is the one reader behind `snapshotHasFocus` and `elementHasFocus`. A live element
26+
/// or snapshot answers both keys through KVC; the fixtures answer the same way, so the read that
27+
/// production performs is the read under test.
28+
func testFocusBoolReadsKeyboardFocusBesideTheFocusEnginesFocus() {
29+
XCTAssertTrue(
30+
focusBool(FocusFixture(hasFocus: false, hasKeyboardFocus: true)),
31+
"the field a software keyboard is typing into holds keyboard focus alone"
32+
)
33+
XCTAssertTrue(
34+
focusBool(FocusFixture(hasFocus: true, hasKeyboardFocus: false)),
35+
"the focus engine's focus (tvOS, keyboard navigation) still counts"
36+
)
37+
XCTAssertFalse(focusBool(FocusFixture(hasFocus: false, hasKeyboardFocus: false)))
38+
XCTAssertTrue(
39+
focusBool(KeyboardFocusOnlyFixture()),
40+
"an object that exposes no hasFocus key at all still reports its keyboard focus"
41+
)
42+
XCTAssertFalse(focusBool(NSObject()), "an object with neither key reads as unfocused, not as an exception")
43+
}
44+
}
45+
46+
/// KVC-readable the way XCTest exposes an element's or a snapshot's focus attributes.
47+
private final class FocusFixture: NSObject {
48+
@objc let hasFocus: NSNumber
49+
@objc let hasKeyboardFocus: NSNumber
50+
51+
init(hasFocus: Bool, hasKeyboardFocus: Bool) {
52+
self.hasFocus = NSNumber(value: hasFocus)
53+
self.hasKeyboardFocus = NSNumber(value: hasKeyboardFocus)
54+
}
55+
}
56+
57+
private final class KeyboardFocusOnlyFixture: NSObject {
58+
@objc let hasKeyboardFocus: NSNumber = true
2459
}
2560
#endif

0 commit comments

Comments
 (0)