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

geesawra.industries/old-fella

Old people's social media app

0
1
main
134 lines · 5.9 KB Raw History
import XCTest

extension OldFellaUITests {
    func testImageViewerShortFlickDismisses() {
        let app = XCUIApplication()
        app.launchArguments = ["-ui-testing", "-ui-testing-image-grid", "1", "-ui-testing-signed-in"]
        app.launch()

        let grid = app.cells.containing(.button, identifier: "postText-post-1").firstMatch.descendants(matching: .any)[
            "postImageGrid"]
        XCTAssertTrue(grid.waitForExistence(timeout: 5))
        grid.buttons["Attached image"].tap()

        let close = app.buttons["imageViewerClose"]
        let viewport = app.scrollViews.containing(.any, identifier: "imageViewerImage").firstMatch
        XCTAssertTrue(close.waitForExistence(timeout: 5))
        XCTAssertTrue(viewport.waitForExistence(timeout: 5))

        let start = viewport.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5))
        start.press(
            forDuration: 0.01,
            thenDragTo: start.withOffset(CGVector(dx: 0, dy: 60)),
            withVelocity: .fast,
            thenHoldForDuration: 0
        )

        let viewerDismissed = XCTNSPredicateExpectation(
            predicate: NSPredicate(format: "exists == false"),
            object: close
        )
        XCTAssertEqual(XCTWaiter.wait(for: [viewerDismissed], timeout: 5), .completed)
        XCTAssertTrue(grid.exists)
    }

    func testImageViewerFlickDismissalDependsOnDirectionAndZoom() {
        let app = XCUIApplication()
        app.launchArguments = ["-ui-testing", "-ui-testing-image-grid", "1", "-ui-testing-signed-in"]
        app.launch()

        let grid = app.cells.containing(.button, identifier: "postText-post-1").firstMatch.descendants(matching: .any)[
            "postImageGrid"]
        XCTAssertTrue(grid.waitForExistence(timeout: 5))

        for zoomed in [false, true] {
            for offset in [
                CGVector(dx: 0, dy: -75), CGVector(dx: 0, dy: 75),
                CGVector(dx: -75, dy: 0), CGVector(dx: 75, dy: 0)
            ] {
                grid.buttons["Attached image"].tap()
                let close = app.buttons["imageViewerClose"]
                let image = app.descendants(matching: .any)["imageViewerImage"].firstMatch
                let viewport = app.scrollViews.containing(.any, identifier: "imageViewerImage").firstMatch
                XCTAssertTrue(close.waitForExistence(timeout: 5))
                XCTAssertTrue(image.waitForExistence(timeout: 5))
                if zoomed {
                    image.pinch(withScale: 2, velocity: 1)
                    XCTAssertNotEqual(image.value as? String, "100%")
                }
                let start = viewport.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5))
                start.press(
                    forDuration: 0.01, thenDragTo: start.withOffset(offset), withVelocity: .fast, thenHoldForDuration: 0
                )

                let shouldDismiss = !zoomed || offset.dx == 0
                XCTAssertEqual(
                    close.waitForNonExistence(timeout: shouldDismiss ? 5 : 1), shouldDismiss,
                    "Zoomed: \(zoomed), offset: \(offset)")
                if !shouldDismiss && close.exists {
                    XCTAssertTrue(image.isHittable)
                    XCTAssertNotEqual(image.value as? String, "100%")
                    close.tap()
                    XCTAssertTrue(close.waitForNonExistence(timeout: 5))
                }
                if close.exists { return }
            }
        }
    }

    func testImageGridWithOneImage() {
        assertImageGrid(imageCount: 1, expectedColumnCount: 1)
    }

    func testImageGridWithThreeImages() {
        assertImageGrid(imageCount: 3, expectedColumnCount: 2)
    }

    func testImageGridWithSevenImages() {
        assertImageGrid(imageCount: 7, expectedColumnCount: 4)
    }

    func testImageGridWithTenImages() {
        assertImageGrid(imageCount: 10, expectedColumnCount: 5)
    }

    func assertImageGrid(imageCount: Int, expectedColumnCount: Int) {
        let app = XCUIApplication()
        app.launchArguments = ["-ui-testing", "-ui-testing-image-grid", "\(imageCount)"]
        app.launchArguments.append("-ui-testing-signed-in")
        app.launch()

        let grid = app.cells.containing(.button, identifier: "postText-post-1").firstMatch.descendants(matching: .any)[
            "postImageGrid"]
        XCTAssertTrue(grid.waitForExistence(timeout: 5))
        XCTAssertTrue(app.buttons["postText-post-1"].waitForExistence(timeout: 5))
        XCTAssertFalse(grid.buttons["showMoreImagesButton"].exists)

        let images = (1...imageCount).map { index in
            let imageLabel = imageCount == 1 ? "Attached image" : "Attached image \(index) of \(imageCount)"
            return grid.buttons[imageLabel]
        }
        for image in images {
            XCTAssertTrue(image.waitForExistence(timeout: 5))
            XCTAssertGreaterThan(image.frame.width, 0)
            XCTAssertGreaterThan(image.frame.height, 0)
            XCTAssertGreaterThanOrEqual(image.frame.minX, grid.frame.minX - 1)
            XCTAssertGreaterThanOrEqual(image.frame.minY, grid.frame.minY - 1)
            XCTAssertLessThanOrEqual(image.frame.maxX, grid.frame.maxX + 1)
            XCTAssertLessThanOrEqual(image.frame.maxY, grid.frame.maxY + 1)
        }
        let attachedImages = grid.buttons.matching(NSPredicate(format: "label BEGINSWITH %@", "Attached image"))
        XCTAssertEqual(attachedImages.count, imageCount)

        let firstRowY = images[0].frame.minY
        let firstRowCount = images.filter { abs($0.frame.minY - firstRowY) < 2 }.count
        XCTAssertEqual(firstRowCount, expectedColumnCount)
    }

    func openPadActions(_ app: XCUIApplication) {
        let toggle = app.buttons["togglePadActionsButton"]
        XCTAssertTrue(toggle.waitForExistence(timeout: 10))
        if !app.buttons["addFeedColumnButton"].exists { toggle.tap() }
        XCTAssertTrue(app.buttons["addFeedColumnButton"].waitForExistence(timeout: 5))
    }
}