SPB Git

Repository Public

ScannerUITests.swift 8.4 KB 186 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP ID & Card Vault7// File             : SPVaultUITests/ScannerUITests.swift8// Module           : Tests9// Repository       : https://git.spboucher.ai/sp-id-card-vault10// Project Website  : Not specified11// Created          : 2026-12-0212// Last Modified    : 2026-12-0213//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import XCTest4243/// End-to-end scanner runs in the Simulator, where the camera is replaced by44/// fictitious demo cards photographed by `SimulatedCamera`. Everything after the45/// shutter (detection, perspective, OCR, security-code mask, preview) is the46/// real pipeline.47///48/// The app is launched with `-screen scanner`; the unlocked shell is expected to49/// present `ScannerFlowView` for it. If it does not, the tests are skipped.50@MainActor51final class ScannerUITests: XCTestCase {52    private var app: XCUIApplication!53    private var shot = 05455    override func setUp() async throws {56        continueAfterFailure = false57        app = XCUIApplication()58        app.launchArguments = ["-uiTesting", "-seedDemo", "-screen", "scanner"]59        app.launch()60    }6162    /// Saves a screenshot when `SPV_SHOTS` names a directory (visual audit runs:63    /// `TEST_RUNNER_SPV_SHOTS=/path xcodebuild test …`).64    private func capture(_ name: String) {65        guard let directory = ProcessInfo.processInfo.environment["SPV_SHOTS"] else { return }66        shot += 167        let url = URL(fileURLWithPath: directory).appendingPathComponent(String(format: "%02d-%@.png", shot, name))68        try? FileManager.default.createDirectory(atPath: directory, withIntermediateDirectories: true)69        try? app.screenshot().pngRepresentation.write(to: url)70    }7172    private func element(_ identifier: String) -> XCUIElement {73        app.descendants(matching: .any).matching(identifier: identifier).firstMatch74    }7576    private func waitEnabled(_ identifier: String, timeout: TimeInterval = 30, file: StaticString = #filePath, line: UInt = #line) -> XCUIElement {77        let target = element(identifier)78        let ready = NSPredicate(format: "exists == true AND isEnabled == true")79        let result = XCTWaiter.wait(for: [XCTNSPredicateExpectation(predicate: ready, object: target)], timeout: timeout)80        XCTAssertEqual(result, .completed, "\(identifier) never became available", file: file, line: line)81        return target82    }8384    private func openScanner(kind: String) throws {85        let row = element("scanner.kind.\(kind)")86        guard element("scanner.kind.creditCard").waitForExistence(timeout: 20) else {87            throw XCTSkip("The unlocked shell does not present the scanner for `-screen scanner` yet.")88        }89        capture("kind-picker")90        for _ in 0..<6 where !row.isHittable { app.swipeUp() }91        row.tap()92    }9394    func testBankCardScanMasksTheSecurityCode() throws {95        try openScanner(kind: "creditCard")9697        // Front.98        waitEnabled("scanner.shutter").tap()99        let useFront = waitEnabled("scanner.usePhoto")100        capture("front-review")101        XCTAssertTrue(element("scanner.corner.0").exists, "manual crop handles are offered")102        XCTAssertTrue(element("scanner.retake").exists)103        useFront.tap()104105        // Back: capture, then the mask must be validated before anything else.106        _ = waitEnabled("scanner.shutter")107        XCTAssertTrue(element("scanner.noBack").exists)108        capture("back-capture")109        element("scanner.shutter").tap()110        waitEnabled("scanner.usePhoto").tap()111112        let validate = waitEnabled("scanner.maskValidated")113        capture("mask-validation")114        XCTAssertTrue(element("scanner.mask").exists, "a security-code mask is proposed on the demo card")115        XCTAssertFalse(element("scanner.noSecurityCode").isEnabled, "cannot declare 'no code' while a mask is placed")116        XCTAssertFalse(element("scanner.continue").exists, "the preview is not reachable before validation")117        validate.tap()118119        // Preview with proposals.120        let proceed = waitEnabled("scanner.continue")121        capture("preview-front")122        XCTAssertTrue(app.staticTexts["Card Preview"].exists)123        XCTAssertTrue(app.staticTexts["•••• 4242"].exists, "only the last four digits are shown")124        XCTAssertTrue(app.staticTexts["Alex Specimen"].exists)125        XCTAssertTrue(app.staticTexts["Never stored"].exists)126        XCTAssertFalse(app.staticTexts.containing(NSPredicate(format: "label CONTAINS '737'")).firstMatch.exists, "the security code appears nowhere")127        element("scanner.flip").tap()128        capture("preview-back")129        proceed.tap()130131        // The flow hands over its draft and closes.132        let gone = NSPredicate(format: "exists == false")133        XCTAssertEqual(XCTWaiter.wait(for: [XCTNSPredicateExpectation(predicate: gone, object: element("scanner.continue"))], timeout: 10), .completed)134    }135136    func testMaskCanBeRemovedOnlyByAnExplicitChoice() throws {137        try openScanner(kind: "debitCard")138        waitEnabled("scanner.shutter").tap()139        waitEnabled("scanner.usePhoto").tap()140        waitEnabled("scanner.shutter").tap()141        waitEnabled("scanner.usePhoto").tap()142143        _ = waitEnabled("scanner.maskValidated")144        // Remove every proposed mask: validation is no longer possible…145        for _ in 0..<4 where element("scanner.mask").exists {146            element("scanner.mask").tap()147            if element("scanner.mask.remove").waitForExistence(timeout: 3) { element("scanner.mask.remove").tap() }148        }149        XCTAssertFalse(element("scanner.mask").exists)150        XCTAssertFalse(element("scanner.maskValidated").isEnabled)151        // …a mask can be added back by hand…152        element("scanner.addMask").tap()153        XCTAssertTrue(element("scanner.mask").waitForExistence(timeout: 3))154        XCTAssertTrue(element("scanner.maskValidated").isEnabled)155        capture("mask-added-by-hand")156        element("scanner.mask").tap()157        element("scanner.mask.remove").tap()158        // …or the owner states explicitly that there is no code on this side.159        let none = waitEnabled("scanner.noSecurityCode")160        none.tap()161        _ = waitEnabled("scanner.continue")162    }163164    func testIdentityDocumentWithoutBack() throws {165        try openScanner(kind: "driverLicense")166        waitEnabled("scanner.shutter").tap()167        waitEnabled("scanner.usePhoto").tap()168        waitEnabled("scanner.noBack").tap()169170        _ = waitEnabled("scanner.continue")171        capture("preview-licence")172        XCTAssertFalse(element("scanner.maskValidated").exists, "no mask step for an identity document")173        XCTAssertFalse(element("scanner.flip").exists, "nothing to turn over without a back")174        XCTAssertTrue(app.staticTexts["Alex Specimen"].exists)175        XCTAssertTrue(app.staticTexts["ID-1 · 85.60 × 53.98 mm"].exists)176    }177178    func testCancelClosesTheScanner() throws {179        try openScanner(kind: "loyaltyCard")180        _ = waitEnabled("scanner.shutter")181        element("scanner.close").tap()182        let gone = NSPredicate(format: "exists == false")183        XCTAssertEqual(XCTWaiter.wait(for: [XCTNSPredicateExpectation(predicate: gone, object: element("scanner.shutter"))], timeout: 10), .completed)184    }185}186
Files main
Menu

Theme