97 lines · 5.4 KB · KotlinRaw
package com.cottondb.android.ui.photosimport androidx.compose.ui.test.assertIsNotEnabledimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithContentDescriptionimport androidx.compose.ui.test.onAllNodesWithTextimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.performClickimport androidx.test.ext.junit.runners.AndroidJUnit4import com.cottondb.android.design.CottonThemeimport com.cottondb.android.media.LocalMediaimport com.cottondb.android.media.LocalMediaSourceimport com.cottondb.android.media.MediaPathTierimport com.cottondb.android.navigation.Routeimport com.cottondb.android.traffic.photos.albums.AddableAlbumPhotoimport com.cottondb.android.traffic.photos.albums.AlbumAddStateimport com.cottondb.android.traffic.photos.albums.AlbumCardimport com.cottondb.android.traffic.photos.albums.AlbumDecisionimport com.cottondb.android.traffic.photos.albums.AlbumListStateimport java.time.Clockimport java.time.Instantimport java.time.ZoneIdimport org.junit.Assert.assertEqualsimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWith@RunWith(AndroidJUnit4::class)class AlbumCollectionScreensTest { @get:Rule val compose = createComposeRule() @Test fun firstReadErrorShowsRetryBeforeLoading() { var retries = 0 list(AlbumListState(readProblem = "offline"), onRetry = { retries++ }) compose.onNodeWithText("Your albums could not be read").assertExists() compose.onNodeWithText("Try again").performClick() assertEquals(1, retries) } @Test fun cachedReadErrorKeepsRowsVisible() { list(AlbumListState(albums = listOf(card("a")), hasRead = true, readProblem = "offline")) compose.onNodeWithContentDescription("Album Album a").assertExists() compose.onNodeWithText("offline").assertExists() } @Test fun listKeepDismissAndOpenCarryExactIdAndNativeOrder() { val decisions = mutableListOf<Pair<String, AlbumDecision>>(); val opened = mutableListOf<Route>() list(AlbumListState(albums = listOf(card("first"), card("second")), hasRead = true), onDecide = { id, decision -> decisions += id to decision }, onOpen = { opened += it }) compose.onNodeWithContentDescription("Album Album first").performClick() compose.onAllNodesWithText("Keep")[0].performClick(); compose.onAllNodesWithText("Not this")[0].performClick() assertEquals(Route.PhotosAlbum("first"), opened.single()) assertEquals(listOf("first" to AlbumDecision.Keep, "first" to AlbumDecision.Dismiss), decisions) } @Test fun unknownWriteShowsProblemAndDisablesDecisions() { list(AlbumListState(albums = listOf(card("a")), hasRead = true, outcomeUnknown = true, writeProblem = "Could not confirm")) compose.onNodeWithText("Could not confirm").assertExists() compose.onNodeWithText("Keep").assertIsNotEnabled() compose.onNodeWithText("Not this").assertIsNotEnabled() } @Test fun listLayoutCallbackIsControlled() { var choice: Boolean? = null list(AlbumListState(albums = listOf(card("a")), hasRead = true), prints = true, onPrintsChange = { choice = it }) compose.onNodeWithText("List").performClick() assertEquals(false, choice) } @Test fun addToggleAndSelectedAddAreControlled() { var toggled: String? = null; var added = 0 add(AlbumAddState(photos = listOf(photo("photo:a")), hasRead = true, selected = setOf("photo:a")), onToggle = { toggled = it }, onAdd = { added++ }) compose.onNodeWithContentDescription("Photo tile photo:a: Photo").performClick() compose.onNodeWithContentDescription("Add the selected pictures").performClick() assertEquals("photo:a", toggled); assertEquals(1, added) } @Test fun unknownAddIsDisabledAndEmptyCopyMatchesSwift() { add(AlbumAddState(photos = listOf(photo("photo:a")), hasRead = true, selected = setOf("photo:a"), outcomeUnknown = true)) compose.onNodeWithContentDescription("Add the selected pictures").assertIsNotEnabled() } @Test fun emptyAddCopyMatchesSwift() { add(AlbumAddState(hasRead = true)) compose.onNodeWithText("Nothing left to add.").assertExists() compose.onNodeWithText("Every picture that can go in this album is already in it.").assertExists() } private fun list(state: AlbumListState, prints: Boolean = true, onRetry: () -> Unit = {}, onPrintsChange: (Boolean) -> Unit = {}, onDecide: (String, AlbumDecision) -> Unit = { _, _ -> }, onOpen: (Route) -> Unit = {}) = compose.setContent { CottonTheme { AlbumListScreen(state, AbsentMedia, 0, prints, ZoneId.of("Etc/UTC"), onPrintsChange, {}, onRetry, onDecide, onOpen) } } private fun add(state: AlbumAddState, onToggle: (String) -> Unit = {}, onAdd: () -> Unit = {}) = compose.setContent { CottonTheme { AlbumAddScreen(state, AbsentMedia, 0, Clock.fixed(Instant.parse("2026-01-01T00:00:00Z"), ZoneId.of("Etc/UTC")), {}, {}, onToggle, onAdd) } } private fun card(id: String) = AlbumCard(id, "trip", "open", null, "Album $id", null, "2026-01-01", "2026-01-01", null, null, 1, emptyList(), null, null, null) private fun photo(id: String) = AddableAlbumPhoto(id, null, null, null, null, null, null, "2026-01-01", 1, null, null, null, null, null, false, emptyList(), null, null, null, null, null, emptyList()) private object AbsentMedia : LocalMediaSource { override suspend fun local(key: String, tier: MediaPathTier) = LocalMedia.Absent }}