package com.cottondb.android.ui.photos import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import androidx.compose.ui.graphics.asAndroidBitmap import androidx.compose.ui.test.captureToImage import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.onRoot import androidx.compose.ui.test.performClick import androidx.test.ext.junit.runners.AndroidJUnit4 import com.cottondb.android.design.CottonTheme import com.cottondb.android.ScreenEvidence import com.cottondb.android.media.LocalMedia import com.cottondb.android.media.LocalMediaSource import com.cottondb.android.media.MediaPathTier import com.cottondb.android.navigation.Route import com.cottondb.android.traffic.photos.LibraryMedia import com.cottondb.android.traffic.photos.LibraryMediaKind import com.cottondb.android.traffic.photos.PhotosFilter import com.cottondb.android.traffic.photos.PhotosMonth import org.junit.Assert.assertEquals import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) class PhotosScreenTest { @get:Rule val compose = createComposeRule() @Test fun filterSelectionAndOpenStayControlled() { var shown: PhotosFilter? = null var toggled: String? = null var opened: Route? = null show( state = state(), onShowChange = { shown = it }, onToggleSelected = { toggled = it }, onOpen = { opened = it }, ) compose.onNodeWithContentDescription("Show videos").performClick() compose.onNodeWithContentDescription("Photo tile photo-1: Synthetic photo").performClick() assertEquals(PhotosFilter.Videos, shown) assertEquals(Route.PhotosPhoto("photo-1"), opened) assertEquals(null, toggled) } @Test fun aSelectedTileOnlyDelegatesItsSelection() { var toggled: String? = null var opened: Route? = null show( state = state().copy(selecting = true), onToggleSelected = { toggled = it }, onOpen = { opened = it }, ) compose.onNodeWithContentDescription("Photo tile video-2: Synthetic video").performClick() assertEquals("video-2", toggled) assertEquals(null, opened) } @Test fun unreadableTakesPrecedenceOverCachedTilesWhileRefreshKeepsThem() { show(state().copy(unreadable = true, loading = true)) compose.onNodeWithText("Your photos could not be read").assertExists() compose.onNodeWithContentDescription("Photo tile photo-1: Synthetic photo").assertDoesNotExist() } @Test fun refreshKeepsCachedTiles() { show(state().copy(loading = true)) compose.onNodeWithContentDescription("Photo tile photo-1: Synthetic photo").assertExists() } @Test fun contactSheetToggleRendersTheControlledChoice() { var current by mutableStateOf(state().copy(prints = true)) compose.setContent { CottonTheme { PhotosScreen( state = current, media = AbsentMedia, dataRevision = 0, onBack = {}, onShowChange = {}, onPrintsChange = { current = current.copy(prints = it) }, onSearchingChange = {}, onQueryChange = {}, onRetry = {}, onStartSelecting = null, onStopSelecting = {}, onToggleSelected = {}, onOpen = {}, ) } } compose.onNodeWithContentDescription("Change photo layout").performClick() compose.onNodeWithText("Prints").assertExists() ScreenEvidence.capture("photos-contact-sheet") { compose.onRoot().captureToImage().asAndroidBitmap() } compose.onNodeWithContentDescription("Photo tile photo-1: Synthetic photo").assertExists() compose.onNodeWithContentDescription("Photo tile video-2: Synthetic video").assertExists() } private fun show( state: PhotosScreenState, onShowChange: (PhotosFilter) -> Unit = {}, onPrintsChange: (Boolean) -> Unit = {}, onToggleSelected: (String) -> Unit = {}, onOpen: (Route) -> Unit = {}, ) { compose.setContent { CottonTheme { PhotosScreen( state = state, media = AbsentMedia, dataRevision = 0, onBack = {}, onShowChange = onShowChange, onPrintsChange = onPrintsChange, onSearchingChange = {}, onQueryChange = {}, onRetry = {}, onStartSelecting = null, onStopSelecting = {}, onToggleSelected = onToggleSelected, onOpen = onOpen, ) } } } private fun state() = PhotosScreenState( months = listOf( PhotosMonth( id = "2026-09", title = "September", items = listOf( media("photo-1", LibraryMediaKind.Photo, key = "photos/photo-1", caption = "Synthetic photo"), media("video-2", LibraryMediaKind.Video, key = "videos/video-2", posterKey = "photos/poster-2", caption = "Synthetic video", durationMs = 42_000), ), ), ), hasPhotos = true, hasVideos = true, firstRead = false, ) private fun media( id: String, kind: LibraryMediaKind, key: String? = null, posterKey: String? = null, caption: String, durationMs: Long? = null, ) = LibraryMedia(id, kind, key, null, posterKey, caption, null, "2026-09-22T00:00:00Z", 0, null, false, emptyList(), emptyList(), durationMs) private object AbsentMedia : LocalMediaSource { override suspend fun local(key: String, tier: MediaPathTier) = LocalMedia.Absent } }