diff --git a/pillarbox-player/src/main/java/ch/srgssr/pillarbox/player/tracker/CurrentMediaItemTracker.kt b/pillarbox-player/src/main/java/ch/srgssr/pillarbox/player/tracker/CurrentMediaItemTracker.kt index 8d43bb04e..4068cfd9b 100644 --- a/pillarbox-player/src/main/java/ch/srgssr/pillarbox/player/tracker/CurrentMediaItemTracker.kt +++ b/pillarbox-player/src/main/java/ch/srgssr/pillarbox/player/tracker/CurrentMediaItemTracker.kt @@ -8,7 +8,6 @@ import androidx.annotation.VisibleForTesting import androidx.media3.common.MediaItem import androidx.media3.common.Player import androidx.media3.common.Timeline -import androidx.media3.common.Timeline.Window import androidx.media3.exoplayer.ExoPlayer import ch.srgssr.pillarbox.player.extension.getMediaItemTrackerData import ch.srgssr.pillarbox.player.extension.getMediaItemTrackerDataOrNull @@ -49,10 +48,7 @@ internal class CurrentMediaItemTracker internal constructor( setMediaItem(player.currentMediaItem) } - private val window = Window() - init { - // player.addAnalyticsListener(this) player.addListener(this) player.currentMediaItem?.let { startNewSession(it) } } @@ -97,9 +93,9 @@ internal class CurrentMediaItemTracker internal constructor( * Don't start or stop if new tracker data is added. Only update existing trackers with new data. */ private fun maybeUpdateData(lastMediaItem: MediaItem, newMediaItem: MediaItem) { - val lastTrackerData = lastMediaItem.getMediaItemTrackerData() - val newTrackerData = newMediaItem.getMediaItemTrackerData() trackers?.let { + val lastTrackerData = lastMediaItem.getMediaItemTrackerData() + val newTrackerData = newMediaItem.getMediaItemTrackerData() for (tracker in it) { val newData = newTrackerData.getData(tracker) ?: return val oldData = lastTrackerData.getData(tracker) @@ -112,7 +108,7 @@ internal class CurrentMediaItemTracker internal constructor( private fun stopSession(stopReason: MediaItemTracker.StopReason, positionMs: Long = player.currentPosition) { trackers?.let { - for (tracker in it.list) { + for (tracker in it) { tracker.stop(player, stopReason, positionMs) } } @@ -152,10 +148,7 @@ internal class CurrentMediaItemTracker internal constructor( override fun onPositionDiscontinuity(oldPosition: Player.PositionInfo, newPosition: Player.PositionInfo, reason: Int) { val oldPositionMs = oldPosition.positionMs when (reason) { - Player.DISCONTINUITY_REASON_REMOVE -> { - stopSession(MediaItemTracker.StopReason.Stop, oldPositionMs) - } - + Player.DISCONTINUITY_REASON_REMOVE -> stopSession(MediaItemTracker.StopReason.Stop, oldPositionMs) Player.DISCONTINUITY_REASON_AUTO_TRANSITION -> stopSession(MediaItemTracker.StopReason.EoF, oldPositionMs) else -> { // Nothing diff --git a/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/extension/MediaItemTest.kt b/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/extension/MediaItemTest.kt index 5616addc6..8065fe687 100644 --- a/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/extension/MediaItemTest.kt +++ b/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/extension/MediaItemTest.kt @@ -35,9 +35,9 @@ class MediaItemTest { @Test fun `getMediaItemTrackerData with tag set`() { - val mediaItemTrackerData = MediaItemTrackerData.Builder().apply { - putData(MediaItemTracker::class.java) - }.build() + val mediaItemTrackerData = MediaItemTrackerData.Builder() + .putData(MediaItemTracker::class.java) + .build() val mediaItem = MediaItem.Builder() .setUri(mockk()) diff --git a/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/tracker/CurrentMediaItemTrackerAreEqualTest.kt b/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/tracker/CurrentMediaItemTrackerAreEqualTest.kt index 960da7ce6..ea68613fc 100644 --- a/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/tracker/CurrentMediaItemTrackerAreEqualTest.kt +++ b/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/tracker/CurrentMediaItemTrackerAreEqualTest.kt @@ -10,8 +10,8 @@ import androidx.media3.exoplayer.ExoPlayer import androidx.test.ext.junit.runners.AndroidJUnit4 import ch.srgssr.pillarbox.player.extension.getMediaItemTrackerData import ch.srgssr.pillarbox.player.extension.setTrackerData -import org.junit.Test import org.junit.runner.RunWith +import kotlin.test.Test import kotlin.test.assertFalse import kotlin.test.assertTrue @@ -114,12 +114,12 @@ class CurrentMediaItemTrackerAreEqualTest { fun `areEqual same MediaItemTrackerData content`() { val mediaItem = MediaItem.Builder() .setUri("https://streaming.com/video.mp4") - .setTrackerData(MediaItemTrackerData.Builder().apply { putData(Tracker::class.java, "data1") }.build()) + .setTrackerData(MediaItemTrackerData.Builder().putData(Tracker::class.java, "data1").build()) .build() val mediaItem2 = MediaItem.Builder() .setUri("https://streaming.com/video.mp4") - .setTrackerData(MediaItemTrackerData.Builder().apply { putData(Tracker::class.java, "data1") }.build()) + .setTrackerData(MediaItemTrackerData.Builder().putData(Tracker::class.java, "data1").build()) .build() assertTrue(CurrentMediaItemTracker.areEqual(mediaItem, mediaItem2)) } @@ -128,11 +128,11 @@ class CurrentMediaItemTrackerAreEqualTest { fun `areEqual same MediaItemTrackerData`() { val mediaItem = MediaItem.Builder() .setUri("https://streaming.com/video.mp4") - .setTrackerData(MediaItemTrackerData.Builder().apply { putData(Tracker::class.java, "data1") }.build()) + .setTrackerData(MediaItemTrackerData.Builder().putData(Tracker::class.java, "data1").build()) .build() val mediaItem2 = mediaItem.buildUpon() - .setTrackerData(mediaItem.getMediaItemTrackerData().buildUpon().apply { putData(Tracker::class.java, "data1") }.build()) + .setTrackerData(mediaItem.getMediaItemTrackerData().buildUpon().putData(Tracker::class.java, "data1").build()) .build() assertTrue(CurrentMediaItemTracker.areEqual(mediaItem, mediaItem2)) } @@ -141,11 +141,11 @@ class CurrentMediaItemTrackerAreEqualTest { fun `areEqual same MediaItemTrackerData but different MediaMetadata`() { val mediaItem = MediaItem.Builder() .setUri("https://streaming.com/video.mp4") - .setTrackerData(MediaItemTrackerData.Builder().apply { putData(Tracker::class.java, "data1") }.build()) + .setTrackerData(MediaItemTrackerData.Builder().putData(Tracker::class.java, "data1").build()) .build() val mediaItem2 = mediaItem.buildUpon() - .setTrackerData(mediaItem.getMediaItemTrackerData().buildUpon().apply { putData(Tracker::class.java, "data1") }.build()) + .setTrackerData(mediaItem.getMediaItemTrackerData().buildUpon().putData(Tracker::class.java, "data1").build()) .setMediaMetadata(MediaMetadata.Builder().setTitle("New title").build()) .build() assertTrue(CurrentMediaItemTracker.areEqual(mediaItem, mediaItem2)) @@ -155,11 +155,11 @@ class CurrentMediaItemTrackerAreEqualTest { fun `areNotEqual different data`() { val mediaItem = MediaItem.Builder() .setUri("https://streaming.com/video.mp4") - .setTrackerData(MediaItemTrackerData.Builder().apply { putData(Tracker::class.java, "data1") }.build()) + .setTrackerData(MediaItemTrackerData.Builder().putData(Tracker::class.java, "data1").build()) .build() val mediaItem2 = mediaItem.buildUpon() - .setTrackerData(mediaItem.getMediaItemTrackerData().buildUpon().apply { putData(Tracker::class.java, "data2") }.build()) + .setTrackerData(mediaItem.getMediaItemTrackerData().buildUpon().putData(Tracker::class.java, "data2").build()) .build() assertFalse(CurrentMediaItemTracker.areEqual(mediaItem, mediaItem2)) } diff --git a/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/tracker/FakeMediaItemSource.kt b/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/tracker/FakeMediaItemSource.kt index e776cec1c..618a2f61e 100644 --- a/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/tracker/FakeMediaItemSource.kt +++ b/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/tracker/FakeMediaItemSource.kt @@ -12,15 +12,15 @@ import ch.srgssr.pillarbox.player.extension.setTrackerData class FakeMediaItemSource : MediaItemSource { override suspend fun loadMediaItem(mediaItem: MediaItem): MediaItem { val trackerData = mediaItem.getMediaItemTrackerData() - val itemBuilder = if (mediaItem.localConfiguration == null) { + val itemBuilder = mediaItem.buildUpon() + + if (mediaItem.localConfiguration == null) { val url = when (mediaItem.mediaId) { MEDIA_ID_1 -> URL_MEDIA_1 MEDIA_ID_2 -> URL_MEDIA_2 else -> URL_MEDIA_3 } - mediaItem.buildUpon().setUri(url) - } else { - mediaItem.buildUpon() + itemBuilder.setUri(url) } if (mediaItem.mediaId == MEDIA_ID_NO_TRACKING_DATA) return itemBuilder.build() diff --git a/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/tracker/MediaItemTrackerDataTest.kt b/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/tracker/MediaItemTrackerDataTest.kt index 68f3f8292..d78335f56 100644 --- a/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/tracker/MediaItemTrackerDataTest.kt +++ b/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/tracker/MediaItemTrackerDataTest.kt @@ -43,33 +43,31 @@ class MediaItemTrackerDataTest { @Test fun `media item tracker data are equals`() { - val mediaItemTrackerData1 = MediaItemTrackerData.Builder().apply { - putData(MediaItemTracker1::class.java, "Data1") - putData(MediaItemTracker2::class.java, "Data2") - }.build() - val mediaItemTrackerData2 = MediaItemTrackerData.Builder().apply { - putData(MediaItemTracker1::class.java, "Data1") - putData(MediaItemTracker2::class.java, "Data2") - }.build() + val mediaItemTrackerData1 = MediaItemTrackerData.Builder() + .putData(MediaItemTracker1::class.java, "Data1") + .putData(MediaItemTracker2::class.java, "Data2") + .build() + val mediaItemTrackerData2 = MediaItemTrackerData.Builder() + .putData(MediaItemTracker1::class.java, "Data1") + .putData(MediaItemTracker2::class.java, "Data2") + .build() assertEquals(mediaItemTrackerData1, mediaItemTrackerData2) } @Test fun `media item tracker data are not equals when data changes`() { - val mediaItemTrackerData1 = MediaItemTrackerData.Builder().apply { - putData(MediaItemTracker1::class.java, "Data1") - putData(MediaItemTracker2::class.java, "Data2") - }.build() - val mediaItemTrackerData2 = MediaItemTrackerData.Builder().apply { - putData(MediaItemTracker1::class.java, "Data1") - }.build() + val mediaItemTrackerData1 = MediaItemTrackerData.Builder() + .putData(MediaItemTracker1::class.java, "Data1") + .putData(MediaItemTracker2::class.java, "Data2") + .build() + val mediaItemTrackerData2 = MediaItemTrackerData.Builder() + .putData(MediaItemTracker1::class.java, "Data1") + .build() assertNotEquals(mediaItemTrackerData1, mediaItemTrackerData2) - val mediaItemTrackerData3 = MediaItemTrackerData.Builder().apply { - putData(MediaItemTracker1::class.java, "Data1") - } - val mediaItemTrackerData4 = MediaItemTrackerData.Builder().apply { - putData(MediaItemTracker1::class.java, "Data2") - } + val mediaItemTrackerData3 = MediaItemTrackerData.Builder() + .putData(MediaItemTracker1::class.java, "Data1") + val mediaItemTrackerData4 = MediaItemTrackerData.Builder() + .putData(MediaItemTracker1::class.java, "Data2") assertNotEquals(mediaItemTrackerData3, mediaItemTrackerData4) } diff --git a/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/tracker/MediaItemTrackerTest.kt b/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/tracker/MediaItemTrackerTest.kt index 3b6818c7a..a7f31180a 100644 --- a/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/tracker/MediaItemTrackerTest.kt +++ b/pillarbox-player/src/test/java/ch/srgssr/pillarbox/player/tracker/MediaItemTrackerTest.kt @@ -386,11 +386,10 @@ class MediaItemTrackerTest { ) prepare() play() - // seekTo(10_000) } TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY) - // Wait MediaItemSource has load + // Wait for MediaItemSource to be loaded RobolectricUtil.runMainLooperUntil { player.currentMediaItem?.getMediaItemTrackerDataOrNull() != null } @@ -399,7 +398,6 @@ class MediaItemTrackerTest { .setMediaMetadata(MediaMetadata.Builder().setTitle("New title").build()) .build() player.replaceMediaItem(player.currentMediaItemIndex, mediaUpdate) - // TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_ENDED) verify(exactly = 0) { fakeMediaItemTracker.update(any()) @@ -615,8 +613,7 @@ class MediaItemTrackerTest { companion object { @Throws(TimeoutException::class) - fun runUntilMediaItemTransition(player: Player): Pair? { - // TestPlayerRunHelper.verifyMainTestThread(player) + private fun runUntilMediaItemTransition(player: Player): Pair { val receivedEvent = AtomicReference?>() val listener: Player.Listener = object : Player.Listener { override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) {