package com.cottondb.messages.warm import android.app.Application import androidx.compose.foundation.layout.Box import androidx.compose.ui.test.assertCountEquals import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.compose.ui.test.onAllNodesWithText import androidx.compose.ui.test.onNodeWithTag import com.cottondb.messages.MessagesParts import com.cottondb.messages.chat.ChatData import com.cottondb.messages.chat.ChatModel import com.cottondb.messages.chat.Words import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.SupervisorJob import org.junit.Assert.assertFalse import org.junit.Assert.assertNull 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 import org.robolectric.annotation.GraphicsMode import java.io.File import kotlin.math.abs /** * The old shared toast cannot stand in Comms, and nothing Comms says is held for later (OLD-LOOK item 1, owner, 22 Nov * 2026: on his iPhone, after approving a device, "Approved. … is signing in." stood twice at once, on the old torn-paper * strip in a typewriter face and in Comms' own pill). On Android the paper strip is drawn only by the shell's fan * surface (`PaperToasts` in `AppSurface`, the default `ShellParts.Surface`); Comms draws its own root and says every * `shell.said` in its own pill, once. Signed out, the pill used not to be drawn at all (the welcome returned first), so * a line said then was held and said later, stale, over the chats. Invented words. */ @RunWith(RobolectricTestRunner::class) @GraphicsMode(GraphicsMode.Mode.NATIVE) @Config(sdk = [35], qualifiers = "w400dp-h800dp-mdpi", application = Application::class) class CommsOwnToastTest { @get:Rule val compose = createAndroidComposeRule() private val app = generateSequence(File("").absoluteFile) { it.parentFile }.first { File(it, "src/messages/kotlin/com/cottondb/messages/warm").isDirectory } private val words = Words { set, key -> "$set:$key" } private fun model() = ChatModel(ChatData(null, null, words, CoroutineScope(SupervisorJob()), null), CoroutineScope(SupervisorJob())) @Test fun the_shells_paper_toast_is_never_composed_in_comms() { assertTrue("Comms draws its own surface, so the fan's (and its paper strip) never stands", MessagesParts::class.java.declaredMethods.any { it.name == "Surface" }) File(app, "src/messages").walk().filter { it.extension == "kt" }.forEach { source -> val text = source.readText() listOf("AppSurface(", "PaperToasts(", "SendReceipt(").forEach { raise -> assertFalse("${source.name} raises the old toast ($raise)", raise in text) } } val root = File(app, "src/messages/kotlin/com/cottondb/messages/warm/ChatRoot.kt").readText() assertTrue("what the shell says goes to Comms' own pill", root.contains("LaunchedEffect(shell.said) { shell.said?.let { model.said = it; shell.said = null } }")) // Signed out, the pill stands over the welcome too, before the branch returns. val signedOut = root.substringAfter("if (!signedIn) {").substringBefore("return@Box") assertTrue("the welcome says what was said, now", signedOut.contains("ChatSaying(model)")) } @Test fun a_line_said_while_signed_out_stands_once_over_the_welcome_and_goes() { val model = model() val said = "Can't reach Cotton right now. Nothing was sent." model.said = said compose.setContent { WarmTheme(night = false) { Box { Welcome("Comms", "Talk to the people you know.", "Sign in") {}; ChatSaying(model) } } } compose.waitForIdle() compose.onAllNodesWithText(said).assertCountEquals(1) val (own, paper) = colours("said", 0xFF221F1B.toInt()) assertTrue("drawn on Comms' own pill, the Warm ink ($own of the line's pixels)", own > 0.5f) assertTrue("not on the paper strip ($paper of the line's pixels cream)", paper < 0.05f) compose.mainClock.advanceTimeBy(2700) compose.waitForIdle() compose.onAllNodesWithText(said).assertCountEquals(0) assertNull("said once, then let go: nothing is left to say later over the chats", model.said) } /** The share of the pixels inside the node [tag] of [own] colour, and of the shell's paper cream (Paper.cream, #F0E5D8). */ private fun colours(tag: String, own: Int): Pair { val bounds = compose.onNodeWithTag(tag, useUnmergedTree = true).fetchSemanticsNode().boundsInWindow val view = compose.activity.window.decorView val shot = android.graphics.Bitmap.createBitmap(view.width, view.height, android.graphics.Bitmap.Config.ARGB_8888) compose.runOnIdle { view.draw(android.graphics.Canvas(shot)) } fun near(c: android.graphics.Color, to: Int) = android.graphics.Color.valueOf(to).let { abs(c.red() - it.red()) < 0.04f && abs(c.green() - it.green()) < 0.04f && abs(c.blue() - it.blue()) < 0.04f } var mine = 0; var cream = 0 for (x in bounds.left.toInt() until bounds.right.toInt()) for (y in bounds.top.toInt() until bounds.bottom.toInt()) { val c = android.graphics.Color.valueOf(shot.getPixel(x, y)) if (near(c, own)) mine++ if (near(c, 0xFFF0E5D8.toInt())) cream++ } val all = ((bounds.right.toInt() - bounds.left.toInt()) * (bounds.bottom.toInt() - bounds.top.toInt())).coerceAtLeast(1).toFloat() return mine / all to cream / all } }