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

geesawra.industries/old-fella

Old people's social media app

0
1
main
195 lines · 8.8 KB Raw History
import XCTest

extension OldFellaUITests {
    func testImageViewerOpensSelectedImageZoomsAndDismisses() {
        let app = XCUIApplication()
        app.launchArguments = ["-ui-testing", "-ui-testing-image-grid", "3"]
        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))
        let selectedGridImage = grid.buttons["Attached image 2 of 3"].firstMatch
        XCTAssertTrue(selectedGridImage.waitForExistence(timeout: 5))
        selectedGridImage.tap()

        let viewer = app.buttons["imageViewerClose"]
        XCTAssertTrue(viewer.waitForExistence(timeout: 5))
        let viewerImage = app.descendants(matching: .any)["imageViewerImage"]
        XCTAssertTrue(viewerImage.waitForExistence(timeout: 5))
        XCTAssertTrue(viewerImage.label.contains("2 of 3"), viewerImage.label)

        viewerImage.pinch(withScale: 2, velocity: 1)
        XCTAssertTrue(viewerImage.exists)
        XCTAssertTrue(viewerImage.label.contains("2 of 3"), viewerImage.label)

        app.buttons["imageViewerClose"].tap()
        let viewerDismissed = XCTNSPredicateExpectation(
            predicate: NSPredicate(format: "exists == false"),
            object: viewer
        )
        XCTAssertEqual(XCTWaiter.wait(for: [viewerDismissed], timeout: 5), .completed)
        XCTAssertTrue(grid.exists)
        XCTAssertTrue(grid.buttons["Attached image 1 of 3"].firstMatch.exists)
        XCTAssertTrue(grid.buttons["Attached image 2 of 3"].firstMatch.exists)
        XCTAssertTrue(grid.buttons["Attached image 3 of 3"].firstMatch.exists)
    }

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

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

        let second = app.images["Image 2 of 3"].firstMatch
        XCTAssertTrue(second.waitForExistence(timeout: 5))
        XCTAssertFalse(app.buttons["imageDescriptionButton"].firstMatch.isHittable)
        second.swipeLeft()
        let third = app.images["Image 3 of 3"].firstMatch
        XCTAssertTrue(third.waitForExistence(timeout: 5))
        XCTAssertTrue(third.isHittable)
        third.swipeRight()
        XCTAssertTrue(second.waitForExistence(timeout: 5))
        XCTAssertTrue(second.isHittable)
        second.swipeRight()
        let first = app.images["Image 1 of 3"].firstMatch
        XCTAssertTrue(first.waitForExistence(timeout: 5))
        XCTAssertTrue(first.isHittable)
        XCTAssertTrue(app.buttons["imageDescriptionButton"].firstMatch.isHittable)

        let close = app.buttons["imageViewerClose"]
        let start = first.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5))
        start.press(
            forDuration: 0.01, thenDragTo: start.withOffset(CGVector(dx: 75, dy: 0)), withVelocity: .fast,
            thenHoldForDuration: 0)
        let viewerDismissed = XCTNSPredicateExpectation(
            predicate: NSPredicate(format: "exists == false"),
            object: close
        )
        XCTAssertEqual(XCTWaiter.wait(for: [viewerDismissed], timeout: 5), .completed)
    }

    func testImageViewerShowsDescriptionWithButton() {
        let app = XCUIApplication()
        app.launchArguments = ["-ui-testing", "-ui-testing-image-grid", "1"]
        app.launchArguments.append("-ui-testing-signed-in")
        app.launch()
        let grid = app.cells.containing(.button, identifier: "postText-post-1").firstMatch.descendants(matching: .any)[
            "postImageGrid"
        ].firstMatch
        XCTAssertTrue(grid.waitForExistence(timeout: 5))
        let tile = grid.descendants(matching: .any)["postImageTile-1"].firstMatch
        XCTAssertTrue(tile.waitForExistence(timeout: 5))
        XCTAssertEqual(tile.value as? String, "A blue square.")
        tile.tap()
        let image = app.descendants(matching: .any)["imageViewerImage"].firstMatch
        XCTAssertTrue(image.waitForExistence(timeout: 5))
        image.tap()
        XCTAssertFalse(app.staticTexts["A blue square."].exists)
        let button = app.buttons["imageDescriptionButton"].firstMatch
        XCTAssertTrue(button.waitForExistence(timeout: 5))
        button.tap()
        XCTAssertTrue(app.staticTexts["A blue square."].waitForExistence(timeout: 5))
        button.tap()
        XCTAssertFalse(app.staticTexts["A blue square."].exists)
    }

    func testImageViewerShortDownwardDragKeepsViewerOpen() {
        let app = XCUIApplication()
        app.launchArguments = ["-ui-testing", "-ui-testing-image-grid", "1"]
        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))
        grid.buttons["Attached image"].tap()

        let close = app.buttons["imageViewerClose"]
        let viewerImage = app.descendants(matching: .any)["imageViewerImage"]
        XCTAssertTrue(close.waitForExistence(timeout: 5))
        XCTAssertTrue(viewerImage.waitForExistence(timeout: 5))

        let originalFrame = viewerImage.frame
        let start = viewerImage.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5))
        start.press(
            forDuration: 0.1,
            thenDragTo: start.withOffset(CGVector(dx: 0, dy: 45)),
            withVelocity: .slow,
            thenHoldForDuration: 0
        )

        XCTAssertTrue(viewerImage.exists)
        XCTAssertEqual(viewerImage.frame.midX, originalFrame.midX, accuracy: 2)
        XCTAssertEqual(viewerImage.frame.midY, originalFrame.midY, accuracy: 2)
        XCTAssertTrue(close.exists)
        XCTAssertTrue(close.isHittable)
    }

    func testImageViewerZoomAndPanStayOpen() {
        let app = XCUIApplication()
        app.launchArguments = ["-ui-testing", "-ui-testing-image-grid", "3"]
        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))
        grid.buttons["Attached image 2 of 3"].firstMatch.tap()

        let close = app.buttons["imageViewerClose"]
        let viewerImage = app.descendants(matching: .any)["imageViewerImage"]
        XCTAssertTrue(close.waitForExistence(timeout: 5))
        XCTAssertTrue(viewerImage.waitForExistence(timeout: 5))

        XCTAssertEqual(viewerImage.value as? String, "100%")
        viewerImage.pinch(withScale: 2, velocity: 1)
        XCTAssertNotEqual(viewerImage.value as? String, "100%", "Pinching must enlarge the image")

        let start = viewerImage.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5))
        start.press(
            forDuration: 0.1, thenDragTo: start.withOffset(CGVector(dx: 100, dy: 0)), withVelocity: .slow,
            thenHoldForDuration: 0)
        start.press(
            forDuration: 0.1, thenDragTo: start.withOffset(CGVector(dx: 0, dy: 180)), withVelocity: .slow,
            thenHoldForDuration: 0)

        XCTAssertTrue(viewerImage.exists)
        XCTAssertTrue(close.exists)
        XCTAssertTrue(close.isHittable)
    }

    func testImageViewerDownwardFlickDismisses() {
        let app = XCUIApplication()
        app.launchArguments = ["-ui-testing", "-ui-testing-image-grid", "1"]
        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))
        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: 180)))

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