SPB Git

Repository Public

SPMusicUITests.swift 11.0 KB 202 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : SP Music9// File             : UITests/SPMusicUITests.swift10// Description      : Interface tests on iPhone and iPad, with screenshots.11// Module           : UITests12// Repository       : https://git.spboucher.ai/sp-music-ios13// Created          : 2026-11-1714// Last Modified    : 2026-11-1715//16// =============================================================================17// AUTHOR & PROFESSIONAL AFFILIATION18// =============================================================================19//20// Author           : Simon-Pierre Boucher21// Position         : Professor of Finance22// Institution      : Université du Québec en Outaouais (UQO)23// Department       : Département des sciences administratives24// Organization     : SP Boucher25//26// Website          : https://spboucher.ai27// Git              : https://git.spboucher.ai28//29// =============================================================================30// OFFICIAL CONTACT DIRECTORY31// =============================================================================32//33// General          : info@spboucher.ai34// Professional     : contact@spboucher.ai35// Direct           : simon@spboucher.ai36// Research         : research@spboucher.ai37// Development      : dev@spboucher.ai38// Support          : support@spboucher.ai39// Security/Admin   : admin@spboucher.ai40// Notifications    : notifications@spboucher.ai41//42// Preferred Contact: support@spboucher.ai43//44// =============================================================================45// COPYRIGHT & INTELLECTUAL PROPERTY46// =============================================================================47//48// Copyright (c) 202649// Simon-Pierre Boucher50//51// All rights reserved where applicable.52//53// This file is governed by the applicable project license,54// copyright notices, institutional agreements and relevant law.55//56// Unless expressly authorized by the applicable license or57// rights holder, copying, redistribution, sublicensing,58// publication and commercial exploitation are not permitted.59//60// Existing third-party rights and notices remain unaffected.61//62// =============================================================================63// CONFIDENTIALITY & SECURITY64// =============================================================================65//66// Classification   : Public67// Distribution     : Public repository; use is governed by the project license68// Security Contact : admin@spboucher.ai69//70// Access to this source does not independently grant permission71// to redistribute or disclose confidential materials.72//73// Do not embed credentials, private keys, access tokens,74// certificates or confidential personal information.75//76// =============================================================================77// LICENSE & MAINTENANCE78// =============================================================================79//80// License          : None specified (no license file in this repository)81// Maintainer       : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import XCTest8990/// Drives the real interface on the simulator, against the disposable backend. Each step saves a screenshot to91/// build/shots/ (they are looked at, not just produced).92@MainActor93final class SPMusicUITests: XCTestCase {94    let env = ProcessInfo.processInfo.environment95    var isPad: Bool { UIDevice.current.userInterfaceIdiom == .pad }9697    override func setUp() async throws { continueAfterFailure = false }9899    private func launch(session: Bool, reset: Bool = true) throws -> XCUIApplication {100        let url = try XCTUnwrap(env["SPMUSIC_BASE_URL"], "test backend not configured")101        let app = XCUIApplication()102        app.launchEnvironment = ["SPMUSIC_BASE_URL": url, "SPMUSIC_MUTE": "1", "SPMUSIC_RESET": reset ? "1" : "0"]103        if session { app.launchEnvironment["SPMUSIC_TEST_SESSION"] = try XCTUnwrap(env["SPMUSIC_TEST_SESSION"]) }104        app.launch()105        return app106    }107    private func shot(_ app: XCUIApplication, _ name: String) {108        guard let dir = env["SPMUSIC_SHOTS"] else { return }109        try? FileManager.default.createDirectory(atPath: dir, withIntermediateDirectories: true)110        try? XCUIScreen.main.screenshot().pngRepresentation.write(to: URL(fileURLWithPath: dir).appendingPathComponent("\(isPad ? "ipad" : "iphone")-\(name).png"))111    }112    /// Opens a section: a tab on iPhone, a sidebar row on iPad.113    private func open(_ app: XCUIApplication, tab: String, sidebar: String? = nil) {114        if isPad {115            let row = app.collectionViews.staticTexts[sidebar ?? tab].firstMatch116            if !row.waitForExistence(timeout: 3) { app.buttons["ToggleSidebar"].firstMatch.tap() }117            XCTAssertTrue(row.waitForExistence(timeout: 5), "sidebar row \(sidebar ?? tab)"); row.tap()118        } else { app.tabBars.buttons[tab].firstMatch.tap() }119    }120121    func testBrowseDownloadPlayAndManage() throws {122        let app = try launch(session: true)123        XCTAssertTrue(app.staticTexts["library-counts"].waitForExistence(timeout: 20), "home never showed the synchronised library")124        XCTAssertTrue(app.staticTexts["library-counts"].label.contains("17 titres"))125        shot(app, "01-accueil")126127        // Library → Albums → album page128        if isPad { open(app, tab: "Albums") } else { open(app, tab: "Bibliothèque"); shot(app, "02-bibliotheque"); app.buttons["library-Albums"].firstMatch.tap() }129        let card = app.buttons.matching(NSPredicate(format: "label BEGINSWITH %@", "Fréquences")).firstMatch130        XCTAssertTrue(card.waitForExistence(timeout: 10)); shot(app, "03-albums")131        card.tap()132        XCTAssertTrue(app.staticTexts["album-title"].waitForExistence(timeout: 10))133        sleep(1); shot(app, "04-album")134        XCTAssertTrue(app.buttons.matching(NSPredicate(format: "label CONTAINS %@", "Onde 262 Hz")).firstMatch.waitForExistence(timeout: 5) || app.staticTexts["Onde 262 Hz"].firstMatch.exists, "the tracks of the album are listed")135136        // Download the album and wait for it to be on the device137        let download = app.buttons["download-album"].firstMatch138        XCTAssertTrue(download.waitForExistence(timeout: 5)); download.tap()139        let done = NSPredicate(format: "label == %@", "Téléchargé")140        expectation(for: done, evaluatedWith: download); waitForExpectations(timeout: 60)141        shot(app, "05-album-telecharge")142143        // Play it: mini player, then the full player144        app.buttons["play-all"].firstMatch.tap()145        let mini = app.buttons["mini-player"].firstMatch146        XCTAssertTrue(mini.waitForExistence(timeout: 10)); shot(app, "06-mini-lecteur")147        mini.tap()148        XCTAssertTrue(app.staticTexts["np-title"].waitForExistence(timeout: 10))149        XCTAssertEqual(app.staticTexts["np-title"].label, "Onde 220 Hz")150        let badge = app.buttons["source-badge"].firstMatch151        XCTAssertTrue(badge.waitForExistence(timeout: 10))152        expectation(for: NSPredicate(format: "label BEGINSWITH %@", "Sur l'appareil"), evaluatedWith: badge); waitForExpectations(timeout: 15)153        sleep(3)                                                    // let it play: the elapsed time must move154        if !isPad { XCTAssertNotEqual(app.staticTexts["elapsed"].firstMatch.label, "0:00") }155        shot(app, "07-lecteur")156        app.buttons["next"].firstMatch.tap()157        expectation(for: NSPredicate(format: "label == %@", "Onde 262 Hz"), evaluatedWith: app.staticTexts["np-title"]); waitForExpectations(timeout: 10)158        app.buttons["queue-button"].firstMatch.tap()159        XCTAssertTrue(app.descendants(matching: .any).matching(identifier: "queue-row").firstMatch.waitForExistence(timeout: 5)); shot(app, "08-file-attente")160        app.buttons["play-pause"].firstMatch.tap()                  // pause161        app.buttons["close-player"].firstMatch.tap()162        XCTAssertTrue(app.buttons["mini-player"].firstMatch.waitForExistence(timeout: 5)); sleep(1)      // the player has finished closing163164        // Downloads and storage165        open(app, tab: "Téléchargements")166        XCTAssertTrue(app.staticTexts["Stockage"].firstMatch.waitForExistence(timeout: 10)); sleep(1); shot(app, "10-telechargements")167        XCTAssertTrue(app.staticTexts["Fréquences"].firstMatch.exists || app.buttons.matching(NSPredicate(format: "label CONTAINS %@", "Fréquences")).firstMatch.exists, "the downloaded album is listed")168        app.staticTexts["Stockage"].firstMatch.tap()169        XCTAssertTrue(app.staticTexts["Titres téléchargés"].waitForExistence(timeout: 10)); shot(app, "11-stockage")170171        // Settings172        open(app, tab: "Réglages"); XCTAssertTrue(app.staticTexts["Qualité audio"].firstMatch.waitForExistence(timeout: 10)); shot(app, "12-reglages")173        app.staticTexts["Données cellulaires et téléchargements"].firstMatch.tap()174        XCTAssertTrue(app.switches.firstMatch.waitForExistence(timeout: 5)); shot(app, "13-donnees-cellulaires")175176        // Search (on the device's copy of the library)177        if isPad { open(app, tab: "Recherche") } else { app.tabBars.buttons["Recherche"].firstMatch.tap() }178        let field = app.searchFields.firstMatch179        XCTAssertTrue(field.waitForExistence(timeout: 10)); field.tap(); field.typeText("eclat")180        XCTAssertTrue(app.staticTexts["Éclat 1"].firstMatch.waitForExistence(timeout: 10)); shot(app, "09-recherche")181    }182183    func testSignInScreenAndRecoveryCode() throws {184        let app = try launch(session: false)185        XCTAssertTrue(app.buttons["login-passkey"].waitForExistence(timeout: 20)); shot(app, "20-connexion")186        app.buttons["login-recovery"].tap()187        let field = app.textFields["code-field"].firstMatch.exists ? app.textFields["code-field"].firstMatch : app.textViews["code-field"].firstMatch188        XCTAssertTrue(field.waitForExistence(timeout: 5)); field.tap(); field.typeText("zzzz-zzzz-zzzz")189        app.buttons["code-submit"].tap()190        XCTAssertTrue(app.staticTexts["code-error"].waitForExistence(timeout: 10)); shot(app, "21-code-refuse")     // a wrong code is refused, in words191        field.tap(); field.clearText(); field.typeText(try XCTUnwrap(env["SPMUSIC_TEST_RECOVERY_2"]))192        app.buttons["code-submit"].tap()193        XCTAssertTrue(app.staticTexts["library-counts"].waitForExistence(timeout: 25), "signed in, but the library never appeared")194        shot(app, "22-apres-connexion")195    }196}197198extension XCUIElement {199    func tapIfExists() { if exists && isHittable { tap() } }200    func clearText() { guard let v = value as? String, !v.isEmpty else { return }; typeText(String(repeating: XCUIKeyboardKey.delete.rawValue, count: v.count)) }201}202
Files main
Menu

Theme