Cotton code
146 lines · 5.1 KB · KotlinRaw
package com.cottondb.android.ui.locationsimport androidx.compose.runtime.mutableStateOfimport androidx.compose.ui.test.hasSetTextActionimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithContentDescriptionimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.performClickimport androidx.compose.ui.test.performTextInputimport androidx.test.ext.junit.runners.AndroidJUnit4import com.cottondb.android.day.locations.LocationRowimport com.cottondb.android.day.locations.LocationsStateimport com.cottondb.android.design.CottonThemeimport java.time.Clockimport java.time.Instantimport java.time.ZoneOffsetimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWith@RunWith(AndroidJUnit4::class)class LocationsScreenTest {    @get:Rule    val compose = createComposeRule()    @Test    fun emptyStateOpensTheControlledNewPlaceDialog() {        val state = mutableStateOf(LocationsState(rows = emptyList()))        var opens = 0        show(state, onCreateOpen = { opens += 1; state.value = state.value.copy(composing = true) })        compose.onNodeWithText("Add a place").performClick()        compose.onAllNodes(hasSetTextAction())[0].fetchSemanticsNode()        assertEquals(1, opens)    }    @Test    fun newPlaceFieldsAndAdmissionAreControlledBySuppliedState() {        val state = mutableStateOf(LocationsState(rows = emptyList(), composing = true))        var creates = 0        show(            state = state,            onName = { state.value = state.value.copy(name = it) },            onAddress = { state.value = state.value.copy(address = it) },            onCreate = { creates += 1 },        )        compose.onAllNodes(hasSetTextAction())[0].performTextInput("Library")        compose.onAllNodes(hasSetTextAction())[1].performTextInput("1 Test Way")        compose.onNodeWithText("Add place").performClick()        assertEquals("Library", state.value.name)        assertEquals("1 Test Way", state.value.address)        assertEquals(1, creates)    }    @Test    fun cachedRowsStayVisibleWithNamedReadFailureAndRetry() {        val state = mutableStateOf(            LocationsState(                rows = listOf(row("location-1", "Library")),                readProblem = "Synthetic direct read failure",            ),        )        var refreshes = 0        var opened: String? = null        show(state, onRefresh = { refreshes += 1 }, onOpen = { opened = it })        compose.onNodeWithText("Synthetic direct read failure").fetchSemanticsNode()        compose.onNodeWithContentDescription("Open Library").performClick()        compose.onNodeWithText("Try again").performClick()        assertEquals("location-1", opened)        assertEquals(1, refreshes)    }    @Test    fun recentAppearsOnlyAboveTheThresholdAndLayoutIsControlled() {        val state = mutableStateOf(            LocationsState(                rows = listOf(                    row("one", "One", "2026-09-22T10:00:00Z"),                    row("two", "Two", "2026-09-21T10:00:00Z"),                    row("three", "Three", "2026-09-20T10:00:00Z"),                    row("four", "Four", "2026-09-19T10:00:00Z"),                ),            ),        )        val layouts = mutableListOf<LocationLayout>()        show(state, onLayout = layouts::add)        compose.onNodeWithText("Recent").fetchSemanticsNode()        compose.onNodeWithContentDescription("Change locations layout").performClick()        assertEquals(listOf(LocationLayout.List), layouts)        assertTrue(state.value.rows!!.size > 3)    }    private fun show(        state: androidx.compose.runtime.MutableState<LocationsState>,        onRefresh: () -> Unit = {},        onCreateOpen: () -> Unit = {},        onName: (String) -> Unit = {},        onAddress: (String) -> Unit = {},        onCreate: () -> Unit = {},        onOpen: (String) -> Unit = {},        onLayout: (LocationLayout) -> Unit = {},    ) {        compose.setContent {            CottonTheme {                LocationsScreen(                    state = state.value,                    layout = LocationLayout.Postcards,                    clock = CLOCK,                    zone = ZoneOffset.UTC,                    onRefresh = onRefresh,                    onCreateOpen = onCreateOpen,                    onCreateCancel = {},                    onName = onName,                    onAddress = onAddress,                    onCreate = onCreate,                    onOpen = onOpen,                    onLayout = onLayout,                )            }        }    }    private fun row(id: String, name: String, updatedAt: String? = null) = LocationRow(        id = id,        name = name,        address = "Test address",        lat = null,        lng = null,        note = null,        updatedAt = updatedAt,        linkCount = 0L,    )    private companion object {        val CLOCK: Clock = Clock.fixed(Instant.parse("2026-09-22T12:00:00Z"), ZoneOffset.UTC)    }}