Cotton code
116 lines · 6.6 KB · SwiftRaw
// The shell's card of another device asking to be let in (an invented Walk// Mac, -shell-card-seed YES) stands above Comms and pushes it down: Chats'// title row (New chat, Search) and a conversation's Back, its person and// Call stay clear of it and can be touched, and the card takes at most half// the screen with Refuse whole, at the default size and the largest. The// card is never tapped.import XCTestfinal class ChatShellCardTests: 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 launched(_ extra: [String]) -> XCUIApplication {        let app = XCUIApplication()        app.launchArguments += ["-chat-warm-seed", "YES"] + ShellCard.seed + extra        app.launch()        return app    }    @MainActor private func walk(_ extra: [String]) {        let app = launched(extra)        let compose = app.buttons["chat-compose"]        XCTAssertTrue(compose.waitForExistence(timeout: 30), "Chats came in")        ShellCard.fits(app)        ShellCard.clear(app, compose, "New chat")        ShellCard.clear(app, app.descendants(matching: .any)["chat-search"], "Search")        app.terminate()        let thread = launched(["-chat-warm-open", asha] + extra)        ShellCard.clear(thread, thread.buttons["chat-back"], "a conversation's Back")        ShellCard.clear(thread, thread.buttons["chat-person"], "a conversation's person")        ShellCard.clear(thread, thread.buttons["chat-call"], "a conversation's Call")    }    // RULE: with the card standing, Chats' New chat and Search and a conversation's Back, person and Call are clear of it and can be touched.    @MainActor func testTheCardPushesCommsDown() { walk([]) }    // RULE: at the largest text the card stays within half the screen with Refuse whole, and Comms' top controls are clear of it.    @MainActor func testTheCardPushesCommsDownAtTheLargestText() { walk(Self.largestText) }}/// The shell's card of another device asking to be let in. `seed` stands an/// invented one (Walk Mac, `-shell-card-seed YES`, a development build only):/// it reaches no server, and its Refuse only takes it down and says so (`ShellToastUI`).enum ShellCard {    static let seed = ["-shell-card-seed", "YES"]    /// The card's Refuse: standing on top while it can be touched. Its id is    /// `approval-refuse`, or the card's own `approval-banner` where the card    /// (an app's look) hands its id to everything in it.    @MainActor static func refuse(_ app: XCUIApplication) -> XCUIElement {        app.buttons.matching(NSPredicate(format: "identifier BEGINSWITH %@", "approval-")).firstMatch    }    /// The band the shell's cards stand in, scrolling inside when they are taller than it.    @MainActor static func band(_ app: XCUIApplication) -> XCUIElement {        app.scrollViews.containing(NSPredicate(format: "identifier BEGINSWITH %@", "approval-")).firstMatch    }    /// Everything of the card drawn on screen (inside its band), or nil while none of it is drawn.    @MainActor static func frame(_ app: XCUIApplication) -> CGRect? {        let parts = app.descendants(matching: .any).matching(NSPredicate(format: "identifier BEGINSWITH %@", "approval-"))        guard parts.firstMatch.exists else { return nil }        var frame = parts.allElementsBoundByIndex.reduce(CGRect.null) { $0.union($1.frame) }        let band = band(app)        if band.exists { frame = frame.intersection(band.frame) }        return frame.isNull ? nil : frame    }    /// Whether the card stands on top (its Refuse can be touched) over any of `controls`.    @MainActor static func covers(_ app: XCUIApplication, _ controls: [XCUIElement], wait: TimeInterval) -> Bool {        let refuse = refuse(app)        guard refuse.waitForExistence(timeout: wait), refuse.isHittable, let card = frame(app) else { return false }        return controls.contains { $0.exists && $0.frame.intersects(card) }    }    /// `control` stands, can be touched, and lies wholly outside the card.    @MainActor static func clear(_ app: XCUIApplication, _ control: XCUIElement, _ name: String,                                 file: StaticString = #filePath, line: UInt = #line) {        XCTAssertTrue(control.waitForExistence(timeout: 10), "\(name) stands", file: file, line: line)        let card = frame(app) ?? .null        XCTAssertFalse(card.isNull, "the card stands while \(name) is looked at", file: file, line: line)        XCTAssertTrue(control.isHittable, "\(name) can be touched", file: file, line: line)        // A page drawn over the whole window (a note) covers the card: it stands under that page, over nothing.        if refuse(app).isHittable {            XCTAssertFalse(control.frame.intersects(card), "\(name) \(control.frame) stands clear of the card \(card)", file: file, line: line)        }        let window = app.windows.firstMatch.frame        XCTAssertTrue(window.contains(control.frame), "\(name) \(control.frame) is on screen", file: file, line: line)    }    /// The card fits: no taller than half the screen, and its Refuse whole on screen above the home bar.    @MainActor static func fits(_ app: XCUIApplication, file: StaticString = #filePath, line: UInt = #line) {        let window = app.windows.firstMatch.frame        let refuse = refuse(app)        XCTAssertTrue(refuse.waitForExistence(timeout: 10), "the card stands", file: file, line: line)        let card = frame(app) ?? .null        XCTAssertLessThanOrEqual(card.height, window.height * 0.5, "the card \(card) takes at most half the screen", file: file, line: line)        // Taller than its band, the card scrolls: Refuse is reached by a swipe up inside it, never tapped.        let band = band(app)        for _ in 0..<4 where band.exists && !(refuse.isHittable && refuse.frame.maxY <= band.frame.maxY + 1) {            let start = band.coordinate(withNormalizedOffset: CGVector(dx: 0.9, dy: 0.8))            start.press(forDuration: 0.01, thenDragTo: start.withOffset(CGVector(dx: 0, dy: -band.frame.height * 0.5)))        }        if band.exists {            XCTAssertLessThanOrEqual(refuse.frame.maxY, band.frame.maxY + 1, "Refuse \(refuse.frame) stands whole in the card's band \(band.frame)", file: file, line: line)        }        XCTAssertTrue(refuse.isHittable, "Refuse can be touched", file: file, line: line)        XCTAssertLessThanOrEqual(refuse.frame.maxY, window.maxY - 34, "Refuse \(refuse.frame) stands above the home bar", file: file, line: line)    }}