From f1ef9c689e68e5d38174f0f0a8c131322be0e47f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaquim=20Sta=CC=88hli?= Date: Mon, 29 Jul 2024 13:28:53 +0200 Subject: [PATCH] Rollback test modifications --- .../CommandersActTrackerIntegrationTest.kt | 58 ++++++++++--------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/tracker/commandersact/CommandersActTrackerIntegrationTest.kt b/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/tracker/commandersact/CommandersActTrackerIntegrationTest.kt index a407429a6..c379ec7f2 100644 --- a/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/tracker/commandersact/CommandersActTrackerIntegrationTest.kt +++ b/pillarbox-core-business/src/test/java/ch/srgssr/pillarbox/core/business/tracker/commandersact/CommandersActTrackerIntegrationTest.kt @@ -5,9 +5,11 @@ package ch.srgssr.pillarbox.core.business.tracker.commandersact import android.content.Context +import android.os.Handler import android.os.Looper import androidx.media3.common.MediaItem import androidx.media3.common.Player +import androidx.media3.common.util.Util import androidx.media3.exoplayer.ExoPlayer import androidx.media3.test.utils.FakeClock import androidx.media3.test.utils.robolectric.TestPlayerRunHelper @@ -38,6 +40,7 @@ import io.mockk.verify import io.mockk.verifyOrder import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.android.asCoroutineDispatcher import kotlinx.coroutines.test.TestDispatcher import kotlinx.coroutines.test.UnconfinedTestDispatcher import kotlinx.coroutines.test.advanceTimeBy @@ -85,13 +88,14 @@ class CommandersActTrackerIntegrationTest { } val mediaCompositionWithFallbackService = LocalMediaCompositionWithFallbackService(context) - + // To execute QoSCoordinator Heartbeat in a separate CoroutineContext but on the same thread. + val playerCoroutineContext = Handler(Util.getCurrentOrMainLooper()).asCoroutineDispatcher() player = DefaultPillarbox( context = context, mediaItemTrackerRepository = mediaItemTrackerRepository, mediaCompositionService = mediaCompositionWithFallbackService, clock = clock, - coroutineContext = testDispatcher, + coroutineContext = playerCoroutineContext ) } @@ -561,22 +565,33 @@ class CommandersActTrackerIntegrationTest { TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY) TestPlayerRunHelper.runUntilPendingCommandsAreFullyHandled(player) - player.clearMediaItems() - verifyOrder { commandersAct.enableRunningInBackground() commandersAct.sendTcMediaEvent(capture(tcMediaEvents)) commandersAct.sendTcMediaEvent(capture(tcMediaEvents)) commandersAct.sendTcMediaEvent(capture(tcMediaEvents)) - commandersAct.sendTcMediaEvent(capture(tcMediaEvents)) } confirmVerified(commandersAct) - assertEquals(4, tcMediaEvents.size) + assertEquals(3, tcMediaEvents.size) - assertEquals(listOf(Stop, Pause, Pos, Play), tcMediaEvents.map { it.eventType }) - assertTrue(tcMediaEvents.all { it.assets.isNotEmpty() }) - assertTrue(tcMediaEvents.all { it.sourceId == null }) + tcMediaEvents[0].let { + assertEquals(Pause, it.eventType) + assertTrue(it.assets.isNotEmpty()) + assertNull(it.sourceId) + } + + tcMediaEvents[1].let { + assertEquals(Pos, it.eventType) + assertTrue(it.assets.isNotEmpty()) + assertNull(it.sourceId) + } + + tcMediaEvents[2].let { + assertEquals(Play, it.eventType) + assertTrue(it.assets.isNotEmpty()) + assertNull(it.sourceId) + } } @Test @@ -697,8 +712,6 @@ class CommandersActTrackerIntegrationTest { assertTrue(player.isCurrentMediaItemLive) - player.clearMediaItems() - verifyOrder { commandersAct.enableRunningInBackground() commandersAct.sendTcMediaEvent(capture(tcMediaEvents)) @@ -711,16 +724,12 @@ class CommandersActTrackerIntegrationTest { commandersAct.sendTcMediaEvent(capture(tcMediaEvents)) commandersAct.sendTcMediaEvent(capture(tcMediaEvents)) commandersAct.sendTcMediaEvent(capture(tcMediaEvents)) - commandersAct.sendTcMediaEvent(capture(tcMediaEvents)) } confirmVerified(commandersAct) - assertEquals(11, tcMediaEvents.size) + assertEquals(10, tcMediaEvents.size) - assertEquals( - expected = listOf(Stop, Pause, Pos, Uptime, Pos, Pos, Uptime, Pos, Uptime, Pos, Play), - actual = tcMediaEvents.map { it.eventType }, - ) + assertEquals(listOf(Pause, Pos, Uptime, Pos, Pos, Uptime, Pos, Uptime, Pos, Play), tcMediaEvents.map { it.eventType }) assertTrue(tcMediaEvents.all { it.assets.isNotEmpty() }) assertTrue(tcMediaEvents.all { it.sourceId == null }) } @@ -753,9 +762,7 @@ class CommandersActTrackerIntegrationTest { TestPlayerRunHelper.runUntilPendingCommandsAreFullyHandled(player) - val playerDuration = player.duration.milliseconds - - player.clearMediaItems() + player.stop() verifyOrder { commandersAct.enableRunningInBackground() @@ -775,7 +782,7 @@ class CommandersActTrackerIntegrationTest { assertTrue(tcMediaEvents.all { it.assets.isNotEmpty() }) assertTrue(tcMediaEvents.all { it.sourceId == null }) - val timeShift = (playerDuration - seekPosition).inWholeSeconds + val timeShift = (player.duration.milliseconds - seekPosition).inWholeSeconds val actualTimeShift = tcMediaEvents.first { it.eventType == Pos || it.eventType == Uptime }.timeShift?.inWholeSeconds ?: 0L @@ -818,8 +825,6 @@ class CommandersActTrackerIntegrationTest { assertFalse(player.isCurrentMediaItemLive) - player.clearMediaItems() - verifyOrder { commandersAct.enableRunningInBackground() commandersAct.sendTcMediaEvent(capture(tcMediaEvents)) @@ -829,13 +834,12 @@ class CommandersActTrackerIntegrationTest { commandersAct.sendTcMediaEvent(capture(tcMediaEvents)) commandersAct.sendTcMediaEvent(capture(tcMediaEvents)) commandersAct.sendTcMediaEvent(capture(tcMediaEvents)) - commandersAct.sendTcMediaEvent(capture(tcMediaEvents)) } confirmVerified(commandersAct) - assertEquals(8, tcMediaEvents.size) + assertEquals(7, tcMediaEvents.size) - assertEquals(listOf(Stop, Pause, Pos, Pos, Pos, Pos, Pos, Play), tcMediaEvents.map { it.eventType }) + assertEquals(listOf(Pause, Pos, Pos, Pos, Pos, Pos, Play), tcMediaEvents.map { it.eventType }) assertTrue(tcMediaEvents.all { it.assets.isNotEmpty() }) assertTrue(tcMediaEvents.all { it.sourceId == null }) } @@ -855,8 +859,6 @@ class CommandersActTrackerIntegrationTest { TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_ENDED) - player.clearMediaItems() - verifyOrder { commandersAct.enableRunningInBackground() commandersAct.sendTcMediaEvent(capture(tcMediaEvents))