Cotton code
339 lines · 17.0 KB · SwiftRaw
// HIDDEN ON THE CORE (CONTRACT-2 HIDDEN, the coordinator 26 Nov: built fully behind the off switch, walked on dev with// it on locally). Cinemas Dev against the dev server's invented +1555 account, with `-cinemas-hidden-on` (the local// switch, no longer needed: every build shows Hidden) and `-cinemas-skip-lock` (Face ID passing, on a simulator). By taps:// the invented film goes into a category "Edits"; Hide it from Home's menu, the Hide sheet; Undo on Home (Hidden shut:// the one-use token the core handed back) brings it back; Hide again, gone from Home; You › Hidden asks the core's// unlock and shows it, a search inside finds the film, it plays from inside (HIDE SCREEN iPhone 2), and search and// Carry on outside never show it; leaving the app shuts Hidden (cinemas.lock); in again, Show brings it back to Home.import XCTestfinal class CinemasHiddenDevTests: XCTestCase {    private let category = "Edits"    private let film = "Quiet Signal"    override func setUp() {        continueAfterFailure = false    }    @MainActor private func element(_ app: XCUIApplication, _ id: String) -> XCUIElement {        app.descendants(matching: .any)[id].firstMatch    }    @MainActor private func matching(_ app: XCUIApplication, prefix: String, label: String) -> XCUIElement {        app.descendants(matching: .any).matching(NSPredicate(format: "identifier BEGINSWITH %@ AND label CONTAINS[c] %@", prefix, label)).firstMatch    }    @MainActor private func shot(_ app: XCUIApplication, _ name: String) {        let attachment = XCTAttachment(screenshot: app.screenshot())        attachment.name = name        attachment.lifetime = .keepAlways        add(attachment)    }    /// Scrolls until `target` stands clear above the tab bar.    @MainActor private func reach(_ app: XCUIApplication, _ target: XCUIElement) {        let bar = element(app, "cinemas-tab-home").frame.minY - 20        var swipes = 0        while (!target.exists || !target.isHittable || target.frame.maxY > bar) && swipes < 10 {            app.swipeUp(velocity: .slow)            swipes += 1        }    }    @MainActor private func top(_ app: XCUIApplication) {        for _ in 0..<4 { app.swipeDown(velocity: .fast) }    }    /// Back one screen.    @MainActor private func back(_ app: XCUIApplication) {        app.navigationBars.buttons.element(boundBy: 0).tap()    }    /// The hidden search inside Hidden: `q` typed into it, or emptied.    @MainActor private func searchInside(_ app: XCUIApplication, _ q: String) {        let search = element(app, "cinemas-hidden-search")        XCTAssertTrue(search.waitForExistence(timeout: 8))        search.tap()        if let typed = search.value as? String, !typed.isEmpty, typed != search.placeholderValue {            search.typeText(String(repeating: XCUIKeyboardKey.delete.rawValue, count: typed.count))        }        if !q.isEmpty { search.typeText(q) }    }    /// From a title's page inside Hidden: Move to the Bin.    @MainActor private func binFromItsPage(_ app: XCUIApplication) {        XCTAssertTrue(element(app, "cinemas-title-play").waitForExistence(timeout: 10), "its page inside Hidden")        let more = element(app, "cinemas-act-more")        if more.exists { more.tap() }        let bin = element(app, "cinemas-act-bin")        XCTAssertTrue(bin.waitForExistence(timeout: 5), "Move to the Bin on its page")        bin.tap()    }    @MainActor func testHiddenOnTheCore() throws {        let app = XCUIApplication()        app.launchArguments += ["-cinemas-hidden-on", "-cinemas-skip-lock"]        app.launch()        XCTAssertTrue(element(app, "cinemas-tab-home").waitForExistence(timeout: 40))        // The film in "Edits" (made the first time). A walk stopped half way left Edits hidden: put back first.        let poster = matching(app, prefix: "cinemas-poster-", label: film)        if !poster.waitForExistence(timeout: 20) {            element(app, "cinemas-tab-you").tap()            let row = element(app, "cinemas-you-hidden")            reach(app, row)            row.tap()            let menuInside = matching(app, prefix: "cinemas-row-menu-group:", label: category)            XCTAssertTrue(menuInside.waitForExistence(timeout: 10), "Edits, hidden by a walk stopped half way")            menuInside.tap()            app.buttons["Show this category"].firstMatch.tap()            XCTAssertTrue(app.staticTexts["\(category) is back on Home"].waitForExistence(timeout: 10))            element(app, "cinemas-tab-home").tap()        }        XCTAssertTrue(poster.waitForExistence(timeout: 30))        poster.tap()        let add = element(app, "cinemas-act-group")        XCTAssertTrue(add.waitForExistence(timeout: 10))        add.tap()        XCTAssertTrue(element(app, "cinemas-item-sheet").waitForExistence(timeout: 6))        let tick = matching(app, prefix: "cinemas-sheet-group-", label: category)        if tick.waitForExistence(timeout: 3) {            if !tick.isSelected { tick.tap() }        } else {            element(app, "cinemas-sheet-new-group").tap()            let name = element(app, "cinemas-sheet-name")            XCTAssertTrue(name.waitForExistence(timeout: 4))            name.typeText(category)            app.buttons["Make"].firstMatch.tap()            XCTAssertTrue(tick.waitForExistence(timeout: 8), "the new category, ticked")        }        element(app, "cinemas-sheet-done").tap()        element(app, "cinemas-tab-home").tap()        element(app, "cinemas-tab-home").tap()        top(app)        // A second film into Edits, for Delete forever later (each walk spends one invented film of the dev account).        let other = app.descendants(matching: .any).matching(NSPredicate(format: "identifier BEGINSWITH 'cinemas-poster-' AND NOT (label CONTAINS[c] %@)", film)).firstMatch        var spare: String?        if other.waitForExistence(timeout: 10) {            let name = other.label.components(separatedBy: ",").first?.trimmingCharacters(in: .whitespaces) ?? other.label            spare = name            other.tap()            XCTAssertTrue(add.waitForExistence(timeout: 10))            add.tap()            XCTAssertTrue(tick.waitForExistence(timeout: 6))            if !tick.isSelected { tick.tap() }            element(app, "cinemas-sheet-done").tap()            element(app, "cinemas-tab-home").tap()            element(app, "cinemas-tab-home").tap()            top(app)        }        // Playing in picture-in-picture while its category is hidden: the little window closes (the player's note in        // play-clock.log, read after the walk) and now-playing turns to "Cinemas".        let shownPoster = matching(app, prefix: "cinemas-poster-", label: film)        reach(app, shownPoster)        shownPoster.tap()        let playShown = element(app, "cinemas-title-play")        XCTAssertTrue(playShown.waitForExistence(timeout: 10))        playShown.tap()        let player = element(app, "film-player")        XCTAssertTrue(player.waitForExistence(timeout: 30), "it plays from Home")        Thread.sleep(forTimeInterval: 6)        // The player's own controls come and go: a tap in the middle shows them.        let pip = app.buttons.matching(NSPredicate(format: "label CONTAINS[c] 'picture' OR identifier CONTAINS[c] 'picture'")).firstMatch        for _ in 0..<4 where !pip.exists {            player.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.45)).tap()            _ = pip.waitForExistence(timeout: 2)        }        var floated = false        if pip.exists {            pip.tap()            floated = element(app, "cinemas-tab-home").waitForExistence(timeout: 8)            Thread.sleep(forTimeInterval: 2)            shot(app, "Dev 0 floating in picture-in-picture")        }        if !floated {            XCTContext.runActivity(named: "picture-in-picture did not start on this simulator") { _ in }            shot(app, "Dev 0 the player, no picture-in-picture button found")            let close = app.buttons.matching(NSPredicate(format: "label == 'Close' OR label == 'Done'")).firstMatch            if close.exists { close.tap() } else { player.swipeDown(velocity: .fast) }            XCTAssertTrue(element(app, "cinemas-tab-home").waitForExistence(timeout: 8), "the player closed")        }        element(app, "cinemas-tab-home").tap()        element(app, "cinemas-tab-home").tap()        top(app)        // Hide it from Home's menu.        let menu = matching(app, prefix: "cinemas-row-menu-group:", label: category)        reach(app, menu)        XCTAssertTrue(menu.exists, "its menu on Home")        menu.tap()        app.buttons["Hide this category"].firstMatch.tap()        XCTAssertTrue(element(app, "cinemas-hide-sheet").waitForExistence(timeout: 6))        shot(app, "Dev 1 the Hide sheet")        element(app, "cinemas-hide-confirm").tap()        XCTAssertTrue(app.staticTexts["\(category) is hidden"].waitForExistence(timeout: 10), "the core's words")        if floated {            Thread.sleep(forTimeInterval: 1.5)            shot(app, "Dev 1b a second after hiding: the little window gone")        }        // Undo on Home, Hidden shut: the one-use token the core handed back.        let undo = element(app, "cinemas-toast-undo")        XCTAssertTrue(undo.waitForExistence(timeout: 4))        undo.tap()        XCTAssertTrue(app.staticTexts["\(category) is back on Home"].waitForExistence(timeout: 10), "Undo ran with Hidden shut")        shot(app, "Dev 2 Undo on Home")        // Hide again: gone from Home, the film with it.        top(app)        reach(app, menu)        menu.tap()        app.buttons["Hide this category"].firstMatch.tap()        element(app, "cinemas-hide-confirm").tap()        XCTAssertTrue(app.staticTexts["\(category) is hidden"].waitForExistence(timeout: 10))        XCTAssertTrue(menu.waitForNonExistence(timeout: 8), "gone from Home")        top(app)        XCTAssertFalse(matching(app, prefix: "cinemas-poster-", label: film).exists, "the film too")        shot(app, "Dev 3 Home without it")        // Search outside Hidden finds nothing of it.        element(app, "cinemas-tab-search").tap()        let field = element(app, "cinemas-search-field")        XCTAssertTrue(field.waitForExistence(timeout: 8))        field.tap()        field.typeText("quiet")        Thread.sleep(forTimeInterval: 3)        XCTAssertFalse(matching(app, prefix: "cinemas-search-", label: film).exists, "search outside Hidden finds nothing of it")        shot(app, "Dev 3b search without it")        element(app, "cinemas-tab-home").tap()        // You › Hidden: the core's unlock, then the category inside.        element(app, "cinemas-tab-you").tap()        let row = element(app, "cinemas-you-hidden")        reach(app, row)        row.tap()        XCTAssertTrue(element(app, "cinemas-hidden").waitForExistence(timeout: 8))        let inside = matching(app, prefix: "cinemas-row-head-group:", label: category)        XCTAssertTrue(inside.waitForExistence(timeout: 10), "the hidden category inside")        shot(app, "Dev 4 inside Hidden")        let search = element(app, "cinemas-hidden-search")        search.tap()        search.typeText("quiet")        let found = matching(app, prefix: "cinemas-hidden-found-", label: film)        XCTAssertTrue(found.waitForExistence(timeout: 8), "search inside finds it")        shot(app, "Dev 5 search inside")        found.tap()        let play = element(app, "cinemas-title-play")        XCTAssertTrue(play.waitForExistence(timeout: 10), "its page opens inside Hidden")        // It plays from inside Hidden (HIDE SCREEN iPhone 2: the play says `hidden: true`).        play.tap()        XCTAssertTrue(element(app, "film-player").waitForExistence(timeout: 30), "it plays from inside Hidden")        XCTAssertFalse(app.staticTexts.matching(NSPredicate(format: "label CONTAINS[c] %@", "no such record")).firstMatch.exists, "never \"no such record\"")        Thread.sleep(forTimeInterval: 12)        shot(app, "Dev 6 playing from inside Hidden")        // Leaving the app shuts Hidden (cinemas.lock), and the lock screen no longer names the film.        XCUIDevice.shared.press(.home)        Thread.sleep(forTimeInterval: 3)        app.activate()        let close = app.buttons["Close"].firstMatch        if close.waitForExistence(timeout: 4) { close.tap() }        XCTAssertTrue(element(app, "cinemas-hidden").waitForNonExistence(timeout: 8), "shut on leaving the app")        // Carry on outside Hidden never shows it, though it was just played.        element(app, "cinemas-tab-home").tap()        top(app)        Thread.sleep(forTimeInterval: 3)        XCTAssertFalse(matching(app, prefix: "cinemas-poster-", label: film).exists, "Carry on outside Hidden leaves it out")        shot(app, "Dev 6b Home after playing it")        // The outside Bin's count before a hidden film goes to the Bin.        element(app, "cinemas-tab-you").tap()        let outsideBin = element(app, "cinemas-you-bin")        reach(app, outsideBin)        XCTAssertTrue(outsideBin.waitForExistence(timeout: 10))        let binBefore = outsideBin.label        // In again (the unlock asked again): the hidden film to the Bin from its page inside Hidden.        reach(app, row)        row.tap()        XCTAssertTrue(element(app, "cinemas-hidden").waitForExistence(timeout: 8))        searchInside(app, "quiet")        XCTAssertTrue(found.waitForExistence(timeout: 8))        found.tap()        binFromItsPage(app)        shot(app, "Dev 8 a hidden film moved to the Bin")        if element(app, "cinemas-title-play").exists { back(app) }        searchInside(app, "")        let hiddenBin = element(app, "cinemas-hidden-bin")        reach(app, hiddenBin)        XCTAssertTrue(hiddenBin.waitForExistence(timeout: 10), "a Bin row inside Hidden")        shot(app, "Dev 9 the Bin row inside Hidden")        // Outside (leaving shuts Hidden): the Bin's count is the same and it never shows the film.        element(app, "cinemas-tab-you").tap()        element(app, "cinemas-tab-you").tap()        reach(app, outsideBin)        XCTAssertEqual(outsideBin.label, binBefore, "the outside Bin's count is unchanged")        outsideBin.tap()        XCTAssertTrue(element(app, "cinemas-bin").waitForExistence(timeout: 8))        XCTAssertFalse(matching(app, prefix: "cinemas-bin-", label: film).waitForExistence(timeout: 3), "the outside Bin never shows it")        shot(app, "Dev 10 the outside Bin without it")        back(app)        // Inside Hidden's Bin: Restore.        reach(app, row)        row.tap()        reach(app, hiddenBin)        XCTAssertTrue(hiddenBin.waitForExistence(timeout: 10))        hiddenBin.tap()        XCTAssertTrue(element(app, "cinemas-hidden-bin-page").waitForExistence(timeout: 8), "Hidden's own Bin")        let restore = matching(app, prefix: "cinemas-bin-put-back-", label: film)        XCTAssertTrue(restore.waitForExistence(timeout: 8))        shot(app, "Dev 11 Hidden's Bin with Restore and Delete forever")        restore.tap()        XCTAssertTrue(restore.waitForNonExistence(timeout: 10), "restored out of Hidden's Bin")        back(app)        // Delete forever, asked first: the spare film to the Bin inside Hidden, then gone for good.        if let spare {            searchInside(app, String(spare.prefix(5)))            let spareFound = matching(app, prefix: "cinemas-hidden-found-", label: spare)            XCTAssertTrue(spareFound.waitForExistence(timeout: 8), "the spare film inside Hidden")            spareFound.tap()            binFromItsPage(app)            if element(app, "cinemas-title-play").exists { back(app) }            searchInside(app, "")            reach(app, hiddenBin)            hiddenBin.tap()            let erase = matching(app, prefix: "cinemas-bin-erase-", label: spare)            XCTAssertTrue(erase.waitForExistence(timeout: 8))            erase.tap()            let confirm = app.alerts.buttons.element(boundBy: 0)            XCTAssertTrue(app.alerts.firstMatch.waitForExistence(timeout: 4), "Delete forever is asked first")            shot(app, "Dev 12 Delete forever asked first")            app.alerts.buttons.matching(NSPredicate(format: "label != %@", "Cancel")).firstMatch.tap()            _ = confirm            XCTAssertTrue(erase.waitForNonExistence(timeout: 10), "deleted forever")            shot(app, "Dev 13 after Delete forever")        }        // In again (the unlock asked again), and Stop hiding.        element(app, "cinemas-tab-you").tap()        element(app, "cinemas-tab-you").tap()        reach(app, row)        row.tap()        let menuInside = matching(app, prefix: "cinemas-row-menu-group:", label: category)        XCTAssertTrue(menuInside.waitForExistence(timeout: 10))        menuInside.tap()        app.buttons["Show this category"].firstMatch.tap()        XCTAssertTrue(app.staticTexts["\(category) is back on Home"].waitForExistence(timeout: 10))        element(app, "cinemas-tab-home").tap()        top(app)        let back = matching(app, prefix: "cinemas-row-menu-group:", label: category)        reach(app, back)        XCTAssertTrue(back.exists, "back on Home")        shot(app, "Dev 7 back on Home")    }}