SPB Git

Repository Public

WorkflowUITests.swift 8.9 KB 191 lines swift

1// SP Lecture Studio — Tests2// File: Tests/UITests/WorkflowUITests.swift3// Description: UI tests driving the real app: annotate, record, pause, stop, edit, export — on iPhone, iPad (both orientations) and Mac.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import XCTest89final class WorkflowUITests: XCTestCase {10    var app: XCUIApplication!11    var device = "mac"1213    override func setUp() {14        continueAfterFailure = false15        app = XCUIApplication()16        // A scratch library inside the app container, with one generated 20-page lecture.17        app.launchArguments = ["-LectureDemoPDF", "YES", "-LectureLibraryRoot", "UITest-\(UUID().uuidString.prefix(8))", "-AppleLanguages", "(fr)", "-ApplePersistenceIgnoreState", "YES"]18        #if os(iOS)19        device = UIDevice.current.userInterfaceIdiom == .pad ? "ipad" : "iphone"20        XCUIDevice.shared.orientation = .portrait21        // Fallback when the microphone was not pre-authorized on this simulator.22        addUIInterruptionMonitor(withDescription: "permissions") { alert in23            for label in ["Autoriser", "Allow", "OK"] where alert.buttons[label].exists { alert.buttons[label].tap(); return true }24            return false25        }26        #endif27        app.launch()28    }2930    /// Saves a screenshot on the host so that layouts can be inspected after the run.31    func shot(_ name: String) {32        #if os(macOS)33        let data = app.windows.firstMatch.screenshot().pngRepresentation      // the app window only, never the whole desktop34        #else35        let data = XCUIScreen.main.screenshot().pngRepresentation36        #endif37        let dir = URL(fileURLWithPath: "/tmp/sls-shots")38        try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)39        try? data.write(to: dir.appendingPathComponent("\(device)-\(name).png"))40        let a = XCTAttachment(data: data, uniformTypeIdentifier: "public.png"); a.name = name; a.lifetime = .keepAlways; add(a)41    }4243    func element(_ id: String) -> XCUIElement { app.descendants(matching: .any).matching(identifier: id).firstMatch }4445    func openDemo() {46        let row = element("library.row.Démo — méthode de comparaison")47        XCTAssertTrue(row.waitForExistence(timeout: 20), "demo lecture missing from the library")48        shot("01-library")49        #if os(macOS)50        row.click()51        #else52        row.tap()53        #endif54        if !element("stage.canvas").waitForExistence(timeout: 15) { shot("01b-after-open"); XCTFail("stage did not appear") }55        XCTAssertTrue(element("page.indicator").waitForExistence(timeout: 5))56    }5758    func draw(from a: CGVector, to b: CGVector) {59        let canvas = element("stage.canvas")60        #if os(macOS)61        canvas.coordinate(withNormalizedOffset: a).click(forDuration: 0.1, thenDragTo: canvas.coordinate(withNormalizedOffset: b))62        #else63        canvas.coordinate(withNormalizedOffset: a).press(forDuration: 0.05, thenDragTo: canvas.coordinate(withNormalizedOffset: b))64        #endif65    }6667    #if os(iOS)68    func testAnnotateRecordEditExport() {69        openDemo()70        // Annotate before recording, navigate, undo / redo.71        draw(from: CGVector(dx: 0.25, dy: 0.45), to: CGVector(dx: 0.75, dy: 0.55))72        XCTAssertTrue(element("ink.undo").waitForExistence(timeout: 5))73        let undoEnabled = NSPredicate(format: "isEnabled == true")74        expectation(for: undoEnabled, evaluatedWith: element("ink.undo")); waitForExpectations(timeout: 8)75        shot("02-annotated")76        element("ink.undo").tap()77        element("ink.redo").tap()78        element("page.next").tap()79        XCTAssertTrue(element("page.indicator").label.contains("2") || (element("page.indicator").value as? String ?? "").contains("2") || app.staticTexts["2 / 20"].exists)8081        // Record with the microphone: draw, mark a retake, pause, resume, stop.82        element("record.toggle").tap()83        app.tap()                                             // lets the interruption monitor answer a permission alert if one appears84        XCTAssertTrue(element("record.stop").waitForExistence(timeout: 15), "recording did not start")85        sleep(2)86        draw(from: CGVector(dx: 0.3, dy: 0.35), to: CGVector(dx: 0.7, dy: 0.35))87        element("record.retake").tap()88        sleep(1)89        shot("03-recording")90        element("record.toggle").tap()                        // pause91        sleep(1)92        shot("04-paused")93        XCUIDevice.shared.orientation = .landscapeLeft94        sleep(2)95        shot("05-paused-landscape")96        element("record.toggle").tap()                        // resume, in landscape97        element("page.next").tap()98        draw(from: CGVector(dx: 0.3, dy: 0.5), to: CGVector(dx: 0.6, dy: 0.6))99        sleep(3)100        shot("06-recording-landscape")101        XCUIDevice.shared.orientation = .portrait102        sleep(2)103        element("record.stop").tap()104        let confirm = app.buttons["Arrêter et sauvegarder"]105        XCTAssertTrue(confirm.waitForExistence(timeout: 5)); confirm.tap()106        XCTAssertTrue(element("record.stop").waitForNonExistence(timeout: 15), "recording did not stop")107        shot("07-stopped")108109        // Edit: the montage mode opens, a cut shortens the programme, undo restores it.110        let mode = element("project.mode")111        XCTAssertTrue(mode.waitForExistence(timeout: 5))112        mode.buttons["Monter"].tap()113        XCTAssertTrue(element("editor.timeline").waitForExistence(timeout: 15), "editor did not open")114        sleep(2)115        let clock = element("editor.clock")116        let before = clock.label117        shot("08-editor")118        let timeline = element("editor.timeline")119        timeline.coordinate(withNormalizedOffset: CGVector(dx: 0.02, dy: 0.5)).tap()120        element("editor.in").tap()121        timeline.coordinate(withNormalizedOffset: CGVector(dx: 0.10, dy: 0.5)).tap()122        element("editor.out").tap()123        element("editor.cut").tap()124        sleep(1)125        XCTAssertNotEqual(clock.label, before, "cutting did not change the edited duration")126        shot("09-editor-cut")127        element("editor.undo").tap()128        sleep(1)129        XCTAssertEqual(clock.label.split(separator: "/").last, before.split(separator: "/").last, "undo did not restore the original duration")130        element("editor.play").tap(); sleep(2); element("editor.play").tap()131132        // Export: video + annotated PDF, verified by the app before being listed.133        element("project.export").tap()134        XCTAssertTrue(element("export.start").waitForExistence(timeout: 10))135        shot("10-export-sheet")136        element("export.start").tap()137        XCTAssertTrue(element("export.file.video").waitForExistence(timeout: 180), "no verified video was exported: \(element("export.error").label)")138        XCTAssertTrue(element("export.file.pdf").waitForExistence(timeout: 20))139        shot("11-exported")140        element("export.close").tap()141        XCUIDevice.shared.orientation = .landscapeLeft142        sleep(2)143        shot("12-editor-landscape")144        XCUIDevice.shared.orientation = .portrait145    }146147    func testPreflightPoliciesAndSearchSheets() {148        openDemo()149        element("record.settings").tap()150        app.buttons["Pause et arrêt automatiques…"].tap()151        XCTAssertTrue(element("policy.silence").waitForExistence(timeout: 5))152        shot("20-policies")153        app.buttons["Terminé"].firstMatch.tap()154        element("record.settings").tap()155        app.buttons["Test avant la séance…"].tap()156        XCTAssertTrue(element("preflight.start").waitForExistence(timeout: 5))157        shot("21-preflight")158        app.buttons["Terminé"].firstMatch.tap()159        element("project.info").tap()160        sleep(1)161        shot("22-inspector")162    }163    #else164    func testMacAnnotateNavigateAndExportPDF() {165        openDemo()166        shot("02-workspace")167        draw(from: CGVector(dx: 0.3, dy: 0.45), to: CGVector(dx: 0.7, dy: 0.55))168        sleep(1)169        shot("02b-after-drag")170        XCTAssertTrue((element("stage.canvas").value as? String ?? "").contains("1 annotation"), "the stroke was not added to the page")171        let undoEnabled = NSPredicate(format: "isEnabled == true")172        expectation(for: undoEnabled, evaluatedWith: element("ink.undo")); waitForExpectations(timeout: 8)173        element("tool.rectangle").exists ? element("tool.rectangle").click() : ()174        element("page.next").click()175        draw(from: CGVector(dx: 0.35, dy: 0.4), to: CGVector(dx: 0.65, dy: 0.6))176        shot("03-annotated")177        element("ink.undo").click(); element("ink.redo").click()178        element("project.info").click()179        sleep(1)180        shot("04-inspector")181        element("project.export").click()182        XCTAssertTrue(element("export.start").waitForExistence(timeout: 10))183        shot("05-export-sheet")184        element("export.start").click()185        XCTAssertTrue(element("export.file.pdf").waitForExistence(timeout: 60), "annotated PDF was not exported")186        shot("06-exported")187        element("export.close").click()188    }189    #endif190}191
Files main
Menu

Theme