Cotton code
114 lines · 5.2 KB · KotlinRaw
package com.cottondb.android.ui.dayimport androidx.compose.ui.test.hasSetTextActionimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onAllNodesWithTextimport androidx.compose.ui.test.performTextInputimport androidx.compose.runtime.mutableIntStateOfimport androidx.test.ext.junit.runners.AndroidJUnit4import androidx.test.platform.app.InstrumentationRegistryimport com.cottondb.android.core.CottonCoreimport com.cottondb.android.day.notes.NoteEditorModelimport com.cottondb.android.day.notes.NotesJournalRepositoryimport com.cottondb.android.day.todos.CreateTodoimport com.cottondb.android.day.todos.TodoRepositoryimport com.cottondb.android.design.CottonThemeimport com.cottondb.android.navigation.Routeimport com.cottondb.android.ui.day.notes.NotesJournalScreenimport com.cottondb.android.ui.day.todos.TodoScreenimport java.io.Fileimport java.time.LocalDateimport java.util.Base64import java.util.UUIDimport kotlinx.coroutines.CoroutineScopeimport kotlinx.coroutines.Dispatchersimport kotlinx.coroutines.SupervisorJobimport kotlinx.coroutines.cancelimport kotlinx.coroutines.runBlockingimport kotlinx.serialization.json.buildJsonObjectimport kotlinx.serialization.json.putimport org.junit.Afterimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWith/** Real JNI screen proof: the UI renders rows written to an isolated synthetic workspace. */@RunWith(AndroidJUnit4::class)class DayScreensTest {    @get:Rule val compose = createComposeRule()    private val context = InstrumentationRegistry.getInstrumentation().targetContext    private val container = File(context.noBackupFilesDir, "day-ui-test-${UUID.randomUUID()}")    @After fun removeSyntheticContainer() { container.deleteRecursively() }    @Test fun realJniTodoAndNotesScreensRenderSavedSyntheticRecords() = runBlocking {        val core = CottonCore.open(config())        val writerScope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)        try {            core.call("workspace.startLocal", buildJsonObject {                put("deviceName", "Android Day UI test")                put("appVersion", "0.0.0-instrumented")            })            val todos = TodoRepository(core)            todos.create(CreateTodo("JNI UI todo"))            val todoId = todos.refresh()!!.todos.single { it.title == "JNI UI todo" }.id            val notes = NotesJournalRepository(core)            val editors = mutableMapOf<String, NoteEditorModel>()            val editorFor = { key: String, journalDate: String?, id: String? ->                editors.getOrPut(key) { NoteEditorModel(notes, journalDate, id) }            }            val phase = mutableIntStateOf(0)            compose.setContent {                CottonTheme {                    when (phase.intValue) {                        0 -> TodoScreen(Route.Todos(false), todos, "Etc/UTC", LocalDate.of(2026, 9, 11), {}, {})                        1 -> TodoScreen(Route.TodosTodo(todoId), todos, "Etc/UTC", LocalDate.of(2026, 9, 11), {}, {})                        2 -> NotesJournalScreen(                            route = Route.NotesNote("new"), repository = notes,                            today = LocalDate.of(2026, 9, 11), nowMs = 1_789_128_000_000L,                            timezone = "Etc/UTC", writerScope = writerScope, editorFor = editorFor,                            onNavigate = {}, onBack = {},                        )                        else -> NotesJournalScreen(                            route = Route.Notes, repository = notes,                            today = LocalDate.of(2026, 9, 11), nowMs = 1_789_128_000_000L,                            timezone = "Etc/UTC", writerScope = writerScope, editorFor = editorFor,                            onNavigate = {}, onBack = {},                        )                    }                }            }            compose.waitUntil(timeoutMillis = 5_000) {                compose.onAllNodesWithText("JNI UI todo").fetchSemanticsNodes().isNotEmpty()            }            compose.runOnIdle { phase.intValue = 1 }            compose.waitUntil(timeoutMillis = 5_000) {                compose.onAllNodesWithText("JNI UI todo").fetchSemanticsNodes().isNotEmpty()            }            compose.runOnIdle { phase.intValue = 2 }            compose.awaitIdle()            val fields = compose.onAllNodes(hasSetTextAction())            fields[0].performTextInput("JNI UI note")            fields[1].performTextInput("saved through Compose")            compose.awaitIdle()            editorFor("note:new", null, null).flushPending(writerScope)?.await()            compose.runOnIdle { phase.intValue = 3 }            compose.waitUntil(timeoutMillis = 5_000) {                compose.onAllNodesWithText("JNI UI note").fetchSemanticsNodes().isNotEmpty()            }        } finally {            writerScope.cancel()            core.close()        }    }    private fun config() = buildJsonObject {        put("containerPath", container.absolutePath)        put("appVersion", "0.0.0-instrumented")        put("timeZone", "Etc/UTC")        put("deviceSecret", Base64.getUrlEncoder().withoutPadding().encodeToString(ByteArray(32) { 0x5b }))    }}