Cotton code
111 lines · 4.4 KB · KotlinRaw
package com.cottondb.android.keyboardimport androidx.test.ext.junit.runners.AndroidJUnit4import androidx.test.platform.app.InstrumentationRegistryimport androidx.test.uiautomator.Byimport androidx.test.uiautomator.UiDeviceimport androidx.test.uiautomator.Untilimport com.cottondb.android.capture.CaptureSpeechEventimport com.cottondb.android.platform.PlatformTakeVoiceimport com.cottondb.android.take.TakeVoiceimport java.util.concurrent.CountDownLatchimport java.util.concurrent.TimeUnitimport java.util.concurrent.atomic.AtomicReferenceimport org.junit.Afterimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertTrueimport org.junit.Assume.assumeTrueimport org.junit.Testimport org.junit.runner.RunWith/** * Emulator-gated end-to-end handback proof. * * The debug-only voice seam supplies only synthetic recognizer output. The actual production * activity, boot model, Take writer, keyboard handback model, shared channel, IME and external * editor all remain in the path. */@RunWith(AndroidJUnit4::class)class ImeTakeHandbackTest {    private val instrumentation = InstrumentationRegistry.getInstrumentation()    private val device = UiDevice.getInstance(instrumentation)    private val voice = FinalVoice("synthetic keyboard handback")    private lateinit var fixture: ImeTestFixture    @After    fun restoreDevice() {        try {            if (::fixture.isInitialized) fixture.closeExternalEditorIfOpen()        } finally {            PlatformTakeVoice.testFactory = null            if (::fixture.isInitialized) fixture.restore()        }    }    @Test    fun speakFinalReturnPublishesThenAutoInsertsOnce() {        assumeTrue(InstrumentationRegistry.getArguments().getString("cotton.ime") == "true")        fixture = ImeTestFixture()        fixture.enableCottonImeAndMicrophone()        fixture.bootReadySyntheticWorkspace()        PlatformTakeVoice.testFactory = { voice }        fixture.launchExternalEditor()        await(By.desc("Speak")).click()        assertTrue("Take never started the injected recognizer.", voice.awaitStart())        capture("ime-handback-listening")        await(By.desc("Done")).click()        instrumentation.runOnMainSync { voice.final() }        await(By.desc("Return to keyboard"))        capture("ime-handback-settled")        await(By.desc("Return to keyboard")).click()        val editor = await(By.desc("External keyboard editor"))        val expected = "External synthetic keyboard handback"        assertTrue("The returned take was not inserted into the external editor.", editor.wait(Until.textEquals(expected), UI_TIMEOUT_MILLIS))        fixture.awaitTakeAcknowledgement("synthetic keyboard handback")        // Receipt handling can recreate the IME; the editor must still contain one insertion.        device.waitForIdle()        assertEquals(expected, await(By.desc("External keyboard editor")).text)        capture("ime-handback-inserted")    }    private fun await(selector: androidx.test.uiautomator.BySelector): androidx.test.uiautomator.UiObject2 {        assertTrue("Timed out waiting for $selector", device.wait(object : androidx.test.uiautomator.Condition<UiDevice, Boolean> {            override fun apply(root: UiDevice): Boolean {                if (android.os.Build.VERSION.SDK_INT >= 34) instrumentation.uiAutomation.clearCache()                return root.hasObject(selector)            }        }, UI_TIMEOUT_MILLIS))        return requireNotNull(device.findObject(selector))    }    private fun capture(name: String) {        com.cottondb.android.ScreenEvidence.capture(name) { requireNotNull(instrumentation.uiAutomation.takeScreenshot()) }    }    private class FinalVoice(private val text: String) : TakeVoice {        private val listener = AtomicReference<((CaptureSpeechEvent) -> Unit)?>(null)        private val started = CountDownLatch(1)        override fun start(biasTerms: List<String>, listener: (CaptureSpeechEvent) -> Unit) {            this.listener.set(listener)            started.countDown()        }        override fun finish() = Unit        override fun cancel() = Unit        fun awaitStart(): Boolean = started.await(UI_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)        fun final() {            requireNotNull(listener.get()) { "Take finished before its recognizer started." }                .invoke(CaptureSpeechEvent.Final(text))        }    }    private companion object {        const val UI_TIMEOUT_MILLIS = 12_000L    }}