package com.cottondb.messages.warm import android.app.Application import androidx.compose.foundation.lazy.LazyListState import androidx.compose.runtime.getValue import androidx.compose.runtime.setValue import androidx.compose.ui.test.getBoundsInRoot import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.unit.dp import com.cottondb.messages.chat.ThreadPage import com.cottondb.messages.chat.ThreadState import com.cottondb.messages.chat.Words import com.cottondb.messages.chat.bringIntoSight import com.cottondb.shell.core.CottonCore import kotlinx.coroutines.launch import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertTrue import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import 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 -> 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) } }