Skip to content

Commit

Permalink
Update other tracker if one has no more data
Browse files Browse the repository at this point in the history
  • Loading branch information
StaehliJ committed Feb 14, 2024
1 parent a00d3d0 commit 2f6cbff
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ internal class CurrentMediaItemTracker internal constructor(
val lastTrackerData = lastMediaItem.getMediaItemTrackerData()
val newTrackerData = newMediaItem.getMediaItemTrackerData()
for (tracker in it) {
val newData = newTrackerData.getData(tracker) ?: return
val newData = newTrackerData.getData(tracker) ?: continue
val oldData = lastTrackerData.getData(tracker)
if (newData != oldData) {
tracker.update(newData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ class FakeMediaItemTracker : MediaItemTracker {
override fun stop(player: ExoPlayer, reason: MediaItemTracker.StopReason, positionMs: Long) {
// Nothing
}

class Factory(private val fakeMediaItemTracker: FakeMediaItemTracker) : MediaItemTracker.Factory {
override fun create(): MediaItemTracker {
return fakeMediaItemTracker
}
}
}

class FakeTrackerProvider(private val fakeMediaItemTracker: FakeMediaItemTracker) : MediaItemTrackerProvider {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.player.tracker

import android.content.Context
import androidx.media3.common.MediaItem
import androidx.media3.common.Player
import androidx.media3.datasource.DefaultHttpDataSource
import androidx.media3.exoplayer.DefaultLoadControl
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.test.utils.FakeClock
import androidx.media3.test.utils.robolectric.TestPlayerRunHelper
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import ch.srgssr.pillarbox.player.PillarboxPlayer
import ch.srgssr.pillarbox.player.SeekIncrement
import ch.srgssr.pillarbox.player.data.MediaItemSource
import ch.srgssr.pillarbox.player.extension.setTrackerData
import io.mockk.clearAllMocks
import io.mockk.spyk
import io.mockk.verify
import io.mockk.verifyOrder
import org.junit.After
import org.junit.Before
import org.junit.runner.RunWith
import kotlin.test.Test

@RunWith(AndroidJUnit4::class)
class MultiMediaItemTrackerUpdate {
private lateinit var fakeClock: FakeClock

@Before
fun createPlayer() {
fakeClock = FakeClock(true)
}

@After
fun releasePlayer() {
clearAllMocks()
}

@Test
fun `Remove one tracker data update other tracker data when initialized both in MediaItemSource`() {
val context = ApplicationProvider.getApplicationContext<Context>()
val fakeMediaItemTracker = spyk(FakeMediaItemTracker())
val dummyMediaItemTracker = spyk(DummyTracker())

val player = PillarboxPlayer(
context = context,
dataSourceFactory = DefaultHttpDataSource.Factory(),
seekIncrement = SeekIncrement(),
loadControl = DefaultLoadControl(),
clock = fakeClock,
mediaItemSource = object : MediaItemSource {
override suspend fun loadMediaItem(mediaItem: MediaItem): MediaItem {
val trackerData = MediaItemTrackerData.Builder()
.putData(DummyTracker::class.java, "DummyItemTracker")
.putData(FakeMediaItemTracker::class.java, FakeMediaItemTracker.Data("FakeMediaItemTracker"))
.build()
return mediaItem.buildUpon()
.setUri(FakeMediaItemSource.URL_MEDIA_1)
.setTrackerData(trackerData)
.build()
}
},
mediaItemTrackerProvider = MediaItemTrackerRepository().apply {
registerFactory(DummyTracker::class.java, DummyTracker.Factory(dummyMediaItemTracker))
registerFactory(FakeMediaItemTracker::class.java, FakeMediaItemTracker.Factory(fakeMediaItemTracker))
}
)
player.apply {
player.setMediaItem(
MediaItem.Builder()
.setMediaId(FakeMediaItemSource.MEDIA_ID_1)
.build()
)
prepare()
play()
}
TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY)

val currentMediaItem = player.currentMediaItem!!
val mediaUpdate = currentMediaItem.buildUpon()
.setTrackerData(
MediaItemTrackerData.Builder()
.putData(FakeMediaItemTracker::class.java, FakeMediaItemTracker.Data("New Data"))
.build()
)
.build()
player.replaceMediaItem(0, mediaUpdate)

verify(exactly = 0) {
dummyMediaItemTracker.update(any())
}

verify(exactly = 1) {
dummyMediaItemTracker.start(any(), any())
}

verifyOrder {
fakeMediaItemTracker.start(any(), any())
fakeMediaItemTracker.update(FakeMediaItemTracker.Data("New Data"))
}
player.release()
}

internal class DummyTracker : MediaItemTracker {

override fun start(player: ExoPlayer, initialData: Any?) {
// Nothing it is dummy
}

override fun stop(player: ExoPlayer, reason: MediaItemTracker.StopReason, positionMs: Long) {
// Nothing is is dummy
}

class Factory(private val dummyTracker: DummyTracker = DummyTracker()) : MediaItemTracker.Factory {
override fun create(): MediaItemTracker {
return dummyTracker
}
}
}
}

0 comments on commit 2f6cbff

Please sign in to comment.