// What every Story UI test does: launch Story Dev on the crowded invented// script (sixty-four scenes, sixty characters; Resources/Fixtures/story-ink-crowd.json),// find a scene row, a panel's close mark, and drag from a given part of an// element as a finger would. Nothing here is anyone's real writing.import XCTestenumStoryUI {/// Story Dev on the crowded script, with any extra launch arguments (a text size, a look). @MainActor staticfunc launch(_ extra: [String] = []) -> XCUIApplication {let app = XCUIApplication() app.launchArguments += ["-story-ink-seed", "YES", "-story-ink-crowd", "YES"] + extra app.launch()return app }/// The largest accessibility text size, set at launch.staticlet largestText = ["-UIPreferredContentSizeCategoryName", "UICTContentSizeCategoryAccessibilityXXXL"]/// A scene row by the place in its title. @MainActor staticfunc row(_ app: XCUIApplication, _ words: String) -> XCUIElement { app.buttons.containing(NSPredicate(format: "label CONTAINS[c] %@", words)).firstMatch }/// The home has come in: its first scene row stands. @MainActor staticfunc home(_ app: XCUIApplication) -> XCUIElement {let first = row(app, "Waiting Room") XCTAssertTrue(first.waitForExistence(timeout: 60), "the crowded script came in")return first }/// A button by its exact label. @MainActor staticfunc button(_ app: XCUIApplication, _ label: String) -> XCUIElement { app.buttons.matching(NSPredicate(format: "label == %@", label)).firstMatch }/// A finger put down at `at` (0…1 across and down `element`) and moved/// `by` points quickly, as a swipe that starts on that part of it. @MainActor staticfunc drag(_ element: XCUIElement, at: CGVector, by: CGVector, hold: TimeInterval = 0.05) {let start = element.coordinate(withNormalizedOffset: at) start.press(forDuration: hold, thenDragTo: start.withOffset(by), withVelocity: .fast, thenHoldForDuration: 0.05) }/// A drag from a point of the window (0…1 of its width and height). @MainActor staticfunc drag(_ app: XCUIApplication, from: CGVector, by: CGVector, hold: TimeInterval = 0.05) { drag(app.windows.firstMatch, at: from, by: by, hold: hold) }/// Scrolls the home down until `element` is on screen and can be touched, at most `tries` swipes. @MainActor staticfunc scrollTo(_ element: XCUIElement, in app: XCUIApplication, tries: Int = 40) -> Bool {for _ in0..<tries {if element.exists, element.isHittable { returntrue }// A flick, so the list runs on a way past the finger.let start = app.windows.firstMatch.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.8)) start.press(forDuration: 0.01, thenDragTo: start.withOffset(CGVector(dx: 0, dy: -app.windows.firstMatch.frame.height * 0.5)), withVelocity: .fast, thenHoldForDuration: 0) }return element.exists && element.isHittable }/// Whether `element` stands wholly inside the window's safe part: under/// the clock and above the home indicator, clear of the sides. @MainActor staticfunc inside(_ element: XCUIElement, _ app: XCUIApplication, top: CGFloat = 44, bottom: CGFloat = 20) -> Bool {let window = app.windows.firstMatch.framelet frame = element.framereturn frame.minY >= window.minY + top && frame.maxY <= window.maxY - bottom && frame.minX >= window.minX && frame.maxX <= window.maxX }}/// Every Story UI test: upright to start, upright again after, and the/// screen kept with a failure so it can be looked at.classStoryUITestCase: XCTestCase {overridefunc setUp() { continueAfterFailure = false XCUIDevice.shared.orientation = .portrait }overridefunc tearDown() {iflet run = testRun, run.failureCount > 0 {let shot = XCTAttachment(screenshot: XCUIScreen.main.screenshot()) shot.name = name shot.lifetime = .keepAlways add(shot) } XCUIDevice.shared.orientation = .portrait }/// Another device asking to be let in stands the shell's card at the top/// of the screen (the dev server's own traffic, not this test's). The/// card pushes Story down, so this skips only when it truly stands over/// one of Story's own top controls (`controls`, else the home's title and/// More and a scene's Back): on top, where a finger would land on it. The/// card is never tapped. @MainActor func skipUnderTheShellsCard(_ app: XCUIApplication, wait: TimeInterval = 2, _ controls: [XCUIElement] = []) throws {let tops = controls.isEmpty ? [StoryUI.button(app, "The Long Platform"), StoryUI.button(app, "More"), StoryUI.button(app, "Back to your scenes")] : controlsifShellCard.covers(app, tops, wait: wait) {throw XCTSkip("another device's unlock request stands over Story's own top") } }/// Waits until `element` can be touched (a panel's closing slide has finished). @MainActor func waitHittable(_ element: XCUIElement, _ timeout: TimeInterval = 3) {let deadline = Date().addingTimeInterval(timeout)whileDate() < deadline, !(element.exists && element.isHittable) {RunLoop.current.run(until: Date().addingTimeInterval(0.1)) } }}/// The shell's card of another device asking to be let in. `seed` stands an/// invented one (Walk Mac, `-shell-card-seed YES`, a development build only):/// it reaches no server, and its Refuse only takes it down and says so (`ShellToastUI`).enumShellCard {staticlet seed = ["-shell-card-seed", "YES"]/// The card's Refuse: standing on top while it can be touched. Its id is/// `approval-refuse`, or the card's own `approval-banner` where the card/// (an app's look) hands its id to everything in it. @MainActor staticfunc refuse(_ app: XCUIApplication) -> XCUIElement { app.buttons.matching(NSPredicate(format: "identifier BEGINSWITH %@", "approval-")).firstMatch }/// The band the shell's cards stand in, scrolling inside when they are taller than it. @MainActor staticfunc band(_ app: XCUIApplication) -> XCUIElement { app.scrollViews.containing(NSPredicate(format: "identifier BEGINSWITH %@", "approval-")).firstMatch }/// Everything of the card drawn on screen (inside its band), or nil while none of it is drawn. @MainActor staticfunc frame(_ app: XCUIApplication) -> CGRect? {let parts = app.descendants(matching: .any).matching(NSPredicate(format: "identifier BEGINSWITH %@", "approval-"))guard parts.firstMatch.exists else { returnnil }var frame = parts.allElementsBoundByIndex.reduce(CGRect.null) { $0.union($1.frame) }let band = band(app)if band.exists { frame = frame.intersection(band.frame) }return frame.isNull ? nil : frame }/// Whether the card stands on top (its Refuse can be touched) over any of `controls`. @MainActor staticfunc covers(_ app: XCUIApplication, _ controls: [XCUIElement], wait: TimeInterval) -> Bool {let refuse = refuse(app)guard refuse.waitForExistence(timeout: wait), refuse.isHittable, let card = frame(app) else { returnfalse }return controls.contains { $0.exists && $0.frame.intersects(card) } }/// `control` stands, can be touched, and lies wholly outside the card. @MainActor staticfunc clear(_ app: XCUIApplication, _ control: XCUIElement, _ name: String, file: StaticString = #filePath, line: UInt = #line) { XCTAssertTrue(control.waitForExistence(timeout: 10), "\(name) stands", file: file, line: line)let card = frame(app) ?? .null XCTAssertFalse(card.isNull, "the card stands while \(name) is looked at", file: file, line: line) XCTAssertTrue(control.isHittable, "\(name) can be touched", file: file, line: line)// A page drawn over the whole window (a note) covers the card: it stands under that page, over nothing.if refuse(app).isHittable { XCTAssertFalse(control.frame.intersects(card), "\(name) \(control.frame) stands clear of the card \(card)", file: file, line: line) }let window = app.windows.firstMatch.frame XCTAssertTrue(window.contains(control.frame), "\(name) \(control.frame) is on screen", file: file, line: line) }/// The card fits: no taller than half the screen, and its Refuse whole on screen above the home bar. @MainActor staticfunc fits(_ app: XCUIApplication, file: StaticString = #filePath, line: UInt = #line) {let window = app.windows.firstMatch.framelet refuse = refuse(app) XCTAssertTrue(refuse.waitForExistence(timeout: 10), "the card stands", file: file, line: line)let card = frame(app) ?? .null XCTAssertLessThanOrEqual(card.height, window.height * 0.5, "the card \(card) takes at most half the screen", file: file, line: line)// Taller than its band, the card scrolls: Refuse is reached by a swipe up inside it, never tapped.let band = band(app)for _ in0..<4where band.exists && !(refuse.isHittable && refuse.frame.maxY <= band.frame.maxY + 1) {let start = band.coordinate(withNormalizedOffset: CGVector(dx: 0.9, dy: 0.8)) start.press(forDuration: 0.01, thenDragTo: start.withOffset(CGVector(dx: 0, dy: -band.frame.height * 0.5))) }if band.exists { XCTAssertLessThanOrEqual(refuse.frame.maxY, band.frame.maxY + 1, "Refuse \(refuse.frame) stands whole in the card's band \(band.frame)", file: file, line: line) } XCTAssertTrue(refuse.isHittable, "Refuse can be touched", file: file, line: line) XCTAssertLessThanOrEqual(refuse.frame.maxY, window.maxY - 34, "Refuse \(refuse.frame) stands above the home bar", file: file, line: line) }}