Created
April 29, 2022 08:32
-
-
Save yschimke/29a3b925589fd4b9d2940c1886ab57fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(AndroidJUnit4::class) | |
class OkHttpPlayerTest { | |
@Test | |
fun handlesFastCancellations() { | |
assumeInternet() | |
val context = InstrumentationRegistry.getInstrumentation().targetContext | |
val okHttpClient = OkHttpClient.Builder() | |
.build() | |
val dataSourceFactory: OkHttpDataSource.Factory = OkHttpDataSource.Factory(okHttpClient) | |
val dashFactory = DashMediaSource.Factory(dataSourceFactory) | |
val mediaSource = dashFactory.createMediaSource(MediaItem.Builder() | |
.setUri("https://storage.googleapis.com/wvmedia/clear/h264/tears/tears.mpd") | |
.build()) | |
val duration: Long = 734000 | |
val actionSchedule = ActionSchedule.Builder("handlesFastCancellations") | |
.play() | |
.waitForIsLoading(true) | |
.delay(1000) | |
.seekIncrementally(duration = duration.downTo(duration - 600_000).step(10_000)) | |
.seekAndWait(duration / 2) | |
.seekIncrementally(duration = duration.downTo(duration - 600_000).step(10_000)) | |
.seekAndWait(duration / 2) | |
.seekIncrementally(duration = duration.downTo(duration - 600_000).step(10_000)) | |
.seekAndWait(duration / 2) | |
.seekAndWait(duration - 2_000) | |
.build() | |
ExoPlayerTestRunner.Builder(context) | |
.setMediaSources(mediaSource) | |
.setActionSchedule(actionSchedule) | |
.build() | |
.start() | |
.blockUntilActionScheduleFinished(TIMEOUT_MS.toLong()) | |
.blockUntilEnded(TIMEOUT_MS.toLong()) | |
} | |
private fun assumeInternet() { | |
try { | |
InetAddress.getByName("www.google.com") | |
} catch (uhe: UnknownHostException) { | |
assumeTrue("requires network", false) | |
} | |
} | |
} | |
private fun ActionSchedule.Builder.seekIncrementally(duration: LongProgression, delay: Long = 25): ActionSchedule.Builder = | |
duration.fold(this) { builder, position -> | |
builder.seek(position) | |
.executeRunnable { | |
Thread.sleep(delay) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment