import XCTest private enum PermissionDialogChoice { case allow case deny var buttonLabels: [String] { switch self { case .allow: return ["OK", "Allow", "Allow While Using App", "Allow Full Access"] case .deny: return ["Don’t Allow", "Don't Allow"] } } } final class PermissionDialogUITests: XCTestCase { private let app = XCUIApplication() private let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard") override func setUpWithError() throws { continueAfterFailure = false app.launchArguments = ["-ui-testing", "-reset-permission-demo"] } func testCameraPermissionAllowPath() { openCameraPermissionPath( choice: .allow, expectedIdentifier: "camera-permission-granted" ) } func testCameraPermissionDenyPath() { openCameraPermissionPath( choice: .deny, expectedIdentifier: "camera-permission-denied" ) } private func openCameraPermissionPath( choice: PermissionDialogChoice, expectedIdentifier: String, file: StaticString = #filePath, line: UInt = #line ) { app.terminate() app.resetAuthorizationStatus(for: .camera) app.launch() app.buttons["open-camera-button"].tap() answerPermissionDialog(choice, file: file, line: line) let expectedScreen = app.staticTexts[expectedIdentifier] XCTAssertTrue( expectedScreen.waitForExistence(timeout: 10), "App did not show \(expectedIdentifier) after permission choice.", file: file, line: line ) } private func answerPermissionDialog( _ choice: PermissionDialogChoice, file: StaticString = #filePath, line: UInt = #line ) { let alert = springboard.alerts.firstMatch XCTAssertTrue( alert.waitForExistence(timeout: 5), "Permission dialog did not appear.", file: file, line: line ) for label in choice.buttonLabels { let button = alert.buttons[label].firstMatch if button.exists { button.tap() return } } XCTFail( "Permission dialog did not contain any expected button: \(choice.buttonLabels.joined(separator: ", ")).", file: file, line: line ) } }