2023-10-23 17:47:36 +02:00
|
|
|
//
|
2024-09-06 16:34:30 +03:00
|
|
|
// Copyright 2023, 2024 New Vector Ltd.
|
2023-10-23 17:47:36 +02:00
|
|
|
//
|
2025-01-06 11:27:37 +01:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
|
|
// Please see LICENSE files in the repository root for full details.
|
2023-10-23 17:47:36 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
import Combine
|
|
|
|
@testable import ElementX
|
|
|
|
import Foundation
|
|
|
|
import XCTest
|
|
|
|
|
2023-10-24 18:49:42 +02:00
|
|
|
@MainActor
|
2023-10-23 17:47:36 +02:00
|
|
|
class MediaPlayerProviderTests: XCTestCase {
|
|
|
|
private var mediaPlayerProvider: MediaPlayerProvider!
|
|
|
|
|
|
|
|
private let oggMimeType = "audio/ogg"
|
2024-11-28 08:34:38 +00:00
|
|
|
private let someURL = URL.mockMXCAudio
|
|
|
|
private let someOtherURL = URL.mockMXCFile
|
2023-10-23 17:47:36 +02:00
|
|
|
|
|
|
|
override func setUp() async throws {
|
|
|
|
mediaPlayerProvider = MediaPlayerProvider()
|
|
|
|
}
|
|
|
|
|
|
|
|
func testPlayerStates() async throws {
|
2024-10-16 14:07:54 +03:00
|
|
|
let audioPlayerStateId = AudioPlayerStateIdentifier.timelineItemIdentifier(.randomEvent)
|
2023-10-23 17:47:36 +02:00
|
|
|
// By default, there should be no player state
|
|
|
|
XCTAssertNil(mediaPlayerProvider.playerState(for: audioPlayerStateId))
|
|
|
|
|
2024-09-06 12:57:20 +03:00
|
|
|
let audioPlayerState = AudioPlayerState(id: audioPlayerStateId, title: "", duration: 10.0)
|
2023-10-24 18:49:42 +02:00
|
|
|
mediaPlayerProvider.register(audioPlayerState: audioPlayerState)
|
2023-10-23 17:47:36 +02:00
|
|
|
XCTAssertEqual(audioPlayerState, mediaPlayerProvider.playerState(for: audioPlayerStateId))
|
|
|
|
|
2023-10-24 18:49:42 +02:00
|
|
|
mediaPlayerProvider.unregister(audioPlayerState: audioPlayerState)
|
2023-10-23 17:47:36 +02:00
|
|
|
XCTAssertNil(mediaPlayerProvider.playerState(for: audioPlayerStateId))
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDetachAllStates() async throws {
|
|
|
|
let audioPlayer = AudioPlayerMock()
|
|
|
|
audioPlayer.actions = PassthroughSubject<AudioPlayerAction, Never>().eraseToAnyPublisher()
|
|
|
|
|
2024-10-16 14:07:54 +03:00
|
|
|
let audioPlayerStates = Array(repeating: AudioPlayerState(id: .timelineItemIdentifier(.randomEvent), title: "", duration: 0), count: 10)
|
2023-10-23 17:47:36 +02:00
|
|
|
for audioPlayerState in audioPlayerStates {
|
2023-10-24 18:49:42 +02:00
|
|
|
mediaPlayerProvider.register(audioPlayerState: audioPlayerState)
|
|
|
|
audioPlayerState.attachAudioPlayer(audioPlayer)
|
|
|
|
let isAttached = audioPlayerState.isAttached
|
2023-10-23 17:47:36 +02:00
|
|
|
XCTAssertTrue(isAttached)
|
|
|
|
}
|
|
|
|
|
2023-10-24 18:49:42 +02:00
|
|
|
mediaPlayerProvider.detachAllStates(except: nil)
|
2023-10-23 17:47:36 +02:00
|
|
|
for audioPlayerState in audioPlayerStates {
|
2023-10-24 18:49:42 +02:00
|
|
|
let isAttached = audioPlayerState.isAttached
|
2023-10-23 17:47:36 +02:00
|
|
|
XCTAssertFalse(isAttached)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDetachAllStatesWithException() async throws {
|
|
|
|
let audioPlayer = AudioPlayerMock()
|
|
|
|
audioPlayer.actions = PassthroughSubject<AudioPlayerAction, Never>().eraseToAnyPublisher()
|
|
|
|
|
2024-10-16 14:07:54 +03:00
|
|
|
let audioPlayerStates = Array(repeating: AudioPlayerState(id: .timelineItemIdentifier(.randomEvent), title: "", duration: 0), count: 10)
|
2023-10-23 17:47:36 +02:00
|
|
|
for audioPlayerState in audioPlayerStates {
|
2023-10-24 18:49:42 +02:00
|
|
|
mediaPlayerProvider.register(audioPlayerState: audioPlayerState)
|
|
|
|
audioPlayerState.attachAudioPlayer(audioPlayer)
|
|
|
|
let isAttached = audioPlayerState.isAttached
|
2023-10-23 17:47:36 +02:00
|
|
|
XCTAssertTrue(isAttached)
|
|
|
|
}
|
|
|
|
|
|
|
|
let exception = audioPlayerStates[1]
|
2023-10-24 18:49:42 +02:00
|
|
|
mediaPlayerProvider.detachAllStates(except: exception)
|
2023-10-23 17:47:36 +02:00
|
|
|
for audioPlayerState in audioPlayerStates {
|
2023-10-24 18:49:42 +02:00
|
|
|
let isAttached = audioPlayerState.isAttached
|
2023-10-23 17:47:36 +02:00
|
|
|
if audioPlayerState == exception {
|
|
|
|
XCTAssertTrue(isAttached)
|
|
|
|
} else {
|
|
|
|
XCTAssertFalse(isAttached)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|