149 lines · 7.4 KB · KotlinRaw
package com.cottondb.android.ui.locationsimport androidx.compose.runtime.keyimport androidx.compose.runtime.mutableLongStateOfimport androidx.compose.ui.graphics.asAndroidBitmapimport androidx.compose.ui.test.captureToImageimport androidx.compose.ui.test.onRootimport com.cottondb.android.ScreenEvidenceimport 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.performTextReplacementimport androidx.compose.ui.test.performTextClearanceimport androidx.compose.ui.test.performTextInputimport androidx.test.ext.junit.runners.AndroidJUnit4import com.cottondb.android.LocationsModelsimport com.cottondb.android.core.CottonCallingimport com.cottondb.android.core.CottonEventimport com.cottondb.android.core.MalformedCoreEventExceptionimport com.cottondb.android.design.CottonThemeimport com.cottondb.android.navigation.Routeimport java.time.Clockimport java.time.Instantimport java.time.ZoneOffsetimport kotlinx.coroutines.CoroutineScopeimport kotlinx.coroutines.Dispatchersimport kotlinx.coroutines.SupervisorJobimport kotlinx.coroutines.cancelimport kotlinx.coroutines.flow.MutableSharedFlowimport kotlinx.coroutines.flow.SharedFlowimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.JsonElementimport kotlinx.serialization.json.JsonObjectimport kotlinx.serialization.json.buildJsonObjectimport kotlinx.serialization.json.jsonObjectimport kotlinx.serialization.json.jsonPrimitiveimport kotlinx.serialization.json.putimport org.junit.Afterimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertNullimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWith@RunWith(AndroidJUnit4::class)class LocationsRouteTest { @get:Rule val compose = createComposeRule() private var fixture: Fixture? = null @After fun close() { fixture?.let { current -> compose.runOnIdle { current.models.retain(emptyList()); current.scope.cancel() }; fixture = null } } @Test fun verifiedCreateNavigatesOnceAndDoesNotReplayOnRemount() { val f = show(Route.Locations) compose.waitUntil(5_000) { f.core.locationReads > 0 } compose.waitForIdle() ScreenEvidence.capture("locations-postcards-route") { compose.onRoot().captureToImage().asAndroidBitmap() } compose.onNodeWithContentDescription("Add a place").performClick() compose.onNodeWithContentDescription("Place name").performTextInput(" Library ") compose.onNodeWithContentDescription("Address").performTextInput(" 1 Test Way ") compose.onNodeWithText("Add place").performClick() compose.waitUntil(5_000) { f.opened.size == 1 } compose.runOnIdle { f.remount.longValue++ } compose.waitForIdle() assertEquals(listOf(Route.LocationsLocation(ID)), f.opened) assertEquals("create_location", f.core.writes.single()["tool"]!!.jsonPrimitive.content) assertEquals("Library", f.core.writes.single()["args"]!!.jsonObject["name"]!!.jsonPrimitive.content) assertEquals("1 Test Way", f.core.writes.single()["args"]!!.jsonObject["address"]!!.jsonPrimitive.content) } @Test fun detailRenameReceiptSurvivesRemountAndArchivePopsOnce() { val f = show(Route.LocationsLocation(ID)) compose.waitUntil(5_000) { f.core.detailReads > 0 } compose.waitForIdle() ScreenEvidence.capture("location-detail-route") { compose.onRoot().captureToImage().asAndroidBitmap() } compose.onNodeWithContentDescription("Place name").performTextReplacement("After") compose.onNodeWithContentDescription("Save place name").performClick() compose.waitUntil(5_000) { f.core.writes.any { it["tool"]!!.jsonPrimitive.content == "update_location" } } compose.runOnIdle { f.remount.longValue++ } compose.waitForIdle() assertEquals("After", f.models.detail(Route.LocationsLocation(ID)).state.value.name.value) compose.onNodeWithContentDescription("More actions").performClick() compose.onNodeWithText("Archive").performClick() compose.onNodeWithText("Archive").performClick() compose.waitUntil(5_000) { f.back == 1 } compose.runOnIdle { f.remount.longValue++ } compose.waitForIdle() assertEquals(1, f.back) } @Test fun retainClosesDepartingDetailRoute() { val f = show(Route.LocationsLocation(ID)) compose.waitUntil(5_000) { f.core.detailReads > 0 } lateinit var retired: com.cottondb.android.day.locations.LocationDetailModel compose.runOnIdle { retired = f.models.detail(Route.LocationsLocation(ID)); f.models.retain(emptyList()) } compose.runOnIdle { assertNull(retired.refresh()) } } private fun show(route: Route): Fixture { val fresh = Fixture(); fixture = fresh compose.setContent { CottonTheme { key(fresh.remount.longValue) { LocationsRoute(route, fresh.models, CLOCK, 0, { fresh.back += 1 }, { route -> fresh.opened += route }) } } } return fresh } private class Fixture { val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate) val core = Core() val models = LocationsModels(core, scope) val remount = mutableLongStateOf(0) val opened = mutableListOf<Route>() var back = 0 } private class Core : CottonCalling { override val events: SharedFlow<CottonEvent> = MutableSharedFlow() override val eventErrors: SharedFlow<MalformedCoreEventException> = MutableSharedFlow() var locationReads = 0 var detailReads = 0 var name = "Before" val writes = mutableListOf<JsonObject>() override suspend fun call(method: String, request: JsonObject): JsonElement = when (method) { "read.locations" -> { locationReads++; locations() } "read.location" -> { detailReads++; detail(name) } "write" -> { writes += request val tool = request["tool"]!!.jsonPrimitive.content val args = request["args"]!!.jsonObject when (tool) { "create_location" -> answer(row("Library", "1 Test Way")) "update_location" -> { args["name"]?.jsonPrimitive?.content?.let { name = it }; answer(row(name, "Address")) } "archive_location" -> answer(buildJsonObject { put("id", ID); put("name", name); put("archived", true) }) else -> error(tool) } } else -> error(method) } override suspend fun close() = Unit }}private const val ID = "01234567-89ab-cdef-0123-456789abcdef"private val CLOCK = Clock.fixed(Instant.parse("2026-01-01T00:00:00Z"), ZoneOffset.UTC)private fun answer(value: JsonObject) = buildJsonObject { put("answer", value) }private fun row(name: String, address: String?) = buildJsonObject { put("id", ID); put("name", name); put("address", address); put("lat", null); put("lng", null); put("note", null) }private fun locations() = Json.parseToJsonElement("""{"locations":[{"id":"$ID","name":"Before","address":"Address","lat":null,"lng":null,"note":null,"updatedAt":null,"linkCount":0}]}""")private fun detail(name: String) = Json.parseToJsonElement("""{"location":{"id":"$ID","name":"$name","address":"Address","note":null,"lat":null,"lng":null,"created_at":null},"projects":[],"projectLinks":[]}""")