SPB Git

Repository Public License

AlarmKitProbeUITests.swift 5.9 KB 99 lines swift

1// ORIX Alarm2// File: UITests/AlarmKitProbeUITests.swift3// Description: Simulator validation of the real AlarmKit path: permission prompt, scheduling, system alert, stop and snooze. Skipped unless ORIX_PROBE is set (takes about two minutes per variant).4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import XCTest89@MainActor10final class AlarmKitProbeUITests: XCTestCase {11    private let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")12    private let folder = "/tmp/orix-alarm-probe"1314    private var log: String { (try? String(contentsOfFile: "\(folder)/probe.log", encoding: .utf8)) ?? "" }15    private var lastLine: String { log.split(separator: "\n").last.map(String.init) ?? "" }16    /// Screenshots through XCTest time out while the system alarm alert is up, so the host captures instead:17    /// a file named `capture-<name>` asks the shell loop documented in docs/TESTING.md to run `simctl io screenshot`.18    private func hostShot(_ name: String) { FileManager.default.createFile(atPath: "\(folder)/capture-\(name)", contents: nil); sleep(3) }19    private func waitForLog(_ text: String, timeout: TimeInterval) -> Bool {20        let deadline = Date().addingTimeInterval(timeout)21        while Date() < deadline { if lastLine.contains(text) { return true }; sleep(2) }22        return false23    }24    /// Taps a point of the alert banner at the top of the screen (the banner exposes no accessibility elements to XCTest).25    /// The alert collapses into the Dynamic Island after a few seconds: a long press expands it again first.26    private func tapBanner(x: CGFloat) {27        springboard.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.036)).press(forDuration: 1.2)28        sleep(2); hostShot("expanded")29        springboard.coordinate(withNormalizedOffset: CGVector(dx: x, dy: 0.066)).tap()30    }3132    private func start(variant: String) throws -> XCUIApplication {33        let marker = "\(folder)/enabled"34        try XCTSkipUnless(FileManager.default.fileExists(atPath: marker), "create \(marker) to run the AlarmKit probe")35        try? FileManager.default.removeItem(atPath: "\(folder)/probe.log")36        let app = XCUIApplication()37        app.launchArguments = ["-ORIXReset", "YES", "-ORIXSkipOnboarding", "YES", "-ORIXProbeAlarm", variant]38        app.launch()39        // The prompt has two buttons (refuse, allow) whatever the system language; allow is the second one.40        let prompt = springboard.alerts.firstMatch41        if prompt.waitForExistence(timeout: 6) { prompt.buttons.element(boundBy: 1).tap() }42        XCTAssertTrue(waitForLog("system=[scheduled]", timeout: 20), "the alarm was not scheduled: \(lastLine)")43        XCUIDevice.shared.press(.home)       // the alarm must ring with the app in the background44        XCTAssertTrue(waitForLog("system=[alerting", timeout: 150), "AlarmKit never reported the alarm as alerting: \(lastLine)")45        sleep(3); hostShot("\(variant)-alert")46        return app47    }4849    func testPlainAlarmRingsInBackgroundAndStops() throws {50        _ = try start(variant: "plain")51        tapBanner(x: 0.86)                   // stop52        XCTAssertTrue(waitForLog("system=[] ", timeout: 20), "the alarm is still in the system after Stop: \(lastLine)")53        XCTAssertTrue(waitForLog("acknowledged", timeout: 10), "the stop intent did not reach the app: \(lastLine)")54        XCTAssertTrue(lastLine.contains("enabled=false"), "a one-time alarm must retire after it is stopped: \(lastLine)")55        hostShot("plain-after-stop")56    }5758    func testMissionAlarmOpensTheMission() throws {59        let app = try start(variant: "mission")60        tapBanner(x: 0.86)                   // stop61        XCTAssertTrue(waitForLog("acknowledged", timeout: 20), "the stop intent did not reach the app: \(lastLine)")62        sleep(4); hostShot("mission-after-stop")63        let cameForward = app.state == .runningForeground64        try? "openAppWhenRun brought the app forward: \(cameForward)\n".write(toFile: "\(folder)/mission-foreground.txt", atomically: true, encoding: .utf8)65        if !cameForward { app.activate() }66        XCTAssertTrue(waitForLog("mission=true", timeout: 15), "the wake mission was not presented: \(lastLine)")67        hostShot("mission-presented")68    }6970    func testSnoozeSchedulesFollowUp() throws {71        _ = try start(variant: "snooze")72        tapBanner(x: 0.70)                   // secondary button (snooze), left of stop73        XCTAssertTrue(waitForLog("snoozed", timeout: 20), "the snooze intent did not reach the app: \(lastLine)")74        XCTAssertTrue(waitForLog("system=[countdown]", timeout: 10), "no countdown follow-up in the system: \(lastLine)")75        hostShot("snooze-countdown")76        XCTAssertTrue(waitForLog("system=[alerting]", timeout: 100), "the snooze follow-up did not ring: \(lastLine)")77        sleep(3); hostShot("snooze-follow-up")78        tapBanner(x: 0.86)79        XCTAssertTrue(waitForLog("system=[] ", timeout: 20), "the follow-up is still in the system after Stop: \(lastLine)")80    }8182    /// Stop issued through AlarmKit (no simulator UI involved): the app must observe it, record it and retire the alarm.83    func testObservedStopRetiresOneTimeAlarm() throws {84        _ = try start(variant: "plain-autostop")85        XCTAssertTrue(waitForLog("acknowledged", timeout: 40), "the stop was not observed: \(lastLine)")86        XCTAssertTrue(waitForLog("system=[] ", timeout: 10), lastLine)87        XCTAssertTrue(lastLine.contains("enabled=false"), "a one-time alarm must retire after it is stopped: \(lastLine)")88    }8990    func testObservedStopPresentsMission() throws {91        let app = try start(variant: "mission-autostop")92        XCTAssertTrue(waitForLog("acknowledged", timeout: 40), "the stop was not observed: \(lastLine)")93        app.activate()94        XCTAssertTrue(waitForLog("mission=true", timeout: 15), "the wake mission was not presented: \(lastLine)")95        sleep(2)96        XCTAssertTrue(app.buttons["mission.options"].waitForExistence(timeout: 5))97    }98}99
Files main
Menu

Theme