Cotton code
225 lines · 7.9 KB · KotlinRaw
package com.cottondb.android.ui.locationsimport androidx.compose.runtime.MutableStateimport androidx.compose.runtime.mutableStateOfimport androidx.compose.ui.test.assertIsNotEnabledimport androidx.compose.ui.test.isToggleableimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithContentDescriptionimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.performScrollToimport androidx.compose.ui.test.performClickimport androidx.compose.ui.test.performTextReplacementimport androidx.test.ext.junit.runners.AndroidJUnit4import com.cottondb.android.day.locations.LocationDetailimport com.cottondb.android.day.locations.LocationDetailStateimport com.cottondb.android.day.locations.LocationDraftimport com.cottondb.android.day.locations.LocationFieldimport com.cottondb.android.day.locations.LocationProjectimport com.cottondb.android.day.locations.LocationProjectLinkimport com.cottondb.android.day.locations.LocationReadimport 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 LocationDetailScreenTest {    @get:Rule    val compose = createComposeRule()    @Test    fun titleAndNoteEditsUseTheirExactControlledFields() {        val state = mutableStateOf(detailState())        val edits = mutableListOf<Pair<LocationField, String>>()        show(            state = state,            callbacks = callbacks(                edit = { field, value ->                    edits += field to value                    state.value = state.value.withDraft(                        field,                        state.value.draft(field).copy(value = value),                    )                },            ),        )        compose.onNodeWithContentDescription("Place name")            .performTextReplacement("North Library")        compose.onNodeWithContentDescription("Place notes")            .performScrollTo().performTextReplacement("Ask about the archives")        assertEquals(            listOf(                LocationField.Name to "North Library",                LocationField.Note to "Ask about the archives",            ),            edits,        )    }    @Test    fun addressSaveUsesTheAddressFieldAfterItsControlledDraftChanges() {        val state = mutableStateOf(detailState())        val saves = mutableListOf<LocationField>()        show(            state = state,            callbacks = callbacks(                edit = { field, value ->                    state.value = state.value.withDraft(                        field,                        state.value.draft(field).copy(value = value),                    )                },                save = saves::add,            ),        )        compose.onNodeWithContentDescription("More actions").performClick()        compose.onNodeWithText("Edit address").performClick()        compose.onNodeWithContentDescription("Address")            .performTextReplacement("2 Test Way")        compose.onNodeWithText("Save").performClick()        assertEquals(listOf(LocationField.Address), saves)    }    @Test    fun projectSelectionUsesTheExactProjectId() {        val state = mutableStateOf(            detailState(                projects = listOf(                    LocationProject(                        id = "project-archive",                        slug = "archive",                        name = "Archive project",                        status = "active",                        createdAt = null,                    ),                ),            ),        )        var selected: Pair<String, Boolean>? = null        show(state, callbacks = callbacks(setProject = { id, linked -> selected = id to linked }))        compose.onNodeWithContentDescription("More actions").performClick()        compose.onNodeWithText("Projects…").performClick()        compose.onNode(isToggleable()).performClick()        assertEquals("project-archive" to true, selected)    }    @Test    fun archiveNeedsConfirmationAndKeepDoesNotWrite() {        val state = mutableStateOf(detailState())        var archives = 0        show(state, callbacks = callbacks(archive = { archives += 1 }))        compose.onNodeWithContentDescription("More actions").performClick()        compose.onNodeWithText("Archive").performClick()        compose.onNodeWithText("Keep").performClick()        assertEquals(0, archives)        compose.onNodeWithContentDescription("More actions").performClick()        compose.onNodeWithText("Archive").performClick()        compose.onNodeWithText("Archive").performClick()        assertEquals(1, archives)    }    @Test    fun unknownOutcomeDisablesArchiveInsteadOfAdmittingAnotherWrite() {        val state = mutableStateOf(detailState(outcomeUnknown = true))        show(state)        compose.onNodeWithContentDescription("More actions").performClick()        compose.onNodeWithText("Archive").assertIsNotEnabled()    }    @Test    fun missingLocationAndReadFailureRemainDifferentStates() {        val state = mutableStateOf(            LocationDetailState(                read = LocationRead(                    location = null,                    projects = emptyList(),                    projectLinks = emptyList(),                ),            ),        )        show(state)        compose.onNodeWithText("This place isn't here any more.").fetchSemanticsNode()        state.value = LocationDetailState(readProblem = "Synthetic location read failure")        compose.waitForIdle()        compose.onNodeWithText("Synthetic location read failure").fetchSemanticsNode()        compose.onNodeWithText("Refresh").fetchSemanticsNode()    }    private fun show(        state: MutableState<LocationDetailState>,        callbacks: LocationDetailCallbacks = callbacks(),    ) {        compose.setContent {            CottonTheme {                LocationDetailScreen(                    state = state.value,                    clock = CLOCK,                    zone = ZoneOffset.UTC,                    callbacks = callbacks,                )            }        }    }    private fun callbacks(        refresh: () -> Unit = {},        edit: (LocationField, String) -> Unit = { _, _ -> },        save: (LocationField) -> Unit = {},        resolve: (LocationField, Boolean) -> Unit = { _, _ -> },        setProject: (String, Boolean) -> Unit = { _, _ -> },        archive: () -> Unit = {},    ): LocationDetailCallbacks = LocationDetailCallbacks(        refresh = refresh,        edit = edit,        save = save,        resolve = resolve,        setPin = {},        setProject = setProject,        archive = archive,        back = {},    )    private fun detailState(        projects: List<LocationProject> = emptyList(),        projectLinks: List<LocationProjectLink> = emptyList(),        outcomeUnknown: Boolean = false,    ): LocationDetailState = LocationDetailState(        read = LocationRead(            location = LocationDetail(                id = "location-1",                name = "Library",                address = "1 Test Way",                note = "Original note",                lat = 28.6139,                lng = 77.2090,                createdAt = "2026-09-22T10:00:00Z",            ),            projects = projects,            projectLinks = projectLinks,        ),        name = LocationDraft(value = "Library", base = "Library"),        address = LocationDraft(value = "1 Test Way", base = "1 Test Way"),        note = LocationDraft(value = "Original note", base = "Original note"),        outcomeUnknown = outcomeUnknown,    )    private companion object {        val CLOCK: Clock = Clock.fixed(Instant.parse("2026-09-22T12:00:00Z"), ZoneOffset.UTC)    }}