Cotton code
165 lines · 8.4 KB · SwiftRaw
// CINEMAS ON A REAL LIBRARY'S SHAPES, on Cinemas Dev's invented library (`-cinemas-invent-hard`: 400 more films, a// title of 90 characters, a film named from a bare MKV with no picture), walked by taps: a row opened pages through// every film to the last; the MKV and the long title open; Send to a friend sends, and its Undo takes it back; a film// moved to the Bin waits in You › Bin and Put back brings it home; an empty library says so.import XCTestfinal class CinemasHardTests: XCTestCase {    override func setUp() {        continueAfterFailure = false    }    @MainActor private func element(_ app: XCUIApplication, _ id: String) -> XCUIElement {        app.descendants(matching: .any)[id].firstMatch    }    @MainActor private func launch(_ arguments: [String]) -> XCUIApplication {        let app = XCUIApplication()        app.launchArguments += arguments        app.launch()        XCTAssertTrue(element(app, "cinemas-tab-home").waitForExistence(timeout: 30))        return app    }    @MainActor private func shot(_ app: XCUIApplication, _ name: String) {        let attachment = XCTAttachment(screenshot: app.screenshot())        attachment.name = name        attachment.lifetime = .keepAlways        add(attachment)    }    /// Films opened holds 400 and more: it pages 60 at a time as the finger nears the end, down to the last film.    @MainActor func testARowOfFourHundredPagesToTheEnd() throws {        let app = launch(["-cinemas-invent", "-cinemas-invent-hard", "-cinemas-open", "row:films"])        XCTAssertTrue(element(app, "cinemas-poster-films-f-mkv").waitForExistence(timeout: 10), "the MKV with no picture stands first, drawn")        let last = app.descendants(matching: .any).matching(NSPredicate(format: "identifier ENDSWITH %@", "-f-gen-399")).firstMatch        var swipes = 0        while !last.exists && swipes < 120 {            app.swipeUp(velocity: .fast)            swipes += 1        }        XCTAssertTrue(last.exists, "the 400th film is reached after \(swipes) swipes")        shot(app, "Films: the last of 400")    }    /// Films opened has its count and chips (frame 2): A–Z reorders under the same count; On this phone narrows it.    @MainActor func testARowsChipsAndCount() throws {        let app = launch(["-cinemas-invent", "-cinemas-invent-hard", "-cinemas-open", "row:films"])        let count = element(app, "cinemas-row-count")        XCTAssertTrue(count.waitForExistence(timeout: 10))        let all = count.label        element(app, "cinemas-row-chip-az").tap()        XCTAssertTrue(element(app, "cinemas-row-chip-az").waitForSelected(timeout: 6), "A–Z is on")        XCTAssertEqual(element(app, "cinemas-row-count").label, all, "A–Z keeps every title")        element(app, "cinemas-row-chip-on_phone").tap()        let narrowed = NSPredicate(format: "label != %@", all)        expectation(for: narrowed, evaluatedWith: element(app, "cinemas-row-count"))        waitForExpectations(timeout: 6)        shot(app, "Films: On this phone")    }    /// The MKV named from its file and the 90-character title each open their page and play.    @MainActor func testABareFileAndALongTitleOpen() throws {        var app = launch(["-cinemas-invent", "-cinemas-invent-hard", "-cinemas-open", "title:f-mkv"])        XCTAssertTrue(element(app, "cinemas-title-play").waitForExistence(timeout: 10))        shot(app, "A bare MKV")        app.terminate()        app = launch(["-cinemas-invent", "-cinemas-invent-hard", "-cinemas-open", "title:f-long"])        XCTAssertTrue(element(app, "cinemas-title-play").waitForExistence(timeout: 10))        XCTAssertTrue(element(app, "cinemas-title-play").isHittable, "Play stays in reach under a long title")        shot(app, "A 90-character title")    }    /// Send to a friend: the act opens his people; one not yet added back cannot be sent to; a tap sends, the    /// receipt says so, and Undo takes it back.    @MainActor func testSendToAFriendAndTakeItBack() throws {        let app = launch(["-cinemas-invent", "-cinemas-open", "title:f-ferry"])        let send = element(app, "cinemas-act-send")        XCTAssertTrue(send.waitForExistence(timeout: 10))        send.tap()        XCTAssertTrue(element(app, "cinemas-send").waitForExistence(timeout: 6))        XCTAssertFalse(element(app, "cinemas-send-+15550100002").isEnabled, "someone not yet added back cannot be sent to")        shot(app, "Send: his people")        element(app, "cinemas-send-+15550100001").tap()        let toast = element(app, "cinemas-toast")        XCTAssertTrue(toast.waitForExistence(timeout: 6))        XCTAssertTrue(app.staticTexts["Sent Night Ferry to Arjun"].exists)        element(app, "cinemas-toast-undo").tap()        XCTAssertTrue(app.staticTexts["Taken back"].waitForExistence(timeout: 6))    }    /// Move to the Bin closes the page; the film waits in You › Bin; Put back brings it home.    @MainActor func testTheBinAndPutBack() throws {        let app = launch(["-cinemas-invent", "-cinemas-open", "title:f-salt"])        let more = element(app, "cinemas-act-more")        XCTAssertTrue(more.waitForExistence(timeout: 10))        more.tap()        let bin = element(app, "cinemas-act-bin")        XCTAssertTrue(bin.waitForExistence(timeout: 4))        bin.tap()        XCTAssertTrue(element(app, "cinemas-featured").waitForExistence(timeout: 6), "the page of a binned film closes")        element(app, "cinemas-tab-you").tap()        let row = element(app, "cinemas-you-bin")        XCTAssertTrue(row.waitForExistence(timeout: 6))        // The Bin is in You's last section, under the fold: scrolled up clear of the tab bar first.        let bar = element(app, "cinemas-tab-home").frame.minY - 20        var swipes = 0        while (!row.isHittable || row.frame.maxY > bar) && swipes < 6 {            app.swipeUp(velocity: .slow)            swipes += 1        }        row.tap()        XCTAssertTrue(element(app, "cinemas-bin-f-salt").waitForExistence(timeout: 6))        shot(app, "The Bin")        element(app, "cinemas-bin-put-back-f-salt").tap()        XCTAssertTrue(app.staticTexts["Back in Cinemas"].waitForExistence(timeout: 6))        XCTAssertTrue(element(app, "cinemas-bin-f-salt").waitForNonExistence(timeout: 6))    }    /// A series the importer made before any episode came says so; a web video's page opens with Play.    @MainActor func testAnEmptySeriesAndAVideo() throws {        var app = launch(["-cinemas-invent", "-cinemas-invent-hard", "-cinemas-open", "title:s-empty"])        XCTAssertTrue(app.staticTexts["No episodes in this season yet."].waitForExistence(timeout: 10))        app.terminate()        app = launch(["-cinemas-invent", "-cinemas-open", "title:c-bread"])        XCTAssertTrue(element(app, "cinemas-title-play").waitForExistence(timeout: 10))        shot(app, "A web video, 16:9")    }    /// You › Add films offers files and a whole folder; Add a folder opens Files' picker, which Cancel closes.    @MainActor func testAddAFolderOpensFiles() throws {        let app = launch(["-cinemas-invent", "-cinemas-open", "you"])        let row = element(app, "cinemas-you-add")        XCTAssertTrue(row.waitForExistence(timeout: 8))        row.tap()        let folder = element(app, "cinemas-add-folder")        XCTAssertTrue(folder.waitForExistence(timeout: 6))        folder.tap()        let cancel = app.buttons["Cancel"].firstMatch        XCTAssertTrue(cancel.waitForExistence(timeout: 10), "Files' picker is up")        shot(app, "Add a folder: Files")        cancel.tap()        XCTAssertTrue(folder.waitForExistence(timeout: 6))    }    /// A library with nothing in it says where films come from and offers Add films.    @MainActor func testAnEmptyLibrary() throws {        let app = launch(["-cinemas-invent-empty"])        XCTAssertTrue(app.buttons.matching(NSPredicate(format: "label CONTAINS %@", "Add films")).firstMatch.waitForExistence(timeout: 8))        XCTAssertTrue(element(app, "cinemas-add-folder").exists, "an empty library offers a folder too")        shot(app, "Empty")    }}extension XCUIElement {    /// Waits until the element is selected (a chip turned on).    @MainActor func waitForSelected(timeout: TimeInterval) -> Bool {        let deadline = Date().addingTimeInterval(timeout)        while Date() < deadline {            if exists && isSelected { return true }            Thread.sleep(forTimeInterval: 0.2)        }        return false    }}