Skip to content

Commit

Permalink
Rework some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MGaetan89 committed Dec 17, 2024
1 parent bb3f5ea commit ca652ef
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 190 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package ch.srgssr.pillarbox.player.test.utils

import android.annotation.SuppressLint
import android.os.Looper
import androidx.media3.common.PlaybackParameters
import androidx.media3.common.Player
Expand All @@ -26,25 +27,26 @@ object TestPillarboxRunHelper {
}

/**
* Runs tasks of the main Looper until [Player.Listener.onEvents] matches the
* Runs tasks of the main [Looper] until [Player.Listener.onEvents] matches the
* expected state or a playback error occurs.
*
* <p>If a playback error occurs it will be thrown wrapped in an {@link IllegalStateException}.
* <p>If a playback error occurs it will be thrown wrapped in an [IllegalStateException].
*
* @param player The [Player].
* @param expectedEvent The expected [Player.Event] if null wait until first [Player.Listener.onEvents].
* @param expectedEvents The expected [Player.Event]. If empty, waits until the first [Player.Listener.onEvents].
* @throws TimeoutException If the [RobolectricUtil.DEFAULT_TIMEOUT_MS] is exceeded.
*/
@Throws(TimeoutException::class)
fun runUntilEvent(player: Player, expectedEvent: @Player.Event Int? = null) {
fun runUntilEvents(player: Player, vararg expectedEvents: @Player.Event Int) {
verifyMainTestThread(player)
if (player is ExoPlayer) {
verifyPlaybackThreadIsAlive(player)
}
val receivedCallback = AtomicBoolean(false)
val listener: Player.Listener = object : Player.Listener {
@SuppressLint("WrongConstant")
override fun onEvents(player: Player, events: Player.Events) {
if (expectedEvent?.let { events.contains(it) } != false) {
if (expectedEvents.isEmpty() || events.containsAny(*expectedEvents)) {
receivedCallback.set(true)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package ch.srgssr.pillarbox.player

import androidx.media3.common.Format
import androidx.media3.common.TrackGroup
import androidx.media3.exoplayer.source.MediaPeriod
import androidx.media3.exoplayer.source.TrackGroupArray
import androidx.media3.test.utils.FakeMediaPeriod
import androidx.test.ext.junit.runners.AndroidJUnit4
Expand All @@ -17,128 +18,108 @@ import ch.srgssr.pillarbox.player.tracker.FactoryData
import ch.srgssr.pillarbox.player.tracker.FakeMediaItemTracker
import ch.srgssr.pillarbox.player.tracker.MediaItemTrackerData
import ch.srgssr.pillarbox.player.tracker.MutableMediaItemTrackerData
import io.mockk.clearAllMocks
import io.mockk.mockk
import org.junit.runner.RunWith
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals

@RunWith(AndroidJUnit4::class)
class PillarboxMediaPeriodTest {
private lateinit var format: Format
private lateinit var mediaPeriod: MediaPeriod

@BeforeTest
fun setUp() {
format = Format.Builder()
.setId("FakeId")
.build()
mediaPeriod = FakeMediaPeriod(
/* trackGroupArray = */ TrackGroupArray(TrackGroup(format)),
/* allocator = */ mockk(relaxed = true),
/* singleSampleTimeUs = */ 0L,
/* mediaSourceEventDispatcher = */ mockk(relaxed = true),
)
}

@AfterTest
fun tearDown() {
clearAllMocks()
}

@Test
fun `test track group with no tracker data and no blocked time range`() {
val mediaItemTrackData = MediaItemTrackerData(MutableMediaItemTrackerData.EMPTY)
val blockedTimeRangeList = emptyList<BlockedTimeRange>()
val mediaPeriod = PillarboxMediaPeriod(
mediaPeriod = createFakeChildMediaPeriod(),
mediaItemTrackerData = mediaItemTrackData,
blockedTimeRanges = blockedTimeRangeList
)
val expectedTrackGroup = TrackGroupArray(
TrackGroup(createDummyFormat("DummyId"))
mediaPeriod = mediaPeriod,
mediaItemTrackerData = emptyTrackerData,
blockedTimeRanges = emptyList(),
)
val expectedTrackGroup = TrackGroupArray(TrackGroup(format))
mediaPeriod.prepare(mockk(relaxed = true), 0)
assertEquals(expectedTrackGroup, mediaPeriod.trackGroups)
}

@Test
fun `test track group with tracker data and blocked time range`() {
val mutableMediaItemTrackerData = MutableMediaItemTrackerData()
mutableMediaItemTrackerData[Any()] = FactoryData(FakeMediaItemTracker.Factory(FakeMediaItemTracker()), FakeMediaItemTracker.Data("Test01"))
val mediaItemTrackerData = mutableMediaItemTrackerData.toMediaItemTrackerData()
val blockedTimeRangeList = listOf(BlockedTimeRange(0L, 100L), BlockedTimeRange(200L, 300L))
val mediaPeriod = PillarboxMediaPeriod(
mediaPeriod = createFakeChildMediaPeriod(),
mediaItemTrackerData = mediaItemTrackerData,
blockedTimeRanges = blockedTimeRangeList
)
val expectedTrackGroup = TrackGroupArray(
TrackGroup(createDummyFormat("DummyId")),
TrackGroup(
"Pillarbox-Trackers",
Format.Builder()
.setId("TrackerData:0")
.setSampleMimeType(PILLARBOX_TRACKERS_MIME_TYPE)
.setCustomData(mediaItemTrackerData)
.build()
),
TrackGroup(
"Pillarbox-BlockedTimeRanges",
Format.Builder()
.setSampleMimeType(PILLARBOX_BLOCKED_MIME_TYPE)
.setId("BlockedTimeRanges")
.setCustomData(blockedTimeRangeList)
.build(),
)
mediaPeriod = mediaPeriod,
mediaItemTrackerData = trackerData,
blockedTimeRanges = blockedTimeRanges,
)
val expectedTrackGroup = TrackGroupArray(TrackGroup(format), trackTrackers, trackBlockedTimeRanges)
mediaPeriod.prepare(mockk(relaxed = true), 0)
assertEquals(expectedTrackGroup, mediaPeriod.trackGroups)
}

@Test
fun `test track group with tracker data only`() {
val mutableMediaItemTrackerData = MutableMediaItemTrackerData()
mutableMediaItemTrackerData[Any()] = FactoryData(FakeMediaItemTracker.Factory(FakeMediaItemTracker()), FakeMediaItemTracker.Data("Test01"))
val mediaItemTrackerData = mutableMediaItemTrackerData.toMediaItemTrackerData()
val blockedTimeRangeList = emptyList<BlockedTimeRange>()
val mediaPeriod = PillarboxMediaPeriod(
mediaPeriod = createFakeChildMediaPeriod(),
mediaItemTrackerData = mediaItemTrackerData,
blockedTimeRanges = blockedTimeRangeList
)
val expectedTrackGroup = TrackGroupArray(
TrackGroup(createDummyFormat("DummyId")),
TrackGroup(
"Pillarbox-Trackers",
Format.Builder()
.setId("TrackerData:0")
.setSampleMimeType(PILLARBOX_TRACKERS_MIME_TYPE)
.setCustomData(mediaItemTrackerData)
.build()
)
mediaPeriod = mediaPeriod,
mediaItemTrackerData = trackerData,
blockedTimeRanges = emptyList(),
)
val expectedTrackGroup = TrackGroupArray(TrackGroup(format), trackTrackers)
mediaPeriod.prepare(mockk(relaxed = true), 0)
assertEquals(expectedTrackGroup, mediaPeriod.trackGroups)
}

@Test
fun `test track group with blocked time range only`() {
val mediaItemTrackData = MediaItemTrackerData(MutableMediaItemTrackerData.EMPTY)
val blockedTimeRangeList = listOf(BlockedTimeRange(0L, 100L), BlockedTimeRange(200L, 300L))
val mediaPeriod = PillarboxMediaPeriod(
mediaPeriod = createFakeChildMediaPeriod(),
mediaItemTrackerData = mediaItemTrackData,
blockedTimeRanges = blockedTimeRangeList
)
val expectedTrackGroup = TrackGroupArray(
TrackGroup(createDummyFormat("DummyId")),
TrackGroup(
"Pillarbox-BlockedTimeRanges",
Format.Builder()
.setSampleMimeType(PILLARBOX_BLOCKED_MIME_TYPE)
.setId("BlockedTimeRanges")
.setCustomData(blockedTimeRangeList)
.build(),
)
mediaPeriod = mediaPeriod,
mediaItemTrackerData = emptyTrackerData,
blockedTimeRanges = blockedTimeRanges,
)
val expectedTrackGroup = TrackGroupArray(TrackGroup(format), trackBlockedTimeRanges)
mediaPeriod.prepare(mockk(relaxed = true), 0)
assertEquals(expectedTrackGroup, mediaPeriod.trackGroups)
}

companion object {
fun createFakeChildMediaPeriod(trackGroupArray: TrackGroupArray = createFakeTracks()) =
FakeMediaPeriod(trackGroupArray, mockk(relaxed = true), 0L, mockk(relaxed = true))
private companion object {
private val blockedTimeRanges = listOf(BlockedTimeRange(0L, 100L), BlockedTimeRange(200L, 300L))
private val emptyTrackerData = MediaItemTrackerData(MutableMediaItemTrackerData.EMPTY)
private val trackerData = MutableMediaItemTrackerData().apply {
put(Any(), FactoryData(FakeMediaItemTracker.Factory(FakeMediaItemTracker()), FakeMediaItemTracker.Data("Test01")))
}.toMediaItemTrackerData()

fun createDummyFormat(id: String) = Format.Builder()
.setId(id)
.build()
private val trackBlockedTimeRanges = TrackGroup(
"Pillarbox-BlockedTimeRanges",
Format.Builder()
.setId("BlockedTimeRanges")
.setSampleMimeType(PILLARBOX_BLOCKED_MIME_TYPE)
.setCustomData(blockedTimeRanges)
.build(),
)

private fun createFakeTracks(): TrackGroupArray {
return TrackGroupArray(
TrackGroup(
createDummyFormat("DummyId")
)
)
}
private val trackTrackers = TrackGroup(
"Pillarbox-Trackers",
Format.Builder()
.setId("TrackerData:0")
.setSampleMimeType(PILLARBOX_TRACKERS_MIME_TYPE)
.setCustomData(trackerData)
.build(),
)
}
}
Loading

0 comments on commit ca652ef

Please sign in to comment.