Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MGaetan89 committed Jan 4, 2024
1 parent a2f8fe4 commit fe35fc0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ open class PlayerListenerCommander(player: Player) : ForwardingPlayer(player), L
listeners.remove(listener)
}

private fun notifyAll(run: (Player: Listener) -> Unit) {
private fun notifyAll(run: (Listener) -> Unit) {
val copy = HashSet(listeners)
for (listener in copy) {
run(listener)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@ class TestPlayer(val player: Player) {

suspend fun Player.waitForPlaybackState(state: @Player.State Int) {
waitForCondition {
playbackState == state
it.playbackState == state
}
}

suspend fun Player.waitForPause() {
waitForCondition {
playbackState == Player.STATE_READY && !playWhenReady
it.playbackState == Player.STATE_READY && !it.playWhenReady
}
}

suspend fun Player.waitIsPlaying() {
waitForCondition {
isPlaying
it.isPlaying
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ class TestPlayerCallbackFlow {
}
}

@Test
fun testCurrentBufferedPercentage() = runTest {
val bufferedPercentages = listOf(0, 5, 15, 20, 50, 60, 75, 100)
every { player.bufferedPercentage } returnsMany bufferedPercentages
every { player.isPlaying } returns true

val currentBufferedPercentageFlow = player.currentBufferedPercentageAsFlow()
val actualBufferedPositions = currentBufferedPercentageFlow.take(bufferedPercentages.size).toList()
Assert.assertEquals(bufferedPercentages.map { it / 100f }, actualBufferedPositions)
}

@Test(timeout = 5_000)
fun testUpdateCurrentPositionAfterSeek() = runTest {
val positions = listOf(0L, 1000L, 2000L)
Expand Down Expand Up @@ -214,6 +225,28 @@ class TestPlayerCallbackFlow {
job.cancel()
}

@Test
fun testShuffleModeEnabled() = runTest {
every { player.shuffleModeEnabled } returns false

val listener = PlayerListenerCommander(player)

val shuffleModeEnabledFlow = listener.shuffleModeEnabledAsFlow()
val shuffleModeEnabled = listOf(false, false, true, false, true)
val actualShuffleModeEnabled = mutableListOf<Boolean>()

val job = launch(dispatcher) {
shuffleModeEnabledFlow.take(shuffleModeEnabled.size).toList(actualShuffleModeEnabled)
}

listOf(false, true, false, true)
.forEach(listener::onShuffleModeEnabledChanged)

Assert.assertEquals(shuffleModeEnabled, actualShuffleModeEnabled)

job.cancel()
}

@Test(timeout = 2_000)
fun testPlaybackSpeed() = runTest {
val initialPlaybackRate = 1.5f
Expand Down Expand Up @@ -252,7 +285,7 @@ class TestPlayerCallbackFlow {
}

@Test(timeout = 5_000)
fun testCurrendMediaItem() = runTest {
fun testCurrentMediaItem() = runTest {
val mediaItem1: MediaItem? = null
val mediaItem2: MediaItem = mockk()
val mediaItem3: MediaItem = mockk()
Expand All @@ -273,9 +306,8 @@ class TestPlayerCallbackFlow {
job.cancel()
}


@Test(timeout = 5_000)
fun testCurrendMediaItems() = runTest {
fun testCurrentMediaItems() = runTest {
val item1 = mockk<MediaItem>()
val item2 = mockk<MediaItem>()
val item3 = mockk<MediaItem>()
Expand All @@ -287,7 +319,6 @@ class TestPlayerCallbackFlow {
every { player.getMediaItemAt(1) } returns item2
every { player.getMediaItemAt(2) } returns item3


val fakePlayer = PlayerListenerCommander(player)

Assert.assertEquals(item1, fakePlayer.getMediaItemAt(0))
Expand Down Expand Up @@ -325,5 +356,4 @@ class TestPlayerCallbackFlow {
Assert.assertEquals(videoSizes, actualVideoSize)
job.cancel()
}

}

0 comments on commit fe35fc0

Please sign in to comment.