Cotton code
97 lines · 5.0 KB · SwiftRaw
// Comms on iPhone, a chat with someone not on Cotton yet (owner, Comms build// 19, 22 Nov 2026: at the largest text its banner could push the writing box// under the keyboard). Over invented chats (-chat-warm-seed YES), Ravi's chat// is the one whose person is not on Cotton: with the keyboard up, and with// five lines written, the writing box stands whole above the keyboard and on// the screen, and the banner stays, under the header; at the default text// size and the largest. Shots fb-banner-*.import XCTestfinal class ChatReachBannerTests: XCTestCase {    private let ravi = "11111111-1111-4111-8111-0000000000d1"    private static let largestText = ["-UIPreferredContentSizeCategoryName", "UICTContentSizeCategoryAccessibilityXXXL"]    override func setUp() {        continueAfterFailure = false        XCUIDevice.shared.orientation = .portrait    }    private func keep(_ name: String) {        let screen = XCUIScreen.main.screenshot()        let shot = XCTAttachment(screenshot: screen)        shot.name = name        shot.lifetime = .keepAlways        add(shot)        keepShot(screen, name)    }    /// Waits until `element`'s frame has stood still for two seconds (the keyboard's rise, the box's growth).    @MainActor private func settled(_ element: XCUIElement) {        var last = element.frame, still = 0        for _ in 0..<40 where still < 5 {            Thread.sleep(forTimeInterval: 0.4)            let now = element.frame            still = now == last ? still + 1 : 0            last = now        }    }    /// The writing box lies whole on the screen, above the keyboard and below the banner.    @MainActor private func assertBoxAboveKeyboard(_ app: XCUIApplication, _ when: String) {        let box = app.descendants(matching: .any)["chat-composer"].firstMatch        let banner = app.descendants(matching: .any)["chat-reach"].firstMatch        let keyboard = app.keyboards.firstMatch        XCTAssertTrue(box.waitForExistence(timeout: 5), "\(when): the writing box is drawn")        XCTAssertTrue(keyboard.exists, "\(when): the keyboard is up")        settled(box)        let screen = app.windows.firstMatch.frame        let c = box.frame, k = keyboard.frame        XCTAssertGreaterThan(c.height, 1, "\(when): the writing box is drawn (\(c))")        XCTAssertLessThanOrEqual(c.maxY, k.minY + 1, "\(when): the writing box (\(c)) stands above the keyboard (\(k))")        XCTAssertGreaterThanOrEqual(c.minY, screen.minY, "\(when): the writing box's top is on the screen (\(c))")        XCTAssertTrue(banner.exists, "\(when): the banner stays")        XCTAssertLessThanOrEqual(banner.frame.maxY, c.minY, "\(when): the banner (\(banner.frame)) ends above the writing box (\(c))")        XCTAssertTrue(app.buttons["chat-reach-invite"].isHittable, "\(when): Invite can be tapped")    }    @MainActor private func walk(_ extra: [String], _ shots: String) {        let app = XCUIApplication()        app.launchArguments += ["-chat-warm-seed", "YES", "-chat-warm-open", ravi] + extra        app.launch()        let banner = app.descendants(matching: .any)["chat-reach"].firstMatch        XCTAssertTrue(banner.waitForExistence(timeout: 30), "Ravi's chat says he is not on Cotton yet")        // The card of a device asking to be let in can come down a few seconds in and move things: wait for it.        _ = ShellCard.refuse(app).waitForExistence(timeout: 6)        keep("fb-banner-\(shots)-rest")        // At rest the banner stands no taller than a third of the page, its Invite in sight, and the box on the screen.        let screen = app.windows.firstMatch.frame        XCTAssertLessThanOrEqual(banner.frame.height, screen.height / 3 + 1, "at rest: the banner (\(banner.frame)) is no taller than a third of the page")        XCTAssertTrue(app.buttons["chat-reach-invite"].isHittable, "at rest: Invite can be tapped")        let box = app.descendants(matching: .any)["chat-composer"].firstMatch        XCTAssertLessThanOrEqual(box.frame.maxY, screen.maxY, "at rest: the writing box (\(box.frame)) is on the screen")        let field = app.textViews["chat-composer-text"]        XCTAssertTrue(field.waitForExistence(timeout: 5))        field.tap()        XCTAssertTrue(app.keyboards.firstMatch.waitForExistence(timeout: 10), "the keyboard rose")        Thread.sleep(forTimeInterval: 1.5)        keep("fb-banner-\(shots)-up")        assertBoxAboveKeyboard(app, "the keyboard up")        field.typeText("One\nTwo\nThree\nFour\nFive")        Thread.sleep(forTimeInterval: 1.5)        keep("fb-banner-\(shots)-five-lines")        assertBoxAboveKeyboard(app, "five lines written")    }    // RULE (owner, Comms build 19): in a chat with someone not on Cotton yet, the writing box stands above the keyboard.    @MainActor func testTheWritingBoxStandsAboveTheKeyboardUnderTheBanner() {        walk([], "default")    }    // RULE: the same at the largest text, where the banner's words are tallest.    @MainActor func testTheWritingBoxStandsAboveTheKeyboardUnderTheBannerAtTheLargestText() {        walk(Self.largestText, "largest")    }}