Cotton code
134 lines · 8.6 KB · SwiftRaw
// The notes list and the side page scroll under a swipe that starts on any// part of a row, as a person's thumb lands: a row's title, its preview, its// date, its left and right edges. A swipe across a row still pins it (right)// or offers the bin (left), as on Android (5058a8c87d); a swipe in from the// screen's left edge opens the side page, and a swipe back to the left// closes it. Invented crowd only.import XCTestfinal class NotesListSwipeTests: NotesUITestCase {    /// The parts of a row a thumb lands on, each a point 0…1 across and down it. The right edge stops short    /// of the list's scroll bar (its last 33 points), which a finger drags the other way (the system's scrubber).    private static let words: [(String, CGVector)] = [("title", CGVector(dx: 0.4, dy: 0.2)), ("preview", CGVector(dx: 0.5, dy: 0.5)),                                                      ("date", CGVector(dx: 0.15, dy: 0.85))]    private static let edges: [(String, CGVector)] = [("left edge", CGVector(dx: 0.015, dy: 0.5)), ("right edge", CGVector(dx: 0.9, dy: 0.5))]    /// A swipe up and then down from `part` of a row mid-screen moves the list each time.    @MainActor private func scrollsFrom(_ app: XCUIApplication, _ parts: [(String, CGVector)]) {        for (part, at) in parts {            guard let row = NotesUI.middleRow(app) else { return XCTFail("a row stands mid-screen before the swipe from its \(part)") }            NotesUI.drag(app, row, at: at, by: CGVector(dx: 0, dy: -240), velocity: .slow)            settle()            let up = NotesUI.whereIs(app, row.label) ?? -1_000            XCTAssertLessThan(up, row.frame.minY - 120, "a swipe up from a row's \(part) moved the list up")            guard let back = NotesUI.middleRow(app) else { return XCTFail("a row stands mid-screen after the swipe from its \(part)") }            NotesUI.drag(app, back, at: at, by: CGVector(dx: 0, dy: 240), velocity: .slow)            settle()            let down = NotesUI.whereIs(app, back.label) ?? 10_000            XCTAssertGreaterThan(down, back.frame.minY + 120, "a swipe down from a row's \(part) moved the list down")        }    }    /// Down into the crowd, so the list can go both ways.    @MainActor private func intoTheCrowd(_ app: XCUIApplication) {        NotesUI.list(app)        for _ in 0..<2 { NotesUI.drag(app, from: CGVector(dx: 0.5, dy: 0.8), by: CGVector(dx: 0, dy: -400)) }        settle()    }    // RULE: a swipe up or down that starts on a row's title, preview or date scrolls the list.    @MainActor func testASwipeFromARowsWordsScrollsTheList() throws {        let app = NotesUI.launch()        intoTheCrowd(app)        try skipUnderTheShellsCard(app)        scrollsFrom(app, Self.words)    }    // RULE: a swipe up or down that starts at a row's left or right edge scrolls the list (the left edge also opens the side page, but only sideways).    @MainActor func testASwipeFromARowsEdgesScrollsTheList() throws {        let app = NotesUI.launch()        intoTheCrowd(app)        try skipUnderTheShellsCard(app)        scrollsFrom(app, Self.edges)        XCTAssertFalse(NotesUI.id(app, "side-page").exists, "a swipe up or down at the edge did not open the side page")    }    // RULE: a short swipe right shows Pin and a short swipe left shows the bin; a tap on Pin pins the note, and Unpin takes it off.    @MainActor func testASwipeEachWayOffersItsAct() throws {        let app = NotesUI.launch()        intoTheCrowd(app)        try skipUnderTheShellsCard(app)        guard let row = NotesUI.middleRow(app) else { return XCTFail("a row stands mid-screen") }        let title = row.label.components(separatedBy: ",").first ?? row.label        NotesUI.drag(app, row, at: CGVector(dx: 0.6, dy: 0.5), by: CGVector(dx: -110, dy: 0), velocity: .slow)        let bin = NotesUI.button(app, "Move to the bin")        XCTAssertTrue(bin.waitForExistence(timeout: 3), "a short swipe left showed Move to the bin")        // A swipe back closes it, and nothing is binned.        NotesUI.drag(app, row, at: CGVector(dx: 0.3, dy: 0.5), by: CGVector(dx: 140, dy: 0), velocity: .slow)        XCTAssertTrue(bin.waitForNonExistence(timeout: 3), "a swipe back put the bin away")        XCTAssertTrue(NotesUI.row(app, title + ",").exists, "\(title) is still in the list")        settle()        guard let again = NotesUI.rowsOnScreen(app).first(where: { $0.label.hasPrefix(title + ",") }) else { return XCTFail("\(title) stands") }        NotesUI.drag(app, again, at: CGVector(dx: 0.3, dy: 0.5), by: CGVector(dx: 110, dy: 0), velocity: .slow)        let pin = NotesUI.button(app, "Pin")        XCTAssertTrue(pin.waitForExistence(timeout: 3), "a short swipe right showed Pin")        pin.tap()        // Pinned, it stands at the top, with the pinned ones.        XCTAssertTrue(pin.waitForNonExistence(timeout: 3), "Pin ran and went")        for _ in 0..<12 { NotesUI.drag(app, from: CGVector(dx: 0.5, dy: 0.55), by: CGVector(dx: 0, dy: 400)) }        settle()        let first = NotesUI.rowsOnScreen(app).first        XCTAssertTrue(first?.label.hasPrefix(title + ",") == true, "a tap on Pin pinned \(title) to the top: \(first?.label ?? "none")")        let top = NotesUI.row(app, title + ",")        // Unpin through the hold, so the crowd stays as it was.        top.press(forDuration: 0.8)        let unpin = NotesUI.id(app, "notes-act-unpin")        XCTAssertTrue(unpin.waitForExistence(timeout: 3), "the hold offers Unpin for a pinned note")        unpin.tap()    }    // RULE: a swipe in from the screen's left edge opens the side page; a swipe back to the left, or a tap beside it, closes it.    @MainActor func testTheSidePageOpensFromTheEdgeAndClosesBothWays() throws {        let app = NotesUI.launch()        intoTheCrowd(app)        try skipUnderTheShellsCard(app)        let side = NotesUI.id(app, "side-page")        let edge = app.windows.firstMatch.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 0.5)).withOffset(CGVector(dx: 2, dy: 0))        edge.press(forDuration: 0.05, thenDragTo: edge.withOffset(CGVector(dx: 220, dy: 0)), withVelocity: .fast, thenHoldForDuration: 0.05)        XCTAssertTrue(side.waitForExistence(timeout: 3), "a swipe in from the left edge opened the side page")        settle()        NotesUI.drag(side, at: CGVector(dx: 0.7, dy: 0.5), by: CGVector(dx: -220, dy: 0))        XCTAssertTrue(side.waitForNonExistence(timeout: 3), "a swipe back to the left closed the side page")        NotesUI.id(app, "notes-place").tap()        XCTAssertTrue(side.waitForExistence(timeout: 3), "the place's name opened the side page")        settle()        app.windows.firstMatch.coordinate(withNormalizedOffset: CGVector(dx: 0.95, dy: 0.5)).tap()        XCTAssertTrue(side.waitForNonExistence(timeout: 3), "a tap beside the side page closed it")    }    // RULE: the side page's tags scroll under a swipe that starts on a tag's name, and a tap there still opens that tag.    @MainActor func testTheSidePagesTagsScroll() throws {        let app = NotesUI.launch(["-notes-open", "side"])        let side = NotesUI.id(app, "side-page")        XCTAssertTrue(side.waitForExistence(timeout: 90), "the side page opened")        let first = app.buttons.matching(NSPredicate(format: "identifier BEGINSWITH %@", "side-tag-crowd/")).firstMatch        XCTAssertTrue(first.waitForExistence(timeout: 10), "the crowd's tags stand on the side page")        let tags = NotesUI.seen(app).filter { $0.id.hasPrefix("side-tag-crowd/") && $0.type == .button }        guard let mid = tags.first(where: { $0.frame.minY > app.windows.firstMatch.frame.height * 0.45 && $0.frame.maxY < app.windows.firstMatch.frame.height * 0.75 })        else { return XCTFail("a tag stands mid-page") }        NotesUI.drag(app, mid, at: CGVector(dx: 0.3, dy: 0.5), by: CGVector(dx: 0, dy: -260), velocity: .slow)        settle()        let after = NotesUI.seen(app).first { $0.id == mid.id }?.frame.minY ?? -1_000        XCTAssertLessThan(after, mid.frame.minY - 120, "a swipe up from a tag's name scrolled the side page")        XCTAssertTrue(side.exists, "the side page stayed open")        let tag = NotesUI.id(app, mid.id)        if !tag.isHittable { NotesUI.drag(app, from: CGVector(dx: 0.3, dy: 0.3), by: CGVector(dx: 0, dy: 260)) }        settle()        tag.tap()        XCTAssertTrue(side.waitForNonExistence(timeout: 3), "a tap on a tag closed the side page")        let name = mid.id.replacingOccurrences(of: "side-tag-", with: "")        XCTAssertTrue(NotesUI.id(app, "notes-place").label.contains(name.components(separatedBy: "/").last ?? name),                      "the list stands on the tag tapped, \(name)")    }}