Cotton code
92 lines · 4.1 KB · KotlinRaw
package com.cottondb.android.ui.keyboardimport androidx.compose.ui.graphics.asAndroidBitmapimport androidx.compose.ui.test.captureToImageimport androidx.compose.ui.test.onRootimport androidx.compose.ui.test.onNodeWithTextimport com.cottondb.android.ScreenEvidenceimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithContentDescriptionimport androidx.compose.ui.test.performClickimport androidx.test.ext.junit.runners.AndroidJUnit4import com.cottondb.android.design.CottonThemeimport com.cottondb.android.keyboard.KeyboardSnapshotimport com.cottondb.android.keyboard.KeyboardTakeimport kotlinx.serialization.json.JsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWith@RunWith(AndroidJUnit4::class)class KeyboardSurfaceTest {    @get:Rule val compose = createComposeRule()    @Test fun typingShiftLayerToggleAndBackStayControlled() {        val typed = mutableListOf<String>()        setContent(onType = typed::add)        ScreenEvidence.capture("keyboard-speak") { compose.onRoot().captureToImage().asAndroidBitmap() }        compose.onNodeWithContentDescription("abc").performClick()        ScreenEvidence.capture("keyboard-letters") { compose.onRoot().captureToImage().asAndroidBitmap() }        compose.onNodeWithContentDescription("Shift").performClick()        compose.onNodeWithContentDescription("Q").performClick()        compose.onNodeWithContentDescription("w").performClick()        compose.onNodeWithContentDescription("123").performClick()        ScreenEvidence.capture("keyboard-symbols") { compose.onRoot().captureToImage().asAndroidBitmap() }        compose.onNodeWithContentDescription("1").performClick()        compose.onNodeWithContentDescription("abc").performClick()        compose.onNodeWithContentDescription("Back").performClick()        assertEquals(listOf("Q", "w", "1"), typed)    }    @Test fun doubleShiftLocksUsingInjectedMonotonicTime() {        val typed = mutableListOf<String>()        var now = 100L        setContent(onType = typed::add, nowMillis = { now })        compose.onNodeWithContentDescription("abc").performClick()        compose.onNodeWithContentDescription("Shift").performClick()        now = 200L        compose.onNodeWithContentDescription("Shift").performClick()        compose.onNodeWithContentDescription("A").performClick()        compose.onNodeWithContentDescription("B").performClick()        assertEquals(listOf("A", "B"), typed)    }    @Test fun trayPickUsesOneTakeAndClosesBackToSpeak() {        var used: KeyboardTake? = null        setContent(snapshot = snapshot(), onUseTake = { used = it })        compose.onNodeWithContentDescription("Tray 1").performClick()        ScreenEvidence.capture("keyboard-tray") { compose.onRoot().captureToImage().asAndroidBitmap() }        compose.onNodeWithText("synthetic take").performClick()        assertEquals("take-1", used?.takeId)        compose.onNodeWithContentDescription("abc").performClick()    }    @Test fun returnAndDeleteRemainAvailableOnBothTypingLayers() {        setContent()        compose.onNodeWithContentDescription("abc").performClick()        compose.onNodeWithContentDescription("Return").assertExists()        compose.onNodeWithContentDescription("Delete").assertExists()        compose.onNodeWithContentDescription("123").performClick()        compose.onNodeWithContentDescription("Return").assertExists()        compose.onNodeWithContentDescription("Delete").assertExists()    }    private fun setContent(        snapshot: KeyboardSnapshot? = null,        nowMillis: () -> Long = { 0L },        onType: (String) -> Unit = {},        onUseTake: (KeyboardTake) -> Unit = {},    ) = compose.setContent {        CottonTheme {            KeyboardSurface(snapshot, null, null, onType, {}, {}, onUseTake, {}, {}, nowMillis)        }    }    private fun snapshot() = KeyboardSnapshot(        raw = JsonObject(emptyMap()),        asOf = null,        settings = null,        takes = listOf(KeyboardTake("take-1", "synthetic take", 2, "2026-09-22T00:00:00Z", false)),    )}