Cotton code
125 lines · 6.5 KB · KotlinRaw
package com.cottondb.messages.warmimport android.app.Applicationimport androidx.compose.foundation.lazy.LazyListStateimport androidx.compose.ui.geometry.Offsetimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.performTouchInputimport 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 kotlinx.serialization.json.jsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertTrueimport androidx.compose.ui.test.getBoundsInRootimport androidx.compose.ui.test.longClickimport androidx.compose.ui.unit.dpimport androidx.compose.ui.unit.heightimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Config/** * A CONVERSATION'S GESTURES as a finger makes them on a message (the emulator's faults, kept): pulled toward the * start the times show, wherever on the page the finger began; swiped toward the reading end the message is * answered. */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], qualifiers = "w400dp-h860dp-xhdpi", application = Application::class)class ConversationGesturesTest {    @get:Rule val compose = createComposeRule()    private val words = Words { set, key -> "$set:$key" }    private val threadWords = ThreadWordsWarm("Back", "Call", "Video call", "Not sent", "Try again", "You", "Their page")    private fun msg(id: String, mine: Boolean, text: String, head: Boolean = true, tail: Boolean = true) =        """{"item":"bubble","message":"$id","mine":$mine,"kind":"text","text":"$text","head":$head,"tail":$tail,"time":"19:0${id.last()}",           "pinned":false,"actions":["reply","react","copy"]${if (mine) "" else ""","sender":{"tint":"0","picture":{"initials":"RM","tint":1}}"""}}"""    private fun thread() = ThreadState("t1", ThreadPage.of(CottonCore.json.parseToJsonElement("""        {"thread":"t1","header":{"name":"Rose Martin","picture":{"initials":"RM","tint":1},"status":""},         "items":[{"item":"day","label":"Friday","numeral":"20","words":"Friday · November"},           ${msg("m1", false, "Are we still on for dinner?")},${msg("m2", true, "Yes. Seven at the usual place?")},           ${msg("m3", false, "Perfect.", tail = false)},${msg("m4", false, "I will book.", head = false, tail = false)},${msg("m5", false, "The corner one.", head = false)}]}""").jsonObject, words))    private val replied = mutableListOf<String>()    private val held = mutableListOf<String>()    private fun show() {        val acts = MessageActs({ held += it.id }, { replied += it.id }, { _, _ -> }, {}, { _, _ -> }, {}, { _, _ -> }, {}, {}, VoicePlayer(kotlinx.coroutines.MainScope()))        compose.setContent { WarmTheme(night = false) { Conversation(thread(), LazyListState(), acts, threadWords, {}, {}, {}, {}, {}, {}, {}, {}, null, {}, {}) {} } }        compose.waitForIdle()    }    /** A finger set down on the message and moved `dx` sideways in small steps, still down. */    private fun pullOn(tag: String, dx: Float) = compose.onNodeWithTag(tag).performTouchInput {        down(center)        repeat(10) { moveBy(Offset(dx / 10, 0f)) }    }    @Test    fun pulled_toward_the_start_from_a_message_the_times_show() {        show()        for (m in listOf("m1", "m2")) {            pullOn("bubble-$m", -300f)            compose.onNodeWithTag("time-m1").assertExists()            compose.onNodeWithTag("time-m2").assertExists()            compose.onNodeWithTag("bubble-$m").performTouchInput { up() }            compose.mainClock.advanceTimeBy(1_000)            compose.waitForIdle()            compose.onNodeWithTag("time-m1").assertDoesNotExist()        }        assertEquals(emptyList<String>(), replied)    }    @Test    fun swiped_toward_the_reading_end_a_message_is_answered_and_the_times_stay_hidden() {        show()        pullOn("bubble-m1", 300f)        compose.onNodeWithTag("time-m1").assertDoesNotExist()        compose.onNodeWithTag("bubble-m1").performTouchInput { up() }        compose.mainClock.advanceTimeBy(1_000)        compose.waitForIdle()        assertEquals(listOf("m1"), replied)        check(thread().items.any { it is Item.Message })    }    /**     * A message the core offers no Reply for (in a WhatsApp chat, one WhatsApp cannot find: an export's) is not     * answered by a swipe: before, the swipe opened a reply strip the send then refused.     */    @Test    fun a_message_with_no_reply_is_not_answered_by_a_swipe() {        val plain = """{"item":"bubble","message":"x1","mine":false,"kind":"text","text":"From the export","head":true,"tail":true,"time":"19:01","pinned":false,"actions":["copy","forward"]}"""        val t = ThreadState("t1", ThreadPage.of(CottonCore.json.parseToJsonElement("""{"thread":"t1","header":{"name":"Rose Martin","picture":{"initials":"RM","tint":1},"status":""},"items":[$plain]}""").jsonObject, words))        val acts = MessageActs({ held += it.id }, { replied += it.id }, { _, _ -> }, {}, { _, _ -> }, {}, { _, _ -> }, {}, {}, VoicePlayer(kotlinx.coroutines.MainScope()))        compose.setContent { WarmTheme(night = false) { Conversation(t, LazyListState(), acts, threadWords, {}, {}, {}, {}, {}, {}, {}, {}, null, {}, {}) {} } }        compose.waitForIdle()        pullOn("bubble-x1", 300f)        compose.onNodeWithTag("bubble-x1").performTouchInput { up() }        compose.mainClock.advanceTimeBy(1_000)        compose.waitForIdle()        assertEquals(emptyList<String>(), replied)    }    @Test    fun the_lines_of_a_run_stand_close_and_each_is_still_held_for_its_acts() {        show()        val first = compose.onNodeWithTag("bubble-m3").getBoundsInRoot()        val second = compose.onNodeWithTag("bubble-m4").getBoundsInRoot()        val third = compose.onNodeWithTag("bubble-m5").getBoundsInRoot()        // warm.html's `.them p`: one line under the other, 4px apart; not each padded out to a 48 dp control.        assertTrue("gap ${second.top - first.bottom}", second.top - first.bottom < 8.dp)        assertTrue("gap ${third.top - second.bottom}", third.top - second.bottom < 8.dp)        assertTrue("a one-line message is ${second.height} tall", second.height < 40.dp)        compose.onNodeWithTag("bubble-m4").performTouchInput { longClick() }        compose.onNodeWithTag("bubble-m2").performTouchInput { longClick() }        compose.waitForIdle()        assertEquals(listOf("m4", "m2"), held)    }}