Cotton code
96 lines · 3.6 KB · SwiftRaw
// The side page's drawer (NotesDrawer.swift; the owner, 22 Nov 2026: it pushes// the list and follows the finger): its width leaves a fifth of the list// showing; it springs open and closed by itself; a finger that takes hold// while it moves catches it where it is drawn; let go, a flick decides and a// slow let-go goes to the nearer end.import XCTest@testable import CottonUI#if os(iOS)@MainActorfinal class NotesDrawerTests: XCTestCase {    /// Frames at 60 a second until it stands still, or `limit` frames.    private func run(_ drawer: NotesDrawer, frames limit: Int = 120) {        for _ in 0..<limit { drawer.step(1.0 / 60) }    }    func testItLeavesAFifthOfTheListShowing() {        XCTAssertEqual(NotesDrawer.width(across: 390), 312)        XCTAssertEqual(390 - NotesDrawer.width(across: 390), 78, "a fifth of a 390-point screen stays the list's")        XCTAssertEqual(NotesDrawer.width(across: 1024), 380, "a wide screen's page is no wider than 380")    }    func testItSpringsOpenAndClosedWithNoBounce() {        let drawer = NotesDrawer()        drawer.go(true)        var last: CGFloat = 0        for _ in 0..<40 {            drawer.step(1.0 / 60)            XCTAssertGreaterThanOrEqual(drawer.shown, last, "it only opens on the way open")            XCTAssertLessThanOrEqual(drawer.shown, 1)            last = drawer.shown        }        XCTAssertEqual(drawer.shown, 1, "open within two thirds of a second")        drawer.go(false)        run(drawer, frames: 40)        XCTAssertEqual(drawer.shown, 0, "closed within two thirds of a second")        XCTAssertFalse(drawer.visible)    }    func testAFingerCatchesItMidWayAndItFollows() {        let drawer = NotesDrawer()        drawer.width = 312        drawer.go(true)        run(drawer, frames: 5)        let caught = drawer.shown        XCTAssertTrue(caught > 0.1 && caught < 0.9, "mid-way after five frames: \(caught)")        drawer.grab()        XCTAssertEqual(drawer.shown, caught, "taken hold of, it stays where it is drawn")        drawer.follow(-31.2)        XCTAssertEqual(drawer.shown, caught - 0.1, accuracy: 0.0001, "it follows the finger from there, point for point")        drawer.follow(1_000)        XCTAssertEqual(drawer.shown, 1, "never past open")    }    func testLetGoAFlickDecidesElseTheNearerEnd() {        let drawer = NotesDrawer()        drawer.width = 312        drawer.grab()        drawer.follow(100)        XCTAssertFalse(drawer.release(0), "a slow let-go short of half way closes")        run(drawer)        XCTAssertEqual(drawer.shown, 0)        drawer.grab()        drawer.follow(200)        XCTAssertTrue(drawer.release(0), "a slow let-go past half way opens")        run(drawer)        XCTAssertEqual(drawer.shown, 1)        drawer.grab()        drawer.follow(-40)        XCTAssertFalse(drawer.release(-900), "a flick to the left closes, however little it moved")        run(drawer)        XCTAssertEqual(drawer.shown, 0)        // A flick is over 400 points a second (DECISIONS.md "The drawer", as Android).        drawer.grab()        drawer.follow(40)        XCTAssertFalse(drawer.release(350), "350 points a second is no flick: short of half way, it closes")        run(drawer)        drawer.grab()        drawer.follow(40)        XCTAssertTrue(drawer.release(450), "450 points a second is a flick: it opens")    }    func testReduceMotionGoesAtOnce() {        let drawer = NotesDrawer()        drawer.reduceMotion = true        drawer.go(true)        XCTAssertEqual(drawer.shown, 1)        drawer.go(false)        XCTAssertEqual(drawer.shown, 0)    }}#endif