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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import XCTest
final class ImageQualityTests: XCTestCase {
func testLongPressSavesViewerImageToPhotos() {
let app = XCUIApplication()
app.launchArguments = ["-ui-testing", "-ui-testing-signed-in", "-ui-testing-image-grid", "1"]
app.launch()
let tile = app.buttons["Attached image"].firstMatch
XCTAssertTrue(tile.waitForExistence(timeout: 5))
tile.tap()
let image = app.images["imageViewerImage"].firstMatch
XCTAssertTrue(image.waitForExistence(timeout: 5))
image.press(forDuration: 0.8)
let save = app.buttons["Save Image"].firstMatch
XCTAssertTrue(save.waitForExistence(timeout: 5))
save.tap()
let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
let allow = springboard.alerts.buttons.matching(
NSPredicate(format: "label IN %@", ["OK", "Allow", "Add Photos Only"])
).firstMatch
let saved = app.staticTexts["Saved to Photos"]
let response = XCTNSPredicateExpectation(predicate: NSPredicate { _, _ in saved.exists || allow.exists },
object: nil)
XCTAssertEqual(XCTWaiter.wait(for: [response], timeout: 5), .completed)
if allow.exists { allow.tap() }
XCTAssertTrue(saved.waitForExistence(timeout: 5))
XCTAssertTrue(app.buttons["imageViewerClose"].exists)
}
func testViewerLoadsFullSizeWithoutShowingThumbnail() {
let app = launchImageFixture()
let tile = app.buttons["Attached image"].firstMatch
XCTAssertTrue(tile.waitForExistence(timeout: 5))
tile.tap()
let loading = app.descendants(matching: .any)["imageViewerLoading"].firstMatch
let image = app.images["imageViewerImage"].firstMatch
XCTAssertTrue(loading.waitForExistence(timeout: 5))
XCTAssertFalse(image.exists)
assertDescriptionButtonIsInBottomCorner(app)
let screenshot = XCTAttachment(screenshot: app.screenshot())
screenshot.name = "Image viewer loading"
screenshot.lifetime = .keepAlways
add(screenshot)
XCTAssertTrue(image.waitForExistence(timeout: 8))
XCTAssertEqual(image.frame.width / image.frame.height, 2, accuracy: 0.05)
XCTAssertFalse(loading.exists)
assertDescriptionButtonIsInBottomCorner(app)
app.buttons["imageViewerClose"].tap()
XCTAssertTrue(tile.waitForExistence(timeout: 5))
}
func testViewerFailureOffersRetryWithoutShowingThumbnail() {
let app = launchImageFixture(fails: true)
let tile = app.buttons["Attached image"].firstMatch
XCTAssertTrue(tile.waitForExistence(timeout: 5))
tile.tap()
let image = app.images["imageViewerImage"].firstMatch
let retry = app.buttons["Try Again"].firstMatch
XCTAssertTrue(retry.waitForExistence(timeout: 8))
XCTAssertFalse(image.exists)
assertDescriptionButtonIsInBottomCorner(app)
retry.tap()
XCTAssertTrue(app.descendants(matching: .any)["imageViewerLoading"].firstMatch.waitForExistence(timeout: 5))
assertDescriptionButtonIsInBottomCorner(app)
XCTAssertFalse(image.exists)
XCTAssertTrue(retry.waitForExistence(timeout: 8))
}
func testRevealedQuotedGIFOpensViewerWithoutAnotherReveal() {
let app = XCUIApplication()
app.launchArguments = ["-ui-testing", "-ui-testing-gif", "-ui-testing-signed-in"]
app.launch()
app.openSettings()
let setting = app.switches["showGIFsInline"]
XCTAssertTrue(setting.waitForExistence(timeout: 5))
setting.coordinate(withNormalizedOffset: CGVector(dx: 0.95, dy: 0.5)).tap()
app.closeSettings()
let post = app.cells.containing(.button, identifier: "postText-post-1").firstMatch
let reveal = post.buttons["showReplyGIFButton"].firstMatch
XCTAssertTrue(reveal.waitForExistence(timeout: 5))
reveal.tap()
let gif = post.buttons["GIF"].firstMatch
XCTAssertTrue(gif.waitForExistence(timeout: 5))
gif.tap()
XCTAssertTrue(app.images["imageViewerImage"].firstMatch.waitForExistence(timeout: 5))
XCTAssertFalse(app.buttons["imageViewerShowGIF"].exists)
}
private func assertDescriptionButtonIsInBottomCorner(
_ app: XCUIApplication, file: StaticString = #filePath, line: UInt = #line
) {
let frame = app.buttons["imageDescriptionButton"].firstMatch.frame
let viewport = app.frame
XCTAssertGreaterThan(frame.width, 100, file: file, line: line)
XCTAssertLessThan(frame.midX, viewport.midX, file: file, line: line)
XCTAssertGreaterThan(frame.minY, viewport.maxY - 200, file: file, line: line)
}
private func launchImageFixture(fails: Bool = false) -> XCUIApplication {
let app = XCUIApplication()
app.launchArguments = [
"-ui-testing", "-ui-testing-signed-in", "-ui-testing-image-grid", "1",
"-ui-testing-image-quality", "-ui-testing-image-load-delay"
]
if fails { app.launchArguments.append("-ui-testing-image-failure") }
app.launch()
return app
}
}