Cotton code
103 lines · 6.0 KB · KotlinRaw
package com.cottondb.messages.warmimport android.app.Applicationimport androidx.compose.foundation.lazy.LazyListStateimport androidx.compose.runtime.getValueimport androidx.compose.runtime.setValueimport androidx.compose.ui.test.getBoundsInRootimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.unit.dpimport com.cottondb.messages.chat.ThreadPageimport com.cottondb.messages.chat.ThreadStateimport com.cottondb.messages.chat.Wordsimport com.cottondb.messages.chat.bringIntoSightimport com.cottondb.shell.core.CottonCoreimport kotlinx.coroutines.launchimport kotlinx.serialization.json.jsonObjectimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Config/** * A SEARCH'S MATCH IN SIGHT (the lane's find, kept): Earlier and Later in a chat's search stood the list five items * above the match, so where tall pictures came under it the match was pushed off the top of the screen. The match now * stands whole between the floating header and the foot, whatever is under it. */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], qualifiers = "w400dp-h860dp-xhdpi", application = Application::class)class FoundInSightTest {    @get:Rule val compose = createComposeRule()    private val words = Words { set, key -> "$set:$key" }    private val threadWords = ThreadWordsWarm("Back", "Call", "Video call", "Not sent", "Try again", "You", "Their page")    private fun j(text: String) = CottonCore.json.parseToJsonElement(text).jsonObject    private fun text(id: String, said: String) =        """{"item":"bubble","message":"$id","mine":false,"kind":"text","text":"$said","head":true,"tail":true,"face":true,"time":"19:00","pinned":false,"actions":["reply"]}"""    /** A tall picture (a phone's screenshot), as the core answers its shape. */    private fun tall(id: String) =        """{"item":"bubble","message":"$id","mine":false,"kind":"photo","text":"","head":true,"tail":true,"face":false,"time":"19:01","pinned":false,"actions":["reply"],           "media":[{"kind":"photo","index":"0","width":1080,"height":2400}]}"""    @Test    fun earlier_and_later_bring_a_match_beside_tall_pictures_whole_into_sight() {        // Oldest first: each match has four tall pictures under it.        val items = (1..4).flatMap { n -> listOf(text("hit$n", "Meet at the tea stall $n")) + (1..4).map { tall("p$n$it") } }.joinToString(",")        val t = ThreadState("t1", ThreadPage.of(j("""{"thread":"t1","header":{"name":"Hill trip","picture":{"initials":"HT","tint":1},"status":""},"items":[$items]}"""), words))        val list = LazyListState()        val acts = MessageActs({}, {}, { _, _ -> }, {}, { _, _ -> }, {}, { _, _ -> }, {}, {}, VoicePlayer(kotlinx.coroutines.MainScope()))        lateinit var scope: kotlinx.coroutines.CoroutineScope        compose.setContent {            scope = androidx.compose.runtime.rememberCoroutineScope()            WarmTheme(night = false) { Conversation(t, list, acts, threadWords, {}, {}, {}, {}, {}, {}, {}, {}, null, {}, {}) {} }        }        compose.waitForIdle()        val page = compose.onNodeWithTag("chat-thread").getBoundsInRoot()        // Newest match first, then Earlier to each older one, then Later back.        for (hit in listOf("hit4", "hit3", "hit2", "hit1", "hit2", "hit3")) {            val reversed = t.items.size - 1 - t.items.indexOfFirst { it.id == hit }            compose.runOnIdle { scope.launch { list.bringIntoSight(reversed) } }            compose.waitForIdle()            val b = compose.onNodeWithTag("bubble-$hit").getBoundsInRoot()            // Under the floating header (84 dp of the page's top) and above its foot.            assertTrue("$hit stands at ${b.top}..${b.bottom}, the page ${page.top}..${page.bottom}", b.top >= page.top + 84.dp && b.bottom <= page.bottom)        }    }    /**     * The first match of a search: the page is asked again around it (marked), a new thread and a new list stand, and     * the match is brought into sight on the list as it first stands (on the emulator it stayed at the foot).     */    @Test    fun the_first_match_on_a_page_asked_around_it_stands_in_sight() {        val items = (1..4).flatMap { n -> listOf(text("hit$n", "Meet at the tea stall $n")) + (1..4).map { tall("p$n$it") } }        val page = { json: List<String> -> ThreadState("t1", ThreadPage.of(j("""{"thread":"t1","header":{"name":"Hill trip","picture":{"initials":"HT","tint":1},"status":""},"items":[${json.joinToString(",")}]}"""), words)) }        var t by androidx.compose.runtime.mutableStateOf(page(items))        var list by androidx.compose.runtime.mutableStateOf(LazyListState())        val acts = MessageActs({}, {}, { _, _ -> }, {}, { _, _ -> }, {}, { _, _ -> }, {}, {}, VoicePlayer(kotlinx.coroutines.MainScope()))        // The model's own scope, as ChatModel's (not the composition's).        val scope = kotlinx.coroutines.MainScope()        compose.setContent {            WarmTheme(night = false) { Conversation(t, list, acts, threadWords, {}, {}, {}, {}, {}, {}, {}, {}, null, {}, {}) {} }        }        compose.waitForIdle()        // As ChatModel.showFound: the page around the match, a new list, then the match brought into sight.        compose.runOnIdle {            scope.launch {                t = page(items.map { it.replace("Meet at the tea stall 4\"", "Meet at the tea stall 4\",\"marks\":[[12,21]]") })                list = LazyListState()                val i = t.items.indexOfFirst { it.id == "hit4" }                list.bringIntoSight(t.items.size - 1 - i)            }        }        compose.waitForIdle()        val bounds = compose.onNodeWithTag("chat-thread").getBoundsInRoot()        val b = compose.onNodeWithTag("bubble-hit4").getBoundsInRoot()        assertTrue("hit4 stands at ${b.top}..${b.bottom}, the page ${bounds.top}..${bounds.bottom}", b.top >= bounds.top + 84.dp && b.bottom <= bounds.bottom)    }}