Repository Public License
1// ORIX Scan2// File: UITests/ORIXScanUITests.swift3// Description: UI tests on the running app (isolated store): first launch, library, document, text, export, actions, persistence, appearance.4// Screenshots are attached to the result bundle; the README screenshots come from these runs.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import XCTest910final class ORIXScanUITests: XCTestCase {11 private var app: XCUIApplication!12 private var isPad: Bool { UIDevice.current.userInterfaceIdiom == .pad }1314 override func setUp() {15 continueAfterFailure = false16 XCUIDevice.shared.appearance = .light17 if isPad { XCUIDevice.shared.orientation = .landscapeLeft }18 }1920 private func launch(reset: Bool = true, demo: Bool = true, extra: [String] = []) {21 app = XCUIApplication()22 app.launchArguments = ["-ORIXUITest", "YES", "-ORIXReset", reset ? "YES" : "NO", "-ORIXDemo", demo ? "YES" : "NO", "-AppleLanguages", "(en)"] + extra23 app.launch()24 XCTAssertTrue(app.buttons["home.scan"].waitForExistence(timeout: 90), "home did not appear")25 }2627 private func shot(_ name: String) {28 let attachment = XCTAttachment(screenshot: XCUIScreen.main.screenshot())29 attachment.name = name30 attachment.lifetime = .keepAlways31 add(attachment)32 }3334 private func showLibrary() {35 if isPad {36 // Leave any pushed screen first: the sidebar item may already be selected.37 popToRoot()38 sidebar("sidebar.home")39 sidebar("sidebar.library")40 } else {41 app.tabBars.buttons["Library"].firstMatch.tap()42 }43 }4445 private func showActions() {46 if isPad { popToRoot(); sidebar("sidebar.actions") } else { app.tabBars.buttons["Actions"].firstMatch.tap() }47 }4849 private func sidebar(_ identifier: String) {50 let item = app.staticTexts[identifier].firstMatch51 XCTAssertTrue(item.waitForExistence(timeout: 10), "\(identifier) missing")52 item.tap()53 }5455 private func popToRoot() {56 for _ in 0..<3 {57 let back = app.navigationBars.buttons["BackButton"].firstMatch58 guard back.exists, back.isHittable else { return }59 back.tap()60 }61 }6263 private func openDocument(_ title: String) {64 showLibrary()65 let card = app.buttons.matching(identifier: "library.document").containing(NSPredicate(format: "label CONTAINS %@", title)).firstMatch66 XCTAssertTrue(card.waitForExistence(timeout: 15), "\(title) not in library")67 card.tap()68 XCTAssertTrue(app.buttons["document.export"].waitForExistence(timeout: 15))69 }7071 // MARK: First launch7273 func testFirstLaunchIsUsefulImmediately() {74 launch(demo: false)75 // No onboarding, no account, no permission prompt: the primary action is on screen.76 XCTAssertTrue(app.buttons["home.scan"].isHittable)77 XCTAssertTrue(app.buttons["home.importPhotos"].exists)78 XCTAssertTrue(app.buttons["home.importFiles"].exists)79 XCTAssertTrue(app.descendants(matching: .any)["home.empty"].exists)80 XCTAssertEqual(app.alerts.count, 0)81 shot("first-launch")82 // The Simulator has no camera: the app says so instead of failing.83 app.buttons["home.scan"].tap()84 let alert = app.alerts["Scanner Unavailable"]85 XCTAssertTrue(alert.waitForExistence(timeout: 5))86 alert.buttons["OK"].tap()87 showLibrary()88 XCTAssertTrue(app.buttons["library.empty.scan"].waitForExistence(timeout: 10))89 shot("library-empty")90 }9192 // MARK: Library9394 func testLibrarySearchDeleteAndRestore() {95 launch()96 shot("home")97 showLibrary()98 let documents = app.buttons.matching(identifier: "library.document")99 XCTAssertTrue(documents.firstMatch.waitForExistence(timeout: 15))100 XCTAssertEqual(documents.count, 5)101 shot("library")102103 // Batch selection, then Delete.104 app.buttons["library.options"].firstMatch.tap()105 app.buttons["Select"].firstMatch.tap()106 documents.matching(NSPredicate(format: "label CONTAINS 'Café Receipt'")).firstMatch.tap()107 app.buttons["Delete"].firstMatch.tap()108 XCTAssertTrue(waitForCount(documents, 4))109 if isPad {110 sidebar("sidebar.trash")111 } else {112 app.tabBars.buttons["Home"].firstMatch.tap()113 let trash = app.buttons["home.library.trash"]114 for _ in 0..<4 where !trash.isHittable { app.swipeUp() }115 trash.tap()116 }117 XCTAssertTrue(documents.firstMatch.waitForExistence(timeout: 10))118 XCTAssertEqual(documents.count, 1)119 shot("recently-deleted")120 app.buttons["library.options"].firstMatch.tap()121 app.buttons["Select"].firstMatch.tap()122 documents.firstMatch.tap()123 app.buttons["Restore"].firstMatch.tap()124 XCTAssertTrue(waitForCount(documents, 0))125 if !isPad { app.navigationBars.buttons["BackButton"].firstMatch.tap() }126 showLibrary()127 XCTAssertTrue(waitForCount(documents, 5))128129 // Search reaches the recognized text, not only titles, and ignores accents.130 let search = app.searchFields.firstMatch131 search.tap()132 search.typeText("versement")133 XCTAssertTrue(documents.firstMatch.waitForExistence(timeout: 10))134 XCTAssertEqual(documents.count, 1)135 XCTAssertTrue(documents.firstMatch.label.contains("Hydro"))136 shot("library-search")137 // Delete goes to Recently Deleted and can be undone from there.138 }139140 private func waitForCount(_ query: XCUIElementQuery, _ count: Int, timeout: TimeInterval = 10) -> Bool {141 let deadline = Date().addingTimeInterval(timeout)142 while Date() < deadline {143 if query.count == count { return true }144 RunLoop.current.run(until: Date().addingTimeInterval(0.3))145 }146 return query.count == count147 }148149 // MARK: Document, text, export150151 func testTextSearchReviewTablesAndSearchablePDF() {152 launch()153 openDocument("Invoice 2026-041")154 shot("document")155156 app.buttons["document.text"].tap()157 let search = app.textFields["text.search"]158 XCTAssertTrue(search.waitForExistence(timeout: 20), "recognized text not shown")159 search.tap()160 search.typeText("design")161 let matches = app.staticTexts["text.matches"]162 XCTAssertTrue(matches.waitForExistence(timeout: 5))163 XCTAssertFalse(matches.label.hasPrefix("0 "), matches.label)164 shot("ocr-text")165 app.segmentedControls["text.mode"].buttons["Review"].tap()166 XCTAssertTrue(app.staticTexts.containing(NSPredicate(format: "label CONTAINS 'lines'")).firstMatch.waitForExistence(timeout: 10))167 shot("ocr-review")168 app.segmentedControls["text.mode"].buttons["Tables"].tap()169 XCTAssertTrue(app.buttons["tables.export"].waitForExistence(timeout: 10), "no table extracted from the invoice")170 shot("ocr-tables")171 app.buttons["Done"].firstMatch.tap()172173 // Export: real sizes for the three presets, then search inside the generated PDF.174 app.buttons["document.export"].tap()175 XCTAssertTrue(app.buttons["export.measure"].waitForExistence(timeout: 10))176 app.buttons["export.measure"].tap()177 for preset in ["maximum", "balanced", "small"] {178 XCTAssertTrue(app.staticTexts["export.size.\(preset)"].waitForExistence(timeout: 60), "\(preset) not measured")179 }180 shot("export")181 app.swipeUp()182 XCTAssertTrue(app.buttons["export.preview"].waitForExistence(timeout: 10))183 app.buttons["export.preview"].tap()184 let pdfSearch = app.textFields["preview.search"]185 XCTAssertTrue(pdfSearch.waitForExistence(timeout: 30))186 pdfSearch.tap()187 pdfSearch.typeText("Website prototype")188 let pdfMatches = app.staticTexts["preview.matches"]189 XCTAssertTrue(pdfMatches.waitForExistence(timeout: 5))190 XCTAssertEqual(pdfMatches.label, "1 of 1")191 shot("searchable-pdf")192 pdfSearch.typeText("zzz")193 XCTAssertEqual(pdfMatches.label, "No matches")194 }195196 func testPageEditorFiltersCompareAndCrop() {197 launch()198 openDocument("Lettre Hydro")199 if !isPad {200 app.buttons.matching(identifier: "document.page").firstMatch.tap()201 }202 let blackWhite = app.buttons["editor.filter.blackWhite"]203 XCTAssertTrue(blackWhite.waitForExistence(timeout: 15))204 blackWhite.tap()205 XCTAssertTrue(blackWhite.isSelected)206 app.buttons["editor.filter.color"].tap()207 app.descendants(matching: .any)["editor.compare"].firstMatch.tap()208 XCTAssertTrue(app.staticTexts["Before"].waitForExistence(timeout: 10))209 shot("enhancement")210 app.descendants(matching: .any)["editor.compare"].firstMatch.tap()211 app.buttons["editor.crop"].tap()212 XCTAssertTrue(app.buttons["crop.done"].waitForExistence(timeout: 10))213 shot("crop")214 app.buttons["crop.done"].tap()215 XCTAssertTrue(blackWhite.waitForExistence(timeout: 10))216 }217218 func testOrganizePagesAndSplit() {219 launch()220 openDocument("Quarterly Report")221 XCTAssertEqual(app.buttons.matching(identifier: "document.page").count, 3)222 app.buttons["document.organize"].tap()223 XCTAssertTrue(app.descendants(matching: .any).matching(NSPredicate(format: "label BEGINSWITH 'Page 3'")).firstMatch.waitForExistence(timeout: 10))224 shot("organize")225 app.buttons["Split"].firstMatch.tap()226 XCTAssertTrue(app.navigationBars["Split Document"].waitForExistence(timeout: 5))227 app.navigationBars["Split Document"].buttons["Split"].tap()228 showLibraryAfterSheet()229 let documents = app.buttons.matching(identifier: "library.document")230 XCTAssertTrue(waitForCount(documents, 7, timeout: 20), "expected 5 + 2 parts, found \(documents.count)")231 }232233 private func showLibraryAfterSheet() {234 if !isPad {235 let back = app.navigationBars.buttons["BackButton"].firstMatch236 if back.waitForExistence(timeout: 5) { back.tap() }237 }238 showLibrary()239 }240241 // MARK: ORIX Actions242243 func testRunActionOnDocumentAndBuildOne() {244 launch()245 showActions()246 XCTAssertTrue(app.buttons.matching(identifier: "actions.run").firstMatch.waitForExistence(timeout: 10))247 XCTAssertEqual(app.staticTexts.matching(NSPredicate(format: "label == 'Scan to Searchable PDF'")).count, 1)248 shot("actions")249 app.buttons["actions.new"].tap()250 XCTAssertTrue(app.buttons["workflow.addStep"].waitForExistence(timeout: 10))251 shot("action-builder")252 XCTAssertTrue(app.buttons["workflow.run"].isEnabled)253254 // Run a preset on a library document: every step really executes.255 openDocumentFromAnywhere("Meeting Notes")256 app.buttons["document.more"].tap()257 app.buttons["Run Action"].tap()258 app.buttons["Extract Text"].tap()259 XCTAssertTrue(app.staticTexts["run.finished"].waitForExistence(timeout: 90), "workflow did not finish")260 XCTAssertEqual(app.staticTexts["run.finished"].label, "Finished")261 XCTAssertTrue(app.buttons["run.share"].exists, "no exported text file")262 shot("action-run")263 app.buttons["run.close"].tap()264 }265266 private func openDocumentFromAnywhere(_ title: String) {267 if !isPad {268 let back = app.navigationBars.buttons["BackButton"].firstMatch269 if back.exists { back.tap() }270 }271 openDocument(title)272 }273274 // MARK: Persistence275276 func testDocumentsPersistAfterRelaunch() {277 launch()278 showLibrary()279 let documents = app.buttons.matching(identifier: "library.document")280 XCTAssertTrue(documents.firstMatch.waitForExistence(timeout: 15))281 let before = documents.count282 app.terminate()283 launch(reset: false, demo: false)284 showLibrary()285 XCTAssertTrue(waitForCount(app.buttons.matching(identifier: "library.document"), before, timeout: 20))286 openDocument("Invoice 2026-041")287 app.buttons["document.text"].tap()288 XCTAssertTrue(app.textFields["text.search"].waitForExistence(timeout: 20), "recognized text was not persisted")289 }290291 // MARK: Import292293 func testImportScannedPDFRecognizeAndSearch() throws {294 // An image-only PDF (no text layer) goes through the real import code, on-device recognition and export.295 let fixture = try XCTUnwrap(Bundle(for: Self.self).url(forResource: "ORIX Import Test", withExtension: "pdf"))296 app = XCUIApplication()297 app.launchArguments = ["-ORIXUITest", "YES", "-ORIXReset", "YES", "-ORIXDemo", "NO", "-AppleLanguages", "(en)", "-ORIXImport", fixture.path]298 app.launch()299 XCTAssertTrue(app.buttons["document.export"].waitForExistence(timeout: 90), "imported document did not open")300 let title = app.descendants(matching: .any).matching(NSPredicate(format: "label == 'ORIX Import Test' OR identifier == 'ORIX Import Test'")).firstMatch301 XCTAssertTrue(title.waitForExistence(timeout: 10), "the document should take the file's name")302 // Recognition starts by itself after import; the text screen shows the result when it is done.303 app.buttons["document.text"].tap()304 let search = app.textFields["text.search"]305 XCTAssertTrue(search.waitForExistence(timeout: 60), "imported page was not recognized")306 search.tap()307 search.typeText("lazy dog")308 XCTAssertTrue(app.staticTexts["text.matches"].label.hasPrefix("1 match"), app.staticTexts["text.matches"].label)309 app.buttons["Done"].firstMatch.tap()310 app.buttons["document.export"].tap()311 app.swipeUp()312 XCTAssertTrue(app.buttons["export.preview"].waitForExistence(timeout: 10))313 app.buttons["export.preview"].tap()314 let pdfSearch = app.textFields["preview.search"]315 XCTAssertTrue(pdfSearch.waitForExistence(timeout: 60))316 pdfSearch.tap()317 pdfSearch.typeText("image without")318 XCTAssertTrue(app.staticTexts["preview.matches"].waitForExistence(timeout: 5))319 XCTAssertEqual(app.staticTexts["preview.matches"].label, "1 of 1")320 shot("import-searchable-pdf")321 }322323 // MARK: Launch time324325 func testColdLaunchTime() {326 // Empty library, isolated store. Reported by XCTest in the result bundle; see docs/development/performance.md.327 let options = XCTMeasureOptions()328 options.iterationCount = 5329 measure(metrics: [XCTApplicationLaunchMetric(waitUntilResponsive: true)], options: options) {330 let app = XCUIApplication()331 app.launchArguments = ["-ORIXUITest", "YES", "-ORIXReset", "YES", "-ORIXDemo", "NO"]332 app.launch()333 }334 }335336 // MARK: Appearance and accessibility337338 func testDarkMode() {339 XCUIDevice.shared.appearance = .dark340 launch()341 shot("home-dark")342 showLibrary()343 XCTAssertTrue(app.buttons.matching(identifier: "library.document").firstMatch.waitForExistence(timeout: 15))344 shot("library-dark")345 XCUIDevice.shared.appearance = .light346 }347348 func testLargestDynamicType() {349 launch(extra: ["-UIPreferredContentSizeCategoryName", "UICTContentSizeCategoryAccessibilityXXL"])350 XCTAssertTrue(app.buttons["home.scan"].isHittable)351 shot("home-accessibility-xxl")352 XCTAssertTrue(app.buttons["home.importPhotos"].exists)353 openDocument("Invoice 2026-041")354 XCTAssertTrue(app.buttons["document.export"].isHittable)355 shot("document-accessibility-xxl")356 }357358 func testFrenchLocalization() {359 app = XCUIApplication()360 app.launchArguments = ["-ORIXUITest", "YES", "-ORIXReset", "YES", "-ORIXDemo", "YES", "-AppleLanguages", "(fr-CA)", "-AppleLocale", "fr_CA"]361 app.launch()362 XCTAssertTrue(app.buttons["home.scan"].waitForExistence(timeout: 90))363 XCTAssertEqual(app.buttons["home.scan"].label, "Numériser un document")364 XCTAssertTrue(app.staticTexts["Documents récents"].exists)365 XCTAssertTrue(app.staticTexts["Importer des photos"].exists)366 shot("home-fr")367 if isPad { sidebar("sidebar.library") } else { app.tabBars.buttons["Bibliothèque"].firstMatch.tap() }368 XCTAssertTrue(app.buttons.matching(identifier: "library.document").firstMatch.waitForExistence(timeout: 15))369 shot("library-fr")370 }371372 func testSettingsListsVisionLanguages() {373 launch()374 if isPad {375 sidebar("sidebar.settings")376 } else {377 app.buttons["home.settings"].tap()378 }379 let languages = app.buttons["settings.languages"]380 XCTAssertTrue(languages.waitForExistence(timeout: 10))381 shot("settings")382 languages.tap()383 // The list comes from Vision on this runtime; English and French must be there.384 XCTAssertTrue(app.staticTexts["en-US"].waitForExistence(timeout: 10))385 XCTAssertTrue(app.staticTexts["fr-FR"].exists)386 shot("languages")387 }388}389