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

geesawra.industries/old-fella

Old people's social media app

0
1
main
158 lines · 7.5 KB Raw History
import XCTest

final class PadDetailNavigationTests: XCTestCase {
    func testPostAndProfileOpenToTheRightOfTheirSource() throws {
        try XCTSkipUnless(UIDevice.current.userInterfaceIdiom == .pad, "Requires iPad")
        continueAfterFailure = false
        XCUIDevice.shared.orientation = .landscapeLeft
        defer { XCUIDevice.shared.orientation = .portrait }
        let app = launchApp()
        let following = app.navigationBars["Following"]
        let post = app.buttons["postText-post-1"].firstMatch
        XCTAssertTrue(post.waitForExistence(timeout: 10))
        post.tap()
        let postBar = app.navigationBars["Post"]
        XCTAssertTrue(postBar.waitForExistence(timeout: 5))
        waitForVisibleColumn(postBar, in: app)
        XCTAssertTrue(following.exists)
        XCTAssertGreaterThan(postBar.frame.minX, following.frame.minX)
        XCTAssertTrue(app.navigationBars["Notifications"].exists)
        XCTAssertFalse(postBar.buttons["Following"].exists)

        let postColumn = detailColumn(in: app, navigationBar: postBar)
        postColumn.buttons["profileLink-mae"].firstMatch.tap()
        let profileBar = app.navigationBars["Profile"]
        XCTAssertTrue(profileBar.waitForExistence(timeout: 5))
        waitForVisibleColumn(profileBar, in: app)
        XCTAssertTrue(postBar.exists)
        XCTAssertGreaterThan(profileBar.frame.minX, postBar.frame.minX)
        XCTAssertTrue(app.staticTexts["profileDisplayName"].waitForExistence(timeout: 5))
        XCTAssertFalse(profileBar.buttons["Post"].exists)
        let screenshot = XCTAttachment(screenshot: XCUIScreen.main.screenshot())
        screenshot.name = "Post and profile columns"
        screenshot.lifetime = .keepAlways
        add(screenshot)
        closeButton(in: profileBar).tap()
        XCTAssertTrue(profileBar.waitForNonExistence(timeout: 5))
        XCTAssertTrue(postBar.exists)
        closeButton(in: postBar).tap()
        XCTAssertTrue(postBar.waitForNonExistence(timeout: 5))
        XCTAssertTrue(following.exists)
        XCTAssertTrue(app.navigationBars["Notifications"].exists)
    }

    func testNotificationPostOpensInTheNextColumn() throws {
        try XCTSkipUnless(UIDevice.current.userInterfaceIdiom == .pad, "Requires iPad")
        continueAfterFailure = false
        XCUIDevice.shared.orientation = .landscapeLeft
        defer { XCUIDevice.shared.orientation = .portrait }
        let app = launchApp()
        let notification = app.buttons["notificationPost-notification-1"]
        XCTAssertTrue(notification.waitForExistence(timeout: 10))
        notification.tap()
        let postBar = app.navigationBars["Post"]
        XCTAssertTrue(postBar.waitForExistence(timeout: 5))
        let notifications = app.navigationBars["Notifications"]
        XCTAssertTrue(notifications.exists)
        XCTAssertGreaterThan(postBar.frame.minX, notifications.frame.minX)
        XCTAssertFalse(postBar.buttons["Notifications"].exists)
        XCTAssertTrue(closeButton(in: postBar).exists)
    }

    func testProfileConnectionsAndListedProfileOpenToTheRight() throws {
        try XCTSkipUnless(UIDevice.current.userInterfaceIdiom == .pad, "Requires iPad")
        continueAfterFailure = false
        XCUIDevice.shared.orientation = .landscapeLeft
        defer { XCUIDevice.shared.orientation = .portrait }
        let app = launchApp()
        let mae = app.buttons["profileLink-mae"].firstMatch
        XCTAssertTrue(mae.waitForExistence(timeout: 10))
        mae.tap()

        let profileBar = app.navigationBars["Profile"].firstMatch
        XCTAssertTrue(profileBar.waitForExistence(timeout: 5))
        waitForVisibleColumn(profileBar, in: app)
        let followers = detailColumn(in: app, navigationBar: profileBar).buttons["profile-followers"]
        XCTAssertTrue(followers.waitForExistence(timeout: 5))
        followers.tap()

        let followersBar = app.navigationBars["Followers"]
        XCTAssertTrue(followersBar.waitForExistence(timeout: 5))
        waitForVisibleColumn(followersBar, in: app)
        let alex = detailColumn(in: app, navigationBar: followersBar).buttons["profileConnection-alex"]
        XCTAssertTrue(alex.waitForExistence(timeout: 5))
        alex.tap()

        let destinationBar = app.navigationBars.matching(identifier: "Profile").element(boundBy: 1)
        XCTAssertTrue(destinationBar.waitForExistence(timeout: 5))
        waitForVisibleColumn(destinationBar, in: app)
        XCTAssertGreaterThan(destinationBar.frame.minX, followersBar.frame.minX)
        let name = detailColumn(in: app, navigationBar: destinationBar).staticTexts["profileDisplayName"]
        XCTAssertTrue(name.waitForExistence(timeout: 5))
        XCTAssertEqual(name.label, "Alex Kim")
    }

    func testPortraitKeepsNavigationInTheSourceColumn() throws {
        try XCTSkipUnless(UIDevice.current.userInterfaceIdiom == .pad, "Requires iPad")
        continueAfterFailure = false
        XCUIDevice.shared.orientation = .portrait
        let app = launchApp()
        let post = app.buttons["postText-post-1"].firstMatch
        XCTAssertTrue(post.waitForExistence(timeout: 10))
        post.tap()
        let postBar = app.navigationBars["Post"]
        XCTAssertTrue(postBar.waitForExistence(timeout: 5))
        XCTAssertTrue(postBar.buttons["Following"].exists)
        XCTAssertFalse(closeButton(in: postBar).exists)
    }

    func testSearchResultOpensAProfileColumn() throws {
        try XCTSkipUnless(UIDevice.current.userInterfaceIdiom == .pad, "Requires iPad")
        continueAfterFailure = false
        XCUIDevice.shared.orientation = .landscapeLeft
        defer { XCUIDevice.shared.orientation = .portrait }
        let app = launchApp()
        let actions = app.buttons["togglePadActionsButton"]
        XCTAssertTrue(actions.waitForExistence(timeout: 10))
        actions.tap()
        app.buttons["padSearchButton"].tap()
        let field = app.searchFields.firstMatch
        XCTAssertTrue(field.waitForExistence(timeout: 5))
        field.tap()
        field.typeText("example\n")
        let result = app.buttons["searchAccount-mae"]
        XCTAssertTrue(result.waitForExistence(timeout: 5))
        result.tap()
        let profileBar = app.navigationBars["Profile"]
        XCTAssertTrue(profileBar.waitForExistence(timeout: 5))
        XCTAssertTrue(app.navigationBars["Search"].waitForNonExistence(timeout: 5))
        XCTAssertTrue(closeButton(in: profileBar).exists)
        XCTAssertTrue(app.navigationBars["Notifications"].exists)
    }

    private func launchApp() -> XCUIApplication {
        let app = XCUIApplication()
        app.launchArguments = ["-ui-testing", "-ui-testing-signed-in"]
        app.launch()
        return app
    }

    private func waitForVisibleColumn(_ navigationBar: XCUIElement, in app: XCUIApplication) {
        let visible = XCTNSPredicateExpectation(
            predicate: NSPredicate { _, _ in
                navigationBar.frame.minX >= 0 && navigationBar.frame.maxX <= app.frame.maxX
                    && self.closeButton(in: navigationBar).isHittable
            }, object: nil)
        XCTAssertEqual(XCTWaiter.wait(for: [visible], timeout: 5), .completed)
    }

    private func closeButton(in navigationBar: XCUIElement) -> XCUIElement {
        navigationBar.buttons.matching(NSPredicate(format: "identifier BEGINSWITH %@", "closeColumn-detail-"))
            .firstMatch
    }

    private func detailColumn(in app: XCUIApplication, navigationBar: XCUIElement) -> XCUIElement {
        let columnID = String(closeButton(in: navigationBar).identifier.dropFirst("closeColumn-".count))
        return app.otherElements["padColumn-\(columnID)"].firstMatch
    }
}