Cotton code
73 lines · 2.9 KB · KotlinRaw
package com.cottondb.android.ui.media.videoimport android.os.SystemClockimport androidx.activity.ComponentActivityimport androidx.compose.foundation.layout.sizeimport androidx.compose.runtime.mutableStateOfimport androidx.compose.ui.Modifierimport androidx.compose.ui.test.junit4.createAndroidComposeRuleimport androidx.compose.ui.test.onAllNodesWithContentDescriptionimport androidx.compose.ui.test.onNodeWithContentDescriptionimport androidx.compose.ui.test.performClickimport androidx.compose.ui.unit.dpimport androidx.test.ext.junit.runners.AndroidJUnit4import androidx.test.platform.app.InstrumentationRegistryimport com.cottondb.android.ScreenEvidenceimport com.cottondb.android.media.LocalMediaFileimport com.cottondb.android.testmedia.SyntheticVideoFixtureimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport 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<ComponentActivity>()    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(),        )    }}