Cotton code
131 lines · 7.3 KB · SwiftRaw
// The keyboard in Story never hides what is being typed: a scene's writing// bar (upright and sideways), New script's title, Where and when's last// line at the largest text, and the speaker chooser's find field all stand// above it, and it goes down again by a drag on the scene or Return.import XCTestfinal class StoryKeyboardTests: StoryUITestCase {    /// The software keyboard, once it has risen.    @MainActor private func keyboard(_ app: XCUIApplication) -> XCUIElement {        let keys = app.keyboards.firstMatch        XCTAssertTrue(keys.waitForExistence(timeout: 5), "the keyboard rose")        // Let it finish rising.        _ = app.buttons["__none__"].waitForExistence(timeout: 0.6)        return keys    }    @MainActor private func above(_ element: XCUIElement, _ keys: XCUIElement, _ what: String) {        XCTAssertTrue(element.exists, "\(what) is there")        XCTAssertLessThanOrEqual(element.frame.maxY, keys.frame.minY + 1, "\(what) stands above the keyboard")        XCTAssertGreaterThan(element.frame.minY, 20, "\(what) stands under the clock, not above the screen")    }    /// A field by the hint it is read by.    @MainActor private func field(_ app: XCUIApplication, _ hint: String) -> XCUIElement {        let fields = app.descendants(matching: .any).matching(NSPredicate(format: "label == %@ AND (elementType == %d OR elementType == %d)",                                                                             hint, XCUIElement.ElementType.textField.rawValue, XCUIElement.ElementType.textView.rawValue))        return fields.firstMatch    }    // RULE: a scene's writing bar stands whole above the keyboard, and a drag down on the scene puts the keyboard away.    @MainActor func testTheWritingBarStandsAboveTheKeyboard() {        XCUIDevice.shared.orientation = .portrait        let app = StoryUI.launch()        StoryUI.row(app, "Platform One").tap()        let write = field(app, "Write a line…")        XCTAssertTrue(write.waitForExistence(timeout: 60), "the scene opened")        write.tap()        let keys = keyboard(app)        above(write, keys, "the line being written")        above(StoryUI.button(app, "Send"), keys, "Send")        above(app.descendants(matching: .any).matching(NSPredicate(format: "label == %@", "Next line")).firstMatch, keys, "who speaks next")        let line = app.descendants(matching: .any).matching(identifier: "ink-line").allElementsBoundByIndex.last { $0.isHittable }        XCTAssertNotNil(line, "a line of the scene shows above the bar")        StoryUI.drag(app, from: CGVector(dx: 0.5, dy: 0.25), by: CGVector(dx: 0, dy: 400))        XCTAssertTrue(keys.waitForNonExistence(timeout: 4), "a drag down on the scene put the keyboard away")    }    // RULE: a tap on a line while writing changes that line, the keyboard staying up for it.    @MainActor func testATapOnALineWhileWritingChangesIt() {        let app = StoryUI.launch()        StoryUI.home(app).tap()        let write = field(app, "Write a line…")        XCTAssertTrue(write.waitForExistence(timeout: 10), "the scene opened")        write.tap()        let keys = keyboard(app)        guard let line = app.descendants(matching: .any).matching(identifier: "ink-line").allElementsBoundByIndex.last(where: { $0.isHittable }) else {            return XCTFail("a line can be touched")        }        line.tap()        let editing = app.descendants(matching: .any).matching(NSPredicate(format: "(elementType == %d OR elementType == %d) AND hasKeyboardFocus == true",                                                                              XCUIElement.ElementType.textField.rawValue, XCUIElement.ElementType.textView.rawValue)).firstMatch        XCTAssertTrue(editing.waitForExistence(timeout: 3), "the line opened for changing, with the keyboard's focus")        XCTAssertNotEqual(editing.label, "Write a line…", "the line, not the writing bar, holds the keyboard")        XCTAssertTrue(keys.exists, "the keyboard stayed up for the line")    }    // RULE: sideways, the writing bar stays whole above the keyboard (Android b2cb567155).    @MainActor func testTheWritingBarStandsAboveTheKeyboardSideways() {        XCUIDevice.shared.orientation = .landscapeLeft        let app = StoryUI.launch()        StoryUI.home(app).tap()        let write = field(app, "Write a line…")        XCTAssertTrue(write.waitForExistence(timeout: 10), "the scene opened")        write.tap()        let keys = keyboard(app)        above(write, keys, "the line being written, sideways")        above(StoryUI.button(app, "Send"), keys, "Send, sideways")    }    // RULE: New script's title and its Start stand above the keyboard; Close puts both away.    @MainActor func testNamingAScriptStandsAboveTheKeyboard() throws {        XCUIDevice.shared.orientation = .portrait        let app = StoryUI.launch()        _ = StoryUI.home(app)        try skipUnderTheShellsCard(app)        StoryUI.button(app, "The Long Platform").tap()        StoryUI.button(app, "New script").tap()        let title = field(app, "What is your story called?")        XCTAssertTrue(title.waitForExistence(timeout: 5), "New script opened")        let keys = keyboard(app)        above(title, keys, "the title being typed")        above(StoryUI.button(app, "Start a story"), keys, "Start a story")        StoryUI.button(app, "Close").tap()        XCTAssertTrue(keys.waitForNonExistence(timeout: 4), "Close put the keyboard away with the panel")    }    // RULE: at the largest text, Where and when's last field comes up whole above the keyboard (Android afa0d55c6a).    @MainActor func testWhereAndWhensLastLineStandsAboveTheKeyboardAtTheLargestText() throws {        XCUIDevice.shared.orientation = .portrait        let app = StoryUI.launch(StoryUI.largestText + ["-story-ink-open", "where"])        try skipUnderTheShellsCard(app)        let summary = field(app, "What happens here? (one line, optional)")        XCTAssertTrue(summary.waitForExistence(timeout: 60), "Where and when opened")        for _ in 0..<16 where summary.frame.maxY > app.windows.firstMatch.frame.height * 0.85 {            StoryUI.drag(app, from: CGVector(dx: 0.5, dy: 0.75), by: CGVector(dx: 0, dy: -250))        }        // Where a finger would touch it, never on the shell's card should it have come.        try skipUnderTheShellsCard(app, wait: 0)        summary.coordinate(withNormalizedOffset: CGVector(dx: 0.3, dy: 0.5)).tap()        let keys = keyboard(app)        above(summary, keys, "What happens here?")    }    // RULE: the speaker chooser's find field and what it finds stand above the keyboard.    @MainActor func testFindingASpeakerStandsAboveTheKeyboard() {        XCUIDevice.shared.orientation = .portrait        let app = StoryUI.launch(["-story-ink-open", "speaker"])        let find = field(app, "Find a character")        XCTAssertTrue(find.waitForExistence(timeout: 60), "the speaker chooser opened")        find.tap()        find.typeText("zara")        let keys = keyboard(app)        above(find, keys, "Find a character")        let zara = StoryUI.button(app, "Zara Roy")        XCTAssertTrue(zara.waitForExistence(timeout: 3))        above(zara, keys, "the character found")        zara.tap()        XCTAssertTrue(app.scrollViews["ink-speaker-rows"].waitForNonExistence(timeout: 3), "choosing closed the chooser")    }}