73 lines · 3.5 KB · KotlinRaw
package com.cottondb.android.ui.photosimport android.graphics.Bitmapimport android.graphics.Colorimport androidx.activity.ComponentActivityimport androidx.compose.runtime.mutableStateOfimport androidx.compose.ui.test.assertIsDisplayedimport androidx.compose.ui.test.junit4.createAndroidComposeRuleimport androidx.compose.ui.test.onNodeWithContentDescriptionimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.onAllNodesWithContentDescriptionimport androidx.compose.ui.test.onAllNodesWithTextimport androidx.compose.ui.test.performClickimport androidx.test.ext.junit.runners.AndroidJUnit4import com.cottondb.android.media.LocalMediaFileimport java.io.Fileimport java.io.FileOutputStreamimport java.util.UUIDimport org.junit.Afterimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWith@RunWith(AndroidJUnit4::class)class LocalPhotoViewerTest { @get:Rule val compose = createAndroidComposeRule<ComponentActivity>() private val root = File(androidx.test.platform.app.InstrumentationRegistry.getInstrumentation().targetContext.cacheDir, "photo-viewer-${UUID.randomUUID()}") @After fun cleanUp() { root.deleteRecursively() } @Test fun decodesSyntheticBitmapShowsCaptionAndCloses() { val file = syntheticPng("one.png", Color.rgb(24, 81, 141)) var closed = false compose.setContent { LocalPhotoViewer(LocalMediaFile(file), "Synthetic caption") { closed = true } } compose.waitUntil(5_000) { compose.onAllNodesWithContentDescription("Photo").fetchSemanticsNodes().isNotEmpty() } compose.onNodeWithContentDescription("Photo").assertIsDisplayed() compose.onNodeWithText("Synthetic caption").assertExists() compose.onNodeWithContentDescription("Close").performClick() assertTrue(closed) } @Test fun corruptFileShowsNamedFailureAndPermitsLocalRetry() { val file = root.apply { mkdirs() }.resolve("corrupt.png").apply { writeText("not an image") } compose.setContent { LocalPhotoViewer(LocalMediaFile(file), null) {} } compose.waitUntil(5_000) { compose.onAllNodesWithText("Try again").fetchSemanticsNodes().isNotEmpty() } compose.onNodeWithText("This photo could not be opened.").assertExists() compose.onNodeWithText("Try again").performClick() compose.waitUntil(5_000) { compose.onAllNodesWithText("Try again").fetchSemanticsNodes().isNotEmpty() } } @Test fun replacingFileRecomposesAFreshViewerDecode() { val first = syntheticPng("first.png", Color.RED) val second = syntheticPng("second.png", Color.BLUE) val selected = mutableStateOf(LocalMediaFile(first)) compose.setContent { LocalPhotoViewer(selected.value, null) {} } compose.waitUntil(5_000) { compose.onAllNodesWithContentDescription("Photo").fetchSemanticsNodes().isNotEmpty() } compose.runOnUiThread { selected.value = LocalMediaFile(second) } compose.waitUntil(5_000) { compose.onAllNodesWithContentDescription("Photo").fetchSemanticsNodes().isNotEmpty() } compose.onNodeWithContentDescription("Photo").assertIsDisplayed() } private fun syntheticPng(name: String, color: Int): File { root.mkdirs() val target = root.resolve(name) val bitmap = Bitmap.createBitmap(6, 4, Bitmap.Config.ARGB_8888) try { bitmap.eraseColor(color) FileOutputStream(target).use { output -> check(bitmap.compress(Bitmap.CompressFormat.PNG, 100, output)) } } finally { bitmap.recycle() } return target }}