Cotton code
158 lines · 8.8 KB · KotlinRaw
package com.cottondb.messages.warmimport androidx.compose.ui.test.onNodeWithContentDescriptionimport androidx.compose.ui.test.onAllNodesWithTagimport android.app.Applicationimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.setValueimport androidx.compose.ui.platform.LocalContextimport androidx.compose.ui.test.assertIsFocusedimport androidx.compose.ui.test.assertIsNotFocusedimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.performClickimport androidx.compose.ui.unit.spimport com.cottondb.messages.chat.Composeimport com.cottondb.messages.chat.ComposerStateimport com.cottondb.messages.chat.VoiceRecorderimport com.cottondb.shell.core.CottonCoreimport kotlinx.serialization.json.jsonObjectimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Config/** * THE WRITING BOX as a reply or an edit begins (the emulator's fault, kept): swiped to reply, or Edit chosen from the * held menu, the strip stood over the field but the field had no focus and the keyboard stayed down, so a second tap * was needed before a word could be written. A draft's strip the conversation opens with raises nothing. */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], qualifiers = "w400dp-h860dp-xhdpi", application = Application::class)class WritingBoxTest {    @get:Rule val compose = createComposeRule()    private fun answer(strip: String?) = Compose.of(CottonCore.json.parseToJsonElement("""{"thread":"t1","placeholder":"Message Rose","button":"mic",        "buttonLabel":"Hold to record","attach":true,"attachLabel":"Attach"${strip?.let { ""","strip":$it""" } ?: ""}}""").jsonObject)    private val reply = """{"kind":"reply","message":"m4","label":"Reply to Rose","line":"Got the corner one","cancel":"Cancel"}"""    @Test    fun a_reply_begun_puts_the_finger_in_the_field_and_a_kept_draft_does_not() {        var answer by mutableStateOf(answer(reply))        val state = ComposerState()        compose.setContent {            WarmTheme(night = false) {                WritingBox(state, answer, WritingWords("Send", "Attach", "Hold to record"), VoiceRecorder(LocalContext.current, kotlinx.coroutines.MainScope()), {}, {}, {}, {}, { true }, {})            }        }        compose.waitForIdle()        compose.onNodeWithTag("composer-strip").assertExists()        compose.onNodeWithTag("chat-field").assertIsNotFocused()        // The draft sent, then a new reply begun from another message.        compose.runOnIdle { answer = answer(null) }        compose.waitForIdle()        compose.runOnIdle { answer = answer(reply.replace("m4", "m7")) }        compose.waitForIdle()        compose.onNodeWithTag("chat-field").assertIsFocused()    }    // FEEDBACK1 item 3 (22 Nov 2026), on the emulator sideways with the keyboard up: the box grew to six lines and filled    // the page (Send pushed off the screen, no message in sight). Compact, the plus, the field and Send stand on one    // line, and five lines written stand in two, the field scrolling.    @Test    fun compact_the_box_stands_on_one_line_and_grows_to_two() {        val state = ComposerState()        compose.setContent {            WarmTheme(night = false) {                WritingBox(state, answer(null), WritingWords("Send", "Attach", "Hold to record"), VoiceRecorder(LocalContext.current, kotlinx.coroutines.MainScope()), {}, {}, {}, {}, { true }, {}, compact = true)            }        }        compose.waitForIdle()        val one = compose.onNodeWithTag("chat-composer").fetchSemanticsNode().boundsInRoot.height        val field = compose.onNodeWithTag("chat-field").fetchSemanticsNode().boundsInRoot        val plus = compose.onNodeWithTag("chat-attach").fetchSemanticsNode().boundsInRoot        org.junit.Assert.assertTrue("the plus beside the field, on its line: $plus $field", plus.right <= field.left && plus.bottom > field.top && plus.top < field.bottom)        compose.runOnIdle { state.field.edit { replace(0, length, "one\ntwo\nthree\nfour\nfive") } }        compose.waitForIdle()        val five = compose.onNodeWithTag("chat-composer").fetchSemanticsNode().boundsInRoot.height        val line = with(compose.density) { 26.sp.toPx() }        org.junit.Assert.assertTrue("five lines stand in two: $one -> $five", five - one < line * 1.6f)    }    // The box turns compact as the keyboard rises: the field keeps the finger (on the emulator a first try lost it, and    // the keyboard fell straight back).    @Test    fun turning_compact_keeps_the_focus_in_the_field() {        var compact by mutableStateOf(false)        val state = ComposerState()        compose.setContent {            WarmTheme(night = false) {                WritingBox(state, answer(null), WritingWords("Send", "Attach", "Hold to record"), VoiceRecorder(LocalContext.current, kotlinx.coroutines.MainScope()), {}, {}, {}, {}, { true }, {}, compact = compact)            }        }        compose.onNodeWithTag("chat-field").performClick()        compose.onNodeWithTag("chat-field").assertIsFocused()        compose.runOnIdle { compact = true }        compose.waitForIdle()        compose.onNodeWithTag("chat-field").assertIsFocused()    }    // FEEDBACK1 item 4: pictures picked wait in the writing box, each named as what it is, with one ✕ that takes them out.    @Test    fun waiting_pictures_stand_in_the_box_with_their_cancel() {        val state = ComposerState()        var out = 0        val photo = CottonCore.json.parseToJsonElement("""{"kind":"photo","path":"/invented/none.jpg"}""").jsonObject        val video = CottonCore.json.parseToJsonElement("""{"kind":"video","path":"/invented/none.mp4"}""").jsonObject        state.staged += listOf(photo, video)        compose.setContent {            WarmTheme(night = false) {                WritingBox(state, answer(null), WritingWords("Send", "Attach", "Hold to record"), VoiceRecorder(LocalContext.current, kotlinx.coroutines.MainScope()), {}, {}, {}, {}, { true }, {},                    onUnstage = { out++ }, stagedWords = StagedWords("Cancel", "Photo", "Video"))            }        }        compose.waitForIdle()        org.junit.Assert.assertEquals(2, compose.onAllNodesWithTag("chat-staged-picture").fetchSemanticsNodes().size)        compose.onNodeWithContentDescription("Video").assertExists()        // The row stands above the field.        val row = compose.onNodeWithTag("chat-staged").fetchSemanticsNode().boundsInRoot        val field = compose.onNodeWithTag("chat-field").fetchSemanticsNode().boundsInRoot        org.junit.Assert.assertTrue("the pictures above the field: $row $field", row.bottom <= field.top)        compose.onNodeWithContentDescription("Cancel").performClick()        org.junit.Assert.assertEquals(1, out)        // None waiting: no row.        compose.runOnIdle { state.staged.clear() }        compose.waitForIdle()        compose.onNodeWithTag("chat-staged").assertDoesNotExist()    }    // FEEDBACK-2 A (owner, 23 Nov 2026): writing to a number not on Cotton goes to their WhatsApp through his Mac, and the    // box says so before Send, above the field, in the core's words; with no `place` (an unlinked copy) nothing is drawn.    @Test    fun the_box_says_where_the_message_goes_before_send() {        fun with(place: String?) = Compose.of(CottonCore.json.parseToJsonElement("""{"thread":"t1","placeholder":"Message","button":"send","buttonLabel":"Send","attach":false,"attachLabel":"Attach","sendEnabled":true${place?.let { ""","place":$it""" } ?: ""}}""").jsonObject)        var answer by mutableStateOf(with("""{"id":"whatsapp","mark":"via WhatsApp"}"""))        val state = ComposerState()        compose.setContent {            WarmTheme(night = false) {                WritingBox(state, answer, WritingWords("Send", "Attach", "Hold to record"), VoiceRecorder(LocalContext.current, kotlinx.coroutines.MainScope()), {}, {}, {}, {}, { true }, {})            }        }        compose.waitForIdle()        compose.onNodeWithTag("composer-place").assertExists()        compose.onNodeWithText("via WhatsApp").assertExists()        val mark = compose.onNodeWithTag("composer-place").fetchSemanticsNode().boundsInRoot        val field = compose.onNodeWithTag("chat-field").fetchSemanticsNode().boundsInRoot        org.junit.Assert.assertTrue("the mark above the field: $mark $field", mark.bottom <= field.top)        compose.runOnIdle { answer = with("""{"id":"cotton","mark":"on Cotton"}""") }        compose.waitForIdle()        compose.onNodeWithText("on Cotton").assertExists()        compose.runOnIdle { answer = with(null) }        compose.waitForIdle()        compose.onNodeWithTag("composer-place").assertDoesNotExist()    }}