Cotton code
154 lines · 5.8 KB · KotlinRaw
package com.cottondb.android.ui.settingsimport androidx.compose.ui.test.assertCountEqualsimport androidx.compose.ui.graphics.asAndroidBitmapimport androidx.compose.ui.test.captureToImageimport androidx.compose.ui.test.onRootimport com.cottondb.android.ScreenEvidenceimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onAllNodesWithContentDescriptionimport androidx.compose.ui.test.onNodeWithContentDescriptionimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.performClickimport androidx.test.ext.junit.runners.AndroidJUnit4import com.cottondb.android.boot.BootReadimport com.cottondb.android.boot.SyncStatusReadimport com.cottondb.android.design.CottonThemeimport com.cottondb.android.settings.SettingsAccountimport com.cottondb.android.settings.SettingsReadimport com.cottondb.android.settings.SettingsStateimport org.junit.Assert.assertEqualsimport org.junit.Ruleimport org.junit.Testimport 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,    )}