-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7e6942e
commit 33890a6
Showing
1 changed file
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
/* | ||
* Copyright (C) 2024 OpenAni and contributors. | ||
* | ||
* Use of this source code is governed by the GNU GENERAL PUBLIC LICENSE version 3 license, which can be found at the following link. | ||
* | ||
* https://github.com/open-ani/mediamp/blob/main/LICENSE | ||
*/ | ||
|
||
package org.openani.mediamp | ||
|
||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.test.runTest | ||
import org.openani.mediamp.features.PlaybackSpeed | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNull | ||
import kotlin.test.assertNotNull | ||
import org.openani.mediamp.source.UriMediaData | ||
import org.openani.mediamp.source.MediaExtraFiles | ||
|
||
/** | ||
* Tests for [DummyMediampPlayer]. | ||
*/ | ||
abstract class AbstractPlayerTest { | ||
|
||
abstract fun createMediampPlayer(): MediampPlayer | ||
|
||
@Test | ||
fun `initial state`() = runTest { | ||
val player = createMediampPlayer() | ||
// Initially, the player should be in CREATED state | ||
assertEquals(PlaybackState.CREATED, player.playbackState.value) | ||
|
||
// No media data yet | ||
val mediaData = player.mediaData.first() | ||
assertNull(mediaData) | ||
} | ||
|
||
@Test | ||
fun `set media`() = runTest { | ||
val player = createMediampPlayer() | ||
// Use UriMediaData for demonstration; this requires no real file | ||
val data = UriMediaData( | ||
uri = "file:///fake_video.mp4", | ||
headers = emptyMap(), | ||
extraFiles = MediaExtraFiles() | ||
) | ||
|
||
// Setting the media data should transition the state to READY | ||
player.setMediaData(data) | ||
assertEquals(PlaybackState.READY, player.playbackState.value) | ||
|
||
// mediaData flow should now emit the new MediaData | ||
val currentMediaData = player.mediaData.first() | ||
assertNotNull(currentMediaData) | ||
// Because we used UriMediaData, we can check if it's the same instance | ||
assertEquals(data, currentMediaData) | ||
} | ||
|
||
@Test | ||
fun `resume and pause`() = runTest { | ||
val player = createMediampPlayer() | ||
val data = UriMediaData( | ||
uri = "file:///fake_video.mp4", | ||
headers = emptyMap(), | ||
extraFiles = MediaExtraFiles() | ||
) | ||
|
||
// Setup media data -> READY | ||
player.setMediaData(data) | ||
assertEquals(PlaybackState.READY, player.playbackState.value) | ||
|
||
// Resume -> PLAYING | ||
player.resume() | ||
assertEquals(PlaybackState.PLAYING, player.playbackState.value) | ||
|
||
// Pause -> PAUSED | ||
player.pause() | ||
assertEquals(PlaybackState.PAUSED, player.playbackState.value) | ||
} | ||
|
||
@Test | ||
fun `stop playback`() = runTest { | ||
val player = createMediampPlayer() | ||
val data = UriMediaData( | ||
uri = "file:///fake_video.mp4", | ||
headers = emptyMap(), | ||
extraFiles = MediaExtraFiles() | ||
) | ||
|
||
player.setMediaData(data) | ||
assertEquals(PlaybackState.READY, player.playbackState.value) | ||
|
||
// Resume -> PLAYING | ||
player.resume() | ||
assertEquals(PlaybackState.PLAYING, player.playbackState.value) | ||
|
||
// Stop playback -> FINISHED | ||
player.stopPlayback() | ||
assertEquals(PlaybackState.FINISHED, player.playbackState.value) | ||
|
||
// mediaData flow should now emit `null` because we've released the resource | ||
val mediaDataAfterStop = player.mediaData.first() | ||
assertNull(mediaDataAfterStop) | ||
} | ||
|
||
@Test | ||
fun `close player`() = runTest { | ||
val player = createMediampPlayer() | ||
val data = UriMediaData( | ||
uri = "file:///fake_video.mp4", | ||
headers = emptyMap(), | ||
extraFiles = MediaExtraFiles() | ||
) | ||
|
||
// Go to READY -> PLAYING | ||
player.setMediaData(data) | ||
player.resume() | ||
assertEquals(PlaybackState.PLAYING, player.playbackState.value) | ||
|
||
// Close -> DESTROYED | ||
player.close() | ||
assertEquals(PlaybackState.DESTROYED, player.playbackState.value) | ||
|
||
// Further calls should have no effect, e.g. calling resume or stopPlayback won't change the state | ||
player.resume() | ||
assertEquals(PlaybackState.DESTROYED, player.playbackState.value) | ||
player.stopPlayback() | ||
assertEquals(PlaybackState.DESTROYED, player.playbackState.value) | ||
} | ||
|
||
@Test | ||
fun `seek and skip`() = runTest { | ||
val player = createMediampPlayer() | ||
val data = UriMediaData( | ||
uri = "file:///fake_video.mp4", | ||
headers = emptyMap(), | ||
extraFiles = MediaExtraFiles() | ||
) | ||
|
||
player.setMediaData(data) | ||
player.resume() | ||
|
||
// Check initial dummy position | ||
assertEquals(10000L, player.currentPositionMillis.value) | ||
|
||
// Seek to 20s | ||
player.seekTo(20000L) | ||
assertEquals(20000L, player.currentPositionMillis.value) | ||
|
||
// Skip +5s => 25s | ||
player.skip(5000L) | ||
assertEquals(25000L, player.currentPositionMillis.value) | ||
|
||
// Skip -10s => 15s | ||
player.skip(-10000L) | ||
assertEquals(15000L, player.currentPositionMillis.value) | ||
} | ||
|
||
@Test | ||
fun `toggle pause`() = runTest { | ||
val player = createMediampPlayer() | ||
player.setMediaData( | ||
UriMediaData( | ||
uri = "file:///fake_video.mp4", | ||
headers = emptyMap(), | ||
extraFiles = MediaExtraFiles() | ||
) | ||
) | ||
|
||
// READY -> PLAY | ||
player.resume() | ||
assertEquals(PlaybackState.PLAYING, player.playbackState.value) | ||
|
||
// togglePause -> PAUSE | ||
player.togglePause() | ||
assertEquals(PlaybackState.PAUSED, player.playbackState.value) | ||
|
||
// togglePause -> PLAYING | ||
player.togglePause() | ||
assertEquals(PlaybackState.PLAYING, player.playbackState.value) | ||
} | ||
} |