107 lines · 5.5 KB · KotlinRaw
package com.cottondb.android.ui.dayimport androidx.compose.runtime.mutableIntStateOfimport androidx.compose.ui.test.assertTextContainsimport androidx.compose.ui.test.hasSetTextActionimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithContentDescriptionimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.onAllNodesWithTextimport androidx.compose.ui.test.onAllNodesWithContentDescriptionimport androidx.compose.ui.test.performClickimport androidx.compose.ui.test.performTextReplacementimport androidx.test.ext.junit.runners.AndroidJUnit4import androidx.test.platform.app.InstrumentationRegistryimport com.cottondb.android.core.CottonCoreimport com.cottondb.android.day.alarms.Alarmimport com.cottondb.android.day.alarms.AlarmEditorModelimport com.cottondb.android.day.alarms.AlarmsRepositoryimport com.cottondb.android.design.CottonThemeimport java.io.Fileimport 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 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<String, AlarmEditorModel>() 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 })) }}