package com.cottondb.messages.warm import android.app.Application import androidx.compose.foundation.lazy.LazyListState import androidx.compose.ui.geometry.Offset import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.performTouchInput import com.cottondb.messages.chat.Item import com.cottondb.messages.chat.ThreadPage import com.cottondb.messages.chat.ThreadState import com.cottondb.messages.chat.Words import com.cottondb.shell.core.CottonCore import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import androidx.compose.ui.test.getBoundsInRoot import androidx.compose.ui.test.longClick import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.height import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import 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() private val held = mutableListOf() 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(), 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(), 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) } }