package com.cottondb.android.ui.boot import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.test.assert 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.performClick import com.cottondb.android.boot.BootRefusal import com.cottondb.android.boot.BootState import com.cottondb.android.boot.JoinState import com.cottondb.android.design.CottonTheme import org.junit.Assert.assertEquals import org.junit.Rule import org.junit.Test class BootScreensTest { @get:Rule val compose = createComposeRule() @Test fun startCreatesALocalDatabaseWithTheDeviceFieldsItWasGiven() { var started: Pair? = null compose.setContent { CottonTheme { Gate( boot = BootState.Start, onStartLocal = { name, version -> started = name to version }, ) } } compose.onNodeWithContentDescription("Start a new database").performClick() compose.runOnIdle { assertEquals("Test handset" to "0.1-test", started) } } @Test fun joinChoiceReplacesStartWithTheServerNameForm() { compose.setContent { CottonTheme { Gate(boot = BootState.Start) } } compose.onNodeWithContentDescription("Join my database").performClick() compose.onNodeWithText("Join your Cotton").assertExists() compose.onNodeWithText("The name your other device shows").assertExists() compose.onNodeWithText("This phone's name").assertExists() } @Test fun pendingJoinDisplaysOnlyTheServerDigitsAndOffersCancellation() { var cancelled = 0 compose.setContent { CottonTheme { Gate( boot = BootState.Start, join = JoinState.Pending( digits = "184 920", server = "stonehaven", serverDeviceId = "device-key-from-server", ), showJoinInitially = true, onCancelJoin = { cancelled += 1 }, ) } } compose.onNodeWithContentDescription("Server digits 184 920") .assertExists() .assert(hasSetTextAction().not()) compose.onNodeWithText("Approve this phone on any of your devices. It will ask if the phone shows these numbers.") .assertExists() compose.onNodeWithContentDescription("Cancel joining").performClick() compose.runOnIdle { assertEquals(1, cancelled) } } @Test fun refusedPhoneOffersRetryAndRoutesItToTheRoot() { var retries = 0 compose.setContent { CottonTheme { Gate( boot = BootState.Refused( refusal = BootRefusal(code = "device_not_approved", sentence = null, erase = false), serverName = "stonehaven", ), onRetry = { retries += 1 }, ) } } compose.onNodeWithText("This phone isn't approved yet").assertExists() compose.onNodeWithContentDescription("Try again").performClick() compose.runOnIdle { assertEquals(1, retries) } } @Test fun keepMyCopyFromARevokedPhoneOpensTheJoinForm() { compose.setContent { CottonTheme { Gate( boot = BootState.Refused( refusal = BootRefusal(code = "device_revoked", sentence = null, erase = true), serverName = "stonehaven", ), ) } } compose.onNodeWithContentDescription("Keep my copy").performClick() compose.onNodeWithText("Join your Cotton").assertExists() } @Test fun joinedNameFormShowsTheSetNameError() { compose.setContent { CottonTheme { Gate( boot = BootState.Start, join = JoinState.Joined(needsName = true, error = "Enter a name with at least 2 characters."), showJoinInitially = true, ) } } compose.onNodeWithText("What should we call you?").assertExists() compose.onNodeWithText("Enter a name with at least 2 characters.").assertExists() } @Composable private fun Gate( boot: BootState, join: JoinState = JoinState.Form(), showJoinInitially: Boolean = false, onStartLocal: (String, String) -> Unit = { _, _ -> }, onCancelJoin: () -> Unit = {}, onRetry: () -> Unit = {}, ) { var showJoin by remember { mutableStateOf(showJoinInitially) } CottonBootGate( boot = boot, join = join, showJoin = showJoin, deviceName = "Test handset", appVersion = "0.1-test", onShowJoin = { showJoin = true }, onStartLocal = onStartLocal, onJoin = { _, _, _ -> }, onCancelJoin = { onCancelJoin() showJoin = false }, onRetry = onRetry, onSetName = {}, onErase = {}, onLeave = {}, readyContent = {}, ) } }