Cotton code
60 lines · 2.7 KB · KotlinRaw
package com.cottondb.messages.chatimport com.cottondb.shell.core.CottonCoreimport kotlinx.coroutines.CoroutineScopeimport kotlinx.coroutines.SupervisorJobimport kotlinx.serialization.json.jsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertNullimport org.junit.Assert.assertTrueimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.Shadows.shadowOfimport org.robolectric.annotation.Config/** * One copy, one notice (OLD-LOOK item 1, 22 Nov 2026). On emulator-5554 (Android 15) Copy on a held message showed * Comms' own "Copied" pill over the system's own clipboard preview of the same words: from Android 13 the phone says * every copy itself. Comms now says "Copied" only on a phone that does not. Invented words. */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], application = android.app.Application::class)class CopiedOnceTest {    private val words = Words { set, key -> "$set:$key" }    private fun model(phoneSays: Boolean = android.os.Build.VERSION.SDK_INT >= 33): ChatModel {        val model = ChatModel(ChatData(null, null, words, CoroutineScope(SupervisorJob()), null), CoroutineScope(SupervisorJob()), phoneSays)        model.prefetched["t1"] = ThreadPage.of(CottonCore.json.parseToJsonElement("""            {"thread":"t1","header":{"name":"Kavya Invented","picture":{"initials":"KI","tint":2},"source":"cotton","status":""},             "actions":{"copy":"Copy","copied":"Copied"},             "items":[{"item":"bubble","message":"m1","mine":false,"kind":"text","text":"Saffron lantern ferry.","head":true,"tail":true,"time":"09:01",                       "pinned":false,"actions":["copy"]}]}        """).jsonObject, words)        model.open("t1", unread = false)        return model    }    private fun copyHeld(model: ChatModel): String? {        var board: String? = null        val m = model.thread!!.items.filterIsInstance<Item.Message>().single()        model.act(m, Act("copy", "Copy")) { board = it }        shadowOf(android.os.Looper.getMainLooper()).idle()        return board    }    @Test fun on_a_phone_that_says_every_copy_itself_comms_says_nothing_more() {        val model = model()        assertTrue("Android 13 and later preview every copy", model.phoneSaysCopied)        assertEquals("Saffron lantern ferry.", copyHeld(model))        assertNull("one copy, one notice: the phone's", model.said)        model.copied("Copied")        assertNull(model.said)    }    @Test fun on_an_older_phone_comms_says_copied_once() {        val model = model(phoneSays = false)        assertEquals("Saffron lantern ferry.", copyHeld(model))        assertEquals("Copied", model.said)    }}