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

geesawra.industries/old-fella

Old people's social media app

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

final class ImageGalleryTests: XCTestCase {
    func testFivePhotosFillTwoRows() { assertGallery(rowSizes: [3, 2]) }
    func testSevenPhotosFillTwoRows() { assertGallery(rowSizes: [4, 3]) }
    func testNinePhotosFillThreeRows() { assertGallery(rowSizes: [3, 3, 3]) }
    func testTenPhotosFillTwoRows() { assertGallery(rowSizes: [5, 5]) }

    func testAccessibilityTextUsesOneColumn() {
        assertGallery(rowSizes: [1, 1, 1, 1, 1], accessibilityText: true)
    }

    func testReplyContextShowsThreeImages() { assertGallery(rowSizes: [2, 1], isReplyContext: true) }
    func testReplyContextShowsFiveImages() { assertGallery(rowSizes: [3, 2], isReplyContext: true) }
    func testReplyContextShowsTenImages() { assertGallery(rowSizes: [5, 5], isReplyContext: true) }

    private func assertGallery(rowSizes: [Int], isReplyContext: Bool = false, accessibilityText: Bool = false) {
        let count = rowSizes.reduce(0, +)
        let app = XCUIApplication()
        app.launchArguments = ["-ui-testing", "-ui-testing-signed-in", "-ui-testing-image-grid", "\(count)"]
        if isReplyContext { app.launchArguments.append("-ui-testing-image-grid-reply-context") }
        if accessibilityText {
            app.launchArguments += ["-UIPreferredContentSizeCategoryName", "UICTContentSizeCategoryAccessibilityM"]
        }
        app.launch()
        let container =
            isReplyContext
            ? app.descendants(matching: .any)["replyPostContext-post-1"].firstMatch
            : app.cells.containing(.button, identifier: "postText-post-1").firstMatch
        XCTAssertTrue(container.waitForExistence(timeout: 5))
        let grid = container.descendants(matching: .any)["postImageGrid"].firstMatch
        XCTAssertTrue(grid.waitForExistence(timeout: 5))
        XCTAssertFalse(grid.buttons["showMoreImagesButton"].exists)

        let tileHeight: CGFloat = isReplyContext ? 80 : 112
        var index = 1
        for (rowIndex, rowSize) in rowSizes.enumerated() {
            let tileWidth = (grid.frame.width - CGFloat(rowSize - 1) * 6) / CGFloat(rowSize)
            for column in 0..<rowSize {
                let image = grid.buttons["Attached image \(index) of \(count)"].firstMatch
                XCTAssertTrue(image.waitForExistence(timeout: 5))
                XCTAssertEqual(image.frame.height, tileHeight, accuracy: 1)
                XCTAssertEqual(image.frame.width, tileWidth, accuracy: 1)
                XCTAssertEqual(image.frame.minX, grid.frame.minX + CGFloat(column) * (tileWidth + 6), accuracy: 1)
                XCTAssertEqual(image.frame.minY, grid.frame.minY + CGFloat(rowIndex) * (tileHeight + 6), accuracy: 1)
                index += 1
            }
        }
        XCTAssertEqual(grid.buttons.count, count)
        XCTAssertEqual(grid.frame.height, CGFloat(rowSizes.count) * (tileHeight + 6) - 6, accuracy: 1)

        let lastImage = grid.buttons["Attached image \(count) of \(count)"].firstMatch
        for _ in 0..<5 where !lastImage.isHittable { app.swipeUp() }
        XCTAssertTrue(lastImage.isHittable)
        lastImage.tap()
        let viewer = app.descendants(matching: .any)["imageViewerImage"].firstMatch
        XCTAssertTrue(viewer.waitForExistence(timeout: 5))
        XCTAssertTrue(viewer.label.contains("\(count) of \(count)"))
    }
}