Cotton code
127 lines · 6.7 KB · SwiftRaw
#if os(iOS)import CottonKitimport SwiftUIimport UIKitimport XCTest@testable import CottonUI/// One page in the accessibility tree at a time (walker pass 3, 16 Oct 2026):/// Journal's, News' and Money's lists were read behind an open record,/// Thoughts' cards after a swipe down, and a folded fan read its ~20 tiles/// and the caps as tiny buttons.@MainActorfinal class OnePageReadTests: XCTestCase {    private let key = "invented-db\u{1F}journal"    private func window(_ root: UIViewController) -> UIWindow? {        // A window of the app's own scene: since iOS 26 a window with no scene        // is never put on screen, so no transition in it ever finishes.        guard let scene = UIApplication.shared.connectedScenes.compactMap({ $0 as? UIWindowScene }).first else {            XCTFail("the host app has no window scene")            return nil        }        let window = UIWindow(windowScene: scene)        window.frame = CGRect(x: 0, y: 0, width: 402, height: 874)        window.rootViewController = root        window.makeKeyAndVisible()        return window    }    private func finished(_ stack: TileStack, pages: Int, _ way: String) async {        let deadline = Date().addingTimeInterval(3)        while Date() < deadline {            if stack.viewControllers.count == pages, stack.transitionCoordinator == nil { return }            try? await Task.sleep(for: .milliseconds(20))        }        XCTFail("\(way) never finished: \(stack.viewControllers.count) pages, transition running: \(stack.transitionCoordinator != nil)")    }    /// RULE: on a fan tile's page only the top page is read — the list and every record under the page shown leave the accessibility tree until it goes — and a change of pages asked for while UIKit moves the stack (its edge swipe, the zoom's pull) is made once that move ends, never dropped: a dropped one left a closed page standing, read under the page shown.    func testOnlyTheTopPageIsRead() async {        let navigator = Navigator()        navigator.tile = key        let stack = TileStack(key: key, navigator: navigator)        stack.reduceMotion = true        stack.drawsRoute = { _ in AnyView(Text("Invented entry")) }        guard let window = window(stack) else { return }        defer { window.isHidden = true }        func show() {            stack.update(root: AnyView(Text("Invented list")), rootTitle: "Journal",                         routes: navigator.tileStacks[key] ?? [], titles: [:], environment: EnvironmentValues())        }        func read() -> [Bool] { stack.viewControllers.map { !$0.view.accessibilityElementsHidden } }        show()        XCTAssertEqual(read(), [true], "the list alone is read")        navigator.open(.record(.note, "invented-first"))        show()        await finished(stack, pages: 2, "the first record")        XCTAssertEqual(read(), [false, true], "the list under an open record is not read")        navigator.open(.record(.note, "invented-second"))        show()        await finished(stack, pages: 3, "the second record")        XCTAssertEqual(read(), [false, false, true])        // UIKit's own pop runs; the navigator closes everything while it does.        stack.popViewController(animated: true)        navigator.back(to: 0)        show()        await finished(stack, pages: 1, "a close asked for mid-pop")        XCTAssertEqual(read(), [true], "the list is read again, and nothing closed stands")    }    /// RULE: while a sheet covers Home no page of a tile's stack is read, the top one included — SwiftUI's hiding under a sheet never reaches the UIKit stack — and the top page is read again when the sheet goes (walker pass 4, 16 Oct 2026: Thoughts read under the Databases sheet).    func testNothingIsReadUnderASheet() async {        let navigator = Navigator()        navigator.tile = key        let stack = TileStack(key: key, navigator: navigator)        stack.reduceMotion = true        stack.drawsRoute = { _ in AnyView(Text("Invented entry")) }        guard let window = window(stack) else { return }        defer { window.isHidden = true }        func show() {            stack.update(root: AnyView(Text("Invented list")), rootTitle: "Thoughts",                         routes: navigator.tileStacks[key] ?? [], titles: [:], environment: EnvironmentValues())        }        func read() -> [Bool] { stack.viewControllers.map { !$0.view.accessibilityElementsHidden } }        show()        stack.covered = true        XCTAssertEqual(read(), [false], "the list is not read under the sheet")        navigator.open(.record(.note, "invented-first"))        show()        await finished(stack, pages: 2, "a record opened under the sheet")        XCTAssertEqual(read(), [false, false], "nor a record opened while it stands")        stack.covered = false        XCTAssertEqual(read(), [false, true], "the sheet gone, the top page is read again")    }    /// RULE: a folded or put-away fan is out of the accessibility tree, its arcs' UIKit cards with it — SwiftUI's accessibilityHidden over the arc never reaches them — and an arc hidden by any view over it stays hidden whatever a view nearer it says.    func testAFoldedFanReadsNoCard() {        let geometry = FanGeometry(scale: Theme.paper.scale)        let motion = FanMotion(geometry: geometry, spring: FanSpring(scale: Theme.paper.scale))        let items = (0..<5).map { FanArcItem(key: "k\($0)", label: "Invented \($0)", picture: nil, identifier: "fan-tile-k\($0)") }        motion.recount(items.count)        func arc(folded: Bool, nearer: Bool) -> FanArcView? {            let host = UIHostingController(rootView:                FanArcDeck(items: items, motion: motion, geometry: geometry, reduceMotion: true, onTap: { _ in })                    .fanArcHidden(nearer)                    .frame(width: 402, height: 300)                    .fanArcHidden(folded))            guard let window = window(host) else { return nil }            defer { window.isHidden = true }            host.view.layoutIfNeeded()            func find(_ view: UIView) -> FanArcView? {                if let arc = view as? FanArcView { return arc }                for sub in view.subviews { if let arc = find(sub) { return arc } }                return nil            }            return find(host.view)        }        XCTAssertEqual(arc(folded: false, nearer: false)?.accessibilityElementsHidden, false, "a standing fan is read")        XCTAssertEqual(arc(folded: true, nearer: false)?.accessibilityElementsHidden, true,                       "a folded fan reads no card, though the view nearer the arc does not hide it")        XCTAssertEqual(arc(folded: false, nearer: true)?.accessibilityElementsHidden, true, "an arc put away reads no card")    }}#endif