package com.cottondb.android.ui.settings import androidx.compose.ui.test.assertCountEquals import androidx.compose.ui.graphics.asAndroidBitmap import androidx.compose.ui.test.captureToImage import androidx.compose.ui.test.onRoot import com.cottondb.android.ScreenEvidence import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onAllNodesWithContentDescription import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import androidx.test.ext.junit.runners.AndroidJUnit4 import com.cottondb.android.boot.BootRead import com.cottondb.android.boot.SyncStatusRead import com.cottondb.android.design.CottonTheme import com.cottondb.android.settings.SettingsAccount import com.cottondb.android.settings.SettingsRead import com.cottondb.android.settings.SettingsState import org.junit.Assert.assertEquals import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) class SettingsScreenTest { @get:Rule val compose = createComposeRule() @Test fun noMacSettingsHideManualSyncAndKeepTheLocalCopyExplanation() { compose.setContent { CottonTheme { SettingsScreen( state = SettingsState.Data(read(sync = SyncStatusRead(false, SettingsRead.NO_SERVER_YET, null))), onBack = {}, onRefresh = {}, onJoin = {}, onOpenDevices = {}, onOpenNotificationSettings = {}, onSyncNow = {}, ) } } compose.onNodeWithText("No Mac yet").assertExists() compose.onNodeWithText("Everything stays on this phone").assertExists() compose.onAllNodesWithContentDescription("Sync now").assertCountEquals(0) ScreenEvidence.capture("settings-local") { compose.onRoot().captureToImage().asAndroidBitmap() } } @Test fun retainedReadFailureShowsItsMessageAndUsesTheExplicitCallbacks() { var refreshed = 0 var notifications = 0 var devices = 0 var syncs = 0 compose.setContent { CottonTheme { SettingsScreen( state = SettingsState.Error("Could not sync this database.", read()), onBack = {}, onRefresh = { refreshed += 1 }, onJoin = {}, onOpenDevices = { devices += 1 }, onOpenNotificationSettings = { notifications += 1 }, onSyncNow = { syncs += 1 }, ) } } compose.onNodeWithText("Could not sync this database.").assertExists() ScreenEvidence.capture("settings-sync-error") { compose.onRoot().captureToImage().asAndroidBitmap() } compose.onNodeWithText("Synthetic Owner").assertExists() compose.onNodeWithText("Try again").performClick() compose.onNodeWithContentDescription("Notifications").performClick() compose.onNodeWithContentDescription("Your devices").performClick() compose.onNodeWithContentDescription("Sync now").performClick() assertEquals(1, refreshed) assertEquals(1, notifications) assertEquals(1, devices) assertEquals(1, syncs) } @Test fun pairedSettingsShowsExactBinLabelAndDelegatesItsOnlyAction() { var opened = 0 compose.setContent { CottonTheme { SettingsScreen( state = SettingsState.Data(read()), onBack = {}, onRefresh = {}, onJoin = {}, onOpenDevices = {}, onOpenNotificationSettings = {}, onSyncNow = {}, onOpenBin = { opened += 1 }, binLabel = "2 items", ) } } compose.onNodeWithContentDescription("In the bin, 2 items").performClick() assertEquals(1, opened) } @Test fun unpairedSettingsDoesNotOfferTheBinEvenWhenTheHostProvidesOne() { compose.setContent { CottonTheme { SettingsScreen( state = SettingsState.Data(read().copy(boot = read().boot.copy(paired = false))), onBack = {}, onRefresh = {}, onJoin = {}, onOpenDevices = {}, onOpenNotificationSettings = {}, onSyncNow = {}, onOpenBin = {}, binLabel = "could not read", ) } } compose.onNodeWithContentDescription("In the bin, could not read").assertDoesNotExist() } @Test fun legalPagesRemainAvailableWhenSettingsCannotRead() { var privacy = 0 var terms = 0 compose.setContent { CottonTheme { SettingsScreen( state = SettingsState.Error("Offline", null), onBack = {}, onRefresh = {}, onJoin = {}, onOpenDevices = {}, onOpenNotificationSettings = {}, onSyncNow = {}, onOpenPrivacy = { privacy += 1 }, onOpenTerms = { terms += 1 }, ) } } compose.onNodeWithContentDescription("Privacy").performClick() compose.onNodeWithContentDescription("Terms").performClick() assertEquals(1, privacy) assertEquals(1, terms) } private fun read(sync: SyncStatusRead? = SyncStatusRead(true, "joined", null, true)) = SettingsRead( boot = BootRead( paired = true, stage = "joined", ownerId = null, workspaceId = null, serverUrl = null, serverName = "Cotton", refusal = null, ), account = SettingsAccount("Synthetic Owner"), sync = sync, ) }