diff --git a/pillarbox-player-testutils/src/main/java/ch/srgssr/pillarbox/player/test/utils/PlayerListenerCommander.kt b/pillarbox-player-testutils/src/main/java/ch/srgssr/pillarbox/player/test/utils/PlayerListenerCommander.kt index 7a3d30c78..ab250d310 100644 --- a/pillarbox-player-testutils/src/main/java/ch/srgssr/pillarbox/player/test/utils/PlayerListenerCommander.kt +++ b/pillarbox-player-testutils/src/main/java/ch/srgssr/pillarbox/player/test/utils/PlayerListenerCommander.kt @@ -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) diff --git a/pillarbox-player-testutils/src/main/java/ch/srgssr/pillarbox/player/test/utils/TestPlayer.kt b/pillarbox-player-testutils/src/main/java/ch/srgssr/pillarbox/player/test/utils/TestPlayer.kt index 51686103a..7ce5d93c5 100644 --- a/pillarbox-player-testutils/src/main/java/ch/srgssr/pillarbox/player/test/utils/TestPlayer.kt +++ b/pillarbox-player-testutils/src/main/java/ch/srgssr/pillarbox/player/test/utils/TestPlayer.kt @@ -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 } } } diff --git a/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/TestPlayerCallbackFlow.kt b/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/TestPlayerCallbackFlow.kt index d060237a0..2682afc72 100644 --- a/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/TestPlayerCallbackFlow.kt +++ b/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/TestPlayerCallbackFlow.kt @@ -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) @@ -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() + + 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 @@ -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() @@ -273,9 +306,8 @@ class TestPlayerCallbackFlow { job.cancel() } - @Test(timeout = 5_000) - fun testCurrendMediaItems() = runTest { + fun testCurrentMediaItems() = runTest { val item1 = mockk() val item2 = mockk() val item3 = mockk() @@ -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)) @@ -325,5 +356,4 @@ class TestPlayerCallbackFlow { Assert.assertEquals(videoSizes, actualVideoSize) job.cancel() } - }