Cotton code
153 lines · 5.9 KB · KotlinRaw
package com.cottondb.android.ui.photosimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.setValueimport androidx.compose.ui.graphics.asAndroidBitmapimport androidx.compose.ui.test.captureToImageimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithContentDescriptionimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.onRootimport androidx.compose.ui.test.performClickimport androidx.test.ext.junit.runners.AndroidJUnit4import com.cottondb.android.design.CottonThemeimport com.cottondb.android.ScreenEvidenceimport 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.LibraryMediaimport com.cottondb.android.traffic.photos.LibraryMediaKindimport com.cottondb.android.traffic.photos.PhotosFilterimport com.cottondb.android.traffic.photos.PhotosMonthimport org.junit.Assert.assertEqualsimport org.junit.Ruleimport org.junit.Testimport 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    }}