178 lines · 9.1 KB · KotlinRaw
package com.cottondb.android.ui.photosimport android.graphics.Bitmapimport androidx.compose.runtime.keyimport androidx.compose.runtime.mutableStateOfimport 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.onRootimport androidx.compose.ui.test.performClickimport androidx.test.ext.junit.runners.AndroidJUnit4import androidx.test.platform.app.InstrumentationRegistryimport com.cottondb.android.PhotosModelsimport com.cottondb.android.ScreenEvidenceimport com.cottondb.android.FeatureModelsimport com.cottondb.android.core.CottonCallingimport com.cottondb.android.core.CottonEventimport com.cottondb.android.core.MalformedCoreEventExceptionimport com.cottondb.android.design.CottonThemeimport com.cottondb.android.media.LocalMediaimport com.cottondb.android.media.LocalMediaFileimport com.cottondb.android.media.MediaSourceimport com.cottondb.android.media.EnsuredMediaimport com.cottondb.android.media.MediaPathTierimport com.cottondb.android.media.ResolvedMediaTierimport com.cottondb.android.navigation.Routeimport java.io.Fileimport java.time.Clockimport java.time.Instantimport java.time.ZoneOffsetimport kotlinx.coroutines.CoroutineScopeimport kotlinx.coroutines.Dispatchersimport kotlinx.coroutines.SupervisorJobimport kotlinx.coroutines.cancelimport kotlinx.coroutines.flow.MutableSharedFlowimport kotlinx.coroutines.flow.SharedFlowimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.JsonElementimport kotlinx.serialization.json.JsonObjectimport org.junit.Afterimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWith@RunWith(AndroidJUnit4::class)class PhotosRouteTest { @get:Rule val compose = createComposeRule() private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate) private var mediaFile: File? = null @After fun close() { compose.runOnIdle { scope.cancel() }; mediaFile?.delete() } @Test fun videosRouteReadsOnceAndOpensTheTypedVideoRouteWithoutEnsure() { val core = Core() val source = bitmapMedia() val models = PhotosModels(core, scope) var opened: Route? = null compose.setContent { CottonTheme { PhotosRoute(Route.Photos("videos"), models, source, CLOCK, 0, {}, { opened = it }) } } compose.waitUntil(5_000) { core.reads == 1 && source.keys.isNotEmpty() } ScreenEvidence.capture("photos-route-videos") { compose.onRoot().captureToImage().asAndroidBitmap() } compose.onNodeWithContentDescription("Photo tile video-1: Synthetic video").performClick() assertEquals(Route.VideosVideo("video-1"), opened) assertTrue(source.keys.all { it == "media/poster-1" }) assertTrue(core.methods.all { it == "read.photos" }) } @Test fun remountKeepsTheRetainedFilterAndRefreshesOnlyReadPhotos() { val core = Core() val source = bitmapMedia() val models = PhotosModels(core, scope) val generation = mutableStateOf(0) compose.setContent { CottonTheme { key(generation.value) { PhotosRoute(Route.Photos("photos"), models, source, CLOCK, 0, {}, {}) } } } compose.waitUntil(5_000) { core.reads == 1 } compose.onNodeWithContentDescription("Show videos").performClick() compose.runOnIdle { generation.value++ } compose.waitUntil(5_000) { core.reads == 2 } compose.onNodeWithContentDescription("Photo tile video-1: Synthetic video").assertExists() compose.onNodeWithContentDescription("Photo tile photo-1: Synthetic photo").assertDoesNotExist() assertTrue(core.methods.all { it == "read.photos" }) } @Test fun appNavigationMapsVideosAliasAndBackRetiresTheVisit() { val core = Core() val source = bitmapMedia() val features = features(core, source) compose.setContent { CottonTheme { com.cottondb.android.ui.navigation.AppNavigation( home = com.cottondb.android.home.HomeState.Error("Synthetic Home"), boot = com.cottondb.android.boot.BootRead(true, "joined", null, "synthetic-workspace", null, "Cotton", null), features = features, onJoin = {}, onLeave = {}, onRefreshHome = {}, onSaveArrangement = {}, notificationRoute = Route.Videos, ) } } compose.waitUntil(5_000) { core.reads == 1 && source.keys.contains("media/poster-1") } compose.onNodeWithContentDescription("Photo tile video-1: Synthetic video").assertExists() lateinit var retired: com.cottondb.android.traffic.photos.PhotosLibraryModel compose.runOnIdle { retired = features.photos.library(Route.Photos("videos")) } compose.onNodeWithContentDescription("Back").performClick() compose.waitForIdle() compose.runOnIdle { assertEquals(null, retired.refresh()) } } @Test fun appNavigationAdmitsTheDirectPhotosRoute() { val core = Core() val source = bitmapMedia() val features = features(core, source) compose.setContent { CottonTheme { com.cottondb.android.ui.navigation.AppNavigation( home = com.cottondb.android.home.HomeState.Error("Synthetic Home"), boot = com.cottondb.android.boot.BootRead(true, "joined", null, "synthetic-workspace", null, "Cotton", null), features = features, onJoin = {}, onLeave = {}, onRefreshHome = {}, onSaveArrangement = {}, notificationRoute = Route.Photos(null), ) } } compose.waitUntil(5_000) { core.reads == 1 && source.keys.contains("media/photo-1") } compose.onNodeWithContentDescription("Photo tile photo-1: Synthetic photo").assertExists() assertTrue(core.methods.all { it == "read.photos" }) } private fun features(core: Core, source: MediaSource): FeatureModels { val context = InstrumentationRegistry.getInstrumentation().targetContext val access = com.cottondb.android.passwords.VaultAccess(core) val session = com.cottondb.android.passwords.VaultSession(access, com.cottondb.android.passwords.VaultOperations(access, com.cottondb.android.passwords.VaultKeyRepublisher {}), scope, {}) return FeatureModels(core, CLOCK, scope, source, com.cottondb.android.ui.traffic.messages.AndroidWhatsAppExportStager(context), com.cottondb.android.PasswordModels(core, scope, CLOCK, session) {}, object : com.cottondb.android.search.SearchRecentsStore { override fun load() = emptyList<String>() override fun save(recents: List<String>) = Unit }, com.cottondb.android.ui.imports.AndroidSafFileStager(context, "photos-import-test", "document")) } private fun bitmapMedia(): BitmapMedia { val file = File(InstrumentationRegistry.getInstrumentation().targetContext.cacheDir, "photos-route-synthetic.png") Bitmap.createBitmap(8, 8, Bitmap.Config.ARGB_8888).also { bitmap -> bitmap.eraseColor(android.graphics.Color.rgb(79, 130, 167)) file.outputStream().use { check(bitmap.compress(Bitmap.CompressFormat.PNG, 100, it)) } bitmap.recycle() } mediaFile = file return BitmapMedia(file) } private class BitmapMedia(private val file: File) : MediaSource { val keys = java.util.concurrent.CopyOnWriteArrayList<String>() override suspend fun local(key: String, tier: MediaPathTier): LocalMedia { keys += key return LocalMedia.Present(LocalMediaFile(file), ResolvedMediaTier.Thumb) } override suspend fun ensure(key: String): EnsuredMedia = error("Photos library never ensures media") override suspend fun viewed(key: String) = false } private class Core : CottonCalling { override val events: SharedFlow<CottonEvent> = MutableSharedFlow() override val eventErrors: SharedFlow<MalformedCoreEventException> = MutableSharedFlow() val methods = mutableListOf<String>(); var reads = 0 override suspend fun call(method: String, request: JsonObject): JsonElement { methods += method; check(request.isEmpty()); reads += 1 return Json.parseToJsonElement("""{"photos":[{"id":"photo-1","key":"media/photo-1","preview":null,"caption":"Synthetic photo","description":null,"takenAt":"2026-09-22T00:00:00Z","sizeBytes":1,"contentType":"image/png","favourite":false,"people":[],"projects":[]}],"videos":[{"id":"video-1","key":"media/video-1","posterKey":"media/poster-1","preview":null,"caption":"Synthetic video","description":null,"takenAt":"2026-09-21T00:00:00Z","sizeBytes":1,"contentType":"video/mp4","favourite":false,"people":[],"projects":[],"durationMs":42000}],"albums":[],"duplicates":0,"faceSuggestions":0}""") } override suspend fun close() = Unit } private companion object { val CLOCK: Clock = Clock.fixed(Instant.parse("2026-09-22T12:00:00Z"), ZoneOffset.UTC) }}