* migrated a lot of unit tests to Swift Testing and added a new implementation for deferred fulfillment more tests migration Cleaned the code manually to establish some good patterns more code improvements some more code improvements removed empty tests update project * more pr suggestions and cleanups * removed the TestSetup pattern * fixing claude not reusing tests * pr suggestion + added indent rule to swiftformat so that we can prevent AIs to change that
86 lines
3.2 KiB
Swift
86 lines
3.2 KiB
Swift
//
|
|
// Copyright 2025 Element Creations Ltd.
|
|
// Copyright 2023-2025 New Vector Ltd.
|
|
//
|
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
|
// Please see LICENSE files in the repository root for full details.
|
|
//
|
|
|
|
import Combine
|
|
@testable import ElementX
|
|
import Foundation
|
|
import Testing
|
|
|
|
@MainActor
|
|
@Suite
|
|
struct MediaPlayerProviderTests {
|
|
private var mediaPlayerProvider: MediaPlayerProvider
|
|
|
|
private let oggMimeType = "audio/ogg"
|
|
private let someURL = URL.mockMXCAudio
|
|
private let someOtherURL = URL.mockMXCFile
|
|
|
|
init() async {
|
|
mediaPlayerProvider = MediaPlayerProvider()
|
|
}
|
|
|
|
@Test
|
|
func playerStates() {
|
|
let audioPlayerStateId = AudioPlayerStateIdentifier.timelineItemIdentifier(.randomEvent)
|
|
// By default, there should be no player state
|
|
#expect(mediaPlayerProvider.playerState(for: audioPlayerStateId) == nil)
|
|
|
|
let audioPlayerState = AudioPlayerState(id: audioPlayerStateId, title: "", duration: 10.0)
|
|
mediaPlayerProvider.register(audioPlayerState: audioPlayerState)
|
|
#expect(audioPlayerState == mediaPlayerProvider.playerState(for: audioPlayerStateId))
|
|
|
|
mediaPlayerProvider.unregister(audioPlayerState: audioPlayerState)
|
|
#expect(mediaPlayerProvider.playerState(for: audioPlayerStateId) == nil)
|
|
}
|
|
|
|
@Test
|
|
func detachAllStates() {
|
|
let audioPlayer = AudioPlayerMock()
|
|
audioPlayer.actions = PassthroughSubject<AudioPlayerAction, Never>().eraseToAnyPublisher()
|
|
|
|
let audioPlayerStates = Array(repeating: AudioPlayerState(id: .timelineItemIdentifier(.randomEvent), title: "", duration: 0), count: 10)
|
|
for audioPlayerState in audioPlayerStates {
|
|
mediaPlayerProvider.register(audioPlayerState: audioPlayerState)
|
|
audioPlayerState.attachAudioPlayer(audioPlayer)
|
|
let isAttached = audioPlayerState.isAttached
|
|
#expect(isAttached)
|
|
}
|
|
|
|
mediaPlayerProvider.detachAllStates(except: nil)
|
|
for audioPlayerState in audioPlayerStates {
|
|
let isAttached = audioPlayerState.isAttached
|
|
#expect(!isAttached)
|
|
}
|
|
}
|
|
|
|
@Test
|
|
func detachAllStatesWithException() {
|
|
let audioPlayer = AudioPlayerMock()
|
|
audioPlayer.actions = PassthroughSubject<AudioPlayerAction, Never>().eraseToAnyPublisher()
|
|
|
|
let audioPlayerStates = Array(repeating: AudioPlayerState(id: .timelineItemIdentifier(.randomEvent), title: "", duration: 0), count: 10)
|
|
for audioPlayerState in audioPlayerStates {
|
|
mediaPlayerProvider.register(audioPlayerState: audioPlayerState)
|
|
audioPlayerState.attachAudioPlayer(audioPlayer)
|
|
let isAttached = audioPlayerState.isAttached
|
|
#expect(isAttached)
|
|
}
|
|
|
|
let exception = audioPlayerStates[1]
|
|
mediaPlayerProvider.detachAllStates(except: exception)
|
|
for audioPlayerState in audioPlayerStates {
|
|
let isAttached = audioPlayerState.isAttached
|
|
if audioPlayerState == exception {
|
|
#expect(isAttached)
|
|
} else {
|
|
#expect(!isAttached)
|
|
}
|
|
}
|
|
}
|
|
}
|