Skip to content

Commit

Permalink
Multiplex tests based on MediaProduct arguments via @parameterized
Browse files Browse the repository at this point in the history
This reduces the code required per test. However, since some tests require a
single MediaProduct while others require 2, I have refactored the current class
to be specific to the former (since all of its current tests only use a single
MediaProduct) and then we can have another class for the tests that will use
two MediaProducts to avoid worthless (repeated) test runs.
  • Loading branch information
stoyicker committed Jun 25, 2024
1 parent 8290842 commit 9566935
Showing 1 changed file with 26 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import com.tidal.sdk.player.events.playlogtest.PlayLogTestDefaultEventReporterCo
import com.tidal.sdk.player.events.reflectionComponentFactoryF
import com.tidal.sdk.player.playbackengine.model.Event
import com.tidal.sdk.player.playbackengine.model.Event.MediaProductEnded
import com.tidal.sdk.player.repeatableflakytest.RepeatableFlakyTest
import com.tidal.sdk.player.repeatableflakytest.RepeatableFlakyTestRule
import com.tidal.sdk.player.setBodyFromFile
import kotlin.time.Duration.Companion.milliseconds
Expand Down Expand Up @@ -55,6 +54,8 @@ import org.junit.Before
import org.junit.BeforeClass
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import org.mockito.Mockito.atMost
import org.mockito.Mockito.mock
import org.mockito.Mockito.verifyNoMoreInteractions
Expand All @@ -63,7 +64,8 @@ import org.mockito.kotlin.argThat
import org.mockito.kotlin.eq
import org.mockito.kotlin.verify

class PlayLogTest {
@RunWith(Parameterized::class)
internal class SingleMediaProductPlayLogTest(private val mediaProduct: MediaProduct) {

@get:Rule
val server = MockWebServer()
Expand All @@ -79,6 +81,16 @@ class PlayLogTest {

@Before
fun setUp() {
responseDispatcher[
"https://api.tidal.com/v1/tracks/${mediaProduct.productId}/playbackinfo?playbackmode=STREAM&assetpresentation=FULL&audioquality=LOW&immersiveaudio=true".toHttpUrl(),
] = {
MockResponse().setBodyFromFile(
"api-responses/playbackinfo/tracks/playlogtest/get_1_bts.json",
)
}
responseDispatcher["https://test.audio.tidal.com/1_bts.m4a".toHttpUrl()] = {
MockResponse().setBodyFromFile("raw/playlogtest/1_bts.m4a")
}
EventReporterModuleRoot.reflectionComponentFactoryF = {
PlayLogTestDefaultEventReporterComponentFactory(eventReporterCoroutineScope)
}
Expand Down Expand Up @@ -130,6 +142,15 @@ class PlayLogTest {
originalEventReporterComponentFactoryF =
EventReporterModuleRoot.reflectionComponentFactoryF
}

@JvmStatic
@Parameterized.Parameters
fun parameters() = setOf(
MediaProduct(ProductType.TRACK, "1", "TESTA", "456"),
MediaProduct(ProductType.TRACK, "1", null, "789"),
MediaProduct(ProductType.TRACK, "1", "TESTB", null),
MediaProduct(ProductType.TRACK, "1", null, null),
)
}

@After
Expand All @@ -147,38 +168,8 @@ class PlayLogTest {
verifyNoMoreInteractions(eventSender)
}

@RepeatableFlakyTest // Playback may take longer in practice than it should in theory
@Test
fun loadAndPlayUntilEndNoNulls() =
loadAndPlayUntilEnd(MediaProduct(ProductType.TRACK, "1", "TESTA", "456"))

@RepeatableFlakyTest // Playback may take longer in practice than it should in theory
@Test
fun loadAndPlayUntilEndNullSourceType() =
loadAndPlayUntilEnd(MediaProduct(ProductType.TRACK, "1", null, "789"))

@RepeatableFlakyTest // Playback may take longer in practice than it should in theory
@Test
fun loadAndPlayUntilEndNullSourceId() =
loadAndPlayUntilEnd(MediaProduct(ProductType.TRACK, "1", "TESTB", null))

@RepeatableFlakyTest // Playback may take longer in practice than it should in theory
@Test
fun loadAndPlayUntilEndNullSourceTypeNullSourceId() =
loadAndPlayUntilEnd(MediaProduct(ProductType.TRACK, "1", null, null))

private fun loadAndPlayUntilEnd(mediaProduct: MediaProduct) = runTest {
responseDispatcher[
"https://api.tidal.com/v1/tracks/1/playbackinfo?playbackmode=STREAM&assetpresentation=FULL&audioquality=LOW&immersiveaudio=true".toHttpUrl(),
] = {
MockResponse().setBodyFromFile(
"api-responses/playbackinfo/tracks/playlogtest/get_1_bts.json"
)
}
responseDispatcher["https://test.audio.tidal.com/1_bts.m4a".toHttpUrl()] = {
MockResponse().setBodyFromFile("raw/playlogtest/1_bts.m4a")
}

fun loadAndPlayUntilEnd() = runTest {
player.playbackEngine.load(mediaProduct)
player.playbackEngine.play()
withContext(Dispatchers.Default.limitedParallelism(1)) {
Expand Down Expand Up @@ -208,35 +199,10 @@ class PlayLogTest {
)
}

@Test
fun loadAndPlayThenPauseThenPlayNoNulls() =
loadAndPlayThenPauseThenPlay(MediaProduct(ProductType.TRACK, "1", "TESTA", "456"))

@Test
fun loadAndPlayThenPauseThenPlayNullSourceType() =
loadAndPlayThenPauseThenPlay(MediaProduct(ProductType.TRACK, "1", null, "789"))

@Test
fun loadAndPlayThenPauseThenPlayNullSourceId() =
loadAndPlayThenPauseThenPlay(MediaProduct(ProductType.TRACK, "1", "TESTB", null))

@Test
fun loadAndPlayThenPauseThenPlayNullSourceTypeNullSourceId() =
loadAndPlayThenPauseThenPlay(MediaProduct(ProductType.TRACK, "1", null, null))

@Suppress("LongMethod")
private fun loadAndPlayThenPauseThenPlay(mediaProduct: MediaProduct) = runTest {
@Test
fun loadAndPlayThenPauseThenPlay() = runTest {
val gson = Gson()
responseDispatcher[
"https://api.tidal.com/v1/tracks/1/playbackinfo?playbackmode=STREAM&assetpresentation=FULL&audioquality=LOW&immersiveaudio=true".toHttpUrl(),
] = {
MockResponse().setBodyFromFile(
"api-responses/playbackinfo/tracks/playlogtest/get_1_bts.json",
)
}
responseDispatcher["https://test.audio.tidal.com/1_bts.m4a".toHttpUrl()] = {
MockResponse().setBodyFromFile("raw/playlogtest/1_bts.m4a")
}

player.playbackEngine.load(mediaProduct)
player.playbackEngine.play()
Expand Down

0 comments on commit 9566935

Please sign in to comment.