Skip to content

Commit

Permalink
Small code change
Browse files Browse the repository at this point in the history
  • Loading branch information
MGaetan89 committed Feb 13, 2024
1 parent 7717b26 commit a00d3d0
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) }
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uri>())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))
}
Expand All @@ -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))
}
Expand All @@ -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))
Expand All @@ -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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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())
Expand Down Expand Up @@ -615,8 +613,7 @@ class MediaItemTrackerTest {

companion object {
@Throws(TimeoutException::class)
fun runUntilMediaItemTransition(player: Player): Pair<MediaItem?, Int>? {
// TestPlayerRunHelper.verifyMainTestThread(player)
private fun runUntilMediaItemTransition(player: Player): Pair<MediaItem?, Int> {
val receivedEvent = AtomicReference<Pair<MediaItem?, Int>?>()
val listener: Player.Listener = object : Player.Listener {
override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) {
Expand Down

0 comments on commit a00d3d0

Please sign in to comment.