141 lines · 6.4 KB · KotlinRaw
package com.cottondb.android.ui.media.videoimport android.os.SystemClockimport androidx.test.ext.junit.runners.AndroidJUnit4import androidx.test.platform.app.InstrumentationRegistryimport com.cottondb.android.media.LocalMediaFileimport com.cottondb.android.testmedia.SyntheticVideoFixtureimport java.io.Fileimport java.util.UUIDimport java.util.concurrent.atomic.AtomicReferenceimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertTrueimport org.junit.Testimport org.junit.runner.RunWith/** Actual platform playback proof over a device-generated MP4; no network or native core is involved. */@RunWith(AndroidJUnit4::class)class AndroidVideoPlayerControllerTest { private val instrumentation = InstrumentationRegistry.getInstrumentation() private val context = instrumentation.targetContext @Test fun disposedControllerReleasesAndRefusesAnotherFile() { SyntheticVideoFixture.create(context).use { fixture -> val focus = Focus() val controller = onMain { AndroidVideoPlayerController(context, focus) } try { onMain { controller.bind(LocalMediaFile(fixture.file)) } await("prepared") { controller.state.value is VideoPlaybackState.Paused } onMain { controller.dispose() } assertEquals(VideoPlaybackState.Idle, controller.state.value) onMain { controller.bind(LocalMediaFile(fixture.file)) } assertEquals("disposed controllers refuse another file", VideoPlaybackState.Idle, controller.state.value) } finally { onMain { controller.dispose() } } } } @Test fun missingFileFailsAndBackgroundingPausesAnActiveVideo() { val missing = File(context.cacheDir, "missing-video-${UUID.randomUUID()}.mp4") val controller = onMain { AndroidVideoPlayerController(context, Focus()) } try { onMain { controller.bind(LocalMediaFile(missing)) } assertTrue(controller.state.value is VideoPlaybackState.Failed) SyntheticVideoFixture.create(context).use { fixture -> onMain { controller.bind(LocalMediaFile(fixture.file)) controller.onAppBackgrounded() } await("prepared while backgrounded") { controller.state.value is VideoPlaybackState.Paused } assertTrue("preparing in background must never autoplay", controller.state.value !is VideoPlaybackState.Playing) onMain { controller.toggle() } await("playing") { controller.state.value is VideoPlaybackState.Playing } onMain { controller.onAppBackgrounded() } assertTrue("backgrounding pauses rather than continuing playback", controller.state.value is VideoPlaybackState.Paused) } } finally { onMain { controller.dispose() } } } @Test fun seekDuringPreparingAndStaleFocusCannotTouchANewerPlayer() { SyntheticVideoFixture.create(context).use { first -> SyntheticVideoFixture.create(context).use { second -> val focus = Focus() val controller = onMain { AndroidVideoPlayerController(context, focus) } try { onMain { controller.bind(LocalMediaFile(first.file)) controller.seekTo(40) } await("first prepared") { controller.state.value is VideoPlaybackState.Paused } onMain { controller.toggle() } await("first playing") { controller.state.value is VideoPlaybackState.Playing } val oldLoss = focus.losses.single() onMain { controller.bind(LocalMediaFile(second.file)) } onMain { oldLoss.invoke() } instrumentation.waitForIdleSync() await("second prepared after stale focus") { controller.state.value is VideoPlaybackState.Paused } assertTrue("an old focus callback cannot fail the new player", controller.state.value !is VideoPlaybackState.Failed) } finally { onMain { controller.dispose() } } } } } @Test fun deniedFocusReleasesThePlayerAndAnExplicitRebindCanRetry() { SyntheticVideoFixture.create(context).use { fixture -> val focus = Focus(granted = false) val controller = onMain { AndroidVideoPlayerController(context, focus) } try { onMain { controller.bind(LocalMediaFile(fixture.file)) } await("prepared before denied focus") { controller.state.value is VideoPlaybackState.Paused } onMain { controller.toggle() } assertTrue(controller.state.value is VideoPlaybackState.Failed) focus.granted = true onMain { controller.bind(LocalMediaFile(fixture.file)) } await("prepared after explicit retry") { controller.state.value is VideoPlaybackState.Paused } onMain { controller.toggle() } await("playing after explicit retry") { controller.state.value is VideoPlaybackState.Playing } } finally { onMain { controller.dispose() } } } } private fun await(label: String, predicate: () -> Boolean) { val deadline = SystemClock.uptimeMillis() + 5_000L while (!predicate() && SystemClock.uptimeMillis() < deadline) SystemClock.sleep(20) assertTrue("timed out waiting for $label", predicate()) } private fun <T> onMain(block: () -> T): T { val value = AtomicReference<T>() val failure = AtomicReference<Throwable>() instrumentation.runOnMainSync { try { value.set(block()) } catch (thrown: Throwable) { failure.set(thrown) } } failure.get()?.let { throw it } return value.get() } private class Focus(var granted: Boolean = true) : VideoAudioFocus { var requests = 0 var abandons = 0 val losses = mutableListOf<() -> Unit>() override fun request(onLost: () -> Unit): Boolean { requests += 1 losses += onLost return granted } override fun abandon() { abandons += 1 } }}