package com.cottondb.android.keyboard import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.platform.app.InstrumentationRegistry import androidx.test.uiautomator.By import androidx.test.uiautomator.UiDevice import androidx.test.uiautomator.Until import com.cottondb.android.capture.CaptureSpeechEvent import com.cottondb.android.platform.PlatformTakeVoice import com.cottondb.android.take.TakeVoice import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit import java.util.concurrent.atomic.AtomicReference import org.junit.After import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import org.junit.Assume.assumeTrue import org.junit.Test import 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 { 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, 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 } }