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

geesawra.industries/old-fella

Old people's social media app

0
1
main
102 lines · 4.8 KB Raw History
import Darwin
import XCTest
import os

final class DeviceScrollProfile: XCTestCase {
    func testScrollFeed() throws {
        let environment = ProcessInfo.processInfo.environment
        try XCTSkipUnless(environment["OLDFELLA_DEVICE_SCROLL_PROFILE"] == "1", "Run with just profile.")
        let seconds = Double(environment["OLDFELLA_PROFILE_SECONDS"] ?? "40") ?? 40
        let fixture = environment["OLDFELLA_PROFILE_FEED"] ?? "live"
        let direction = environment["OLDFELLA_PROFILE_DIRECTION"] ?? "alternating"
        continueAfterFailure = false
        let app = XCUIApplication()
        app.launchArguments = ["-scroll-profiling"]
        if fixture == "rich" {
            app.launchArguments += ["-ui-testing", "-ui-testing-signed-in", "-ui-testing-rich-scroll"]
        }
        defer { app.terminate() }
        app.launch()
        XCTAssertTrue(
            app.buttons["feedPickerButton"].waitForExistence(timeout: 45), "Sign in before profiling the live feed."
        )
        let list = preparedFeed(in: app)
        let notification = try XCTUnwrap(environment["OLDFELLA_PROFILE_START_NOTIFICATION"])
        waitForRecorder(notification: notification)
        recordScroll(app: app, list: list, seconds: seconds, fixture: fixture, direction: direction)
        waitForRecorder(notification: notification + ".finished", marker: "OLDFELLA_PROFILE_FINISHED", timeout: 240)
    }

    private func recordScroll(
        app: XCUIApplication, list: XCUIElement, seconds: Double, fixture: String, direction: String
    ) {
        let log = OSLog(subsystem: "com.oldfella.profiling", category: .pointsOfInterest)
        let signpostID = OSSignpostID(log: log)
        var gestures = 0
        var pageRequests = 0
        let start = ProcessInfo.processInfo.systemUptime
        print("OLDFELLA_SCROLL_BEGIN feed=\(fixture) seconds=\(seconds) direction=\(direction)")
        os_signpost(.begin, log: log, name: "OldFellaScrollProfile", signpostID: signpostID)
        repeat {
            if direction == "manual" {
                Thread.sleep(forTimeInterval: 0.1)
                continue
            }
            if direction == "forward" || gestures % 8 < 4 {
                list.swipeUp(velocity: .fast)
            } else {
                list.swipeDown(velocity: .fast)
            }
            gestures += 1
            if direction == "forward" {
                let more = app.buttons["loadMorePostsButton"].firstMatch
                if more.exists && more.isHittable && more.isEnabled {
                    os_signpost(.event, log: log, name: "OldFellaLoadMorePosts")
                    more.tap()
                    pageRequests += 1
                    print("OLDFELLA_PROFILE_LOAD_MORE count=\(pageRequests)")
                }
            }
        } while ProcessInfo.processInfo.systemUptime - start < seconds
        os_signpost(.end, log: log, name: "OldFellaScrollProfile", signpostID: signpostID)
        let elapsed = ProcessInfo.processInfo.systemUptime - start
        print("OLDFELLA_SCROLL_END gestures=\(gestures) pageRequests=\(pageRequests) elapsed=\(elapsed)")
    }

    private func preparedFeed(in app: XCUIApplication) -> XCUIElement {
        let list = app.collectionViews.firstMatch
        XCTAssertTrue(list.waitForExistence(timeout: 45), "The feed must load before scrolling starts.")
        let firstPost = list.buttons.matching(NSPredicate(format: "identifier BEGINSWITH %@", "postText-")).firstMatch
        XCTAssertTrue(firstPost.waitForExistence(timeout: 10), "The feed needs posts for a scrolling measurement.")
        let initialPost = app.buttons[firstPost.identifier]
        let initialY = initialPost.frame.minY
        list.swipeUp(velocity: .fast)
        XCTAssertTrue(
            !initialPost.exists || initialPost.frame.minY < initialY - 10,
            "The first swipe must move the feed."
        )
        for _ in 0..<2 { list.swipeUp(velocity: .fast) }
        return list
    }

    private func waitForRecorder(
        notification: String, marker: String = "OLDFELLA_PROFILE_READY", timeout: TimeInterval = 90
    ) {
        let ready = expectation(description: "The recorder is ready")
        ready.assertForOverFulfill = false
        let center = CFNotificationCenterGetDarwinNotifyCenter()
        let observer = Unmanaged.passUnretained(ready).toOpaque()
        CFNotificationCenterAddObserver(
            center, observer,
            { _, observer, _, _, _ in
                guard let observer else { return }
                Unmanaged<XCTestExpectation>.fromOpaque(observer).takeUnretainedValue().fulfill()
            }, notification as CFString, nil, .deliverImmediately)
        defer {
            CFNotificationCenterRemoveObserver(center, observer, CFNotificationName(notification as CFString), nil)
        }
        print(marker)
        fflush(stdout)
        wait(for: [ready], timeout: timeout)
    }
}