package com.cottondb.messages.warm import android.app.Application import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.lazy.LazyListState import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onRoot import androidx.compose.ui.unit.Density import com.cottondb.messages.chat.ThreadPage import com.cottondb.messages.chat.ThreadState import com.cottondb.messages.chat.Voice import com.cottondb.messages.chat.Words import com.cottondb.shell.core.CottonCore import com.github.takahirom.roborazzi.captureRoboImage import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.MainScope import kotlinx.serialization.json.jsonObject import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config import org.robolectric.annotation.GraphicsMode import java.io.File /** * THE VOICE PLAYER DRAWN as voice-player.html draws it (owner, 23 Nov 2026, item 2), in a conversation of invented * notes: theirs not played (the accent round and the dot), played, playing at 1.5× with its pill and countdown, * paused partway, gone, a note with no waveform; yours at rest and with its words; and, in a second frame, loading * and offline with Try again. Light, night and the largest text; with CHAT_SHOTS naming a folder each is kept there. */ @RunWith(RobolectricTestRunner::class) @GraphicsMode(GraphicsMode.Mode.NATIVE) @Config(sdk = [35], qualifiers = "w400dp-h860dp-xhdpi", application = Application::class) class VoicePlayerShotsTest { @get:Rule val compose = createComposeRule() private val shots: File? = System.getenv("CHAT_SHOTS")?.let { File(it).apply { mkdirs() } } private val words = Words { set, key -> "$set:$key" } private val voiceWords = VoiceWords( play = "Play voice message", pause = "Pause", loading = "Loading voice message", notPlayed = "not played", left = "{time} left", speed = "Playback speed", speedValue = "{n} times", speedShort = "{n}×", gone = "Voice message not available", unplayable = "This voice message can't be played.", offline = "This file can't be fetched right now.", fileGone = "This file has gone. It was kept for 30 days, and no phone of yours took it in time.", retry = "Try again", ) private val wave = "[0.05,0.1,0.32,0.4,0.22,0.5,0.61,0.3,0.12,0.44,0.52,0.2,0.08,0.35,0.6,0.48,0.25,0.1,0.3,0.42,0.18,0.06,0.28,0.5]" private val them = ""","sender":{"tint":"0","picture":{"initials":"RM","tint":1}}""" private fun note(id: String, mine: Boolean, length: String, extra: String = "", media: String = "") = """{"item":"bubble","message":"$id","mine":$mine,"kind":"voice","text":"","head":true,"tail":true,"time":"18:4${id.last()}","pinned":false,"actions":["reply"]${if (mine) "" else them}$extra, "media":[{"index":0,"kind":"voice","length":"$length","waveform":$wave$media}]}""" private fun thread(vararg items: String) = ThreadState("t1", ThreadPage.of(CottonCore.json.parseToJsonElement(""" {"thread":"t1","header":{"name":"Rose Martin","picture":{"initials":"RM","tint":1},"status":"", "calls":[{"video":false,"label":"Call"},{"video":true,"label":"Video call"}]}, "items":[{"item":"day","label":"Monday","numeral":"23","words":"Monday · November"},${items.joinToString(",")}], "actions":{"reply":"Reply"}}""").jsonObject, words)) private class Engine(private val at: Long) : VoiceEngine { override var onEnd: (() -> Unit)? = null private var place = at override fun open(file: File) = true override fun start() {} override fun pause() {} override fun seekTo(ms: Long) { place = ms } override val position: Long get() = place override val duration: Long get() = 42_000 override fun speed(times: Float) {} override fun release() {} } private fun shoot(name: String, night: Boolean, scale: Float, player: VoicePlayer, t: ThreadState) { player.words = voiceWords val acts = MessageActs({}, {}, { _, _ -> }, {}, { _, _ -> }, {}, { _, _ -> }, {}, {}, player) val threadWords = ThreadWordsWarm("Back", "Call", "Video call", "Not sent", "Try again", "You", "Their page", "Not on Cotton yet", "", "Invite") compose.setContent { val d = LocalDensity.current CompositionLocalProvider(LocalDensity provides Density(d.density, scale)) { WarmTheme(night = night) { Box(Modifier.fillMaxSize()) { Conversation(t, LazyListState(), acts, threadWords, {}, {}, {}, {}, {}, {}, {}, {}, null, {}, {}) {} } } } } compose.waitForIdle() shots?.let { compose.onRoot().captureRoboImage(File(it, "$name.png").path) } } private fun voice(length: String) = Voice(42, length, emptyList(), "k") /** Paused partway on v3, then v2 playing at 1.5× from 16 s. */ private fun atRest(): VoicePlayer { val engines = ArrayDeque(listOf(Engine(9_000), Engine(16_000))) val p = VoicePlayer(MainScope(), engines = { engines.removeFirst() }, fileOf = { File("invented.m4a") }) p.toggle("v3", voice("0:42")); compose.waitForIdle() p.toggle("v3", voice("0:42")); compose.waitForIdle() p.toggle("v2", voice("0:42")); compose.waitForIdle() p.nextSpeed() return p } private fun mixed() = thread( note("v1", false, "0:42", media = ""","played":false"""), note("v2", false, "0:42"), note("v3", false, "0:42"), note("v4", false, "0:18", media = ""","gone":true"""), """{"item":"bubble","message":"v5","mine":false,"kind":"voice","text":"","head":true,"tail":true,"time":"18:45","pinned":false,"actions":[]$them,"media":[{"index":0,"kind":"voice","length":"0:31"}]}""", note("v6", true, "0:30", extra = ""","tick":"read""""), note("v7", true, "0:05", media = ""","transcript":"On my way, give me ten minutes""""), ) private fun failing(): Pair { val never = CompletableDeferred() val p = VoicePlayer(MainScope(), engines = { Engine(0) }, fileOf = { key -> if (key.endsWith("w1:0")) never.await() else null }) val t = thread(note("w1", false, "0:42"), note("w2", false, "2:30"), note("w3", true, "0:14")) p.toggle("w2", Voice(150, "2:30", emptyList(), "msg:w2:0")); compose.waitForIdle() p.toggle("w3", Voice(14, "0:14", emptyList(), "msg:w3:0")); compose.waitForIdle() p.toggle("w1", Voice(42, "0:42", emptyList(), "msg:w1:0")); compose.waitForIdle() return p to t } private fun check() { compose.onNodeWithTag("voice-not-played-v1", useUnmergedTree = true).assertIsDisplayed() compose.onNodeWithTag("voice-speed-v2", useUnmergedTree = true).assertIsDisplayed() compose.onNodeWithTag("voice-speed-v3", useUnmergedTree = true).assertIsDisplayed() compose.onNodeWithTag("voice-speed-v1", useUnmergedTree = true).assertDoesNotExist() compose.onNodeWithTag("voice-not-played-v2", useUnmergedTree = true).assertDoesNotExist() compose.onNodeWithTag("voice-gone-v4", useUnmergedTree = true).assertIsDisplayed() compose.onNodeWithTag("voice-not-played-v6", useUnmergedTree = true).assertDoesNotExist() } @Test fun day() { shoot("fb23-2-voice-day", false, 1f, atRest(), mixed()); check() } @Test fun night() { shoot("fb23-2-voice-night", true, 1f, atRest(), mixed()); check() } @Test fun largest_text() { shoot("fb23-2-voice-largest", false, 2f, atRest(), mixed()); compose.onNodeWithTag("voice-speed-v2", useUnmergedTree = true).assertExists() } /** Your note started (the pill showing) stands as wide as your note at rest: its round stays under the finger. */ @Test fun your_note_keeps_its_width_once_started() { val p = VoicePlayer(MainScope(), engines = { Engine(9_000) }, fileOf = { File("invented.m4a") }) p.toggle("v6", voice("0:30")); compose.waitForIdle() p.toggle("v6", voice("0:30")); compose.waitForIdle() shoot("fb23-2-voice-yours-started", false, 1f, p, mixed()) compose.onNodeWithTag("voice-speed-v6", useUnmergedTree = true).assertExists() val started = compose.onNodeWithTag("voice-v6", useUnmergedTree = true).fetchSemanticsNode().boundsInRoot.left val atRest = compose.onNodeWithTag("voice-v7", useUnmergedTree = true).fetchSemanticsNode().boundsInRoot.left org.junit.Assert.assertEquals(atRest, started, 0.5f) } @Test fun loading_and_offline() { val (p, t) = failing() shoot("fb23-2-voice-failing", false, 1f, p, t) compose.onNodeWithTag("voice-offline-w2", useUnmergedTree = true).assertIsDisplayed() compose.onNodeWithTag("voice-offline-w3", useUnmergedTree = true).assertIsDisplayed() } @Test fun loading_and_offline_night() { val (p, t) = failing(); shoot("fb23-2-voice-failing-night", true, 1f, p, t) } }