import ApplicationServices import Carbon import Foundation import Testing @testable import ParcelPlatform struct MenuShortcutTests { @Test func menuBooleansRequireBooleanValues() throws { #expect(try MenuAXValue.boolean(kCFBooleanTrue, attribute: kAXEnabledAttribute)) #expect(try !MenuAXValue.boolean(kCFBooleanFalse, attribute: kAXEnabledAttribute)) for value: CFTypeRef in [NSNumber(value: 0), NSNumber(value: 1), "true" as CFString] { #expect(throws: InputError.unexpectedAccessibilityValue(kAXEnabledAttribute)) { try MenuAXValue.boolean(value, attribute: kAXEnabledAttribute) } } } @Test(arguments: [Int.min, -1, 0, 34, Int.max]) func menuIntegersAreExact(value: Int) throws { #expect( try MenuAXValue.integer(NSNumber(value: value), attribute: kAXMenuItemCmdVirtualKeyAttribute) == value) } @Test func menuIntegersRejectCoercion() { let invalid: [CFTypeRef] = [ NSNumber(value: 34.5), NSNumber(value: -0.5), NSNumber(value: Double.infinity), NSNumber(value: Double.nan), NSNumber(value: UInt64.max), kCFBooleanTrue, "34" as CFString, ] for attribute in [ kAXMenuItemCmdVirtualKeyAttribute, kAXMenuItemCmdModifiersAttribute, kAXMenuItemCmdGlyphAttribute, ] { for value in invalid { #expect(throws: InputError.unexpectedAccessibilityValue(attribute)) { try MenuAXValue.integer(value, attribute: attribute) } } } } @Test(arguments: [ (AXError.cannotComplete, 0.30, AXPlatformError.timedOut), (.cannotComplete, 0.001, .messagingFailure), (.invalidUIElement, 0.001, .elementInvalid), (.apiDisabled, 0.001, .notPermitted), ]) func menuFailuresRetainContext(status: AXError, elapsed: Double, cause: AXPlatformError) { #expect(throws: InputError.accessibility(pid: 42, attribute: kAXMenuBarAttribute, cause: cause)) { try MenuAXValue.checkCall(status, elapsed: elapsed, timeout: 0.3, pid: 42, attribute: kAXMenuBarAttribute) } } @MainActor @Test(arguments: [AXPlatformError.timedOut, .messagingFailure, .other(-25200)]) func transientMenuChildrenFailureRetriesOnce(cause: AXPlatformError) async throws { var reads = 0 var delays = 0 let shortcut = MenuShortcut(pid: 42, path: ["Open"], hotKey: HotKey(keyCode: 31)) let result = try await MenuShortcutReader.retryTransientChildrenFailure(delay: { delays += 1 }) { reads += 1 if reads == 1 { throw InputError.accessibility(pid: 42, attribute: kAXChildrenAttribute, cause: cause) } return [shortcut] } #expect(result == [shortcut]) #expect(reads == 2) #expect(delays == 1) } @MainActor @Test func persistentMenuChildrenFailureStopsAfterTheRetry() async { var reads = 0 let error = InputError.accessibility(pid: 42, attribute: kAXChildrenAttribute, cause: .timedOut) await #expect(throws: error) { try await MenuShortcutReader.retryTransientChildrenFailure(delay: {}) { reads += 1 throw error } } #expect(reads == 2) } @MainActor @Test(arguments: [ InputError.accessibilityRequired, .accessibility(pid: 42, attribute: kAXChildrenAttribute, cause: .notPermitted), .accessibility(pid: 42, attribute: kAXChildrenAttribute, cause: .elementInvalid), .accessibility(pid: 42, attribute: kAXMenuBarAttribute, cause: .timedOut), .unexpectedAccessibilityValue(kAXChildrenAttribute), ]) func nonTransientMenuFailuresDoNotRetry(error: InputError) async { var reads = 0 var delays = 0 await #expect(throws: error) { try await MenuShortcutReader.retryTransientChildrenFailure(delay: { delays += 1 }) { reads += 1 throw error } } #expect(reads == 1) #expect(delays == 0) } @MainActor @Test func menuRetryDelayCancellationPreventsAnotherRead() async { var reads = 0 await #expect(throws: CancellationError.self) { try await MenuShortcutReader.retryTransientChildrenFailure(delay: { throw CancellationError() }) { reads += 1 throw InputError.accessibility(pid: 42, attribute: kAXChildrenAttribute, cause: .messagingFailure) } } #expect(reads == 1) } @Test func successfulMenuCallDoesNotThrow() throws { try MenuAXValue.checkCall(.success, elapsed: 0.3, timeout: 0.3, pid: 42, attribute: kAXMenuBarAttribute) } @Test func knownGlyphResolvesUnmappedCharacter() throws { let resolver = MenuKeyResolver(characters: [:]) #expect( try resolver.resolve(virtualKey: nil, character: "\u{F704}", modifiers: 0, glyph: kMenuF1Glyph) == [HotKey(keyCode: UInt16(kVK_F1), modifiers: .command)]) #expect(throws: InputError.unresolvedMenuCharacter("?")) { try resolver.resolve(virtualKey: nil, character: "?", modifiers: 0, glyph: 0) } #expect(throws: InputError.unsupportedMenuGlyph(9999)) { try resolver.resolve(virtualKey: nil, character: "?", modifiers: 0, glyph: 9999) } } @Test(arguments: [kAXTitleAttribute, kAXMenuItemCmdCharAttribute]) func menuStringsDoNotCoerceOtherAXTypes(attribute: String) throws { #expect(try MenuAXValue.string("" as CFString, attribute: attribute).isEmpty) #expect(try MenuAXValue.string("Résumé" as CFString, attribute: attribute) == "Résumé") for value: CFTypeRef in [NSNumber(value: 1), kCFBooleanTrue, [] as CFArray] { #expect(throws: InputError.unexpectedAccessibilityValue(attribute)) { try MenuAXValue.string(value, attribute: attribute) } } } @Test func menuElementsAndChildrenRequireAXElements() throws { let element = AXUIElementCreateApplication(42) #expect(CFEqual(try MenuAXValue.element(element, attribute: kAXMenuBarAttribute), element)) #expect(try MenuAXValue.children([] as CFArray).isEmpty) let children = try MenuAXValue.children([element, element] as CFArray) #expect(children.count == 2) #expect(children.allSatisfy { CFEqual($0, element) }) for value: CFTypeRef in ["menu" as CFString, NSNumber(value: 1), kCFBooleanTrue, [] as CFArray] { #expect(throws: InputError.unexpectedAccessibilityValue(kAXMenuBarAttribute)) { try MenuAXValue.element(value, attribute: kAXMenuBarAttribute) } } for value: CFTypeRef in ["children" as CFString, NSNumber(value: 1), element, [element, "invalid"] as CFArray] { #expect(throws: InputError.unexpectedAccessibilityValue(kAXChildrenAttribute)) { try MenuAXValue.children(value) } } } @Test(arguments: [-1, 128, 65535, Int.max]) func invalidVirtualKeysAreNotSilentlyReplacedByCharactersOrGlyphs(code: Int) throws { let shortcuts = try MenuFixture.example(badAttributes: [ kAXMenuItemCmdVirtualKeyAttribute: NSNumber(value: code), kAXMenuItemCmdCharAttribute: "a" as CFString, kAXMenuItemCmdGlyphAttribute: NSNumber(value: kMenuF1Glyph), ]).read() #expect(shortcuts.map(\.path) == [["Before"], ["Broken", "Nested"], ["After"]]) #expect(shortcuts.map(\.hotKey.keyCode) == [1, 2, 3]) let invalid = HotKey(keyCode: 0, modifiers: .command) let good = HotKey(keyCode: 3, modifiers: .command) let conflicts = ShortcutConflicts.detect( hotKeys: [invalid, good], symbolicHotKeys: [], menuShortcuts: shortcuts) #expect(conflicts == [ShortcutConflict(hotKey: good, owner: .application(pid: 42, menuPath: ["After"]))]) } @Test(arguments: [ kAXTitleAttribute, kAXEnabledAttribute, kAXMenuItemCmdCharAttribute, kAXMenuItemCmdVirtualKeyAttribute, kAXMenuItemCmdModifiersAttribute, kAXMenuItemCmdGlyphAttribute, ]) func malformedItemAttributesDoNotHideOtherShortcuts(attribute: String) throws { let shortcuts = try MenuFixture.example(badAttributes: [attribute: [] as CFArray]).read() let nestedPath = attribute == kAXTitleAttribute ? ["Nested"] : ["Broken", "Nested"] #expect(shortcuts.map(\.path) == [["Before"], nestedPath, ["After"]]) #expect(shortcuts.map(\.hotKey.keyCode) == [1, 2, 3]) } @Test func accessibilityValueInsteadOfCommandCharacterSkipsOnlyThatItem() throws { var point = CGPoint(x: 1, y: 2) let value = try #require(AXValueCreate(.cgPoint, &point)) let shortcuts = try MenuFixture.example(badAttributes: [ kAXMenuItemCmdCharAttribute: value, kAXMenuItemCmdVirtualKeyAttribute: NSNumber(value: 0), ]).read() #expect(shortcuts.map(\.path) == [["Before"], ["Broken", "Nested"], ["After"]]) #expect(shortcuts.map(\.hotKey.keyCode) == [1, 2, 3]) } @Test func numericEnabledValueDoesNotEnableTheItemOrHideItsNeighbors() throws { let shortcuts = try MenuFixture.example(badAttributes: [ kAXEnabledAttribute: NSNumber(value: 1), kAXMenuItemCmdVirtualKeyAttribute: NSNumber(value: 0), ]).read() #expect(shortcuts.map(\.path) == [["Before"], ["Broken", "Nested"], ["After"]]) #expect(shortcuts.map(\.hotKey.keyCode) == [1, 2, 3]) } @Test func itemFailuresAndUnresolvableKeysDoNotHideOtherShortcuts() throws { let errors: [InputError] = [ .accessibility(pid: 42, attribute: kAXEnabledAttribute, cause: .messagingFailure), .accessibility(pid: 42, attribute: kAXEnabledAttribute, cause: .timedOut), .unexpectedAccessibilityValue(kAXEnabledAttribute), ] for error in errors { let shortcuts = try MenuFixture.example(badErrors: [kAXEnabledAttribute: error]).read() #expect(shortcuts.map(\.hotKey.keyCode) == [1, 2, 3]) } for attributes: [String: CFTypeRef] in [ [kAXMenuItemCmdCharAttribute: "?" as CFString], [kAXMenuItemCmdGlyphAttribute: NSNumber(value: 9999)], [kAXMenuItemCmdModifiersAttribute: NSNumber(value: -1)], ] { let shortcuts = try MenuFixture.example(badAttributes: attributes).read() #expect(shortcuts.map(\.hotKey.keyCode) == [1, 2, 3]) } } @Test(arguments: [AXPlatformError.timedOut, .messagingFailure, .elementInvalid, .other(-25200)]) func unreadableSubmenuDoesNotDiscardReadableSiblingShortcuts(cause: AXPlatformError) throws { let shortcuts = try MenuFixture.example(badErrors: [ kAXChildrenAttribute: .accessibility(pid: 42, attribute: kAXChildrenAttribute, cause: cause) ]).read() #expect(shortcuts.map(\.path) == [["Before"], ["After"]]) #expect(shortcuts.map(\.hotKey.keyCode) == [1, 3]) } @Test func helpSearchFieldWithAdvertisedChildrenIsNotAMenuBranch() throws { let fixture = MenuFixture.example( badAttributes: [ kAXRoleAttribute: kAXTextFieldRole as CFString, kAXMenuItemCmdVirtualKeyAttribute: NSNumber(value: 0), ], badErrors: [ kAXChildrenAttribute: .accessibility( pid: 42, attribute: kAXChildrenAttribute, cause: .other(AXError.failure.rawValue)) ]) var fieldReads: [String] = [] let shortcuts = try fixture.read( childrenCounts: [0: 3, 2: 1], onAttributeRead: { index, attribute in if index == 2 { fieldReads.append(attribute) } }) #expect(fieldReads == [kAXRoleAttribute]) #expect(shortcuts.map(\.path) == [["Before"], ["After"]]) #expect(shortcuts.map(\.hotKey.keyCode) == [1, 3]) } @Test func knownEmptyLeafDoesNotRequestItsRejectedChildren() throws { let fixture = MenuFixture(items: [ .init(children: [1]), .init( attributes: [ kAXTitleAttribute: "Open" as CFString, kAXMenuItemCmdVirtualKeyAttribute: NSNumber(value: 31), ], errors: [ kAXChildrenAttribute: .accessibility( pid: 42, attribute: kAXChildrenAttribute, cause: .messagingFailure) ]), ]) var requestedLeafChildren = false let shortcuts = try fixture.read( childrenCounts: [0: 1, 1: 0], onAttributeRead: { index, attribute in if index == 1, attribute == kAXChildrenAttribute { requestedLeafChildren = true } }) #expect(!requestedLeafChildren) #expect(shortcuts.map(\.path) == [["Open"]]) #expect(shortcuts.map(\.hotKey.keyCode) == [31]) } @Test func nonEmptyChildCountStillTraversesTheSubmenu() throws { let shortcuts = try MenuFixture.example().read(childrenCounts: [0: 3, 2: 1]) #expect(shortcuts.map(\.path) == [["Before"], ["Broken", "Nested"], ["After"]]) } @Test(arguments: [AXPlatformError.timedOut, .messagingFailure, .notPermitted]) func unreadableMenuRootStillFailsTheRead(cause: AXPlatformError) { let error = InputError.accessibility(pid: 42, attribute: kAXChildrenAttribute, cause: cause) let fixture = MenuFixture(items: [.init(errors: [kAXChildrenAttribute: error])]) #expect(throws: error) { try fixture.read() } } @Test func lostChildrenPermissionIsNotAnUnreadableBranch() { let error = InputError.accessibility(pid: 42, attribute: kAXChildrenAttribute, cause: .notPermitted) #expect(throws: error) { try MenuFixture.example(badErrors: [kAXChildrenAttribute: error]).read() } } @Test func oneTraversalDeadlineDoesNotExpireTheNextRead() throws { #expect(throws: InputError.menuTraversalLimit) { try MenuFixture.example().read(deadline: .now.advanced(by: .seconds(-1))) } #expect(try MenuFixture.example().read().map(\.hotKey.keyCode) == [1, 2, 3]) } @Test func cancellationStopsTraversalBeforeReadingAttributes() { #expect(throws: CancellationError.self) { try MenuFixture.example().read(checkCancellation: { throw CancellationError() }) } } @Test func structuralFailuresAndLostPermissionStillFailTheRead() throws { let structural = InputError.unexpectedAccessibilityValue(kAXChildrenAttribute) #expect(throws: structural) { try MenuFixture.example(badErrors: [kAXChildrenAttribute: structural]).read() } #expect(throws: structural) { try MenuFixture.example(badAttributes: [kAXChildrenAttribute: "invalid" as CFString]).read() } let permission = InputError.accessibility(pid: 42, attribute: kAXEnabledAttribute, cause: .notPermitted) #expect(throws: permission) { try MenuFixture.example(badErrors: [kAXEnabledAttribute: permission]).read() } #expect(throws: InputError.menuTraversalLimit) { try MenuFixture.example(badErrors: [kAXTitleAttribute: .menuTraversalLimit]).read() } } @Test func disabledItemStillExcludesItsSubmenu() throws { for attributes: [String: CFTypeRef] in [ [kAXEnabledAttribute: kCFBooleanFalse], [kAXEnabledAttribute: kCFBooleanFalse, kAXTitleAttribute: [] as CFArray], ] { let shortcuts = try MenuFixture.example(badAttributes: attributes).read() #expect(shortcuts.map(\.path) == [["Before"], ["After"]]) } let disabledWithUnreadableTitle = try MenuFixture.example( badAttributes: [kAXEnabledAttribute: kCFBooleanFalse], badErrors: [ kAXTitleAttribute: .accessibility(pid: 42, attribute: kAXTitleAttribute, cause: .notPermitted) ] ).read() #expect(disabledWithUnreadableTitle.map(\.path) == [["Before"], ["After"]]) } @Test(arguments: [ (Optional.none, "absent"), (.some(""), "[]"), (.some("A"), "[U+41]"), (.some("é"), "[U+E9]"), (.some("e\u{301}"), "[U+65,U+301]"), (.some("\u{F704}"), "[U+F704]"), (.some("\u{1F511}"), "[U+1F511]"), ]) func rejectedVirtualKeyDiagnosticUsesOnlyCharacterCodePoints(character: String?, codePoints: String) { #expect( MenuKeyResolver.rejectedVirtualKeyDiagnostic(virtualKey: 128, character: character, modifiers: 9, glyph: 42) == "Rejected AXMenuItemCmdVirtualKey value 128, character code points \(codePoints), glyph 42, modifiers 9" ) } @Test(arguments: [(Optional.none, "absent"), (.some(0), "0"), (.some(9999), "9999")]) func rejectedVirtualKeyDiagnosticPreservesGlyphPresence(glyph: Int?, value: String) { #expect( MenuKeyResolver.rejectedVirtualKeyDiagnostic(virtualKey: 128, character: nil, modifiers: 0, glyph: glyph) == "Rejected AXMenuItemCmdVirtualKey value 128, character code points absent, glyph \(value), modifiers 0" ) } @Test func noticeErrorSixIsMalformedMenuDataNotAnEventTapFailure() { let error = InputError.unexpectedAccessibilityValue(kAXMenuItemCmdVirtualKeyAttribute) #expect((error as NSError).code == 6) #expect((error as NSError).domain == "ParcelPlatform.InputError") #expect((InputError.tapEnableFailed as NSError).code != 6) #expect( (InputError.accessibility(pid: 42, attribute: kAXTitleAttribute, cause: .messagingFailure) as NSError).code != 6) #expect(error.localizedDescription == "The app returned invalid menu shortcut data.") #expect(String(reflecting: error).contains(kAXMenuItemCmdVirtualKeyAttribute)) } @Test(arguments: inputFailures) func everyInputErrorHasShortHumanGuidance(error: InputError, english: String) throws { #expect(try #require(error.errorDescription) == english) #expect((error as NSError).localizedDescription == english) #expect(!english.contains("AX")) #expect(!english.contains("-50")) #expect(!english.contains("raw-secret")) #expect(english.count <= 90) } @Test(arguments: inputFailures, ["en", "es", "fr", "de", "ja", "zh-Hans", "ko", "pt-BR"]) func everyInputErrorHasCompiledTranslations(failure: (InputError, String), language: String) throws { let (error, english) = failure let url = try #require( InputError.localizationBundle.url(forResource: language.lowercased(), withExtension: "lproj")) let bundle = try #require(Bundle(url: url)) let message = bundle.localizedString( forKey: error.localizationKey, value: "missing-translation", table: "Input") #expect(message != "missing-translation") #expect(!message.isEmpty) #expect(language == "en" ? message == english : message != english) #expect(!message.contains("raw-secret")) } @Test func inputCatalogCoversEveryGuidanceKeyInEveryShippedLanguage() throws { let root = URL(fileURLWithPath: #filePath) .deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent() .deletingLastPathComponent() let url = root.appendingPathComponent("Sources/ParcelPlatform/Resources/Input.xcstrings") let catalog = try JSONDecoder().decode(InputCatalog.self, from: Data(contentsOf: url)) #expect(catalog.sourceLanguage == "en") #expect(Set(catalog.strings.keys) == Set(Self.inputFailures.map(\.1))) let languages = Set(["en", "es", "fr", "de", "ja", "zh-Hans", "ko", "pt-BR"]) for (key, entry) in catalog.strings { #expect(Set(entry.localizations.keys) == languages) for (language, localization) in entry.localizations { #expect(localization.stringUnit.state == "translated") #expect(!localization.stringUnit.value.isEmpty) #expect(language == "en" ? localization.stringUnit.value == key : localization.stringUnit.value != key) } } } private struct InputCatalog: Decodable { let sourceLanguage: String let strings: [String: Entry] struct Entry: Decodable { let localizations: [String: Localization] } struct Localization: Decodable { let stringUnit: Unit } struct Unit: Decodable { let state: String let value: String } } private static let inputFailures: [(InputError, String)] = [ (.accessibilityRequired, "Allow Accessibility access in System Settings."), (.duplicateHotKey(HotKey(keyCode: 0, modifiers: .command)), "That shortcut is already registered."), (.switcherCaptureAlreadyActive, "Keyboard selection is already active."), (.eventTapInputUnavailable, "Keyboard input is unavailable right now."), (.carbon(operation: "raw-secret", status: -50), "The keyboard shortcut could not be updated."), (.tapCreationFailed, "Keyboard monitoring could not start."), (.tapEnableFailed, "Keyboard monitoring could not be enabled."), (.tapTimedOut, "Input monitoring took too long to respond."), (.tapRunLoopSourceFailed, "The input listener could not start."), ( .tapFallbackFailed(carbonStatus: -50, cause: .unresolvedMenuCharacter("raw-secret")), "The keyboard shortcut could not be registered." ), (.mouseMonitorCreationFailed, "Mouse monitoring could not start."), (.missingMouseEvent, "The mouse event is no longer available."), (.windowListUnavailable, "The window list could not be read."), (.malformedSymbolicHotKey("raw-secret"), "A system shortcut contains invalid data."), (.eventCreationFailed, "The keyboard event could not be created."), (.invalidProcess(42), "The app is no longer available."), ( .accessibility(pid: 42, attribute: "raw-secret", cause: .notPermitted), "Allow Accessibility access in System Settings." ), (.accessibility(pid: 42, attribute: "raw-secret", cause: .timedOut), "The app took too long to respond."), ( .accessibility(pid: 42, attribute: "raw-secret", cause: .messagingFailure), "The app did not respond to the menu request." ), ( .accessibility(pid: 42, attribute: "raw-secret", cause: .elementInvalid), "The app menu is no longer available." ), ( .accessibility(pid: 42, attribute: "raw-secret", cause: .attributeUnsupported), "The app menu could not be read." ), (.accessibility(pid: 42, attribute: "raw-secret", cause: .noValue), "The app menu could not be read."), ( .accessibility(pid: 42, attribute: "raw-secret", cause: .unexpectedValue("raw-secret")), "The app menu could not be read." ), ( .accessibility(pid: 42, attribute: "raw-secret", cause: .unavailable("raw-secret")), "The app menu could not be read." ), (.accessibility(pid: 42, attribute: "raw-secret", cause: .other(-50)), "The app menu could not be read."), (.unexpectedAccessibilityValue("raw-secret"), "The app returned invalid menu shortcut data."), (.menuTraversalLimit, "The app menu is too large or slow to check."), (.keyboardLayoutUnavailable, "The current keyboard layout is unavailable."), (.keyboardTranslationFailed(-50), "The current keyboard layout could not be read."), (.unresolvedMenuCharacter("raw-secret"), "A menu shortcut could not be matched to the keyboard layout."), (.unsupportedMenuGlyph(9999), "A menu shortcut uses an unsupported key symbol."), ] @Test func characterResolutionTakesPrecedenceOverDecorativeGlyph() throws { let resolver = MenuKeyResolver(characters: ["a": [.init(code: 0, modifiers: [])]]) #expect( try resolver.resolve(virtualKey: nil, character: "a", modifiers: 0, glyph: 9999) == [HotKey(keyCode: 0, modifiers: .command)]) } } struct MenuFixture { struct Item { var attributes: [String: CFTypeRef] = [:] var errors: [String: InputError] = [:] var children: [Int] = [] } let items: [Item] static func example( badAttributes: [String: CFTypeRef] = [:], badErrors: [String: InputError] = [:] ) -> Self { var bad = [kAXTitleAttribute: "Broken" as CFTypeRef] bad.merge(badAttributes) { _, replacement in replacement } return Self(items: [ Item(children: [1, 2, 4]), Item(attributes: [ kAXTitleAttribute: "Before" as CFString, kAXMenuItemCmdVirtualKeyAttribute: NSNumber(value: 1), ]), Item(attributes: bad, errors: badErrors, children: [3]), Item(attributes: [ kAXTitleAttribute: "Nested" as CFString, kAXMenuItemCmdVirtualKeyAttribute: NSNumber(value: 2), ]), Item(attributes: [ kAXTitleAttribute: "After" as CFString, kAXMenuItemCmdVirtualKeyAttribute: NSNumber(value: 3), ]), ]) } func read( childrenCounts: [Int: Int] = [:], deadline: ContinuousClock.Instant = ContinuousClock.now.advanced(by: .seconds(5)), checkCancellation: @escaping @Sendable () throws -> Void = {}, onAttributeRead: (Int, String) -> Void = { _, _ in } ) throws -> [MenuShortcut] { let elements = items.indices.map { AXUIElementCreateApplication(pid_t(10_000 + $0)) } let walker = MenuWalker( pid: 42, layout: MenuKeyResolver(characters: ["a": [.init(code: 0, modifiers: [])]]), timeout: 0.3, deadline: deadline, checkCancellation: checkCancellation) return try walker.walk( elements[0], attribute: { element, name in let index = try #require(elements.firstIndex { CFEqual($0, element) }) onAttributeRead(index, name) let item = items[index] if let error = item.errors[name] { throw error } if let value = item.attributes[name] { return value } if name == kAXChildrenAttribute { return item.children.map { elements[$0] } as CFArray } return nil }, childrenCount: { element in childrenCounts[try #require(elements.firstIndex { CFEqual($0, element) })] }) } }