Cotton code
93 lines · 5.1 KB · SwiftRaw
// A chat row's swipes (owner, Comms 28): exactly one act a side, right Unread (or Read), left Archive; a full swipe// runs it; Pin, Mute and Delete only in the long press. And while a row stands swiped open its name and last line stay// in view (on Comms 28 three acts slid them off the screen, leaving only the time). Over Comms Dev's invented chats (-chat-warm-seed YES, the core's own// chat.list answers), at the default size and the largest.import XCTestfinal class ChatListSwipeTests: XCTestCase {    private let asha = "11111111-1111-4111-8111-0000000000a1"    private static let largestText = ["-UIPreferredContentSizeCategoryName", "UICTContentSizeCategoryAccessibilityXXXL"]    override func setUp() {        continueAfterFailure = false        XCUIDevice.shared.orientation = .portrait    }    private func keep(_ name: String) {        let shot = XCTAttachment(screenshot: XCUIScreen.main.screenshot())        shot.name = name        shot.lifetime = .keepAlways        add(shot)    }    @MainActor private func launched(_ extra: [String]) -> XCUIApplication {        let app = XCUIApplication()        app.launchArguments += ["-chat-warm-seed", "YES"] + extra        app.launch()        XCTAssertTrue(row(app).waitForExistence(timeout: 30), "Asha's chat is listed")        _ = ShellCard.refuse(app).waitForExistence(timeout: 6)        return app    }    private func row(_ app: XCUIApplication) -> XCUIElement {        app.descendants(matching: .any)["chat-row-\(asha)"].firstMatch    }    /// The acts a swipe left standing: the table's buttons beside the row, by label.    @MainActor private func shown(_ app: XCUIApplication, _ row: XCUIElement) -> [String] {        let table = app.tables.firstMatch        return table.buttons.allElementsBoundByIndex            .filter { $0.exists && $0.isHittable && abs($0.frame.midY - row.frame.midY) < row.frame.height / 2 && !$0.identifier.hasPrefix("chat-row-") }            .map(\.label)            .filter { !$0.isEmpty }    }    /// Swiped open one way: exactly the one act stands, and the row keeps its width (it slides; it never squeezes its    /// name and last line out to leave only the time).    @MainActor private func opens(_ app: XCUIApplication, left: Bool, act: String, shot: String) {        let row = row(app)        let rest = row.frame.minX        let from = row.coordinate(withNormalizedOffset: CGVector(dx: left ? 0.75 : 0.25, dy: 0.5))        from.press(forDuration: 0.05, thenDragTo: from.withOffset(CGVector(dx: left ? -110 : 110, dy: 0)), withVelocity: .slow, thenHoldForDuration: 0.2)        Thread.sleep(forTimeInterval: 0.8)        keep("chat-swipe-\(shot)")        XCTAssertEqual(shown(app, row), [act], "one act a side")        // The row slides by its acts' width: three acts slid the picture, the name and the last line off the screen,        // leaving the time. The name starts after the side, the picture and the gap (18 + 54 + 13 points; the picture        // grows with the text up to 1.3 times): its first letters stay on the screen.        let name = row.frame.minX + 18 + 54 * (shot.hasPrefix("largest") ? 1.3 : 1) + 13        XCTAssertGreaterThanOrEqual(name, app.windows.firstMatch.frame.minX + 4, "swiped open, the name and last line stay in view (row from \(rest) to \(row.frame.minX))")        // Closed again by a tap on the row itself.        row.coordinate(withNormalizedOffset: CGVector(dx: left ? 0.2 : 0.9, dy: 0.5)).tap()        Thread.sleep(forTimeInterval: 0.6)    }    @MainActor private func swipes(_ extra: [String], _ shots: String) {        let app = launched(extra)        opens(app, left: true, act: "Archive", shot: "\(shots)-left")        opens(app, left: false, act: "Read", shot: "\(shots)-right")  // Asha's chat has unread messages: Read.        // The long press keeps every act.        row(app).press(forDuration: 1.0)        for act in ["Pin", "Mute", "Archive"] {            XCTAssertTrue(app.buttons[act].waitForExistence(timeout: 5), "\(act) in the long press")        }        XCTAssertTrue(app.buttons.matching(NSPredicate(format: "label BEGINSWITH 'Delete'")).firstMatch.exists, "Delete in the long press")        keep("chat-swipe-\(shots)-held")    }    // RULE (owner, Comms 28): one act a side, the row's words standing while it is swiped; the rest in the long press.    @MainActor func testOneActASideAndTheRowKeepsItsWords() { swipes([], "default") }    @MainActor func testOneActASideAndTheRowKeepsItsWordsAtTheLargestText() { swipes(Self.largestText, "largest") }    // RULE (owner, Comms 28): a full swipe left archives the chat at once.    @MainActor func testAFullSwipeLeftArchives() {        let app = launched([])        let row = row(app)        row.coordinate(withNormalizedOffset: CGVector(dx: 0.9, dy: 0.5))            .press(forDuration: 0.05, thenDragTo: row.coordinate(withNormalizedOffset: CGVector(dx: -0.2, dy: 0.5)), withVelocity: .fast, thenHoldForDuration: 0)        let gone = XCTNSPredicateExpectation(predicate: NSPredicate(format: "exists == false"), object: row)        XCTAssertEqual(XCTWaiter.wait(for: [gone], timeout: 5), .completed, "archived, it leaves the list")        keep("chat-swipe-full-archived")    }}