122 lines · 4.4 KB · KotlinRaw
package com.cottondb.androidimport androidx.test.ext.junit.runners.AndroidJUnit4import com.cottondb.android.capture.CaptureSpeechEventimport com.cottondb.android.core.CottonCallingimport com.cottondb.android.core.CottonEventimport com.cottondb.android.core.MalformedCoreEventExceptionimport com.cottondb.android.take.TakePhaseimport com.cottondb.android.take.TakeVoiceimport kotlinx.coroutines.CoroutineScopeimport kotlinx.coroutines.Dispatchersimport kotlinx.coroutines.SupervisorJobimport kotlinx.coroutines.cancelimport kotlinx.coroutines.runBlockingimport kotlinx.coroutines.withContextimport kotlinx.coroutines.flow.MutableSharedFlowimport kotlinx.coroutines.flow.SharedFlowimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.JsonElementimport kotlinx.serialization.json.JsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertNullimport org.junit.Assert.assertTrueimport org.junit.Testimport org.junit.runner.RunWith@RunWith(AndroidJUnit4::class)class TakeModelsTest { @Test fun permissionDecisionStartsArrivalOnlyAfterGrantAndNeverTwice() = runBlocking { withContext(Dispatchers.Main) { val core = TakeCore() val voice = TakeVoiceFake() val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate) val models = TakeModels(core, scope, scope, monotonicMillis = { 100L }) try { val model = models.take("take-visit") { voice } // Creating a retained visit must not open the microphone before the host's result callback. assertNull(voice.listener) model.arrive()?.join() assertTrue(voice.listener != null) assertEquals(1, core.calls) assertNull(model.arrive()) models.retain(emptySet()) } finally { models.retain(emptySet()); scope.cancel() } } } @Test fun retiredVisitClosesItsVoiceAndLateSpeechCannotCreateWrites() = runBlocking { withContext(Dispatchers.Main) { val core = TakeCore() val voice = TakeVoiceFake() val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate) val models = TakeModels(core, scope, scope, monotonicMillis = { 100L }) try { val model = models.take("take-visit") { voice } model.arrive()?.join() val late = voice.listener ?: error("Take voice did not start") models.retain(emptySet()) late(CaptureSpeechEvent.Final("late synthetic words")) assertTrue(voice.cancelled > 0) assertEquals(1, core.calls) } finally { models.retain(emptySet()); scope.cancel() } } } @Test fun backgroundReleasesMicAndKeepsSettledWordsWithoutWritingThem() = runBlocking { withContext(Dispatchers.Main) { val core = TakeCore() val voice = TakeVoiceFake() val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate) val models = TakeModels(core, scope, scope, monotonicMillis = { 100L }) try { val model = models.take("take-visit") { voice } model.arrive()?.join() voice.listener?.invoke(CaptureSpeechEvent.Partial("keep these synthetic words")) models.onAppBackgrounded() assertTrue(voice.cancelled > 0) assertEquals(TakePhase.Settled, model.state.value.phase) assertEquals("keep these synthetic words", model.state.value.text) assertEquals(1, core.calls) models.retain(emptySet()) } finally { models.retain(emptySet()); scope.cancel() } } } private class TakeVoiceFake : TakeVoice { var listener: ((CaptureSpeechEvent) -> Unit)? = null var cancelled = 0 override fun start( biasTerms: List<String>, listener: (CaptureSpeechEvent) -> Unit, ) { this.listener = listener } override fun finish() = Unit override fun cancel() { cancelled += 1 } } private class TakeCore : CottonCalling { override val events: SharedFlow<CottonEvent> = MutableSharedFlow() override val eventErrors: SharedFlow<MalformedCoreEventException> = MutableSharedFlow() var calls = 0 override suspend fun call(method: String, request: JsonObject): JsonElement { calls += 1 check(method == "read.take") return Json.parseToJsonElement( """{"corpusCount":0,"vocabularyTerms":[],"biasTerms":[]}""", ) } override suspend fun close() = Unit }}