| 1 | #if os(macOS) |
| 2 | import AppKit |
| 3 | import Observation |
| 4 | import SwiftUI |
| 5 | import Testing |
| 6 | @testable import EquipoKit |
| 7 | |
| 8 | @MainActor @Observable private final class RememberValue { var checked = true } |
| 9 | |
| 10 | @MainActor @Suite(.serialized) struct ProjectsAccessibilityTests { |
| 11 | @Test func customCheckboxUsesNativeAccessibilityRepresentationOnBothPlatforms() throws { |
| 12 | let source = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().appendingPathComponent("Sources/EquipoKit/UI/RosterViews.swift") |
| 13 | let text = try String(contentsOf: source, encoding: .utf8) |
| 14 | let style = try #require(text.components(separatedBy: "private struct RosterCheckStyle:").last) |
| 15 | #expect(style.contains(".accessibilityRepresentation")) |
| 16 | #expect(style.contains("Toggle(isOn: configuration.$isOn)")) |
| 17 | #expect(!style.contains(".isSelected")) |
| 18 | } |
| 19 | @Test func rememberControlExposesToggleRoleValueAndAction() async throws { |
| 20 | let value = RememberValue() |
| 21 | let title = String(format: EquipoStrings.text("projects.remember_name"), "my project") |
| 22 | let host = NSHostingView(rootView: RosterCheck(title: title, isOn: Binding(get: { value.checked }, set: { value.checked = $0 })).environment(\.accessibilityEnabled, true)) |
| 23 | host.frame = NSRect(x: 0, y: 0, width: 500, height: 80) |
| 24 | let window = NSWindow(contentRect: host.frame, styleMask: [.borderless], backing: .buffered, defer: false) |
| 25 | window.contentView = host |
| 26 | defer { window.orderOut(nil); window.contentView = nil } |
| 27 | window.orderBack(nil) |
| 28 | host.layoutSubtreeIfNeeded() |
| 29 | try await Task.sleep(for: .milliseconds(30)) |
| 30 | // SwiftUI exposes Objective-C accessibility objects without declaring |
| 31 | // conformance to AppKit's full Swift protocol. Query its public selectors. |
| 32 | func attribute(_ object: NSObject, _ name: String) -> Any? { |
| 33 | let selector = NSSelectorFromString(name) |
| 34 | guard object.responds(to: selector) else { return nil } |
| 35 | return object.perform(selector)?.takeUnretainedValue() |
| 36 | } |
| 37 | func elements(_ element: NSObject, depth: Int = 0) -> [NSObject] { |
| 38 | guard depth < 20 else { return [] } |
| 39 | return [element] + ((attribute(element, "accessibilityChildren") as? [NSObject]) ?? []).flatMap { elements($0, depth: depth + 1) } |
| 40 | } |
| 41 | func checkbox() throws -> NSObject { |
| 42 | let all = elements(host) |
| 43 | return try #require(all.first { ["AXCheckBox", "AXSwitch"].contains(attribute($0, "accessibilityRole") as? String ?? "") }, "Roles: \(all.map { attribute($0, "accessibilityRole") as? String ?? "nil" })") |
| 44 | } |
| 45 | let control = try checkbox() |
| 46 | #expect(attribute(control, "accessibilityLabel") as? String == title || attribute(control, "accessibilityTitle") as? String == title) |
| 47 | #expect((attribute(control, "accessibilityValue") as? NSNumber)?.boolValue == true) |
| 48 | let selector = NSSelectorFromString("accessibilityPerformPress") |
| 49 | #expect(control.responds(to: selector)) |
| 50 | let press = unsafeBitCast(control.method(for: selector), to: (@convention(c) (AnyObject, Selector) -> Bool).self) |
| 51 | #expect(press(control, selector)) |
| 52 | #expect(!value.checked) |
| 53 | host.layoutSubtreeIfNeeded() |
| 54 | try await Task.sleep(for: .milliseconds(30)) |
| 55 | #expect((attribute(try checkbox(), "accessibilityValue") as? NSNumber)?.boolValue == false) |
| 56 | } |
| 57 | } |
| 58 | #endif |
| 59 | |