Cotton code
98 lines · 4.7 KB · KotlinRaw
package com.cottondb.contactsimport android.app.Applicationimport androidx.activity.ComponentActivityimport androidx.compose.material3.Textimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.setValueimport androidx.compose.ui.Modifierimport androidx.compose.ui.platform.testTagimport androidx.compose.ui.test.assertTextEqualsimport androidx.compose.ui.test.junit4.createAndroidComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport kotlinx.coroutines.CompletableDeferredimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.JsonObjectimport kotlinx.serialization.json.jsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertFalseimport org.junit.Assert.assertNullimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Config/** * CONTACTS ANDROID AT SIZE (walk on an emulator with 1,500 invented contacts, 28 Nov 2026): the home stands on its last * answer while the next is asked (it blanked for half a second under each letter typed, and on each back from a * person), and You follows a save into the account that stopped (it said "Not synced yet" with a Sync now that could * not move it, while the core asked for Sign in again). */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], qualifiers = "w400dp-h860dp-mdpi", application = Application::class)class ContactsAtSizeTest {    @get:Rule val compose = createAndroidComposeRule<ComponentActivity>()    private fun json(text: String): JsonObject = Json.parseToJsonElement(text).jsonObject    private fun home(title: String) = json("""{"title":"$title"}""")    @Test    fun the_home_stands_on_its_last_answer_while_a_new_search_is_asked() {        val kept = mutableMapOf<String, JsonObject>()        val second = CompletableDeferred<JsonObject>()        var query by mutableStateOf("")        compose.setContent {            val (answer, _) = answerOf(0, kept, query, last = "home") { if (query.isEmpty()) home("Everyone") else second.await() }            Text(answer?.let { it["title"].toString().trim('"') } ?: "(blank)", Modifier.testTag("shown"))        }        compose.onNodeWithTag("shown").assertTextEquals("Everyone")        query = "Arlo"        compose.waitForIdle()        compose.onNodeWithTag("shown").assertTextEquals("Everyone")        second.complete(home("Found Arlo"))        compose.waitUntil(2_000) { kept["home"]?.get("title").toString().contains("Found") }        compose.onNodeWithTag("shown").assertTextEquals("Found Arlo")    }    @Test    fun a_screen_opened_again_draws_its_last_answer_at_once() {        val kept = mutableMapOf("home" to home("Everyone"))        val never = CompletableDeferred<JsonObject>()        compose.setContent {            val (answer, _) = answerOf(0, kept, "all", last = "home") { never.await() }            Text(answer?.let { it["title"].toString().trim('"') } ?: "(blank)", Modifier.testTag("shown"))        }        compose.onNodeWithTag("shown").assertTextEquals("Everyone")    }    @Test    fun a_screen_without_last_still_starts_blank_for_a_new_key() {        val never = CompletableDeferred<JsonObject>()        compose.setContent {            val (answer, _) = answerOf(0, mutableMapOf(), "person-b") { never.await() }            Text(answer?.toString() ?: "(blank)", Modifier.testTag("shown"))        }        compose.onNodeWithTag("shown").assertTextEquals("(blank)")    }    @Test    fun a_stopped_save_signs_you_out_and_says_why() {        val failed = json("""{"stand":"failed","backup":{"sent":20,"of":7884,"line":"Cotton Contacts cannot be reached right now. Try again when you are online."}}""")        assertFalse("Sign in again, as the core's account offers", YouStand.signedIn(held = true, home = failed))        assertEquals("Cotton Contacts cannot be reached right now. Try again when you are online.", YouStand.saving(failed))    }    @Test    fun the_save_under_way_is_said_in_place_of_the_sync_line() {        val saving = json("""{"stand":"backing_up","backup":{"sent":158,"of":7884,"line":"Syncing your data to your account… 158 of 7,884"}}""")        assertTrue(YouStand.signedIn(held = true, home = saving))        assertEquals("Syncing your data to your account… 158 of 7,884", YouStand.saving(saving))        val connected = json("""{"stand":"connected","backup":{"sent":0,"of":0,"line":""}}""")        assertTrue(YouStand.signedIn(held = true, home = connected))        assertNull(YouStand.saving(connected))        assertFalse("no session, not signed in", YouStand.signedIn(held = false, home = connected))    }}