Cotton code
103 lines · 6.6 KB · KotlinRaw
package com.cottondb.contactsimport android.app.Applicationimport androidx.activity.ComponentActivityimport androidx.compose.ui.test.assertIsDisplayedimport androidx.compose.ui.test.assertTextEqualsimport androidx.compose.ui.test.junit4.createAndroidComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.performClickimport com.cottondb.shell.objimport com.cottondb.shell.strimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.JsonObjectimport kotlinx.serialization.json.jsonObjectimport org.json.JSONObjectimport org.junit.Assert.assertEqualsimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Configimport java.io.File/** * SHELL DRAW 3: Contacts' Circle tab draws `circle.list` whole, and its No longer on Cotton section says "Left Cotton" * and "This number belongs to someone else on Cotton now" under each name, with Take off the list (circle.remove). * The answers are laid as circle.rs lays them, in the shipped circle lines' words. Invented people. */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], qualifiers = "w400dp-h860dp-mdpi", application = Application::class)class CircleGoneTest {    @get:Rule val compose = createAndroidComposeRule<ComponentActivity>()    private val repo = generateSequence(File("").absoluteFile) { it.parentFile }.first { File(it, "apps/registry.json").isFile }    private val words = JSONObject(File(repo, "crates/cotton-core/kits/shell/lines/circle.json").readText())    private fun line(path: String): String = path.split('.').let { p -> p.dropLast(1).fold(words) { o, k -> o.getJSONObject(k) }.getString(p.last()) }    private fun json(text: String): JsonObject = Json.parseToJsonElement(text).jsonObject    private fun list() = json(        """{"title": "${line("title")}", "intro": "${line("intro")}", "sections": [            {"id": "in", "title": "${line("sections.in")}", "count": 1, "rows": [{"id": "p-in", "person": "p-in", "number": "+15550100001", "words": "Ines Example", "initials": "IE",                "detail": "${line("check.checked")}", "state": "in", "acts": [], "call": {"method": "contacts.person", "request": {"id": "p-in"}}}]},            {"id": "gone", "title": "${line("sections.gone")}", "count": 2, "rows": [                {"id": "p-left", "person": "p-left", "number": "+15550100002", "words": "Lena Example", "initials": "LE", "detail": "${line("detail.left")}", "state": "left",                 "acts": [{"id": "remove", "words": "${line("acts.forget")}", "call": {"method": "circle.remove", "request": {"person": "p-left"}}}],                 "call": {"method": "contacts.person", "request": {"id": "p-left"}}},                {"id": "+15550100003", "person": null, "number": "+15550100003", "words": "Chen Example", "initials": "CE", "detail": "${line("detail.changed")}", "state": "changed",                 "acts": [{"id": "remove", "words": "${line("acts.forget")}", "call": {"method": "circle.remove", "request": {"number": "+15550100003"}}}]}]}],          "add": {"words": "${line("add.title")}", "call": {"method": "circle.add.page", "request": {}}}, "empty": null}""",    )    @Test    fun no_longer_on_cotton_says_left_and_changed_under_each_name() {        var acted: JsonObject? = null        compose.setContent { CircleList(list(), null, onRow = {}, onAct = { acted = it }, onSignIn = {}, signIn = "Sign in") }        compose.onNodeWithTag("contacts-section-gone").assertTextEquals("No longer on Cotton")        compose.onNodeWithTag("contacts-row-detail-p-left", useUnmergedTree = true).assertTextEquals("Left Cotton")        compose.onNodeWithTag("contacts-row-detail-+15550100003", useUnmergedTree = true).assertTextEquals("This number belongs to someone else on Cotton now")        compose.onNodeWithText("Your Circle").assertIsDisplayed()        // Take off the list runs the act the core put on the row: circle.remove for that person.        compose.onNodeWithTag("contacts-row-act-remove-p-left", useUnmergedTree = true).assertTextEquals("Take off the list").performClick()        assertEquals("circle.remove", acted?.obj("call")?.str("method"))        assertEquals("p-left", acted?.obj("call")?.obj("request")?.str("person"))    }    @Test    fun signed_out_says_the_cores_words_and_offers_sign_in() {        var signIn = 0        compose.setContent {            CircleList(json("""{"signedOut": {"title": "${line("signed_out.title")}", "message": "${line("signed_out.message")}"}}"""), null,                onRow = {}, onAct = {}, onSignIn = { signIn++ }, signIn = "Sign in")        }        compose.onNodeWithText("Sign in to have a Circle").assertIsDisplayed()        compose.onNodeWithText(line("signed_out.message")).assertIsDisplayed()        compose.onNodeWithTag("contacts-circle-sign-in").performClick()        assertEquals(1, signIn)    }    @Test    fun out_of_reach_says_the_cores_line() {        compose.setContent { CircleList(json("""{"title": "${line("title")}", "sections": [], "unreachable": "${line("unreachable")}"}"""), null, {}, {}, {}, "Sign in") }        compose.onNodeWithTag("contacts-circle-unreachable").assertTextEquals(line("unreachable"))    }    /** The person page's Circle part, as circle.rs `person_part` lays it for someone who left and a number that changed hands. */    @Test    fun the_person_page_says_left_cotton_and_offers_take_off_the_list() {        var acted: JsonObject? = null        val left = json("""{"state": "left", "words": "${line("detail.left")}", "acts": [{"id": "remove", "words": "${line("acts.forget")}", "call": {"method": "circle.remove", "request": {"person": "p-left"}}}]}""")        compose.setContent { androidx.compose.foundation.layout.Column { PersonCircle(left, "Circle") { acted = it } } }        compose.onNodeWithTag("contacts-person-circle-left").assertTextEquals("Left Cotton")        compose.onNodeWithTag("contacts-person-circle-act-remove").assertTextEquals("Take off the list").performClick()        assertEquals("circle.remove", acted?.obj("call")?.str("method"))    }    @Test    fun the_person_page_says_the_number_belongs_to_someone_else() {        val changed = json("""{"state": "changed", "words": "${line("detail.changed")}", "acts": [{"id": "remove", "words": "${line("acts.forget")}", "call": {"method": "circle.remove", "request": {"person": "p-chen"}}}]}""")        compose.setContent { androidx.compose.foundation.layout.Column { PersonCircle(changed, "Circle") {} } }        compose.onNodeWithTag("contacts-person-circle-changed").assertTextEquals("This number belongs to someone else on Cotton now")    }}