package com.cottondb.messages.warm import android.app.Application import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.performClick import androidx.compose.ui.test.performTextInput import com.cottondb.messages.chat.Compose import com.cottondb.shell.core.CottonCore import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse 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 /** * 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) class PollMakerTest { @get:Rule val compose = createComposeRule() private fun 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, 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) } }