Cotton code
109 lines · 5.0 KB · SwiftRaw
#if os(iOS)import CottonKitimport SwiftUIimport UIKitimport XCTest@testable import CottonUI/// The zoomed path on a fan tile's page, in UIKit itself (lane zoomframe,/// 15 Oct 2026): a record opened by a touch on a slip grows out of it, and/// every way back works. The navigator's guards (`TileStackTests`) never/// opened through the zoom, so a zoom push that never finished — the record/// drawn, the list still under it, back doing nothing — passed them.@MainActorfinal class TileStackZoomTests: XCTestCase {    private let key = "invented-db\u{1F}alarms"    private let record = Route.record(.note, "invented-zoomed")    /// RULE: A record opened by the slip zoom finishes its push — the list under it leaves the window, so it leaves the accessibility tree — and goes back by the X, by the edge swipe and by the settled tile's tap, each finishing too; a push left unfinished keeps the list readable under the record and makes every way back do nothing.    func testAZoomedRecordCoversTheListAndGoesBackEveryWay() async {        let navigator = Navigator()        navigator.tile = key        let stack = TileStack(key: key, navigator: navigator)        // A page that reads, lays out and reports drawn, as a record's does —        // well inside the ceiling, so the zoom waits on the page itself.        stack.settle = 2        stack.drawsRoute = { _ in AnyView(DrawingPage()) }        // 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 {            return XCTFail("the host app has no window scene")        }        let window = UIWindow(windowScene: scene)        window.frame = CGRect(x: 0, y: 0, width: 402, height: 874)        window.rootViewController = stack        window.makeKeyAndVisible()        defer { window.isHidden = true }        let list = stack.viewControllers[0]        let slip = UIView(frame: CGRect(x: 29, y: 197, width: 165, height: 135))        list.view.addSubview(slip)        stack.sources.register(slip)        func show() {            stack.update(root: AnyView(Color.clear), rootTitle: "Alarms", routes: navigator.tileStacks[key] ?? [],                         titles: [:], environment: EnvironmentValues())        }        func open() async {            stack.sources.touched(at: slip.convert(CGPoint(x: slip.bounds.midX, y: slip.bounds.midY), to: window))            navigator.open(record)            show()            await finished(stack, pages: 2, "the zoom")            XCTAssertEqual((stack.viewControllers.last as? RecordHost)?.zoomed, true, "it did not grow out of the slip")            XCTAssertNil(list.view.window, "the list stayed in the window under the zoomed record")        }        func back(_ way: String) async {            await finished(stack, pages: 1, way)            XCTAssertEqual(navigator.tileStacks[key] ?? [], [], "\(way): the tile's stack kept the record")            XCTAssertNotNil(list.view.window, "\(way): the list did not come back")        }        show()        // The list stands in the window before a finger can land on its slip.        let deadline = Date().addingTimeInterval(3)        while list.view.window == nil, Date() < deadline { try? await Task.sleep(for: .milliseconds(20)) }        XCTAssertNotNil(slip.window, "the list never stood in the window")        await open()        TileBack.of(titles: ["Alarms"], key: key, navigator: navigator)?.back(to: 0)        show()        await back("the X")        await open()        // The edge swipe ends as UIKit's own pop; the stack tells the navigator.        stack.popViewController(animated: true)        await back("the edge swipe")        await open()        // The settled tile's tap (`FanHome.toRoot`).        navigator.back(to: 0)        show()        await back("the settled tile's tap")    }    /// A record's page in miniature: a scroll of content that reports drawn    /// once it has read, as a tool's page does after its load.    private struct DrawingPage: View {        @Environment(\.pageDrawn) private var pageDrawn        var body: some View {            ScrollView {                Text("Invented record").frame(maxWidth: .infinity, minHeight: 600)            }            .background(Color.white)            .task {                try? await Task.sleep(for: .milliseconds(10))                pageDrawn?()            }        }    }    /// Waits for the stack to stand at `pages` with no transition running.    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)")    }}#endif