103 lines · 5.8 KB · KotlinRaw
package com.cottondb.cinemasimport android.app.Applicationimport androidx.compose.ui.test.assertIsDisplayedimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.performClickimport androidx.compose.ui.unit.dpimport com.cottondb.cinemas.stream.CoreActimport com.cottondb.cinemas.stream.Homeimport com.cottondb.cinemas.stream.Loadedimport com.cottondb.cinemas.stream.ReceivedLineimport com.cottondb.cinemas.stream.ReceivedPageimport com.cottondb.cinemas.stream.ReceivedRowimport com.cottondb.cinemas.stream.SharedListimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.JsonElementimport kotlinx.serialization.json.JsonPrimitiveimport kotlinx.serialization.json.jsonObjectimport kotlinx.serialization.json.jsonPrimitiveimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertNullimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Configimport java.io.File/** * A CINEMAS LIST SHARED WITH HIM (CONTACTS-ANSWERS 10.7, 4c95802070): Home's "Shared with you" line read from * `cinemas.home`'s `received` and drawn from it, a row opening with the call it carries (`cinemas.shared`), the page drawn * whole from `cinemas.shared`'s answer, and Leave asking its confirm before calling `cinemas.shared.leave`. Words are the * kit's `cinemas.json` `shared.*`; every name and number is invented. */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], qualifiers = "w400dp-h1400dp-mdpi", application = Application::class)class ReceivedListTest { @get:Rule val compose = createComposeRule() private val repo = generateSequence(File("").absoluteFile) { it.parentFile }.first { File(it, "apps/registry.json").isFile } private val kit = Json.parseToJsonElement(File(repo, "crates/cotton-core/kits/shell/lines/cinemas.json").readText()).jsonObject private fun w(path: String, vararg fills: Pair<String, String>): String = fills.fold(path.split(".").fold<String, JsonElement>(kit) { at, k -> at.jsonObject[k]!! }.jsonPrimitive.content) { s, (k, v) -> s.replace("{$k}", v) } private fun q(text: String) = Json.encodeToString(JsonPrimitive.serializer(), JsonPrimitive(text)) private fun j(text: String) = Json.parseToJsonElement(text).jsonObject private val owner = "+15550100007" private val detail = w("shared.detail", "name" to "Mira Example", "count" to "2") private val home = j( """{"featured":null,"rows":[],"empty":null, "received":{"title":${q(w("shared.title"))},"rows":[{"owner":"$owner","id":"cinemas-g-1","words":"Rainy Sundays","detail":${q(detail)},"initials":"ME", "call":{"method":"cinemas.shared","request":{"owner":"$owner","id":"cinemas-g-1"}}}]}}""", ) private val opened = j( """{"owner":"$owner","id":"cinemas-g-1","title":"Rainy Sundays","from":{"number":"$owner","name":"Mira Example"},"detail":${q(detail)}, "items":[{"id":"f-1","title":"An Invented Film","year":1999,"kind":"film"},{"id":"f-2","title":"Another Invented Film","year":2004,"kind":"film"}],"unopened":0, "leave":{"words":${q(w("shared.leave"))},"red":true,"confirm":${q(w("shared.leave_confirm", "name" to "Mira Example"))}, "call":{"method":"cinemas.shared.leave","request":{"owner":"$owner","id":"cinemas-g-1"}}}}""", ) @Test fun homeReadsReceivedAndNoneIsNull() { val received = Home.of(home).received!! assertEquals(w("shared.title"), received.title) val row = received.rows.single() assertEquals("cinemas.shared", row.call.method) assertEquals(j("""{"owner":"$owner","id":"cinemas-g-1"}"""), row.call.request) assertNull(Home.of(j("""{"featured":null,"rows":[],"empty":null,"received":null}""")).received) } @Test fun theLineIsDrawnFromReceivedAndARowOpensWithItsCall() { var tapped: ReceivedRow? = null compose.setContent { ReceivedLine(Home.of(home).received!!) { tapped = it } } compose.onNodeWithText(w("shared.title")).assertIsDisplayed() compose.onNodeWithText("Rainy Sundays").assertIsDisplayed() compose.onNodeWithText(detail).assertIsDisplayed() compose.onNodeWithTag("received-$owner-cinemas-g-1").performClick() assertEquals(CoreAct("cinemas.shared", j("""{"owner":"$owner","id":"cinemas-g-1"}""")), tapped?.call) } @Test fun thePageIsDrawnFromSharedAndLeaveAsksFirst() { var left: CoreAct? = null compose.setContent { ReceivedPage(Loaded.Ready(SharedList.of(opened)), back = "Back", cancel = "Cancel", bottom = 0.dp, onBack = {}, onRetry = {}, onLeave = { left = it }) } compose.onNodeWithText("RAINY SUNDAYS").assertIsDisplayed() compose.onNodeWithText(detail).assertIsDisplayed() compose.onNodeWithText("An Invented Film").assertIsDisplayed() compose.onNodeWithText("2004").assertIsDisplayed() compose.onNodeWithTag("received-leave").performClick() assertNull(left) compose.onNodeWithText(w("shared.leave_confirm", "name" to "Mira Example")).assertIsDisplayed() compose.onNodeWithTag("received-leave-no").performClick() assertNull(left) compose.onNodeWithTag("received-leave").performClick() compose.onNodeWithTag("received-leave-yes").performClick() assertEquals(CoreAct("cinemas.shared.leave", j("""{"owner":"$owner","id":"cinemas-g-1"}""")), left) } @Test fun aRefusalIsTheCoresLine() { compose.setContent { ReceivedPage(Loaded.Failed("The list could not be read."), back = "Back", cancel = "Cancel", bottom = 0.dp, onBack = {}, onRetry = {}, onLeave = {}) } compose.onNodeWithTag("received-refused").assertIsDisplayed() }}