package com.cottondb.messages.warm import android.app.Application import androidx.compose.foundation.lazy.LazyListState import androidx.compose.ui.semantics.SemanticsProperties import androidx.compose.ui.semantics.getOrNull import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.text.LinkAnnotation import com.cottondb.messages.chat.ChatRow import com.cottondb.messages.chat.ThreadPage import com.cottondb.messages.chat.ThreadState import com.cottondb.messages.chat.Words import com.cottondb.messages.numberTapped import com.cottondb.shell.core.CottonCore import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Assert.assertNull import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config /** * A NUMBER TAPPED ANYWHERE opens its chat as the core routes it (FEEDBACK-2 A): in a message's words (the core's * `numbers`), and from outside Comms (a contact's Message, an `sms:` link). And the chat list says where a text on its * way to WhatsApp stands. */ @RunWith(RobolectricTestRunner::class) @Config(sdk = [35], qualifiers = "w400dp-h860dp-xhdpi", application = Application::class) class NumberTappedTest { @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 @Test fun a_number_in_a_message_opens_its_chat() { val text = "Noor's new number is +1 555 010 7001, write to her" val bubble = """{"item":"bubble","message":"m1","mine":false,"kind":"text","text":"$text","head":true,"tail":true,"time":"19:00","pinned":false,"actions":["reply"],"numbers":[{"at":[21,36],"number":"+15550107001"}]}""" val t = ThreadState("t1", ThreadPage.of(j("""{"thread":"t1","header":{"name":"Bimal Example","picture":{"initials":"BE","tint":1},"status":""},"items":[$bubble]}"""), words)) var opened: String? = null val acts = MessageActs({}, {}, { _, _ -> }, {}, { _, _ -> }, {}, { _, _ -> }, {}, {}, VoicePlayer(kotlinx.coroutines.MainScope()), onNumber = { opened = it }) compose.setContent { WarmTheme(night = false) { Conversation(t, LazyListState(), acts, threadWords, {}, {}, {}, {}, {}, {}, {}, {}, null, {}, {}) {} } } compose.waitForIdle() val said = compose.onNodeWithTag("numbers-m1", useUnmergedTree = true).fetchSemanticsNode().config.getOrNull(SemanticsProperties.Text)!!.first() val links = said.getLinkAnnotations(0, said.length) assertEquals(1, links.size) assertEquals("+1 555 010 7001", said.text.substring(links[0].start, links[0].end)) val link = links[0].item as LinkAnnotation.Clickable compose.runOnIdle { link.linkInteractionListener!!.onClick(link) } assertEquals("+15550107001", opened) } @Test fun a_number_tapped_outside_comms_is_read_from_its_link() { assertEquals("+15550107001", numberTapped("smsto:+15550107001")) assertEquals("+15550107001", numberTapped("sms:%2B15550107001?body=Hello")) assertEquals("+91 90000 64411", numberTapped("SMSTO:+91%2090000%2064411")) assertNull(numberTapped("https://example.com/+15550107001")) assertNull(numberTapped("tel:+15550107001")) assertNull(numberTapped("smsto:")) assertNull(numberTapped(null)) } @Test fun the_chat_list_says_where_a_waiting_text_stands() { val row = ChatRow.of( j("""{"thread":"t9","name":"+15550190198","group":false,"source":"cotton","picture":{"initials":"#","tint":2},"lastLine":{"text":"Hello from Comms","mine":true,"tick":"failed","state":"Not sent: This number is not on WhatsApp"},"when":"16:00","unread":0,"markedUnread":false,"pinned":false,"muted":false,"archived":false,"leading":[],"trailing":[]}"""), words, ) assertEquals("Not sent: This number is not on WhatsApp", row.state) compose.setContent { WarmTheme(night = false) { RowView(row, ChatsWords("New chat", "You", "Muted", "Pinned", "Unread", "More"), {}, {}) } } compose.waitForIdle() compose.onNodeWithTag("chat-row-state-t9", useUnmergedTree = true).assertExists() val drawn = compose.onNodeWithTag("chat-row-state-t9", useUnmergedTree = true).fetchSemanticsNode().config.getOrNull(SemanticsProperties.Text)!!.first().text // A state longer than about half the line stands whole on its own line under the message, which keeps its words. assertEquals("Not sent: This number is not on WhatsApp", drawn) compose.onNodeWithText("You: Hello from Comms", useUnmergedTree = true).assertExists() } }