package com.cottondb.messages.warmimport android.app.Applicationimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.performClickimport androidx.compose.ui.test.performTextInputimport com.cottondb.messages.chat.Composeimport com.cottondb.shell.core.CottonCoreimport kotlinx.serialization.json.jsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertFalseimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Config/** * MAKING A POLL in a WhatsApp chat (chat CONTRACT.md "Making a poll"): the plus offers Poll where the composer's * answer names one, even with no files to attach; the sheet sends the question, the options written (a new line stands * under the last), and whether more than one may be chosen. Invented words. */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], qualifiers = "w400dp-h860dp-xhdpi", application = Application::class)classPollMakerTest { @get:Ruleval compose = createComposeRule()privatefun answer(attach: Boolean) = Compose.of(CottonCore.json.parseToJsonElement("""{"thread":"t1","placeholder":"Message Bimal","button":"mic", "buttonLabel":"Hold to record","attach":$attach,"attachLabel":"Attach","poll":{"label":"Poll","question":"Ask a question","option":"Option", "add":"Add an option","many":"Let people choose more than one","send":"Send","most":12}}""").jsonObject) @Test fun a_poll_alone_still_opens_the_plus_and_offers_only_poll() {val words = answer(attach = false) assertTrue("the plus stands for a poll alone", words.attach) assertFalse(words.files)var polled = 0 compose.setContent {WarmTheme(night = false) {AttachSheet(AttachWords("Camera", "Photos and videos", "File", "Close"), {}, {}, {}, {}, files = words.files, poll = words.poll?.label, onPoll = { polled++ }) } } compose.onNodeWithTag("chat-attach-camera").assertDoesNotExist() compose.onNodeWithTag("chat-attach-poll").performClick() assertEquals(1, polled) } @Test fun the_sheet_sends_the_question_the_options_and_how_many_may_be_chosen() {val words = answer(attach = true).poll!!var sent: Triple<String, List<String>, Boolean>? = null compose.setContent { WarmTheme(night = false) { PollSheet(words, "Close", onSend = { q, o, m -> sent = Triple(q, o, m) }, onClose = {}) } } compose.onNodeWithTag("chat-poll-send").performClick() assertEquals("nothing goes with no question and no options", null, sent) compose.onNodeWithTag("chat-poll-question").performTextInput("Dinner?") compose.onNodeWithTag("chat-poll-option-0").performTextInput("Thai") compose.onNodeWithTag("chat-poll-option-1").performTextInput("Pizza") compose.onNodeWithTag("chat-poll-option-2").performTextInput("Dosa") compose.onNodeWithTag("chat-poll-option-3").assertExists() compose.onNodeWithTag("chat-poll-many").performClick() compose.onNodeWithTag("chat-poll-send").performClick() assertEquals(Triple("Dinner?", listOf("Thai", "Pizza", "Dosa"), true), sent) }}