Cotton code
61 lines · 3.3 KB · KotlinRaw
package com.cottondb.notesimport android.app.Applicationimport androidx.compose.ui.semantics.SemanticsActionsimport androidx.compose.ui.test.junit4.createAndroidComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.text.TextLayoutResultimport androidx.compose.ui.text.font.FontFamilyimport com.cottondb.writing.editor.Spanimport com.cottondb.writing.editor.Stylingimport com.cottondb.writing.theme.Writingimport com.cottondb.writing.theme.WritingThemeimport com.cottondb.writing.theme.WritingTypeimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.jsonObjectimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Configimport org.robolectric.annotation.GraphicsModeimport kotlin.math.abs/** * Settings' theme cards (the launch walk, 22 Nov 2026, Words on emulator-5554): the sample's ticked checklist line * stood crossed out with no box beside it, where the editor draws a ticked box. The card now paints what the editor * paints behind the words: the ticked box in the theme's accent. */@RunWith(RobolectricTestRunner::class)@GraphicsMode(GraphicsMode.Mode.NATIVE)@Config(sdk = [35], application = Application::class)class ThemeSampleBoxTest {    @get:Rule val compose = createAndroidComposeRule<androidx.activity.ComponentActivity>()    private val theme = WritingTheme.of("linen", "Linen", Json.parseToJsonElement("""{"ground":"#F7F5F0","text":"#2E2E31","heading":"#1C1C1E","secondary":"#8A8A8F","accent":"#D2453A"}""").jsonObject)    private val type = WritingType(FontFamily.Default, 17f, 1.5f, 600f, FontFamily.Monospace)    /** RULE: the sample's ticked item has its ticked box, as in the editor. */    @Test fun the_ticked_item_has_its_box() {        val text = "- [x] Checklist"        compose.setContent { Writing(theme, type) { ThemeSample(theme, type, text, Styling(listOf(Span(0, 6, "mark", "glyph", glyph = "ticked"))), chosen = false) {} } }        compose.waitForIdle()        val node = compose.onNodeWithTag("theme-sample", useUnmergedTree = true)        val out = ArrayList<TextLayoutResult>()        node.fetchSemanticsNode().config[SemanticsActions.GetTextLayoutResult].action!!.invoke(out)        val laid = out.single()        val box = laid.getBoundingBox(0).let { a -> laid.getBoundingBox(5).let { b -> androidx.compose.ui.geometry.Rect(a.left, a.top, b.right, b.bottom) } }        val at = node.fetchSemanticsNode().positionInWindow        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)) }        val accent = android.graphics.Color.valueOf(0xFFD2453A.toInt())        var hits = 0        for (x in (at.x + box.left).toInt() until (at.x + box.right).toInt()) for (y in (at.y + box.top).toInt() until (at.y + box.bottom).toInt()) {            val c = android.graphics.Color.valueOf(shot.getPixel(x, y))            if (abs(c.red() - accent.red()) < 0.08f && abs(c.green() - accent.green()) < 0.08f && abs(c.blue() - accent.blue()) < 0.08f) hits++        }        assertTrue("no ticked box drawn beside the item ($hits accent pixels)", hits > 20)    }}