SPB Git

Repository Public MIT

GlassAudioPlayerTests.swift 7.0 KB 160 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Tests/SPGlassMediaTests/GlassAudioPlayerTests.swift8// Module           : Tests9// Repository       : https://git.spboucher.ai/sp-glasskit10// Project Website  : https://glass.spboucher.ai11// 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          : MIT33//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 Foundation42import AVFoundation43import Observation44import Testing45import SPGlassCore46@testable import SPGlassMedia4748/// A playback engine without audio, as a host would write for previews.49@MainActor50@Observable51final class FakeAudioPlayback: GlassAudioPlayback {52    var isPlaying = false53    var currentTime: TimeInterval = 054    var duration: TimeInterval55    var isAvailable: Bool56    var seeks: [TimeInterval] = []5758    init(duration: TimeInterval = 200, isAvailable: Bool = true) {59        self.duration = duration60        self.isAvailable = isAvailable61    }6263    func play() { if isAvailable { isPlaying = true } }64    func pause() { isPlaying = false }65    func seek(to time: TimeInterval) {66        seeks.append(time)67        currentTime = time68    }69}7071@Suite("Audio player")72struct GlassAudioPlayerTests {73    private let english = Locale(identifier: "en_US")7475    @Test func seekTargetsAreClampedToTheDuration() {76        #expect(GlassPlaybackMath.clampedSeek(50, duration: 200) == 50)77        #expect(GlassPlaybackMath.clampedSeek(-5, duration: 200) == 0)78        #expect(GlassPlaybackMath.clampedSeek(900, duration: 200) == 200)79        #expect(GlassPlaybackMath.clampedSeek(.nan, duration: 200) == 0)80        #expect(GlassPlaybackMath.clampedSeek(.infinity, duration: 200) == 200)81        #expect(GlassPlaybackMath.clampedSeek(10, duration: 0) == 0)82        #expect(GlassPlaybackMath.clampedSeek(10, duration: .nan) == 0)83        #expect(GlassPlaybackMath.clampedSeek(10, duration: .infinity) == 0)84    }8586    @Test func skippingStaysInsideTheMedia() {87        #expect(GlassPlaybackMath.skip(from: 100, by: 15, duration: 200) == 115)88        #expect(GlassPlaybackMath.skip(from: 5, by: -15, duration: 200) == 0)89        #expect(GlassPlaybackMath.skip(from: 195, by: 15, duration: 200) == 200)90        #expect(GlassPlaybackMath.skip(from: 100, by: .nan, duration: 200) == 100)91    }9293    @Test func fractionAndRemainingTime() {94        #expect(GlassPlaybackMath.fraction(currentTime: 50, duration: 200) == 0.25)95        #expect(GlassPlaybackMath.fraction(currentTime: 50, duration: 0) == 0)96        #expect(GlassPlaybackMath.fraction(currentTime: 500, duration: 200) == 1)97        #expect(GlassPlaybackMath.remaining(currentTime: 50, duration: 200) == 150)98        #expect(GlassPlaybackMath.remaining(currentTime: 500, duration: 200) == 0)99        #expect(GlassPlaybackMath.remaining(currentTime: 5, duration: -1) == 0)100    }101102    @Test func timesUseTheDurationFormatStyle() {103        #expect(GlassPlaybackMath.formatted(0, locale: english) == "0:00")104        #expect(GlassPlaybackMath.formatted(65.9, locale: english) == "1:05")105        #expect(GlassPlaybackMath.formatted(3725, locale: english) == "1:02:05")106        #expect(GlassPlaybackMath.formatted(-4, locale: english) == "0:00")107        #expect(GlassPlaybackMath.formatted(.nan, locale: english) == "0:00")108        #expect(GlassPlaybackMath.formatted(.infinity, locale: english) == "0:00")109        #expect(GlassPlaybackMath.formattedRemaining(currentTime: 135, duration: 200, locale: english) == "\u{2212}1:05")110    }111112    @Test func spokenPositionNamesBothTimes() {113        let spoken = GlassPlaybackMath.spokenPosition(currentTime: 65, duration: 200, locale: english)114        #expect(spoken.contains("1:05"))115        #expect(spoken.contains("3:20"))116    }117118    @Test func skipSymbolsAreNumberedWhenSFSymbolsHasThem() {119        #expect(GlassAudioPlayerSymbols.skip(15, forward: true) == "goforward.15")120        #expect(GlassAudioPlayerSymbols.skip(30, forward: false) == "gobackward.30")121        #expect(GlassAudioPlayerSymbols.skip(20, forward: true) == "goforward")122        #expect(GlassAudioPlayerSymbols.skip(7, forward: false) == "gobackward")123    }124125    @Test func interruptionsPauseAndResumeOnlyWhenAppropriate() {126        typealias Policy = GlassAudioInterruptionPolicy127        #expect(Policy.action(for: .began, wasPlayingWhenInterrupted: true, resumesAfterInterruption: true) == .pause)128        #expect(Policy.action(for: .ended(shouldResume: true), wasPlayingWhenInterrupted: true, resumesAfterInterruption: true) == .resume)129        #expect(Policy.action(for: .ended(shouldResume: false), wasPlayingWhenInterrupted: true, resumesAfterInterruption: true) == .none)130        #expect(Policy.action(for: .ended(shouldResume: true), wasPlayingWhenInterrupted: false, resumesAfterInterruption: true) == .none)131        #expect(Policy.action(for: .ended(shouldResume: true), wasPlayingWhenInterrupted: true, resumesAfterInterruption: false) == .none)132    }133134    @MainActor135    @Test func aFakePlaybackDrivesTheProtocol() {136        let playback = FakeAudioPlayback()137        #expect(!playback.isPlaying)138        playback.play()139        #expect(playback.isPlaying)140        playback.seek(to: GlassPlaybackMath.skip(from: playback.currentTime, by: -15, duration: playback.duration))141        playback.seek(to: GlassPlaybackMath.skip(from: 190, by: 15, duration: playback.duration))142        #expect(playback.seeks == [0, 200])143        let unavailable = FakeAudioPlayback(isAvailable: false)144        unavailable.play()145        #expect(!unavailable.isPlaying)146    }147148    @MainActor149    @Test func aPlayerWithoutItemIsUnavailableAndNeverStarts() {150        let playback = GlassAVAudioPlayback(player: AVPlayer())151        #expect(!playback.isAvailable)152        #expect(!playback.isPlaying)153        #expect(playback.duration == 0)154        playback.play()155        playback.seek(to: 30)156        #expect(playback.player.rate == 0)157        #expect(playback.currentTime == 0)158    }159}160
Files main
Menu

Theme