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

geesawra.industries/old-fella

Old people's social media app

0
1
main
79 lines · 3.2 KB Raw History
import XCTest

final class ScrollPerformanceTests: XCTestCase {
    func testFeedDoesNotBuildEveryRowBeforeScrolling() {
        assertLazyFeed(fixture: "-ui-testing-image-scroll", firstPostID: "image-scroll-0", rowCount: 24)
    }

    func testRichFeedDoesNotBuildEveryRowBeforeScrolling() {
        assertLazyFeed(
            fixture: "-ui-testing-rich-scroll",
            firstPostID: "at://mae.example/app.bsky.feed.post/scroll-0-main", rowCount: 48
        )
    }

    private func assertLazyFeed(fixture: String, firstPostID: String, rowCount: Int) {
        continueAfterFailure = false
        let app = XCUIApplication()
        app.launchArguments = [
            "-ui-testing", "-ui-testing-signed-in", fixture, "-profile-view-updates"
        ]
        app.launch()
        XCTAssertTrue(app.buttons["postText-\(firstPostID)"].waitForExistence(timeout: 10))
        app.buttons["Read view update counts"].tap()
        let snapshot = app.staticTexts["viewUpdateCounts"].value as? String ?? ""
        let createdRows = snapshot.split(separator: ";").filter { $0.hasPrefix("FeedPostRow-") }
        let attachment = XCTAttachment(string: snapshot)
        attachment.name = "Feed rows created before scrolling"
        attachment.lifetime = .keepAlways
        add(attachment)
        print("Feed rows created before scrolling: \(createdRows.count). \(snapshot)")
        XCTAssertGreaterThan(createdRows.count, 0)
        XCTAssertLessThan(
            createdRows.count, rowCount,
            "The feed must leave offscreen rows unbuilt until they approach the viewport."
        )
    }

    func testImageFeedScrolling() {
        measureScrolling(fixture: "-ui-testing-image-scroll", firstPostID: "image-scroll-0", waitsForImage: true)
    }

    func testRichFeedScrolling() {
        measureScrolling(
            fixture: "-ui-testing-rich-scroll",
            firstPostID: "at://mae.example/app.bsky.feed.post/scroll-0-main"
        )
    }

    private func measureScrolling(fixture: String, firstPostID: String, waitsForImage: Bool = false) {
        continueAfterFailure = false
        let app = XCUIApplication()
        app.launchArguments = ["-ui-testing", "-ui-testing-signed-in", fixture]
        let options = XCTMeasureOptions()
        options.iterationCount = 5
        options.invocationOptions = [.manuallyStart, .manuallyStop]

        measure(
            metrics: [
                XCTOSSignpostMetric.scrollingAndDecelerationMetric,
                XCTCPUMetric(application: app),
                XCTMemoryMetric(application: app)
            ], options: options
        ) {
            app.launch()
            let firstPost = app.buttons["postText-\(firstPostID)"]
            XCTAssertTrue(firstPost.waitForExistence(timeout: 10))
            if waitsForImage {
                let firstRow = app.cells.containing(.button, identifier: "postText-\(firstPostID)").firstMatch
                XCTAssertTrue(firstRow.buttons["Attached image"].firstMatch.waitForExistence(timeout: 5))
            }
            let list = app.collectionViews.firstMatch
            startMeasuring()
            for _ in 0..<6 { list.swipeUp(velocity: .fast) }
            stopMeasuring()
            XCTAssertFalse(firstPost.isHittable)
            app.terminate()
        }
    }
}