SelfieCam/SelfieCamUITests/SelfieCamUITests.swift

131 lines
4.0 KiB
Swift

//
// SelfieCamUITests.swift
// SelfieCamUITests
//
// Created by Matt Bruce on 1/4/26.
//
import XCTest
final class SelfieCamUITests: XCTestCase {
private var app: XCUIApplication!
override func setUpWithError() throws {
continueAfterFailure = false
app = XCUIApplication()
addUIInterruptionMonitor(withDescription: "System Permission Alerts") { alert in
let preferredButtons = [
"Allow",
"Allow While Using App",
"OK",
"Continue"
]
for title in preferredButtons where alert.buttons[title].exists {
alert.buttons[title].tap()
return true
}
if let firstButton = alert.buttons.allElementsBoundByIndex.first {
firstButton.tap()
return true
}
return false
}
}
override func tearDownWithError() throws {
}
@MainActor
func testAppStorePortraitScreenshots() throws {
app.launchArguments = ["-ui-testing-screenshots"]
app.launchEnvironment = [
"ENABLE_DEBUG_PREMIUM": "1"
]
app.launch()
if !waitForCameraScreen(timeout: 8) {
app.terminate()
app.launch()
}
saveScreenshot(named: "01-launch-screen")
XCTAssertTrue(waitForCameraScreen(timeout: 25), "Camera screen did not appear")
XCTAssertTrue(app.otherElements["screenshot-camera-placeholder"].waitForExistence(timeout: 5), "Screenshot camera placeholder did not appear")
RunLoop.current.run(until: Date().addingTimeInterval(0.6))
saveScreenshot(named: "02-camera-ring-light")
let settingsButton = app.buttons["Settings"]
XCTAssertTrue(settingsButton.waitForExistence(timeout: 10), "Settings button not found")
settingsButton.tap()
XCTAssertTrue(app.otherElements["settings-sheet"].waitForExistence(timeout: 8), "Settings sheet did not appear")
RunLoop.current.run(until: Date().addingTimeInterval(0.8))
saveScreenshot(named: "03-settings-popup")
}
@MainActor
func testLaunchPerformance() throws {
measure(metrics: [XCTApplicationLaunchMetric()]) {
XCUIApplication().launch()
}
}
// MARK: - Helpers
@MainActor
private func waitForCameraScreen(timeout: TimeInterval) -> Bool {
let onboardingActions = [
"Get Started",
"Continue",
"Next",
"Done"
]
let deadline = Date().addingTimeInterval(timeout)
while Date() < deadline {
if app.otherElements["screenshot-mode-active"].exists {
return true
}
_ = tapFirstExistingButton(labels: onboardingActions)
tapFirstSpringboardButton(labels: ["Allow", "Allow While Using App", "OK", "Continue"])
if app.alerts.firstMatch.exists {
app.tap()
}
RunLoop.current.run(until: Date().addingTimeInterval(0.2))
}
return app.otherElements["screenshot-mode-active"].exists
}
@MainActor
private func tapFirstSpringboardButton(labels: [String]) {
let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
for label in labels {
let button = springboard.buttons[label]
if button.waitForExistence(timeout: 0.2) {
button.tap()
return
}
}
}
@MainActor
private func tapFirstExistingButton(labels: [String]) -> Bool {
for label in labels {
let button = app.buttons[label]
if button.exists {
button.tap()
return true
}
}
return false
}
@MainActor
private func saveScreenshot(named name: String) {
let attachment = XCTAttachment(screenshot: app.screenshot())
attachment.name = name
attachment.lifetime = .keepAlways
add(attachment)
}
}