156 lines · 8.3 KB · KotlinRaw
package com.cottondb.messages.warmimport android.app.Applicationimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.lazy.LazyListStateimport androidx.compose.runtime.CompositionLocalProviderimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.rememberimport androidx.compose.runtime.setValueimport androidx.compose.ui.Modifierimport androidx.compose.ui.platform.LocalDensityimport androidx.compose.ui.semantics.SemanticsPropertiesimport androidx.compose.ui.semantics.getOrNullimport androidx.compose.ui.test.assertIsDisplayedimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.onRootimport androidx.compose.ui.test.performClickimport androidx.compose.ui.unit.Densityimport com.cottondb.messages.chat.Itemimport com.cottondb.messages.chat.ThreadPageimport com.cottondb.messages.chat.ThreadStateimport com.cottondb.messages.chat.Wordsimport com.cottondb.shell.core.CottonCoreimport com.github.takahirom.roborazzi.captureRoboImageimport kotlinx.coroutines.MainScopeimport kotlinx.serialization.json.jsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Configimport org.robolectric.annotation.GraphicsModeimport 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<Pair<String, Int>>() 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<Item.Message?>(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<Item.Message>().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") }}