166 lines · 5.6 KB · KotlinRaw
package com.cottondb.android.ui.bootimport androidx.compose.runtime.Composableimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.rememberimport androidx.compose.runtime.setValueimport androidx.compose.ui.test.assertimport 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.performClickimport com.cottondb.android.boot.BootRefusalimport com.cottondb.android.boot.BootStateimport com.cottondb.android.boot.JoinStateimport com.cottondb.android.design.CottonThemeimport org.junit.Assert.assertEqualsimport org.junit.Ruleimport org.junit.Testclass BootScreensTest { @get:Rule val compose = createComposeRule() @Test fun startCreatesALocalDatabaseWithTheDeviceFieldsItWasGiven() { var started: Pair<String, String>? = 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 = {}, ) }}