package com.cottondb.android.ui.media.video import android.os.SystemClock import androidx.activity.ComponentActivity import androidx.compose.foundation.layout.size import androidx.compose.runtime.mutableStateOf import androidx.compose.ui.Modifier import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.compose.ui.test.onAllNodesWithContentDescription import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.performClick import androidx.compose.ui.unit.dp import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.platform.app.InstrumentationRegistry import com.cottondb.android.ScreenEvidence import com.cottondb.android.media.LocalMediaFile import com.cottondb.android.testmedia.SyntheticVideoFixture import org.junit.Assert.assertTrue import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith /** Proves the shipping pane acquires platform audio focus and drives its real SurfaceView to completion. */ @RunWith(AndroidJUnit4::class) class VideoPlayerPaneTest { @get:Rule val compose = createAndroidComposeRule() private val instrumentation = InstrumentationRegistry.getInstrumentation() @Test fun paneUsesProductionFocusAndCompletesRenderedVideo() { SyntheticVideoFixture.create(instrumentation.targetContext, frameCount = 20).use { fixture -> val visible = mutableStateOf(true) try { compose.setContent { if (visible.value) VideoPlayerPane( file = LocalMediaFile(fixture.file), modifier = Modifier.size(192.dp), ) } awaitNode("Play video") compose.onNodeWithContentDescription("Play video").performClick() awaitNode("Pause video") ScreenEvidence.capture("video-player-pane-device-frame") { // Compose capture excludes some separately composed SurfaceView buffers. This is the // displayed system frame from the pane's actual render surface. requireNotNull(instrumentation.uiAutomation.takeScreenshot()) } awaitNode("Play video again") } finally { compose.runOnIdle { visible.value = false } compose.waitForIdle() } } } private fun awaitNode(contentDescription: String) { val deadline = SystemClock.uptimeMillis() + 5_000L while ( compose.onAllNodesWithContentDescription(contentDescription) .fetchSemanticsNodes().isEmpty() && SystemClock.uptimeMillis() < deadline ) { SystemClock.sleep(20) } assertTrue( "timed out waiting for $contentDescription", compose.onAllNodesWithContentDescription(contentDescription).fetchSemanticsNodes().isNotEmpty(), ) } }