import AppKit import SwiftUI import Observation @MainActor @Observable private final class TabState { var selected = 0 var locale = Locale(identifier: "en") } private struct TabHost: View { @Bindable var state: TabState var body: some View { NeutralTabPicker(title: "panel.page", selection: $state.selected, options: [(0, "panel.overview"), (1, "panel.processes")]) .environment(\.locale, state.locale) .frame(width: 164, height: 32) } } @main struct TabPickerChecks { @MainActor static func main() { _ = NSApplication.shared NSApp.setActivationPolicy(.accessory) Task { @MainActor in let state = TabState() let window = NSWindow(contentRect: NSRect(x: 40, y: 40, width: 220, height: 70), styleMask: [.titled], backing: .buffered, defer: false) window.isReleasedWhenClosed = false let host = NSHostingView(rootView: TabHost(state: state)) window.contentView = host window.makeKeyAndOrderFront(nil) await until { find(host) != nil } let control = find(host)! await until { control.buttons.first?.title == "Overview" } precondition(control.selectedIndex == 0) precondition(control.accessibilityRole() == .tabGroup) precondition(control.accessibilityTabs()?.count == 2) let first = control.buttons[0], second = control.buttons[1] for button in [first, second] { precondition(button.accessibilityRole() == .radioButton) precondition(button.accessibilitySubrole() == .tabButtonSubrole) precondition(button.isAccessibilityElement() && button.accessibilityChildren()?.isEmpty == true, "Each tab must expose a single accessibility element") } precondition(first.isAccessibilitySelected() && !second.isAccessibilitySelected()) precondition(first.canBecomeKeyView && !second.canBecomeKeyView) state.selected = 1 await until { control.selectedIndex == 1 } precondition(second.isAccessibilitySelected() && !first.isAccessibilitySelected()) precondition((second.accessibilityValue() as? NSNumber)?.boolValue == true) precondition(control.accessibilitySelectedChildren()?.first as? NSButton === second) first.performClick(nil) await until { state.selected == 0 } precondition(second.accessibilityPerformPress()) await until { state.selected == 1 } precondition(window.makeFirstResponder(second)) for (code, selected) in [(UInt16(123), 0), (124, 1), (124, 1), (115, 0), (119, 1)] { let event = NSEvent.keyEvent(with: .keyDown, location: .zero, modifierFlags: [], timestamp: 0, windowNumber: window.windowNumber, context: nil, characters: "", charactersIgnoringModifiers: "", isARepeat: false, keyCode: code)! window.sendEvent(event) await until { state.selected == selected } precondition(window.firstResponder === control.buttons[selected], "Arrow navigation must retain focus on the selected tab") } for (locale, title) in [("zh-Hans", "概览"), ("zh-Hant", "概覽"), ("en", "Overview")] { state.locale = Locale(identifier: locale) await until { control.buttons[0].title == title } precondition(find(host) === control, "Locale updates must retain the mounted native control") precondition(control.buttons[0] === first && control.buttons[1] === second) precondition(control.selectedIndex == 1) } window.orderOut(nil); window.close() print("PASS: flat tab binding, native/AX press, tab roles and selection, arrow/Home/End focus navigation and in-place three-language updates") fflush(stdout); NSApp.terminate(nil) } NSApp.run() } @MainActor private static func until(_ condition: () -> Bool) async { let deadline = ContinuousClock.now.advanced(by: .seconds(3)) while !condition() && ContinuousClock.now < deadline { try? await Task.sleep(for: .milliseconds(20)) } precondition(condition(), "Timed out waiting for native tab update") } @MainActor private static func find(_ view: NSView) -> FlatTabControl? { if let control = view as? FlatTabControl { return control } return view.subviews.lazy.compactMap { find($0) }.first } }