Cotton code
272 lines · 16.7 KB · SwiftRaw
// The shared Look screen as every app's tests drive it (shared/DECISIONS.md "Look"; Chrome/LookScreen.swift):// papers by their light and dark halves, inks, faces and the size, each read selected by its trait; Reset asks// first. Only selection, reset and carry-over are proven (owner, 23 Nov 2026): no test per paper or ink.import XCTestenum LookUI {    @MainActor static func id(_ app: XCUIApplication, _ id: String) -> XCUIElement {        app.descendants(matching: .any).matching(identifier: id).firstMatch    }    @MainActor static func paper(_ app: XCUIApplication, _ paper: String, dark: Bool) -> XCUIElement {        id(app, "look-paper-\(dark ? "dark" : "light")-\(paper)")    }    /// Whether `element` stands wholly inside the window (read from frames: an element scrolled off the paper row    /// has no activation point, and asking it whether it is hittable fails the test).    @MainActor static func onScreen(_ app: XCUIApplication, _ element: XCUIElement) -> Bool {        guard element.exists else { return false }        let window = app.windows.firstMatch.frame, frame = element.frame        return frame.minX >= window.minX && frame.maxX <= window.maxX && frame.minY >= window.minY + 44 && frame.maxY <= window.maxY - 20    }    /// Brings `element` into the window: the page scrolls up and down, the paper row sideways.    @MainActor static func reach(_ app: XCUIApplication, _ element: XCUIElement, tries: Int = 10) {        let papers = element.identifier.hasPrefix("look-paper-")        // Each appearance has its own row of papers (FEEDBACK-4 A1).        let row = id(app, element.identifier.hasPrefix("look-paper-dark-") ? "look-papers-dark" : "look-papers-light")        for _ in 0..<tries {            let frame = element.frame, window = app.windows.firstMatch.frame            if frame.maxY > window.maxY - 20 { app.swipeUp(velocity: .slow) } else if frame.minY < window.minY + 44 { app.swipeDown(velocity: .slow) } else { break }        }        guard papers else { return }        for _ in 0..<6 where !onScreen(app, element) {            if element.frame.minX < 0 { row.swipeRight(velocity: .slow) } else { row.swipeLeft(velocity: .slow) }        }    }    @MainActor static func tap(_ app: XCUIApplication, _ element: XCUIElement, file: StaticString = #filePath, line: UInt = #line) {        XCTAssertTrue(element.waitForExistence(timeout: 20), "\(element) stands", file: file, line: line)        reach(app, element)        element.tap()        // An ink sets the home-screen icon, and iOS says so in its own alert (expected, owner 23 Nov 2026): close it.        if element.identifier.hasPrefix("look-ink-") { closeIconAlert(app) }    }    /// Closes iOS's "You have changed the icon" alert when it comes.    @MainActor static func closeIconAlert(_ app: XCUIApplication, timeout: TimeInterval = 4) {        let alert = app.alerts.firstMatch        guard alert.waitForExistence(timeout: timeout) else { return }        alert.buttons.firstMatch.tap()        _ = alert.waitForNonExistence(timeout: 4)    }    /// Waits until `element` reads selected (the core has answered and the screen drawn it again).    @MainActor static func waitSelected(_ element: XCUIElement, _ selected: Bool = true, timeout: TimeInterval = 15) -> Bool {        let wanted = NSPredicate(format: "isSelected == %@", NSNumber(value: selected))        return XCTWaiter().wait(for: [XCTNSPredicateExpectation(predicate: wanted, object: element)], timeout: timeout) == .completed    }    /// The size the screen shows now.    @MainActor static func size(_ app: XCUIApplication) -> String { id(app, "look-size-value").label }    /// Reset to default, asked first: `act` is the app's own act in its ask.    @MainActor static func reset(_ app: XCUIApplication, act: String, file: StaticString = #filePath, line: UInt = #line) {        let reset = id(app, "look-reset")        XCTAssertTrue(reset.waitForExistence(timeout: 20), "Reset stands at the foot", file: file, line: line)        reach(app, reset, tries: 16)        reset.tap()        // The act by its identifier, or by its words where the app's ask names none (Story's panel).        let byId = id(app, act)        let yes = byId.waitForExistence(timeout: 5) ? byId : app.buttons[act].firstMatch        XCTAssertTrue(yes.waitForExistence(timeout: 10), "Reset asks first", file: file, line: line)        yes.tap()        closeIconAlert(app)    }}/// One app's Look, proven the same way in each (owner, 23 Nov 2026: selection, reset and carry-over only).struct LookCase {    /// Launches the app and opens Look (no reset).    let open: @MainActor () -> XCUIApplication    /// The defaults: the light and dark paper, the ink, the face and the size's label.    let defaults: (light: String, dark: String, ink: String, face: String, size: String)    /// Other choices to make: a light paper, a dark paper, an ink and a face (each not the default).    let other: (light: String, dark: String, ink: String, face: String)    /// The app's own act in its Reset ask.    let resetAct: String    @MainActor func standsAtDefaults(_ app: XCUIApplication, timeout: TimeInterval = 3) -> Bool {        LookUI.waitSelected(LookUI.paper(app, defaults.light, dark: false), timeout: timeout)            && LookUI.paper(app, defaults.dark, dark: true).isSelected            && LookUI.id(app, "look-ink-\(defaults.ink)").isSelected            && LookUI.id(app, "look-face-\(defaults.face)").isSelected            && LookUI.size(app) == defaults.size    }    /// Opens Look at the defaults, whatever an earlier run left.    @MainActor func fresh() -> XCUIApplication {        let app = open()        XCTAssertTrue(LookUI.id(app, "look-preview").waitForExistence(timeout: 30), "Look opens on its preview")        if !standsAtDefaults(app) {            LookUI.reset(app, act: resetAct)            XCTAssertTrue(standsAtDefaults(app, timeout: 15), "Reset brings back the defaults")            app.swipeDown(velocity: .fast)            app.swipeDown(velocity: .fast)        }        return app    }    /// FEEDBACK-4 A1 and A2: Paper is two rows, one for when the phone is light and one for when it is dark, under    /// their own headings and each with one paper chosen; and the sample at the top redraws at once with every choice    /// (a paper in either row, an ink, a face), read as the sample's own pixels before and after.    @MainActor func paperRowsAndTheSample(file: StaticString = #filePath, line: UInt = #line) {        let app = fresh()        let preview = LookUI.id(app, "look-preview")        func chosen(dark: Bool) -> Int {            app.buttons.matching(NSPredicate(format: "identifier BEGINSWITH %@ AND selected == true", "look-paper-\(dark ? "dark" : "light")-")).count        }        for (dark, heading) in [(false, "When the phone is light"), (true, "When the phone is dark")] {            let row = LookUI.id(app, "look-papers-\(dark ? "dark" : "light")")            XCTAssertTrue(row.waitForExistence(timeout: 10), "a row of papers for \(heading)", file: file, line: line)            let title = app.staticTexts[heading]            XCTAssertTrue(title.exists, "\(heading) heads its row", file: file, line: line)            XCTAssertLessThan(title.frame.maxY, row.frame.minY + 4, "\(heading) stands above its row", file: file, line: line)            XCTAssertEqual(chosen(dark: dark), 1, "one paper chosen for \(heading)", file: file, line: line)        }        XCTAssertFalse(app.staticTexts["Paper"].exists, "no single Paper heading over both", file: file, line: line)        let shot = XCTAttachment(screenshot: app.screenshot())        shot.name = "look-paper-rows"        shot.lifetime = .keepAlways        XCTContext.runActivity(named: "Paper in two rows") { $0.add(shot) }        // The sample redraws with each choice.        func sample() -> Data {            LookUI.reach(app, preview)            return preview.screenshot().pngRepresentation        }        func redraws(_ what: String, _ tap: XCUIElement) {            let before = sample()            LookUI.tap(app, tap)            XCTAssertTrue(LookUI.waitSelected(tap), "\(what) is chosen", file: file, line: line)            var after = sample()            for _ in 0..<10 where after == before { usleep(200_000); after = sample() }            XCTAssertNotEqual(after, before, "the sample redraws when \(what) is chosen", file: file, line: line)        }        redraws("a dark paper", LookUI.paper(app, other.dark, dark: true))        XCTAssertEqual(chosen(dark: true), 1, "still one paper chosen for dark", file: file, line: line)        redraws("a light paper", LookUI.paper(app, other.light, dark: false))        XCTAssertEqual(chosen(dark: false), 1, "still one paper chosen for light", file: file, line: line)        redraws("an ink", LookUI.id(app, "look-ink-\(other.ink)"))        redraws("a face", LookUI.id(app, "look-face-\(other.face)"))        LookUI.reset(app, act: resetAct, file: file, line: line)        XCTAssertTrue(standsAtDefaults(app, timeout: 15), "Reset brings every choice back", file: file, line: line)    }    /// Every choice follows a tap, each half apart; Reset asks and brings every one back.    @MainActor func selectionAndReset(file: StaticString = #filePath, line: UInt = #line) {        let app = fresh()        LookUI.tap(app, LookUI.paper(app, other.dark, dark: true))        XCTAssertTrue(LookUI.waitSelected(LookUI.paper(app, other.dark, dark: true)), "\(other.dark) is chosen for dark", file: file, line: line)        XCTAssertTrue(LookUI.waitSelected(LookUI.paper(app, defaults.dark, dark: true), false) || other.dark == defaults.dark, "the old dark is not", file: file, line: line)        XCTAssertTrue(LookUI.paper(app, defaults.light, dark: false).isSelected, "light is untouched", file: file, line: line)        LookUI.tap(app, LookUI.paper(app, other.light, dark: false))        XCTAssertTrue(LookUI.waitSelected(LookUI.paper(app, other.light, dark: false)), "\(other.light) is chosen for light", file: file, line: line)        LookUI.tap(app, LookUI.id(app, "look-ink-\(other.ink)"))        XCTAssertTrue(LookUI.waitSelected(LookUI.id(app, "look-ink-\(other.ink)")), "\(other.ink) is chosen", file: file, line: line)        LookUI.tap(app, LookUI.id(app, "look-face-\(other.face)"))        XCTAssertTrue(LookUI.waitSelected(LookUI.id(app, "look-face-\(other.face)")), "\(other.face) is chosen", file: file, line: line)        LookUI.tap(app, LookUI.id(app, "look-size-larger"))        let value = LookUI.id(app, "look-size-value")        let grew = XCTNSPredicateExpectation(predicate: NSPredicate(format: "label != %@", defaults.size), object: value)        XCTAssertEqual(XCTWaiter().wait(for: [grew], timeout: 10), .completed, "the size grows", file: file, line: line)        LookUI.reset(app, act: resetAct, file: file, line: line)        XCTAssertTrue(standsAtDefaults(app, timeout: 15), "Reset brings every choice back", file: file, line: line)    }    /// A face chosen reaches the drawn words and stands when the app opens again, also on a server deployed before    /// Look (`-look-served-before YES`: no `type_face` column; the owner's iPhone on 23 Nov 2026, where choosing a    /// font changed nothing).    @MainActor func faceReachesTheWords(_ openWith: @MainActor ([String]) -> XCUIApplication, file: StaticString = #filePath, line: UInt = #line) {        let before = ["-look-served-before", "YES"]        var app = openWith(before)        XCTAssertTrue(LookUI.id(app, "look-preview").waitForExistence(timeout: 30), file: file, line: line)        if !standsAtDefaults(app) { LookUI.reset(app, act: resetAct, file: file, line: line) }        let face = LookUI.id(app, "look-face-\(other.face)")        LookUI.tap(app, face)        XCTAssertTrue(LookUI.waitSelected(face), "\(other.face) is chosen", file: file, line: line)        let drawnFile = LookUI.faceFiles[other.face] ?? other.face        let drawn = XCTNSPredicateExpectation(predicate: NSPredicate(format: "value BEGINSWITH %@", drawnFile), object: LookUI.id(app, "look-drawn-face"))        XCTAssertEqual(XCTWaiter().wait(for: [drawn], timeout: 10), .completed, "the words are drawn in \(drawnFile): \(LookUI.drawnFace(app))", file: file, line: line)        app.terminate()        app = openWith(before)        XCTAssertTrue(LookUI.id(app, "look-preview").waitForExistence(timeout: 30), file: file, line: line)        XCTAssertTrue(LookUI.waitSelected(LookUI.id(app, "look-face-\(other.face)")), "\(other.face) still stands", file: file, line: line)        XCTAssertTrue(LookUI.drawnFace(app).hasPrefix(drawnFile), "still drawn in \(drawnFile): \(LookUI.drawnFace(app))", file: file, line: line)        LookUI.reset(app, act: resetAct, file: file, line: line)        XCTAssertTrue(standsAtDefaults(app, timeout: 15), file: file, line: line)        XCTAssertEqual(LookUI.drawnFace(app), "own", "Reset: the app's own face again", file: file, line: line)    }    /// A choice stands when the app opens again.    @MainActor func carryOver(file: StaticString = #filePath, line: UInt = #line) {        var app = fresh()        LookUI.tap(app, LookUI.paper(app, other.dark, dark: true))        XCTAssertTrue(LookUI.waitSelected(LookUI.paper(app, other.dark, dark: true)), file: file, line: line)        LookUI.tap(app, LookUI.id(app, "look-ink-\(other.ink)"))        XCTAssertTrue(LookUI.waitSelected(LookUI.id(app, "look-ink-\(other.ink)")), file: file, line: line)        app.terminate()        app = open()        XCTAssertTrue(LookUI.id(app, "look-preview").waitForExistence(timeout: 30), file: file, line: line)        XCTAssertTrue(LookUI.waitSelected(LookUI.paper(app, other.dark, dark: true)), "\(other.dark) still stands for dark", file: file, line: line)        XCTAssertTrue(LookUI.waitSelected(LookUI.id(app, "look-ink-\(other.ink)")), "\(other.ink) still stands", file: file, line: line)        LookUI.reset(app, act: resetAct, file: file, line: line)        XCTAssertTrue(standsAtDefaults(app, timeout: 15), file: file, line: line)    }    /// The quick look (LOOK-CHANGE.md: one look at each paper and each face on one screen): each paper's light half,    /// then each face, chosen on Look, the app opened again on `main`, its screen kept as an attachment; Reset after.    @MainActor func quickLook(_ main: @MainActor () -> XCUIApplication) {        var app = fresh()        let papers = app.descendants(matching: .any)            .matching(NSPredicate(format: "identifier BEGINSWITH 'look-paper-light-'")).allElementsBoundByIndex            .map { String($0.identifier.dropFirst("look-paper-light-".count)) }        let faces = ["own", "serif", "typewriter", "hyperlegible", "dyslexic"]        func keep(_ name: String) {            app.terminate()            let shown = main()            sleep(4)            let shot = XCTAttachment(screenshot: shown.screenshot())            shot.name = name            shot.lifetime = .keepAlways            XCTContext.runActivity(named: name) { $0.add(shot) }            shown.terminate()            app = open()            XCTAssertTrue(LookUI.id(app, "look-preview").waitForExistence(timeout: 30))        }        for paper in papers {            LookUI.tap(app, LookUI.paper(app, paper, dark: false))            _ = LookUI.waitSelected(LookUI.paper(app, paper, dark: false))            keep("paper-\(paper)")        }        LookUI.tap(app, LookUI.paper(app, defaults.light, dark: false))        _ = LookUI.waitSelected(LookUI.paper(app, defaults.light, dark: false))        for face in faces {            LookUI.tap(app, LookUI.id(app, "look-face-\(face)"))            _ = LookUI.waitSelected(LookUI.id(app, "look-face-\(face)"))            keep("face-\(face)")        }        LookUI.reset(app, act: resetAct)        XCTAssertTrue(standsAtDefaults(app, timeout: 15), "Reset brings every choice back")    }    /// At the largest text, Reset at the foot is still reached.    @MainActor func largestText(_ open: @MainActor () -> XCUIApplication, file: StaticString = #filePath, line: UInt = #line) {        let app = open()        let reset = LookUI.id(app, "look-reset")        XCTAssertTrue(reset.waitForExistence(timeout: 30), file: file, line: line)        LookUI.reach(app, reset, tries: 20)        XCTAssertTrue(reset.isHittable, "Reset is reached at the largest text", file: file, line: line)    }}extension LookUI {    /// The face Look's drawn words stand in (Cotton Dev's probe: the PostScript name the phone resolves, `own` for the    /// app's own face).    @MainActor static func drawnFace(_ app: XCUIApplication) -> String {        (id(app, "look-drawn-face").value as? String) ?? ""    }    /// Each face's file, as its PostScript name begins.    static let faceFiles = ["serif": "Literata", "typewriter": "CourierPrime", "hyperlegible": "AtkinsonHyperlegible", "dyslexic": "OpenDyslexic"]}