package com.cottondb.days import android.app.Application import androidx.compose.foundation.background import androidx.compose.foundation.layout.Box import androidx.compose.ui.Modifier 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 androidx.compose.ui.test.performClick import com.cottondb.days.pen.Pen import com.cottondb.days.pen.PenTheme import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse 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 Days (OLD-LOOK item 1, owner, 22 Nov 2026: on his iPhone a line was said twice * at once, on the old torn-paper strip in a typewriter face and in the app's own pill). On Android the paper strip is * drawn only by the shell's fan surface (`PaperToasts` in `AppSurface`, the default `ShellParts.Surface`); Days draws * its own surface, and every `shell.said` is said at Days' foot in the pen's quiet ink, once. Invented words. */ @RunWith(RobolectricTestRunner::class) @GraphicsMode(GraphicsMode.Mode.NATIVE) @Config(sdk = [35], qualifiers = "w400dp-h700dp-mdpi", application = Application::class) class DaysOwnToastTest { @get:Rule val compose = createAndroidComposeRule() private val app = generateSequence(File("").absoluteFile) { it.parentFile }.first { File(it, "src/days/kotlin/com/cottondb/days").isDirectory } @Test fun the_shells_paper_toast_is_never_composed_in_days() { assertTrue("Days draws its own surface, so the fan's (and its paper strip) never stands", DaysParts::class.java.declaredMethods.any { it.name == "Surface" }) File(app, "src/days").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) } } // What the shell says is handed to Days' own foot, and taken off the shell's. assertTrue(File(app, "src/days/kotlin/com/cottondb/days/DaysSurface.kt").readText().contains("LaunchedEffect(shell.said) { shell.said?.let { tell(it); shell.said = null } }")) } @Test fun a_said_line_stands_once_at_the_foot_in_days_ink_with_its_undo() { val moved = "Moved to Sometime" var undone = 0 compose.setContent { PenTheme { Box(Modifier.background(Pen.sheet)) { FootSaid(moved, "Undo") { undone++ } } } } compose.waitForIdle() compose.onAllNodesWithText(moved).assertCountEquals(1) val (sheet, paper) = colours("days-said", 0xFFF1F2F6.toInt()) assertTrue("on Days' own sheet ($sheet of the line's pixels)", sheet > 0.5f) assertTrue("not on the paper strip ($paper of the line's pixels cream)", paper < 0.05f) compose.onNodeWithTag("days-undo").performClick() assertEquals(1, undone) } /** 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.02f && abs(c.green() - it.green()) < 0.02f && abs(c.blue() - it.blue()) < 0.02f } 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 } }