Cotton code
111 lines · 5.8 KB · SwiftRaw
// Comms on iPhone while typing (owner, Comms build 19, 22 Nov 2026: the// keyboard and the writing box covered the last few messages). In a chat of// many invented messages (-chat-warm-seed YES, -chat-warm-long), the newest// (a picture with its caption) stands whole above the writing box, nothing is// drawn behind the box, when the keyboard rises, as the// box grows to five lines, and when a message lands while typing; at the// default text size and the largest. Shots fb-keyboard-*.import XCTestfinal class ChatKeyboardTests: XCTestCase {    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", "-chat-warm-long", "300", "-chat-warm-open", "chat-warm-long"] + extra        app.launch()        return app    }    private func keep(_ name: String) {        let shot = XCTAttachment(screenshot: XCUIScreen.main.screenshot())        shot.name = name        shot.lifetime = .keepAlways        add(shot)    }    /// Waits until `element`'s frame has stood still for two seconds (the keyboard's rise, the page's move after it).    @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 newest message ends above the writing box and lies whole on the screen.    @MainActor private func assertNewestSeen(_ id: String, _ app: XCUIApplication, _ when: String) {        let bubble = app.descendants(matching: .any)["chat-bubble-\(id)"].firstMatch        let box = app.descendants(matching: .any)["chat-composer"].firstMatch        XCTAssertTrue(bubble.waitForExistence(timeout: 10), "\(id) is drawn \(when)")        settled(bubble)        let screen = app.windows.firstMatch.frame        let b = bubble.frame, c = box.frame        XCTAssertLessThanOrEqual(b.maxY, c.minY, "\(when): the newest message (\(b)) ends above the writing box (\(c))")        XCTAssertLessThanOrEqual(b.maxY, screen.maxY, "\(when): the newest message's foot is on the screen (\(b))")        // Whole on the screen when it fits between the header and the box; taller than that room (a picture at the        // largest text over five lines and the keyboard), its foot, the newest words, stands seen under the header.        let head = app.buttons["chat-person"].frame.maxY        if b.height <= c.minY - head {            XCTAssertGreaterThanOrEqual(b.minY, screen.minY, "\(when): the newest message's top is on the screen (\(b))")        } else {            XCTAssertGreaterThan(b.maxY, head + 20, "\(when): taller than the room, the newest message's foot is seen under the header (\(b))")        }        XCTAssertGreaterThan(b.height, 1, "\(when): the newest message is drawn")        let keyboard = app.keyboards.firstMatch        if keyboard.exists { XCTAssertLessThanOrEqual(c.maxY, keyboard.frame.minY + 1, "\(when): the writing box stands above the keyboard") }        // Nothing of the conversation is drawn behind the writing box (owner: a picture tile showed through under it).        let bubbles = app.descendants(matching: .any).matching(NSPredicate(format: "identifier BEGINSWITH 'chat-bubble-'")).allElementsBoundByIndex        XCTAssertFalse(bubbles.isEmpty, "\(when): bubbles are drawn")        let under = bubbles.filter { let o = $0.frame.intersection(c); return !o.isNull && o.height > 0.5 && o.width > 0.5 }.map { "\($0.identifier) \($0.frame)" }        XCTAssertEqual(under, [], "\(when): bubbles behind the writing box (\(c))")    }    @MainActor private func walk(_ extra: [String], _ shots: String) {        let app = launched(extra)        XCTAssertTrue(app.tables["chat-thread"].waitForExistence(timeout: 30), "the long chat opened")        // 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-keyboard-\(shots)-rest")        assertNewestSeen("long-299", app, "at rest")        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-keyboard-\(shots)-up")        assertNewestSeen("long-299", app, "the keyboard up")        field.typeText("One\nTwo\nThree\nFour\nFive")        Thread.sleep(forTimeInterval: 1.5)        keep("fb-keyboard-\(shots)-five-lines")        assertNewestSeen("long-299", app, "five lines written")        // Sent while typing: it lands at the foot, above the writing box, the keyboard still up.        app.buttons["chat-composer-send"].tap()        Thread.sleep(forTimeInterval: 1.5)        keep("fb-keyboard-\(shots)-landed")        assertNewestSeen("seed-sent-1", app, "a message landed while typing")        XCTAssertTrue(app.keyboards.firstMatch.exists, "the keyboard stays up after Send")    }    // RULE (owner, Comms build 19): with the keyboard up the newest message sits just above the writing box, and stays    // there as the box grows and as a message lands.    @MainActor func testTheNewestMessageStandsAboveTheWritingBox() {        walk([], "default")    }    // RULE: the same at the largest text.    @MainActor func testTheNewestMessageStandsAboveTheWritingBoxAtTheLargestText() {        walk(Self.largestText, "largest")    }}