~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/old-fella

Old people's social media app

0
1
main
222 lines · 11.5 KB Raw History
import XCTest

extension OldFellaUITests {
    func testCustomFeedShowsLoadingCardsAndRefreshSpinner() {
        let app = XCUIApplication()
        app.launchArguments = [
            "-ui-testing", "-ui-testing-signed-in", "-ui-testing-start-custom-feed", "-ui-testing-feed-delay"
        ]
        app.launch()
        XCTAssertTrue(app.navigationBars["Nature"].waitForExistence(timeout: 5))
        XCTAssertTrue(app.otherElements["initialFeedLoadingCards"].exists)
        let spinner = app.descendants(matching: .any)["initialFeedRefreshControl"].firstMatch
        XCTAssertTrue(spinner.waitForExistence(timeout: 2))
        XCTAssertTrue(app.buttons["postText-post-1"].waitForExistence(timeout: 20))
        XCTAssertTrue(spinner.waitForNonExistence(timeout: 2))
    }

    func testProfileLoadingWithFastDetails() {
        let app = XCUIApplication()
        app.launchArguments = [
            "-ui-testing", "-ui-testing-signed-in", "-ui-testing-profile-fast-load", "-ui-testing-dark"
        ]
        app.launch()
        XCTAssertTrue(app.buttons["postText-post-1"].waitForExistence(timeout: 10))
        app.buttons["profileLink-mae"].firstMatch.tap()
        let spinner = app.descendants(matching: .any)["initialProfileRefreshControl"].firstMatch
        XCTAssertTrue(spinner.exists)
        let header = app.staticTexts["profileDisplayName"]
        XCTAssertTrue(header.waitForExistence(timeout: 2))
        XCTAssertFalse(app.staticTexts["Loading profile"].exists)
        let before = XCTAttachment(screenshot: app.screenshot())
        before.name = "Fast profile loading"
        before.lifetime = .keepAlways
        add(before)
        XCTAssertTrue(app.buttons["postText-post-1"].waitForExistence(timeout: 8))
        XCTAssertTrue(spinner.waitForNonExistence(timeout: 2))
        XCTAssertTrue(header.exists)
        let after = XCTAttachment(screenshot: app.screenshot())
        after.name = "Fast profile loaded"
        after.lifetime = .keepAlways
        add(after)
        let refreshedPost = app.buttons["postText-profile-post-refreshed"]
        XCTAssertFalse(refreshedPost.exists)
        pullProfileToRefresh(in: app)
        XCTAssertTrue(refreshedPost.waitForExistence(timeout: 10))
        XCTAssertTrue(spinner.waitForNonExistence(timeout: 2))
        XCTAssertTrue(header.exists)
    }

    func testProfileFeedShowsLoadingCardsAndRefreshSpinner() {
        let app = XCUIApplication()
        app.launchArguments = [
            "-ui-testing", "-ui-testing-signed-in", "-ui-testing-profile-details-delay",
            "-ui-testing-profile-feed-delay"
        ]
        app.launch()
        XCTAssertTrue(app.buttons["postText-post-1"].waitForExistence(timeout: 10))
        app.buttons["profileLink-mae"].firstMatch.tap()
        let spinner = app.descendants(matching: .any)["initialProfileRefreshControl"].firstMatch
        XCTAssertTrue(spinner.waitForExistence(timeout: 2))
        XCTAssertFalse(app.staticTexts["Loading profile"].exists)
        XCTAssertTrue(app.staticTexts["profileDisplayName"].waitForExistence(timeout: 5))
        XCTAssertTrue(app.otherElements["profilePostsLoading"].exists)
        XCTAssertTrue(spinner.waitForExistence(timeout: 2))
        pullProfileToRefresh(in: app)
        XCTAssertTrue(spinner.exists)
        let screenshot = XCTAttachment(screenshot: app.screenshot())
        screenshot.name = "Initial profile refresh spinner"
        screenshot.lifetime = .keepAlways
        add(screenshot)
        XCTAssertTrue(app.buttons["postText-post-1"].waitForExistence(timeout: 20))
        XCTAssertTrue(spinner.waitForNonExistence(timeout: 2))
        XCTAssertFalse(app.otherElements["profilePostsLoading"].exists)
        XCTAssertTrue(app.staticTexts["profileDisplayName"].exists)
    }

    private func pullProfileToRefresh(in app: XCUIApplication) {
        let list = app.collectionViews["profilePostList"]
        let start = list.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.4))
        let end = list.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.85))
        start.press(forDuration: 0.05, thenDragTo: end, withVelocity: .slow, thenHoldForDuration: 0.2)
    }

    func testPreferenceFailureStaysBlockedAfterForeground() {
        let app = XCUIApplication()
        app.launchArguments = ["-ui-testing", "-ui-testing-signed-in", "-ui-testing-preferences-fail"]
        app.launch()
        let error = app.staticTexts["Could not load content preferences"]
        XCTAssertTrue(error.waitForExistence(timeout: 10))
        XCUIDevice.shared.press(.home)
        app.activate()
        XCTAssertTrue(error.waitForExistence(timeout: 5))
        XCTAssertFalse(app.otherElements["Loading content preferences"].exists)
        XCTAssertFalse(app.buttons["postText-post-1"].exists)
        XCTAssertFalse(app.tables.firstMatch.exists)
    }

    func testCachedPreferenceRefreshFailureShowsContentWithoutNotice() {
        let app = XCUIApplication()
        app.launchArguments = ["-ui-testing", "-ui-testing-signed-in", "-ui-testing-preferences-refresh-fail"]
        app.launch()
        XCTAssertTrue(app.buttons["postText-post-1"].waitForExistence(timeout: 15))
        XCUIDevice.shared.press(.home)
        app.activate()
        XCTAssertTrue(app.buttons["postText-post-1"].waitForExistence(timeout: 10))
        XCTAssertFalse(
            app.staticTexts.containing(NSPredicate(format: "label CONTAINS 'Could not refresh content preferences'"))
                .firstMatch.exists)
        XCTAssertFalse(app.buttons["Retry"].exists)
    }

    func testIPadShowsHandlesDuringSessionRestoration() throws {
        try XCTSkipUnless(UIDevice.current.userInterfaceIdiom == .pad, "Requires iPad")
        XCUIDevice.shared.orientation = .landscapeLeft
        defer { XCUIDevice.shared.orientation = .portrait }
        let app = XCUIApplication()
        app.launchArguments = ["-ui-testing", "-ui-testing-signed-in", "-ui-testing-restore-delay"]
        app.launch()
        XCTAssertTrue(app.images["moveColumn-following"].waitForExistence(timeout: 2))
        XCTAssertTrue(app.images["moveColumn-notifications"].exists)
        XCTAssertTrue(app.buttons["postText-post-1"].waitForExistence(timeout: 10))
        XCTAssertTrue(app.images["moveColumn-following"].exists)
        XCTAssertTrue(app.images["moveColumn-notifications"].exists)
    }

    func testIPadDraggingKeepsFeedViewUpdatesBounded() throws {
        try XCTSkipUnless(UIDevice.current.userInterfaceIdiom == .pad, "Requires iPad")
        XCUIDevice.shared.orientation = .landscapeLeft
        defer { XCUIDevice.shared.orientation = .portrait }
        let app = XCUIApplication()
        app.launchArguments = ["-ui-testing", "-ui-testing-signed-in", "-profile-view-updates"]
        app.launch()
        XCTAssertTrue(app.buttons["postText-post-1"].waitForExistence(timeout: 10))
        let handle = app.images["moveColumn-notifications"]
        XCTAssertTrue(handle.waitForExistence(timeout: 5))
        app.buttons["Reset view update counts"].tap()
        let start = handle.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5))
        start.press(forDuration: 0.2, thenDragTo: start.withOffset(CGVector(dx: -(app.frame.width - 12) / 2, dy: 0)))
        app.buttons["Read view update counts"].tap()
        let snapshot = app.staticTexts["viewUpdateCounts"].value as? String ?? ""
        let counts = Dictionary(
            uniqueKeysWithValues: snapshot.split(separator: ";").compactMap { entry -> (String, Int)? in
                let pair = entry.split(separator: "=")
                guard pair.count == 2, let count = Int(pair[1]) else { return nil }
                return (String(pair[0]), count)
            })
        for name in ["FeedView", "NotificationsView"] {
            XCTAssertLessThanOrEqual(
                counts[name, default: 0], 4, "Dragging must not rebuild \(name) on each update: \(snapshot)")
        }
    }

    func testIPadShowsFollowingAndNotifications() throws {
        try XCTSkipUnless(UIDevice.current.userInterfaceIdiom == .pad, "Requires iPad")
        XCUIDevice.shared.orientation = .landscapeLeft
        defer { XCUIDevice.shared.orientation = .portrait }
        let app = XCUIApplication()
        app.launchArguments = ["-ui-testing", "-ui-testing-signed-in"]
        app.launch()

        XCTAssertTrue(app.buttons["postText-post-1"].waitForExistence(timeout: 10))
        XCTAssertEqual(app.staticTexts.matching(identifier: "Following").count, 1)
        XCTAssertEqual(app.staticTexts.matching(identifier: "Notifications").count, 1)
        XCTAssertTrue(app.navigationBars["Following"].exists)
        XCTAssertTrue(app.navigationBars["Notifications"].exists)
        app.buttons["postText-post-1"].tap()
        XCTAssertTrue(app.navigationBars.buttons.element(boundBy: 0).waitForExistence(timeout: 5))
        app.navigationBars.buttons.element(boundBy: 0).tap()
        openPadActions(app)
        XCTAssertTrue(app.buttons["yourProfileButton"].exists)
        app.buttons["Settings"].tap()
        XCTAssertTrue(app.navigationBars["Settings"].waitForExistence(timeout: 5))
        app.buttons["Done"].tap()
        app.buttons["New post"].tap()
        XCTAssertTrue(app.textViews["composeTextField"].waitForExistence(timeout: 5))
    }

    func testIPadLandscapeShowsIndependentLoadingSpinnersInEveryColumn() throws {
        try XCTSkipUnless(UIDevice.current.userInterfaceIdiom == .pad, "Requires iPad")
        XCUIDevice.shared.orientation = .landscapeLeft
        defer { XCUIDevice.shared.orientation = .portrait }
        let app = XCUIApplication()
        app.launchArguments = [
            "-ui-testing", "-ui-testing-signed-in", "-ui-testing-three-loading-columns",
            "-ui-testing-restore-delay", "-ui-testing-feed-delay", "-ui-testing-notification-delay"
        ]
        app.launch()
        XCTAssertTrue(app.navigationBars["Nature"].waitForExistence(timeout: 5))
        let feeds = app.descendants(matching: .any).matching(identifier: "initialFeedRefreshControl")
        XCTAssertTrue(feeds.firstMatch.waitForExistence(timeout: 2))
        XCTAssertEqual(feeds.count, 2)
        let notifications = app.descendants(matching: .any)["initialNotificationsRefreshControl"].firstMatch
        XCTAssertTrue(notifications.exists)
        for spinner in feeds.allElementsBoundByIndex + [notifications] {
            XCTAssertFalse(spinner.frame.isEmpty)
            XCTAssertTrue(app.frame.contains(spinner.frame))
        }
        let screenshot = XCTAttachment(screenshot: app.screenshot())
        screenshot.name = "Three independent column spinners"
        screenshot.lifetime = .keepAlways
        add(screenshot)
        XCTAssertTrue(app.buttons["postText-post-1"].firstMatch.waitForExistence(timeout: 20))
        XCTAssertTrue(feeds.firstMatch.waitForNonExistence(timeout: 3))
        XCTAssertTrue(notifications.exists)
        XCTAssertTrue(app.buttons["notificationPost-notification-1"].waitForExistence(timeout: 25))
        XCTAssertTrue(notifications.waitForNonExistence(timeout: 3))
    }

    func testPostActionAccessibilityCounts() {
        let app = XCUIApplication()
        app.launchArguments = ["-ui-testing", "-ui-testing-signed-in"]
        app.launch()
        let like = app.buttons["Like"].firstMatch
        XCTAssertTrue(like.waitForExistence(timeout: 10))
        XCTAssertEqual(like.value as? String, "18 likes")
        XCTAssertEqual(app.buttons["Repost"].firstMatch.value as? String, "3 reposts")
        like.tap()
        let unlike = app.buttons["Unlike"].firstMatch
        XCTAssertTrue(unlike.waitForExistence(timeout: 5))
        XCTAssertEqual(unlike.value as? String, "19 likes")
    }
}