1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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)"))
}
}