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

geesawra.industries/old-fella

Old people's social media app

0
1
main
209 lines · 10.8 KB Raw History
import XCTest

final class PostTouchTargetTests: XCTestCase {
    private let postID = "at://mae.example/app.bsky.feed.post/tap-target"

    func testCardSpacesOpenShortPostExactlyOnce() {
        let app = launch()
        let text = app.buttons["postText-\(postID)"].firstMatch
        XCTAssertTrue(text.waitForExistence(timeout: 10))
        attachScreenshot(app, name: "Post card tap targets")
        let row = app.cells.containing(.button, identifier: "postText-\(postID)").firstMatch
        let profile = row.buttons["profileLink-mae"]
        let like = row.buttons["Like"]
        let repost = row.buttons["Repost"]
        let reply = row.buttons["replyButton"]
        let quote = row.buttons["quoteButton"]
        let controls = row.buttons["postControls-\(postID)"]
        let timestamp = row.staticTexts["postTimestamp-\(postID)"]
        let points = [
            CGPoint(x: text.frame.maxX - 8, y: text.frame.midY),
            CGPoint(x: text.frame.minX + 5, y: text.frame.midY),
            CGPoint(x: (profile.frame.maxX + timestamp.frame.minX) / 2, y: profile.frame.midY),
            CGPoint(x: text.frame.minX + 12, y: (profile.frame.maxY + text.frame.minY) / 2),
            CGPoint(x: text.frame.minX + 12, y: (text.frame.maxY + like.frame.minY) / 2),
            CGPoint(x: (like.frame.maxX + repost.frame.minX) / 2, y: like.frame.midY),
            CGPoint(x: (repost.frame.maxX + reply.frame.minX) / 2, y: like.frame.midY),
            CGPoint(x: (reply.frame.maxX + quote.frame.minX) / 2, y: like.frame.midY),
            CGPoint(x: (quote.frame.maxX + controls.frame.minX) / 2, y: like.frame.midY),
            CGPoint(x: row.frame.minX + 5, y: text.frame.midY),
            CGPoint(x: row.frame.midX, y: row.frame.minY + 5),
            CGPoint(x: row.frame.midX, y: like.frame.maxY + 8)
        ]
        for point in points {
            assertOpensPost(in: app, at: point)
        }
    }

    func testSpaceBesideMultilineTextOpensPost() {
        let app = launch(kind: "multiline")
        let text = app.buttons["postText-\(postID)"].firstMatch
        XCTAssertTrue(text.waitForExistence(timeout: 10))
        for offset in [0.15, 0.5, 0.85] {
            let point = CGPoint(x: text.frame.maxX - 8, y: text.frame.minY + text.frame.height * offset)
            assertOpensPost(in: app, at: point)
        }
    }

    func testImageOnlyPostHasAccessibleNavigationAndMediaKeepsItsAction() {
        let app = launch(kind: "image")
        let image = app.buttons["Attached image"].firstMatch
        XCTAssertTrue(image.waitForExistence(timeout: 10), app.debugDescription)
        attachScreenshot(app, name: "Image-only post tap targets")
        XCTAssertTrue(app.buttons["openPost-\(postID)"].firstMatch.exists)
        let row = app.cells.containing(.button, identifier: "openPost-\(postID)").firstMatch
        assertOpensPost(in: app, at: CGPoint(x: row.frame.minX + 5, y: image.frame.midY))
        let like = row.buttons["Like"]
        assertOpensPost(in: app, at: CGPoint(x: row.frame.midX, y: (image.frame.maxY + like.frame.minY) / 2))
        image.tap()
        let close = app.buttons["imageViewerClose"]
        XCTAssertTrue(close.waitForExistence(timeout: 5))
        close.tap()
        XCTAssertTrue(app.navigationBars["Following"].exists)
        XCTAssertFalse(app.navigationBars["Post"].exists)
    }

    func testAvatarNameAndHandleKeepProfileTargets() {
        let app = launch()
        let profile = app.buttons["profileLink-mae"].firstMatch
        XCTAssertTrue(profile.waitForExistence(timeout: 10))
        for offset in [CGVector(dx: 0.1, dy: 0.5), CGVector(dx: 0.7, dy: 0.2), CGVector(dx: 0.7, dy: 0.8)] {
            profile.coordinate(withNormalizedOffset: offset).tap()
            XCTAssertTrue(app.staticTexts["profileDisplayName"].waitForExistence(timeout: 5))
            XCTAssertEqual(app.staticTexts["profileDisplayName"].label, "Mae Rivers")
            XCTAssertTrue(app.navigationBars.buttons["Following"].exists)
            app.navigationBars.buttons["Following"].tap()
            XCTAssertTrue(app.navigationBars["Following"].exists)
        }
    }

    func testActionsKeepTheirOwnTargets() {
        let app = launch()
        let row = app.cells.containing(.button, identifier: "postText-\(postID)").firstMatch
        XCTAssertTrue(row.waitForExistence(timeout: 10))
        row.buttons["Like"].coordinate(withNormalizedOffset: CGVector(dx: 0.9, dy: 0.5)).tap()
        XCTAssertTrue(row.buttons["Unlike"].waitForExistence(timeout: 5))
        row.buttons["Repost"].coordinate(withNormalizedOffset: CGVector(dx: 0.1, dy: 0.5)).tap()
        XCTAssertTrue(row.buttons["Undo repost"].waitForExistence(timeout: 5))
        for identifier in ["replyButton", "quoteButton"] {
            row.buttons[identifier].tap()
            app.dismissComposer()
            XCTAssertTrue(app.navigationBars["Following"].exists)
        }
        row.buttons["postControls-\(postID)"].tap()
        app.buttons["Share post"].tap()
        let copy = app.otherElements["ActivityListView"].cells["Copy"].firstMatch
        XCTAssertTrue(copy.waitForExistence(timeout: 5))
        copy.tap()
        XCTAssertTrue(copy.waitForNonExistence(timeout: 5))
        row.buttons["postControls-\(postID)"].tap()
        app.buttons["Moderation"].tap()
        XCTAssertTrue(app.buttons["Mute account"].waitForExistence(timeout: 5))
        XCTAssertFalse(app.navigationBars["Post"].exists)
    }

    func testMentionsLinksAndQuotesKeepTheirDestinations() {
        let app = launch(kind: "links")
        let text = app.buttons["postText-\(postID)"].firstMatch
        XCTAssertTrue(text.waitForExistence(timeout: 10))
        let mention = text.links.matching(NSPredicate(format: "label CONTAINS %@", "@alex.example")).firstMatch
        mention.tap()
        XCTAssertTrue(app.staticTexts["profileDisplayName"].waitForExistence(timeout: 5))
        XCTAssertEqual(app.staticTexts["profileDisplayName"].label, "Alex Kim")
        XCTAssertTrue(app.navigationBars.buttons["Following"].exists)
        app.navigationBars.buttons["Following"].tap()
        let link = text.links.matching(NSPredicate(format: "label CONTAINS %@", "https://example.invalid/article"))
            .firstMatch
        link.tap()
        XCTAssertTrue(app.buttons["Close"].waitForExistence(timeout: 5))
        app.buttons["Close"].tap()
        XCTAssertTrue(app.navigationBars["Following"].exists)
        app.buttons["quotedPostLink"].firstMatch.tap()
        XCTAssertTrue(app.navigationBars["Post"].waitForExistence(timeout: 5))
        XCTAssertTrue(app.buttons["postText-post-2"].firstMatch.exists)
        XCTAssertTrue(app.navigationBars.buttons["Following"].exists)
    }

    func testSelectedDetailPostDoesNotOpenItselfAgain() {
        let app = launch()
        let text = app.buttons["postText-\(postID)"].firstMatch
        XCTAssertTrue(text.waitForExistence(timeout: 10))
        text.coordinate(withNormalizedOffset: CGVector(dx: 0.1, dy: 0.5)).tap()
        XCTAssertTrue(app.navigationBars["Post"].waitForExistence(timeout: 5))
        text.coordinate(withNormalizedOffset: CGVector(dx: 0.95, dy: 0.5)).tap()
        text.coordinate(withNormalizedOffset: CGVector(dx: 0.1, dy: 0.5)).tap()
        XCTAssertTrue(app.navigationBars.buttons["Following"].exists)
        app.navigationBars.buttons["Following"].tap()
        XCTAssertTrue(app.navigationBars["Following"].exists)
    }

    func testLargeTextKeepsThePostTarget() {
        let app = launch(kind: "multiline", contentSize: "UICTContentSizeCategoryAccessibilityXXXL")
        let text = app.buttons["postText-\(postID)"].firstMatch
        XCTAssertTrue(text.waitForExistence(timeout: 10))
        XCTAssertGreaterThan(text.frame.height, 90)
        attachScreenshot(app, name: "Post targets with large text")
        assertOpensPost(in: app, at: CGPoint(x: text.frame.maxX - 8, y: text.frame.midY))
    }

    func testThreadReplyPaddingOpensTheReply() {
        continueAfterFailure = false
        let app = XCUIApplication()
        app.launchArguments = ["-ui-testing", "-ui-testing-signed-in"]
        app.launch()
        let root = app.buttons["postText-post-1"].firstMatch
        XCTAssertTrue(root.waitForExistence(timeout: 10))
        root.coordinate(withNormalizedOffset: CGVector(dx: 0.1, dy: 0.2)).tap()
        XCTAssertTrue(app.navigationBars["Post"].waitForExistence(timeout: 5))
        let reply = app.buttons["postText-post-2"].firstMatch
        XCTAssertTrue(reply.waitForExistence(timeout: 5))
        let point = CGPoint(x: reply.frame.minX - 8, y: reply.frame.midY)
        app.coordinate(withNormalizedOffset: .zero).withOffset(CGVector(dx: point.x, dy: point.y)).tap()
        XCTAssertTrue(app.navigationBars.buttons["Post"].waitForExistence(timeout: 5))
        XCTAssertFalse(app.buttons["openPost-post-2"].exists)
        XCTAssertTrue(app.buttons["openPost-post-1"].exists)
        app.navigationBars.buttons["Post"].tap()
        XCTAssertTrue(app.navigationBars.buttons["Following"].exists)
    }

    func testScrollingAcrossMediaDoesNotOpenPost() {
        let app = launch(kind: "image")
        let image = app.buttons["Attached image"].firstMatch
        XCTAssertTrue(image.waitForExistence(timeout: 10))
        let start = image.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.8))
        start.press(forDuration: 0.01, thenDragTo: start.withOffset(CGVector(dx: 0, dy: -180)))
        XCTAssertTrue(app.navigationBars["Following"].exists)
        XCTAssertFalse(app.navigationBars["Post"].exists)
        XCTAssertFalse(app.buttons["imageViewerClose"].exists)
    }

    private func assertOpensPost(in app: XCUIApplication, at point: CGPoint) {
        app.coordinate(withNormalizedOffset: .zero).withOffset(CGVector(dx: point.x, dy: point.y)).tap()
        XCTAssertTrue(app.navigationBars["Post"].waitForExistence(timeout: 5))
        XCTAssertTrue(
            app.buttons["postText-\(postID)"].firstMatch.exists || app.buttons["Attached image"].firstMatch.exists)
        XCTAssertTrue(app.navigationBars.buttons["Following"].exists)
        app.navigationBars.buttons["Following"].tap()
        XCTAssertTrue(app.navigationBars["Following"].exists)
    }

    private func attachScreenshot(_ app: XCUIApplication, name: String) {
        let attachment = XCTAttachment(screenshot: app.screenshot())
        attachment.name = name
        attachment.lifetime = .keepAlways
        add(attachment)
    }

    private func launch(kind: String = "short", contentSize: String? = nil) -> XCUIApplication {
        continueAfterFailure = false
        let app = XCUIApplication()
        app.launchArguments = ["-ui-testing", "-ui-testing-signed-in", "-ui-testing-post-targets"]
        if let contentSize {
            app.launchArguments += ["-UIPreferredContentSizeCategoryName", contentSize]
        }
        app.launchEnvironment["OLDFELLA_UI_TEST_POST_KIND"] = kind
        app.launchEnvironment["OLDFELLA_UI_TEST_LINK_HOST"] = "example.invalid"
        app.launch()
        return app
    }
}