package com.cottondb.android.ui.day import androidx.compose.runtime.mutableIntStateOf import androidx.compose.ui.test.assertTextContains import androidx.compose.ui.test.hasSetTextAction import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.onAllNodesWithText import androidx.compose.ui.test.onAllNodesWithContentDescription import androidx.compose.ui.test.performClick import androidx.compose.ui.test.performTextReplacement import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.platform.app.InstrumentationRegistry import com.cottondb.android.core.CottonCore import com.cottondb.android.day.alarms.Alarm import com.cottondb.android.day.alarms.AlarmEditorModel import com.cottondb.android.day.alarms.AlarmsRepository import com.cottondb.android.design.CottonTheme import java.io.File import java.util.Base64 import java.util.UUID import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancel import kotlinx.coroutines.runBlocking import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.put import org.junit.After import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith /** Real JNI Compose proof for durable alarm intent, never Android scheduling. */ @RunWith(AndroidJUnit4::class) class AlarmScreensTest { @get:Rule val compose = createComposeRule() private val context = InstrumentationRegistry.getInstrumentation().targetContext private val container = File(context.noBackupFilesDir, "alarm-ui-test-${UUID.randomUUID()}") @After fun removeSyntheticContainer() { container.deleteRecursively() } @Test fun newAlarmEditNoopSaveAndToggleRenderThroughTheRealCore(): Unit = runBlocking { val core = CottonCore.open(config()) val writerScope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate) try { core.call("workspace.startLocal", buildJsonObject { put("deviceName", "Android alarm UI test") put("appVersion", "0.0.0-instrumented") }) val repository = AlarmsRepository(core) val editors = mutableMapOf() val editorFor = { key: String, initial: Alarm? -> editors.getOrPut(key) { AlarmEditorModel(repository, initial) } } val revision = mutableIntStateOf(0) compose.setContent { CottonTheme { AlarmsScreen(repository, writerScope, editorFor, revision.intValue.toLong(), onBack = {}) } } compose.waitUntil(5_000) { compose.onAllNodesWithText("No alarms yet.").fetchSemanticsNodes().isNotEmpty() } compose.onNodeWithContentDescription("New").performClick() compose.waitUntil(5_000) { compose.onAllNodesWithText("New alarm").fetchSemanticsNodes().isNotEmpty() } compose.onAllNodes(hasSetTextAction()).fetchSemanticsNodes().single().let { compose.onAllNodes(hasSetTextAction())[0].performTextReplacement("JNI Compose alarm") } compose.onNodeWithText("Save").performClick() compose.waitUntil(5_000) { compose.onAllNodesWithText("JNI Compose alarm").fetchSemanticsNodes().isNotEmpty() } compose.onNodeWithText("New alarm").assertDoesNotExist() // Opening the created row must not inherit create success from its separate editor. compose.onNodeWithText("JNI Compose alarm").performClick() compose.waitUntil(5_000) { compose.onAllNodesWithText("Edit alarm").fetchSemanticsNodes().isNotEmpty() } compose.onAllNodes(hasSetTextAction())[0].performTextReplacement("Edited JNI alarm") compose.onNodeWithText("Save").performClick() compose.waitUntil(5_000) { compose.onAllNodesWithText("Edited JNI alarm").fetchSemanticsNodes().isNotEmpty() } compose.onNodeWithText("Edit alarm").assertDoesNotExist() // A persisted update success is acknowledged; reopening stays open for an unchanged Save. compose.onNodeWithText("Edited JNI alarm").performClick() compose.waitUntil(5_000) { compose.onAllNodesWithText("Edit alarm").fetchSemanticsNodes().isNotEmpty() } compose.onNodeWithText("Save").performClick() compose.waitUntil(5_000) { compose.onAllNodesWithText("Edit alarm").fetchSemanticsNodes().isEmpty() } compose.onNodeWithContentDescription("Turn off Edited JNI alarm").performClick() compose.waitUntil(5_000) { compose.onAllNodesWithContentDescription("Turn on Edited JNI alarm").fetchSemanticsNodes().isNotEmpty() } compose.onNodeWithText("Edited JNI alarm").assertExists() compose.onNodeWithText("Edited JNI alarm").performClick() compose.waitUntil(5_000) { compose.onAllNodesWithText("Edit alarm").fetchSemanticsNodes().isNotEmpty() } compose.onAllNodes(hasSetTextAction())[0].assertTextContains("Edited JNI alarm") } 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) { 0x72 })) } }