From 604eb7a72f657116838ee922fc8f563bdcf1b4b5 Mon Sep 17 00:00:00 2001 From: dcvz Date: Thu, 11 Apr 2024 14:36:35 +0200 Subject: [PATCH] Fix tests --- Sources/SwiftAudioEx/AudioPlayer.swift | 2 +- Tests/SwiftAudioExTests/AudioPlayerTests.swift | 4 ++-- .../QueuedAudioPlayerTests.swift | 15 ++++++++++++--- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Sources/SwiftAudioEx/AudioPlayer.swift b/Sources/SwiftAudioEx/AudioPlayer.swift index 4e4d023..966c295 100755 --- a/Sources/SwiftAudioEx/AudioPlayer.swift +++ b/Sources/SwiftAudioEx/AudioPlayer.swift @@ -65,7 +65,7 @@ public class AudioPlayer: AVPlayerWrapperDelegate { try action() - if playWhenReady == true { + if playWhenReady == true, playbackError == nil { self.playWhenReady = true } } diff --git a/Tests/SwiftAudioExTests/AudioPlayerTests.swift b/Tests/SwiftAudioExTests/AudioPlayerTests.swift index f71bb2e..b84b4d8 100644 --- a/Tests/SwiftAudioExTests/AudioPlayerTests.swift +++ b/Tests/SwiftAudioExTests/AudioPlayerTests.swift @@ -224,7 +224,7 @@ class AudioPlayerTests: XCTestCase { self.audioPlayer.load(item: Source.getAudioItem(), playWhenReady: true) waitTrue(self.audioPlayer.playbackError == nil, timeout: 5) - waitEqual(self.playerStateEventListener.statesWithoutBuffering, [.loading, .failed, .loading, .playing], timeout: 5) + waitEqual(self.playerStateEventListener.statesWithoutBuffering, [.loading, .failed, .idle, .loading, .playing], timeout: 5) } func testLoadResourceSucceedsAfterPreviousFailureWithPlayWhenReady() { @@ -411,7 +411,7 @@ class AudioPlayerTests: XCTestCase { func testSeekingBeforeLoadingComplete() { audioPlayer.load(item: FiveSecondSource.getAudioItem(), playWhenReady: true) - XCTAssertTrue(audioPlayer.playerState == .loading) + XCTAssertTrue(audioPlayer.playerState == .buffering) audioPlayer.seek(to: 4.75) waitTrue(self.audioPlayer.currentTime > 4.75, timeout: 5) } diff --git a/Tests/SwiftAudioExTests/QueuedAudioPlayerTests.swift b/Tests/SwiftAudioExTests/QueuedAudioPlayerTests.swift index 2dcff06..453624d 100644 --- a/Tests/SwiftAudioExTests/QueuedAudioPlayerTests.swift +++ b/Tests/SwiftAudioExTests/QueuedAudioPlayerTests.swift @@ -81,9 +81,9 @@ class QueuedAudioPlayerTests: XCTestCase { func testLoadAfterRemoval() { testRemovingItemAfterAdding() - audioPlayer.load(item: Source.getAudioItem()) + audioPlayer.load(item: Source.getAudioItem(), playWhenReady: true) XCTAssertNotEqual(audioPlayer.currentItem?.getSourceUrl(), FiveSecondSource.getAudioItem().getSourceUrl()) - waitEqual(self.playerStateEventListener.statesWithoutBuffering, [.loading, .idle, .loading, .playing], timeout: 5) + waitTrue(self.playerStateEventListener.statesWithoutBuffering.contains(.playing), timeout: 5) XCTAssertEqual(audioPlayer.playerState, AudioPlayerState.playing) } @@ -100,7 +100,16 @@ class QueuedAudioPlayerTests: XCTestCase { XCTAssertEqual(audioPlayer.items.count, 1) XCTAssertEqual(audioPlayer.currentItem?.getSourceUrl(), ShortSource.getAudioItem().getSourceUrl()) } - + + // Covers: https://github.com/doublesymmetry/SwiftAudioEx/pull/81 + func testAddingItemWhenOnlyOneTrackInQueue() throws { + audioPlayer.add(item: FiveSecondSource.getAudioItem()) + audioPlayer.play() + try audioPlayer.add(items: [ShortSource.getAudioItem()], at: 0) + XCTAssertEqual(audioPlayer.items.count, 2) + XCTAssertEqual(audioPlayer.currentIndex, 1) + } + // MARK: - Next Items func testNextItemsEmptyOnCreate() {