#if os(macOS) import AppKit import Observation import SwiftUI import Testing @testable import EquipoKit @MainActor @Observable private final class RememberValue { var checked = true } @MainActor @Suite(.serialized) struct ProjectsAccessibilityTests { @Test func customCheckboxUsesNativeAccessibilityRepresentationOnBothPlatforms() throws { let source = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().appendingPathComponent("Sources/EquipoKit/UI/RosterViews.swift") let text = try String(contentsOf: source, encoding: .utf8) let style = try #require(text.components(separatedBy: "private struct RosterCheckStyle:").last) #expect(style.contains(".accessibilityRepresentation")) #expect(style.contains("Toggle(isOn: configuration.$isOn)")) #expect(!style.contains(".isSelected")) } @Test func rememberControlExposesToggleRoleValueAndAction() async throws { let value = RememberValue() let title = String(format: EquipoStrings.text("projects.remember_name"), "my project") let host = NSHostingView(rootView: RosterCheck(title: title, isOn: Binding(get: { value.checked }, set: { value.checked = $0 })).environment(\.accessibilityEnabled, true)) host.frame = NSRect(x: 0, y: 0, width: 500, height: 80) let window = NSWindow(contentRect: host.frame, styleMask: [.borderless], backing: .buffered, defer: false) window.contentView = host defer { window.orderOut(nil); window.contentView = nil } window.orderBack(nil) host.layoutSubtreeIfNeeded() try await Task.sleep(for: .milliseconds(30)) // SwiftUI exposes Objective-C accessibility objects without declaring // conformance to AppKit's full Swift protocol. Query its public selectors. func attribute(_ object: NSObject, _ name: String) -> Any? { let selector = NSSelectorFromString(name) guard object.responds(to: selector) else { return nil } return object.perform(selector)?.takeUnretainedValue() } func elements(_ element: NSObject, depth: Int = 0) -> [NSObject] { guard depth < 20 else { return [] } return [element] + ((attribute(element, "accessibilityChildren") as? [NSObject]) ?? []).flatMap { elements($0, depth: depth + 1) } } func checkbox() throws -> NSObject { let all = elements(host) return try #require(all.first { ["AXCheckBox", "AXSwitch"].contains(attribute($0, "accessibilityRole") as? String ?? "") }, "Roles: \(all.map { attribute($0, "accessibilityRole") as? String ?? "nil" })") } let control = try checkbox() #expect(attribute(control, "accessibilityLabel") as? String == title || attribute(control, "accessibilityTitle") as? String == title) #expect((attribute(control, "accessibilityValue") as? NSNumber)?.boolValue == true) let selector = NSSelectorFromString("accessibilityPerformPress") #expect(control.responds(to: selector)) let press = unsafeBitCast(control.method(for: selector), to: (@convention(c) (AnyObject, Selector) -> Bool).self) #expect(press(control, selector)) #expect(!value.checked) host.layoutSubtreeIfNeeded() try await Task.sleep(for: .milliseconds(30)) #expect((attribute(try checkbox(), "accessibilityValue") as? NSNumber)?.boolValue == false) } } #endif