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 } }