Cotton code
78 lines · 3.3 KB · KotlinRaw
package com.cottondb.android.ui.meetingsimport androidx.test.ext.junit.runners.AndroidJUnit4import androidx.test.platform.app.InstrumentationRegistryimport com.cottondb.android.media.LocalMediaFileimport java.io.Fileimport java.nio.ByteBufferimport java.nio.ByteOrderimport kotlinx.coroutines.flow.firstimport kotlinx.coroutines.runBlockingimport kotlinx.coroutines.withTimeoutimport org.junit.Assert.assertTrueimport org.junit.Testimport org.junit.runner.RunWith@RunWith(AndroidJUnit4::class)class AndroidMeetingPlayerControllerTest {    @Test    fun productionMediaPlayerPreparesSeeksAndCompletesGeneratedSilentWav() {        val instrumentation = InstrumentationRegistry.getInstrumentation()        val file = File(instrumentation.targetContext.cacheDir, "meeting-player-silent.wav")        writeSilentWav(file)        val focus = Focus()        var controller: AndroidMeetingPlayerController? = null        try {            instrumentation.runOnMainSync {                controller = AndroidMeetingPlayerController(instrumentation.targetContext, focus).also {                    it.bind(LocalMediaFile(file))                }            }            val active = requireNotNull(controller)            awaitState(active) { it is MeetingPlaybackState.Paused }            instrumentation.runOnMainSync { active.onAppBackgrounded() }            assertTrue(active.state.value is MeetingPlaybackState.Idle)            instrumentation.runOnMainSync { active.bind(LocalMediaFile(file)) }            awaitState(active) { it is MeetingPlaybackState.Paused }            instrumentation.runOnMainSync { active.seekTo(250) }            assertTrue(active.state.value is MeetingPlaybackState.Paused)            instrumentation.runOnMainSync { active.toggle() }            awaitState(active) { it is MeetingPlaybackState.Playing || it is MeetingPlaybackState.Completed }            awaitState(active, timeoutSeconds = 4) { it is MeetingPlaybackState.Completed }        } finally {            instrumentation.runOnMainSync { controller?.release() }            file.delete()        }    }    private fun awaitState(        controller: AndroidMeetingPlayerController,        timeoutSeconds: Long = 2,        predicate: (MeetingPlaybackState) -> Boolean,    ) {        runBlocking {            withTimeout(timeoutSeconds * 1_000) {                controller.state.first(predicate)            }        }        Unit    }    private fun writeSilentWav(file: File) {        val sampleRate = 8_000        val samples = ByteArray(sampleRate * 2)        file.outputStream().use { out ->            fun bytes(text: String) = out.write(text.encodeToByteArray())            fun int(value: Int) = out.write(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(value).array())            fun short(value: Int) = out.write(ByteBuffer.allocate(2).order(ByteOrder.LITTLE_ENDIAN).putShort(value.toShort()).array())            bytes("RIFF"); int(36 + samples.size); bytes("WAVEfmt "); int(16)            short(1); short(1); int(sampleRate); int(sampleRate * 2); short(2); short(16)            bytes("data"); int(samples.size); out.write(samples)        }    }    private class Focus : MeetingAudioFocus {        override fun request(onInterrupted: () -> Unit) = true        override fun abandon() = Unit    }}