Cotton code
137 lines · 7.2 KB · SwiftRaw
import CottonKitimport CottonPaperimport CottonValuesimport XCTest@testable import CottonUI#if canImport(ReadiumNavigator)import ReadiumNavigator#endif/// The reader's faces as they are drawn (`ReaderFace`), and on the phone the/// page Readium is handed (`ReaderLook`) and the tiers the hand opens/// (`ReaderFan`), dealt from the core's fan. Which face is worn, each knob's/// words and choices, and the rows a choice or a pencil writes are the/// core's (`phone::reader`'s tests).final class ReaderFacesTests: XCTestCase {    // MARK: The faces    /// RULE: Each face is a whole from its material: ground, ink, secondary ink, and a hairline of its ink.    func testEachFaceIsItsMaterial() {        XCTAssertEqual(ReaderFace.paper.ground, BookFaceMaterial.paperGround.colour)        XCTAssertEqual(ReaderFace.sepia.ink, BookFaceMaterial.sepiaInk.colour)        XCTAssertEqual(ReaderFace.night.secondary, BookFaceMaterial.nightSecondary.colour)        XCTAssertEqual(ReaderFace.night.hairline, BookFaceMaterial.nightInk.colour.opacity(0.20))        XCTAssertEqual(ReaderFace.paper.hairline, BookFaceMaterial.paperInk.colour.opacity(0.15))        XCTAssertTrue(ReaderFace.night.dark)        XCTAssertFalse(ReaderFace.sepia.dark)    }    #if os(iOS) && canImport(ReadiumNavigator)    // MARK: The page Readium is handed    /// RULE: The EPUB's page takes the face's ground and ink as Readium colours, never Readium's own themes.    @MainActor    func testTheEpubWearsTheFacesGroundAndInk() {        let preferences = ReaderLook.epub(ReaderSettings(), face: .sepia)        XCTAssertEqual(preferences.backgroundColor?.rawValue, 0xE9D9B7)        XCTAssertEqual(preferences.textColor?.rawValue, 0x3A2D1F)        XCTAssertNil(preferences.theme)        XCTAssertEqual(preferences.publisherStyles, true)        XCTAssertEqual(ReaderLook.colour(.paperGround).rawValue, 0xF3EADA)        XCTAssertEqual(ReaderLook.colour(.nightGround).rawValue, 0x1B1A18)        XCTAssertEqual(ReaderLook.colour(.pencilTeal).rawValue, 0x2F7F7A)    }    /// RULE: Serif is Libre Caslon, Sans the system's sans-serif, Dyslexic Readium's OpenDyslexic, Publisher none.    @MainActor    func testTheTypefacesAreTheirFamilies() {        XCTAssertNil(ReaderLook.family(.publisher))        XCTAssertEqual(ReaderLook.family(.serif), ReaderLook.caslon)        XCTAssertEqual(ReaderLook.family(.sans), .sansSerif)        XCTAssertEqual(ReaderLook.family(.dyslexic), .openDyslexic)        var knobs = ReaderSettings()        knobs.knobs = .init(size: 1.2, margins: 1.5, typeface: .serif, layout: "scroll", publisherStyles: false)        let preferences = ReaderLook.epub(knobs, face: .paper)        XCTAssertEqual(preferences.fontFamily, ReaderLook.caslon)        XCTAssertEqual(preferences.fontSize, 1.2)        XCTAssertEqual(preferences.pageMargins, 1.5)        XCTAssertEqual(preferences.publisherStyles, false)        XCTAssertEqual(preferences.scroll, true)        let config = ReaderLook.epubConfiguration(knobs, face: .paper)        XCTAssertEqual(config.fontFamilyDeclarations.map(\.fontFamily), [ReaderLook.caslon], "Caslon is declared; Readium declares OpenDyslexic itself")        XCTAssertEqual(config.editingActions, [.copy], "Copy alone stays in the book's menu")    }    @MainActor    func testThePdfWearsTheFacesGround() {        var settings = ReaderSettings()        settings.knobs.pdfFit = "page"        let preferences = ReaderLook.pdf(settings, face: .night)        XCTAssertEqual(preferences.backgroundColor?.rawValue, 0x1B1A18)        XCTAssertEqual(preferences.fit, .page)    }    // MARK: The tiers    private func fan() throws -> ReaderFanAnswer {        try XCTUnwrap(try JSONDecoder().decode(ReaderAnswer.self, from: Data(BookReaderTests.opened.utf8)).fan)    }    /// RULE: The hand opens one flat tier, the core's: the faces as pages in their own ground, then the knobs each saying what it is set to, resting on the face chosen; no card has a tier behind it.    @MainActor    func testTheHandOpensOneFlatTierOfFacesThenKnobs() throws {        let tier = BookReading.handTier(try fan())        XCTAssertEqual(tier.id, BookReading.handID)        XCTAssertEqual(tier.cards.map(\.label), ["Paper", "Sepia", "Night", "Type size", "Follow the phone's light and dark"])        XCTAssertEqual(tier.raised, "face-sepia")        let night = try XCTUnwrap(tier.cards[2].swatch)        XCTAssertEqual(night, FanSwatch(kind: .page, ground: ReaderFace.night.ground, ink: ReaderFace.night.ink, line: ReaderFace.night.hairline))        XCTAssertNil(tier.cards[3].swatch, "a knob is a word, not a page")        XCTAssertEqual(tier.cards[3].item?.detail, "120%", "a knob says what the core says it is set to")        XCTAssertTrue(tier.cards.allSatisfy { $0.next == nil }, "one tier: nothing stands behind a card")    }    /// RULE: The hand's tier is the only one the hand opens: a face's tap wears it and the tier stays; a knob's tap opens its choices over it, and the hub returns to it.    @MainActor    func testAKnobOpensItsChoicesOverTheHandsTier() throws {        let tiers = FanTiers()        tiers.setRoot(FanTier(id: "reader-root", cards: []))        let fan = try fan()        tiers.push(BookReading.handTier(fan)) { card in            guard let choices = BookReading.choicesTier(fan, knob: String(card.id.dropFirst(BookReading.knobPrefix.count))) else { return }            tiers.push(choices) { _ in }        }        XCTAssertEqual(tiers.depth, 2)        tiers.choose("face-night")        XCTAssertEqual(tiers.open?.id, BookReading.handID, "a face has no tier of its own")        tiers.choose("knob-size")        XCTAssertEqual(tiers.open?.id, BookReading.choicesPrefix + "size")        tiers.pop()        XCTAssertEqual(tiers.open?.id, BookReading.handID)    }    /// RULE: A knob's tier deals the core's choices, resting on the one set.    @MainActor    func testAKnobsTierRestsOnItsValue() throws {        let sizes = try XCTUnwrap(BookReading.choicesTier(try fan(), knob: "size"))        XCTAssertEqual(sizes.cards.map(\.label), ["85%", "100%", "120%"])        XCTAssertEqual(sizes.raised, "choice-2")        XCTAssertNil(BookReading.choicesTier(try fan(), knob: "weight"), "a knob the core deals no choices for has none")    }    /// RULE: A selection offers the core's pencils and Note; a drawn highlight the pencils, Note, and Remove last and red, resting on its own pencil; a pencil is drawn by its line.    @MainActor    func testTheSelectionAndHighlightTiers() throws {        let selection = BookReading.selectionTier(try fan())        XCTAssertEqual(selection.cards.map(\.label), ["Red", "Yellow", "Teal", "Note"])        XCTAssertEqual(selection.raised, "pencil-yellow")        XCTAssertEqual(selection.cards.first?.swatch?.line, HighlightPencil.red.colour, "a pencil is drawn by its line")        XCTAssertNil(selection.cards.last?.swatch)        let highlight = BookReading.highlightTier(try fan(), BookHighlight(id: "h", at: "{}", pencil: .teal))        XCTAssertEqual(highlight.cards.map(\.label), ["Red", "Yellow", "Teal", "Note", "Remove"])        XCTAssertEqual(highlight.raised, "pencil-teal")        XCTAssertEqual(highlight.cards.last?.red, true)        XCTAssertEqual(highlight.cards.filter(\.red).count, 1)    }    #endif}