Cotton code
119 lines · 6.7 KB · SwiftRaw
// A message held: its menu always whole on screen (owner, FEEDBACK-4 B1,// Comms 26: "the menu (Reply, Forward, Pin, Star, ...) is cut off by the// message box below"). Over Comms Dev's invented chats (-chat-warm-seed YES):// the newest message, just above the writing box, and one at the top, each// held, and again with the keyboard up; every act of the menu stands inside// the screen, above the writing box (never under it), and answers a tap. At the default size and the// largest.import XCTestfinal class ChatHoldMenuTests: 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 opened(_ extra: [String]) -> XCUIApplication {        let app = XCUIApplication()        app.launchArguments += ["-chat-warm-seed", "YES", "-chat-warm-open", asha] + extra        app.launch()        XCTAssertTrue(app.tables["chat-thread"].waitForExistence(timeout: 30), "Asha's chat opened")        _ = ShellCard.refuse(app).waitForExistence(timeout: 6)        return app    }    /// The message bubbles standing on screen, top first.    @MainActor private func shown(_ app: XCUIApplication) -> [XCUIElement] {        let screen = app.windows.firstMatch.frame        return app.descendants(matching: .any)            .matching(NSPredicate(format: "identifier BEGINSWITH 'chat-bubble-m-'"))            .allElementsBoundByIndex            // A message still on its way holds its state line ("Not sent · Try again", a button of its own): left out.            .filter { $0.exists && $0.isHittable && $0.frame.minY > screen.minY + 120 && $0.frame.maxY < screen.maxY - 60 }            .filter { !$0.label.contains("Not sent") && !$0.label.contains("Waiting to send") && !$0.label.contains("Sending") }            .sorted { $0.frame.minY < $1.frame.minY }    }    /// Held, every act of its menu stands whole on screen and answers a tap.    @MainActor private func whole(_ bubble: XCUIElement, _ app: XCUIApplication, _ shot: String) {        // The menu stands whole above the writing box: never under it (cut off) nor over it.        let composer = app.textViews["chat-composer-text"]        let foot = composer.exists ? composer.frame.minY : app.windows.firstMatch.frame.maxY        bubble.press(forDuration: 1.0)        let acts = app.buttons.matching(NSPredicate(format: "identifier BEGINSWITH 'chat-act-'"))        XCTAssertTrue(app.buttons["chat-act-copy"].waitForExistence(timeout: 5), "the menu opens, each act under its own name")        Thread.sleep(forTimeInterval: 0.6)        keep("hold-menu-\(shot)")        let screen = app.windows.firstMatch.frame        let scroller = app.scrollViews["chat-menu-scroll"]        let scrolls = scroller.exists        // A menu too tall for the room scrolls inside its own frame, which stands whole above the writing box; an act        // part-way out of that frame is checked once scrolled to.        if scrolls {            XCTAssertLessThanOrEqual(scroller.frame.maxY, foot + 0.5, "the menu's frame above the writing box (\(foot)): \(scroller.frame)")            XCTAssertGreaterThanOrEqual(scroller.frame.minY, screen.minY)        }        for act in acts.allElementsBoundByIndex where act.exists {            if scrolls, !act.isHittable || act.frame.maxY > scroller.frame.maxY + 0.5 || act.frame.minY < scroller.frame.minY - 0.5 { continue }            let f = act.frame            XCTAssertTrue(act.isHittable, "\(act.identifier) answers a tap")            XCTAssertGreaterThanOrEqual(f.minY, screen.minY, "\(act.identifier) inside the screen's top: \(f)")            XCTAssertLessThanOrEqual(f.maxY, foot + 0.5, "\(act.identifier) above the writing box (\(foot)): \(f)")        }        if scrolls {            // A menu too tall for the room scrolls to its last act, which then stands whole too.            let last = acts.allElementsBoundByIndex.last!            scroller.swipeUp()            Thread.sleep(forTimeInterval: 0.5)            XCTAssertTrue(last.isHittable, "the last act is reached")            XCTAssertLessThanOrEqual(last.frame.maxY, scroller.frame.maxY + 0.5, "the last act stands whole in the menu: \(last.frame)")        }        // Closed by a tap on the dimmed thread, in the room the menu leaves free (above it, else below it).        let window = app.windows.firstMatch.frame        let shownActs = acts.allElementsBoundByIndex.filter { $0.exists && $0.isHittable }        let menuTop = shownActs.map(\.frame.minY).min() ?? window.midY        let menuFoot = shownActs.map(\.frame.maxY).max() ?? window.midY        let react = app.descendants(matching: .any)["chat-react-row"].firstMatch        let blockTop = min(menuTop - bubble.frame.height, react.exists ? react.frame.minY : .infinity)        let y = blockTop > window.minY + 160 ? window.minY + 130 : min(foot - 12, menuFoot + 24)        app.windows.firstMatch.coordinate(withNormalizedOffset: .zero).withOffset(CGVector(dx: 30, dy: y - window.minY)).tap()        let closed = XCTNSPredicateExpectation(predicate: NSPredicate(format: "exists == false"), object: app.buttons["chat-act-copy"])        XCTAssertEqual(XCTWaiter.wait(for: [closed], timeout: 5), .completed, "the menu closes")    }    @MainActor private func holds(_ extra: [String], _ shots: String) {        let app = opened(extra)        let bubbles = shown(app)        XCTAssertFalse(bubbles.isEmpty, "messages on screen")        whole(bubbles.last!, app, "\(shots)-newest")        Thread.sleep(forTimeInterval: 0.5)        // The one standing highest, when another stands above the newest (at the largest text one may fill the room).        let again = shown(app)        if again.count > 1 { whole(again.first!, app, "\(shots)-top") }        // With the keyboard up the room is shortest: the newest message held again.        app.textViews["chat-composer-text"].tap()        XCTAssertTrue(app.keyboards.firstMatch.waitForExistence(timeout: 5), "the keyboard is up")        Thread.sleep(forTimeInterval: 0.6)        if let newest = shown(app).last { whole(newest, app, "\(shots)-keyboard") }    }    // RULE: a held message's menu stands whole on screen, above the writing box, wherever the message stands.    @MainActor func testAHeldMessagesMenuIsAlwaysWholeOnScreen() {        holds([], "default")    }    @MainActor func testAHeldMessagesMenuIsAlwaysWholeOnScreenAtTheLargestText() {        holds(Self.largestText, "largest")    }}