package com.cottondb.messages.warm import android.app.Application import androidx.compose.foundation.lazy.LazyListState import androidx.compose.ui.test.assertCountEquals import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onAllNodesWithText import androidx.compose.ui.test.onNodeWithTag import com.cottondb.messages.chat.Item import com.cottondb.messages.chat.ThreadPage import com.cottondb.messages.chat.ThreadState import com.cottondb.messages.chat.Words import com.cottondb.shell.core.CottonCore import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config /** * THEIR PICTURE ONCE OVER THEIR WORDS (the iPhone lane's find, kept): in an imported chat whose lines are minutes * apart every line starts a run (`head`), and a starred bubble answered alone is a run of its own; the picture, and * in a group the name, stand only where the core answers `face`. */ @RunWith(RobolectricTestRunner::class) @Config(sdk = [35], qualifiers = "w400dp-h860dp-xhdpi", application = Application::class) class TheirPictureOnceTest { @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 val asha = """{"name":"Asha Rao","tint":2,"picture":{"initials":"AR","tint":2}}""" /** Each line its own run (minutes apart), as the core answers an imported chat. */ private fun theirs(id: String, text: String, face: Boolean, starred: Boolean = false) = """{"item":"bubble","message":"$id","mine":false,"kind":"text","text":"$text","sender":$asha,"head":true,"tail":true,"face":$face,"time":"19:0${id.last()}","pinned":false,"starred":$starred,"actions":["reply"]}""" private fun mine(id: String, text: String) = """{"item":"bubble","message":"$id","mine":true,"kind":"text","text":"$text","head":true,"tail":true,"face":true,"time":"19:05","tick":"sent","pinned":false,"actions":["reply"]}""" @Test fun their_picture_and_name_stand_once_over_their_words_however_long_they_paused() { val items = listOf( theirs("a1", "Leaving now", face = true), theirs("a2", "Traffic is bad", face = false), theirs("a3", "Almost there", face = false, starred = true), mine("b4", "See you soon"), theirs("a5", "Here", face = true), ).joinToString(",") val t = ThreadState("t1", ThreadPage.of(j("""{"thread":"t1","header":{"name":"Hill trip","picture":{"initials":"HT","tint":1},"status":"","group":true},"items":[$items]}"""), words)) val acts = MessageActs({}, {}, { _, _ -> }, {}, { _, _ -> }, {}, { _, _ -> }, {}, {}, VoicePlayer(kotlinx.coroutines.MainScope())) compose.setContent { WarmTheme(night = false) { Conversation(t, LazyListState(), acts, threadWords, {}, {}, {}, {}, {}, {}, {}, {}, null, {}, {}) {} } } compose.waitForIdle() compose.onNodeWithTag("face-a1", useUnmergedTree = true).assertExists() compose.onNodeWithTag("face-a2", useUnmergedTree = true).assertDoesNotExist() compose.onNodeWithTag("face-a3", useUnmergedTree = true).assertDoesNotExist() compose.onNodeWithTag("face-a5", useUnmergedTree = true).assertExists() // The group's name: over a1 and again over a5 (after the person's own words), never over a2 or a3. compose.onAllNodesWithText("Asha Rao", useUnmergedTree = true).assertCountEquals(2) // A star answered alone (head and tail, as `bubble_of` lays it between its neighbours) keeps its place. compose.runOnIdle { t.sent(Item.of(j(theirs("a3", "Almost there", face = false, starred = true)), words)) } compose.waitForIdle() compose.onNodeWithTag("face-a3", useUnmergedTree = true).assertDoesNotExist() } @Test fun a_core_before_face_still_draws_the_picture_at_a_runs_head() { val old = """{"item":"bubble","message":"x1","mine":false,"kind":"text","text":"Hi","sender":$asha,"head":true,"tail":true,"time":"19:00","pinned":false,"actions":[]}""" val m = Item.of(j(old), words) as Item.Message assertEquals(true, m.face) assertEquals("Asha Rao", m.name) } }