Cotton code
150 lines · 5.6 KB · KotlinRaw
package com.cottondb.android.ui.activityimport androidx.compose.runtime.mutableStateOfimport androidx.compose.ui.graphics.asAndroidBitmapimport androidx.compose.ui.test.assertIsNotEnabledimport androidx.compose.ui.test.captureToImageimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithContentDescriptionimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.onRootimport androidx.compose.ui.test.performClickimport androidx.test.ext.junit.runners.AndroidJUnit4import com.cottondb.android.ScreenEvidenceimport com.cottondb.android.activitybin.BinEntryimport com.cottondb.android.activitybin.BinReadimport com.cottondb.android.design.CottonThemeimport java.time.Clockimport java.time.Instantimport java.time.ZoneOffsetimport org.junit.Assert.assertEqualsimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWith@RunWith(AndroidJUnit4::class)class BinScreenTest {    @get:Rule val compose = createComposeRule()    @Test    fun emptyConfirmationCancelsThenRechecksWriteAdmissionBeforeEmptying() {        val calls = Calls()        val canWrite = mutableStateOf(true)        show(read(), canWrite = canWrite, calls = calls)        ScreenEvidence.capture("bin-list") { compose.onRoot().captureToImage().asAndroidBitmap() }        compose.onNodeWithContentDescription("Empty the bin").performClick()        compose.onNodeWithText("Cancel").performClick()        assertEquals(0, calls.empty)        compose.onNodeWithContentDescription("Empty the bin").performClick()        compose.runOnIdle { canWrite.value = false }        compose.onNodeWithContentDescription("Confirm empty bin").assertIsNotEnabled()        assertEquals(0, calls.empty)        compose.runOnIdle { canWrite.value = true }        compose.onNodeWithContentDescription("Confirm empty bin").performClick()        assertEquals(1, calls.empty)    }    @Test    fun restoreIsExplicitAndCachedReadFailureRetainsRowsButBlocksWritesAndRetries() {        val calls = Calls()        show(read(), readProblem = "Replica unavailable", canWrite = mutableStateOf(false), calls = calls)        compose.onNodeWithText("Replica unavailable").assertExists()        compose.onNodeWithContentDescription("Restore Invoice 1").assertIsNotEnabled()        compose.onNodeWithContentDescription("Empty the bin").assertIsNotEnabled()        compose.onNodeWithContentDescription("Try again").performClick()        assertEquals(1, calls.retry)        assertEquals(emptyList<BinEntry>(), calls.restored)    }    @Test    fun restoreAndLayoutToggleDelegateWithoutOpeningRows() {        val calls = Calls()        show(read(), isGrid = false, calls = calls)        compose.onNodeWithContentDescription("Change bin layout").performClick()        compose.onNodeWithContentDescription("Restore Invoice 1").performClick()        assertEquals(true, calls.layout)        assertEquals(listOf(BinEntry("email", "email-1", "Invoice 1", "2026-09-22T11:00:00Z")), calls.restored)    }    @Test    fun initialFailureOffersRetry() {        val calls = Calls()        show(read = null, loading = false, readProblem = "Offline", calls = calls)        compose.onNodeWithText("Couldn't read the bin").assertExists()        compose.onNodeWithContentDescription("Try again").performClick()        assertEquals(1, calls.retry)    }    @Test    fun gridKeepsAnExplicitRestoreForEachKind() {        val calls = Calls()        show(read(), isGrid = true, calls = calls)        ScreenEvidence.capture("bin-grid") { compose.onRoot().captureToImage().asAndroidBitmap() }        compose.onNodeWithContentDescription("Restore Invoice 1").performClick()        assertEquals(listOf("email-1"), calls.restored.map { it.id })    }    @Test    fun oneItemBinHasNoLayoutToggle() {        val one = Calls()        show(BinRead(listOf(BinEntry("email", "one", "One message", "2026-09-22T11:00:00Z")), mapOf("email" to 1L), null), calls = one)        compose.onNodeWithContentDescription("Change bin layout").assertDoesNotExist()    }    private fun show(        read: BinRead?,        loading: Boolean = false,        readProblem: String? = null,        writeProblem: String? = null,        canWrite: androidx.compose.runtime.MutableState<Boolean> = mutableStateOf(true),        isGrid: Boolean = false,        calls: Calls,    ) {        compose.setContent {            CottonTheme {                BinScreen(                    read = read,                    loading = loading,                    readProblem = readProblem,                    writeProblem = writeProblem,                    canWrite = canWrite.value,                    isGrid = isGrid,                    onLayoutChange = { calls.layout = it },                    onRetry = { calls.retry += 1 },                    onRestore = { calls.restored += it },                    onEmpty = { calls.empty += 1 },                    onBack = { calls.back += 1 },                    clock = CLOCK,                )            }        }    }    private fun read() = BinRead(        entries = listOf(            BinEntry("email", "email-1", "Invoice 1", "2026-09-22T11:00:00Z"),            BinEntry("secret", "secret-1", "Synthetic password", "2026-09-21T11:00:00Z"),        ),        counts = mapOf("email" to 1L, "secret" to 1L),        note = null,    )    private class Calls {        var retry = 0        var empty = 0        var back = 0        var layout: Boolean? = null        val restored = mutableListOf<BinEntry>()    }    private companion object {        val CLOCK: Clock = Clock.fixed(Instant.parse("2026-09-22T12:00:00Z"), ZoneOffset.UTC)    }}