package com.cottondb.messages.warm import android.app.Application import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.lazy.LazyListState import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.semantics.SemanticsProperties import androidx.compose.ui.semantics.getOrNull import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.onRoot import androidx.compose.ui.test.performClick import androidx.compose.ui.unit.Density 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 com.github.takahirom.roborazzi.captureRoboImage import kotlinx.coroutines.MainScope import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals 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 import org.robolectric.annotation.GraphicsMode import java.io.File /** * A WHATSAPP POLL drawn as a poll (FEEDBACK-3, CONTRACT "Polls shown"): the core's `bubble.poll` (question, how many * may be chosen, each option's count and bar, his own choice ticked, the total), read as one line, and a tap opens * its Votes with the names. Invented polls; light, night and text at 2.0; with CHAT_SHOTS naming a folder each is kept. */ @RunWith(RobolectricTestRunner::class) @GraphicsMode(GraphicsMode.Mode.NATIVE) @Config(sdk = [35], qualifiers = "w400dp-h860dp-xhdpi", application = Application::class) class PollTest { @get:Rule val compose = createComposeRule() private val shots: File? = System.getenv("CHAT_SHOTS")?.let { File(it).apply { mkdirs() } } private val words = Words { set, key -> "$set:$key" } private val them = ""","sender":{"tint":"0","picture":{"initials":"NK","tint":1}}""" private val dinner = """{"item":"bubble","message":"p1","mine":false,"kind":"poll","head":true,"tail":true,"time":"08:00","pinned":false,"actions":["reply"]$them, "poll":{"question":"Dinner on Friday?","choose":"Select one or more","said":"3 votes","votesTitle":"Votes", "spoken":"Poll: Dinner on Friday?, Select one or more. Thai, 1 vote. Pizza, 3 votes, your choice. Stay in and cook, 1 vote. 3 votes", "options":[{"text":"Thai","votes":1,"said":"1 vote","percent":33,"voters":["You"],"mine":true}, {"text":"Pizza","votes":3,"said":"3 votes","percent":100,"voters":["You","Noor Kapoor","+1 555-019-0977"],"mine":true}, {"text":"Stay in and cook","votes":1,"said":"1 vote","percent":33,"voters":["Noor Kapoor"]}]}}""" private fun trek(votable: Boolean) = """{"item":"bubble","message":"p2","mine":true,"kind":"poll","head":true,"tail":true,"time":"08:05","pinned":false,"actions":["reply"], "poll":{"question":"Which day for the trek?","choose":"Select one","said":"No votes yet","votesTitle":"Votes","spoken":"Poll: Which day for the trek?","votable":$votable, "options":[{"text":"Saturday","votes":0,"said":"No votes","percent":0},{"text":"Sunday","votes":0,"said":"No votes","percent":0}]}}""" private fun thread(votable: Boolean = false) = ThreadState("t1", ThreadPage.of(CottonCore.json.parseToJsonElement(""" {"thread":"t1","header":{"name":"Noor Kapoor","picture":{"initials":"NK","tint":1},"status":"","place":"whatsapp"}, "items":[{"item":"day","label":"Today","numeral":"23","words":"Today ยท November"},$dinner,${trek(votable)}], "actions":{"reply":"Reply"}}""").jsonObject, words)) private val voted = mutableListOf>() private fun draw(night: Boolean = false, scale: Float = 1f, votable: Boolean = false) { val threadWords = ThreadWordsWarm("Back", "Call", "Video call", "Not sent", "Try again", "You", "Their page", "Not on Cotton yet", "", "Invite") val t = thread(votable) compose.setContent { var open by remember { mutableStateOf(null) } val acts = MessageActs({}, {}, { _, _ -> }, {}, { _, _ -> }, {}, { _, _ -> }, {}, {}, VoicePlayer(MainScope()), onPoll = { open = it }, onVote = { m, i -> voted += m.id to i }) val d = LocalDensity.current CompositionLocalProvider(LocalDensity provides Density(d.density, scale)) { WarmTheme(night = night) { Box(Modifier.fillMaxSize()) { Conversation(t, LazyListState(), acts, threadWords, {}, {}, {}, {}, {}, {}, {}, {}, null, {}, {}) {} open?.poll?.let { PollVotesSheet(it, "Close") { open = null } } } } } } compose.waitForIdle() } private fun shot(name: String) = shots?.let { compose.onRoot().captureRoboImage(File(it, "$name.png").path) } @Test fun a_poll_is_drawn_as_a_poll_and_read_as_one_line() { draw() compose.onNodeWithTag("poll-p1").assertIsDisplayed() compose.onNodeWithText("Dinner on Friday?").assertIsDisplayed() compose.onNodeWithText("Select one or more").assertIsDisplayed() compose.onNodeWithTag("poll-p1-said", useUnmergedTree = true).assertIsDisplayed() compose.onNodeWithTag("poll-p1-mine-1", useUnmergedTree = true).assertExists() compose.onNodeWithTag("poll-p1-mine-0", useUnmergedTree = true).assertExists() compose.onNodeWithTag("poll-p2").assertIsDisplayed() compose.onNodeWithText("No votes yet").assertIsDisplayed() val spoken = compose.onNodeWithTag("poll-p1").fetchSemanticsNode().config.getOrNull(SemanticsProperties.ContentDescription)?.joinToString() ?: compose.onNodeWithTag("poll-p1").fetchSemanticsNode().config.getOrNull(SemanticsProperties.Text)?.joinToString().orEmpty() assertTrue(spoken, spoken.contains("Dinner on Friday?") && spoken.contains("your choice")) assertEquals(100, thread().items.filterIsInstance().first { it.id == "p1" }.poll!!.options[1].percent) shot("poll-light") } @Test fun a_tap_opens_its_votes_with_the_names() { draw() compose.onNodeWithTag("poll-p1").performClick() compose.waitForIdle() compose.onNodeWithTag("poll-votes-1", useUnmergedTree = true).assertExists() compose.onNodeWithText("+1 555-019-0977").assertExists() shot("poll-votes-light") } @Test fun at_night_and_at_the_largest_text() { draw(night = true) compose.onNodeWithTag("poll-p1").assertIsDisplayed() shot("poll-night") compose.onNodeWithTag("poll-p1").performClick() compose.waitForIdle() shot("poll-votes-night") } @Test fun at_text_two() { draw(scale = 2f) compose.onNodeWithTag("poll-p1").assertExists() compose.onNodeWithText("Stay in and cook").assertExists() shot("poll-largest") } /** CONTRACT "Documents and votes": where he can vote, a tap on an option is his vote, and the total opens Votes. */ @Test fun where_he_can_vote_a_tap_on_an_option_votes() { draw(votable = true) compose.onNodeWithTag("poll-p2-pick-1", useUnmergedTree = true).assertExists() compose.onNodeWithTag("poll-p2-option-1").performClick() compose.waitForIdle() assertEquals(listOf("p2" to 1), voted) compose.onNodeWithTag("poll-votes-1", useUnmergedTree = true).assertDoesNotExist() compose.onNodeWithTag("poll-p2-said").performClick() compose.waitForIdle() compose.onNodeWithTag("poll-votes-1", useUnmergedTree = true).assertExists() // A poll he cannot vote on still opens its Votes from anywhere. compose.onNodeWithTag("poll-p1-pick-0", useUnmergedTree = true).assertDoesNotExist() shot("poll-votable-light") } }