Cotton code
95 lines · 4.8 KB · SwiftRaw
// The composer's rules apart from its drawing: when send is ready, how tall// the text area stands, what Return does, the waveform's history and the// chip's current option.@testable import CottonComposerimport SwiftUIimport XCTestfinal class ComposerRulesTests: XCTestCase {    func testSendIsReadyWithWordsUnlessTheAppSays() {        XCTAssertFalse(ComposerRules.canSend(text: "", rule: nil))        XCTAssertFalse(ComposerRules.canSend(text: "  \n ", rule: nil))        XCTAssertTrue(ComposerRules.canSend(text: "Ravi enters", rule: nil))        XCTAssertFalse(ComposerRules.canSend(text: "Ravi enters", rule: false))        XCTAssertTrue(ComposerRules.canSend(text: "", rule: true))    }    func testTheAreaGrowsToSevenLinesThenScrolls() {        let line: CGFloat = 20        let inset: CGFloat = 16        // Empty: one line high.        XCTAssertEqual(ComposerRules.height(content: 0, line: line, inset: inset), 36)        // Three lines: as tall as the words.        XCTAssertEqual(ComposerRules.height(content: 76, line: line, inset: inset), 76)        XCTAssertFalse(ComposerRules.scrolls(content: 76, line: line, inset: inset))        // Seven lines exactly: no scrolling yet.        XCTAssertEqual(ComposerRules.height(content: 156, line: line, inset: inset), 156)        XCTAssertFalse(ComposerRules.scrolls(content: 156, line: line, inset: inset))        // Twelve lines: held at seven, and scrolling.        XCTAssertEqual(ComposerRules.height(content: 256, line: line, inset: inset), 156)        XCTAssertTrue(ComposerRules.scrolls(content: 256, line: line, inset: inset))    }    func testAHeldVoiceMessageSlidAwayIsThrownOut() {        // Android's 96 dp, in any direction: a finger that wanders a little still sends.        XCTAssertFalse(ComposerRules.slidAway(.zero))        XCTAssertFalse(ComposerRules.slidAway(CGSize(width: -60, height: -60)))        XCTAssertFalse(ComposerRules.slidAway(CGSize(width: -96, height: 0)))        XCTAssertTrue(ComposerRules.slidAway(CGSize(width: -97, height: 0)))        XCTAssertTrue(ComposerRules.slidAway(CGSize(width: 0, height: -120)))        XCTAssertTrue(ComposerRules.slidAway(CGSize(width: 70, height: 70)))        // A press shorter than the hold is a tap, and dictates.        XCTAssertEqual(ComposerRules.holdToRecord, .milliseconds(300))    }    func testTheLargestTextStopsAtTheCap() {        // Seven lines of the largest text would be 7 × 60; a third of the screen is 280.        XCTAssertEqual(ComposerRules.height(content: 600, line: 60, inset: 16, cap: 280), 280)        // The cap never makes the area shorter than one line.        XCTAssertEqual(ComposerRules.height(content: 0, line: 60, inset: 16, cap: 10), 76)    }    func testReturnSendsOrBreaksAsTheAppSays() {        XCTAssertTrue(ComposerRules.returnSends(appSaysReturnSends: true, shift: false, command: false))        XCTAssertFalse(ComposerRules.returnSends(appSaysReturnSends: false, shift: false, command: false))        // Shift-Return always breaks the line; Command-Return always sends.        XCTAssertFalse(ComposerRules.returnSends(appSaysReturnSends: true, shift: true, command: false))        XCTAssertTrue(ComposerRules.returnSends(appSaysReturnSends: false, shift: false, command: true))    }    func testTheWaveformKeepsTheNewestBars() {        var history: [Double] = []        for level in [0.1, 0.5, 2.0, -1.0] { history = ComposerRules.waveform(history, adding: level, count: 3) }        XCTAssertEqual(history, [0.5, 1.0, 0.0])    }    func testTheChipShowsTheChosenOptionInTheAppsOrder() {        let pick = ComposerPick(            chosen: "meera",            sections: [                .init(id: "kinds", options: [.init(id: "action", name: "Action"), .init(id: "note", name: "Note")]),                .init(id: "people", options: [.init(id: "ravi", name: "Ravi Kumar", tint: .red), .init(id: "meera", name: "Meera Rao", tint: .blue)]),            ],            new: "New character…",            label: "Next line"        )        XCTAssertEqual(pick.current?.name, "Meera Rao")        XCTAssertEqual(pick.sections.flatMap(\.options).map(\.id), ["action", "note", "ravi", "meera"])        var none = pick        none.chosen = "nobody"        XCTAssertNil(none.current)        // The new… row picked: the chip says so.        var naming = pick        naming.chosen = ComposerPick.newID        XCTAssertEqual(naming.current?.name, "New character…")    }    func testOnlyTheLivePhasesDrawTheDictationRow() {        XCTAssertFalse(ComposerDictationPhase.idle.isDictating)        XCTAssertTrue(ComposerDictationPhase.preparing.isDictating)        XCTAssertTrue(ComposerDictationPhase.listening.isDictating)        XCTAssertTrue(ComposerDictationPhase.settling.isDictating)        XCTAssertFalse(ComposerDictationPhase.failed("No microphone").isDictating)    }}