library/image: deep SVG and loader-pipeline coverage (#tests)
- Commit
cd3b97a0255278ca2a83161cab39e87df638c959- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-16 14:10 UTC (19 days ago)
- Parents
e155283
SvgDeepTest pins transform matrices (translate/scale/rotate/ skew/matrix with real Matrix values), style-attribute parsing, every shape family (ellipse/line/polyline/polygon/rounded rect), nested SVG viewports, hidden content, the named-colour table with its Android fallback, and fillMissingDimensions. LoaderDeepTest drives the pipeline branches the basic suite missed: remote bytes -> NETWORK success -> memory hit -> DISK hit on a fresh loader sharing the disk cache, fetcher-supplied bitmaps, throwing fetchers (Error), throwing transformations (skipped), video-frame fallback, PNG bytes reaching the bitmap decoder, AnimatedImage with an undecodable URI, and Success result plumbing. Coverage (createMergedCoverageReport, instruction): library/image 67.7% (7525/11121) -> 84.3% (9375/11121). Remaining missed is concentrated in VideoFrameDecoder (MediaMetadataRetriever, device-only) and AnimatedImageDrawable playback, which need a device. Verified: :library:image:testDebugUnitTest, detekt and lint green without the ignore-failures init script.
Diff
library/image/src/test/java/com/vayunmathur/library/image/LoaderDeepTest.kt
diff --git a/library/image/src/test/java/com/vayunmathur/library/image/LoaderDeepTest.kt b/library/image/src/test/java/com/vayunmathur/library/image/LoaderDeepTest.ktnew file mode 100644index 0000000000..83bc02ff6f--- /dev/null+++ b/library/image/src/test/java/com/vayunmathur/library/image/LoaderDeepTest.kt@@ -0,0 +1,175 @@+package com.vayunmathur.library.image++import android.content.Context+import android.graphics.Bitmap+import android.util.Base64+import androidx.compose.material3.Text+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onRoot+import androidx.core.net.toUri+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.library.image.compose.AnimatedImage+import com.vayunmathur.library.image.fetchers.FetchResult+import com.vayunmathur.library.image.fetchers.Fetcher+import com.vayunmathur.library.image.util.ensureExists+import kotlinx.coroutines.runBlocking+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import java.io.File+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNotNull+import kotlin.test.assertTrue++/**+ * Loader pipeline branches the basic suite does not reach: remote bytes+ * (NETWORK, then memory hit, then DISK hit on a fresh loader sharing the+ * disk cache), fetcher-supplied bitmaps and sources, throwing fetchers and+ * transformations, video-frame fallbacks, and the AnimatedImage compose+ * path when no drawable can be decoded.+ */+@RunWith(RobolectricTestRunner::class)+class LoaderDeepTest {++ @get:Rule val composeRule = createComposeRule()++ private val ctx get() = ApplicationProvider.getApplicationContext<Context>()++ private fun svgBytes(seed: Int) =+ """<svg xmlns="http://www.w3.org/2000/svg" width="20" height="10" viewBox="0 0 20 10"><rect width="20" height="10" fill="#${seed}0${seed}0${seed}0"/></svg>"""+ .toByteArray()++ private fun freshDisk(name: String): DiskCache {+ val dir = File(ctx.cacheDir, "deep-$name-${System.nanoTime()}").ensureExists()+ return DiskCache(dir, 4 * 1024 * 1024)+ }++ private fun request(data: Any?, key: String) =+ ImageRequest.Builder(ctx).data(data).memoryCacheKey(key).size(Size(20, 10)).build()++ @Test fun remoteBytesFlowNetworkThenMemoryThenDisk(): Unit = runBlocking {+ val disk = freshDisk("remote")+ var fetches = 0+ val loader = ImageLoader.Builder(ctx)+ .diskCache(disk)+ .memoryCache(MemoryCache.Builder().build())+ .remote { data, _ ->+ fetches+++ if (data == "pic") FetchResult.Bytes(svgBytes(1)) else null+ }+ .build()++ val first = loader.execute(request("pic", "k-pic"))+ assertTrue(first is ImageResult.Success)+ assertEquals(ImageResult.DataSource.NETWORK, (first as ImageResult.Success).dataSource)+ assertEquals(1, fetches)++ val second = loader.execute(request("pic", "k-pic"))+ assertTrue(second is ImageResult.Success)+ assertTrue((second as ImageResult.Success).isFromMemory)+ assertEquals(1, fetches, "memory hit must not refetch")+ assertNotNull(loader.peekMemoryCache(request("pic", "k-pic")))++ // A fresh loader over the same disk cache serves DISK without fetching.+ var diskFetches = 0+ val diskLoader = ImageLoader.Builder(ctx)+ .diskCache(disk)+ .memoryCache(MemoryCache.Builder().build())+ .remote { _, _ -> diskFetches++; null }+ .build()+ val third = diskLoader.execute(request("pic", "k-pic"))+ assertTrue(third is ImageResult.Success)+ assertEquals(ImageResult.DataSource.DISK, (third as ImageResult.Success).dataSource)+ assertEquals(0, diskFetches)+ assertNotNull(third.bitmap)+ // Success result plumbing: drawable conversion and defaults.+ assertNotNull(third.asBitmapDrawable(ctx.resources))+ assertNotNull(third.drawable)+ }++ @Test fun fetcherBitmapAndThrowingFetcherOutcomes(): Unit = runBlocking {+ val bmp = Bitmap.createBitmap(6, 4, Bitmap.Config.ARGB_8888)+ val bitmapLoader = ImageLoader.Builder(ctx)+ .diskCache(freshDisk("bitmapfetcher"))+ .addFetcher(object : Fetcher {+ override suspend fun fetch(data: Any?, context: Context): FetchResult? =+ if (data == "bmp") FetchResult.BitmapResult(bmp) else null+ })+ .build()+ val hit = bitmapLoader.execute(request("bmp", "k-bmp"))+ assertTrue(hit is ImageResult.Success)+ assertEquals(6, (hit as ImageResult.Success).bitmap.width)++ val throwing = ImageLoader.Builder(ctx)+ .diskCache(freshDisk("throwing"))+ .addFetcher(object : Fetcher {+ override suspend fun fetch(data: Any?, context: Context): FetchResult? =+ throw IllegalStateException("fetch boom")+ })+ .build()+ val failed = throwing.execute(request("anything", "k-throw"))+ assertTrue(failed is ImageResult.Error, "throwing fetcher must surface Error, was $failed")+ }++ @Test fun throwingTransformationIsSkippedAndGoodOneApplies(): Unit = runBlocking {+ val src = Bitmap.createBitmap(4, 4, Bitmap.Config.ARGB_8888)+ val boom = object : Transformation {+ override val cacheKey: String = "boom"+ override suspend fun transform(input: Bitmap, size: Size): Bitmap =+ throw IllegalStateException("transform boom")+ }+ val loader = ImageLoader.Builder(ctx).build()+ val req = ImageRequest.Builder(ctx).data(src).transformations(listOf(boom)).build()+ val result = loader.execute(req)+ assertTrue(result is ImageResult.Success)+ assertEquals(4, (result as ImageResult.Success).bitmap.width)+ }++ @Test fun videoFrameRequestFallsBackToErrorForUnfetchableData(): Unit = runBlocking {+ val loader = ImageLoader.Builder(ctx).diskCache(freshDisk("video")).build()+ val req = ImageRequest.Builder(ctx)+ .data("content://media/external/video/1".toUri())+ .videoFrameMillis(1_000L)+ .build()+ val result = loader.execute(req)+ assertTrue(result is ImageResult.Error, "no frame can be extracted under Robolectric, was $result")+ }++ @Test fun pngBytesReachBitmapDecoderAndFailCleanlyOnHost(): Unit = runBlocking {+ // 1x1 transparent PNG. Robolectric's BitmapFactory shadow cannot+ // decode real PNG bytes, so on host the loader must surface its+ // documented Error (never a crash or a blank Success); on device+ // the same bytes decode through BitmapDecoder.+ val png = Base64.decode(+ "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8Xw8AAwMCAO+ip1sAAAAASUVORK5CYII=",+ Base64.DEFAULT,+ )+ val loader = ImageLoader.Builder(ctx)+ .diskCache(freshDisk("png"))+ .remote { data, _ -> if (data == "png") FetchResult.Bytes(png) else null }+ .build()+ val result = loader.execute(+ ImageRequest.Builder(ctx).data("png").memoryCacheKey("k-png").build(),+ )+ assertTrue(result is ImageResult.Error, "host decode failure must be Error, was $result")+ }++ @Test fun animatedImageWithUndecodableUriComposesToNothing() {+ composeRule.setContent {+ AnimatedImage(uri = "content://media/external/images/999999".toUri(), contentDescription = "anim")+ Text("after")+ }+ composeRule.onRoot().assertExists()+ }++ @Test fun imageResultSuccessDefaults() {+ val bmp = Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888)+ val success = ImageResult.Success(bmp)+ assertEquals(ImageResult.DataSource.MEMORY, success.dataSource)+ assertEquals(false, success.isFromMemory)+ val alias: SuccessResult = success+ assertEquals(bmp, alias.bitmap)+ assertEquals(ImageResult.DataSource.UNKNOWN, ImageResult.DataSource.valueOf("UNKNOWN"))+ }+}
library/image/src/test/java/com/vayunmathur/library/image/decoders/SvgDeepTest.kt
diff --git a/library/image/src/test/java/com/vayunmathur/library/image/decoders/SvgDeepTest.kt b/library/image/src/test/java/com/vayunmathur/library/image/decoders/SvgDeepTest.ktnew file mode 100644index 0000000000..b486ace608--- /dev/null+++ b/library/image/src/test/java/com/vayunmathur/library/image/decoders/SvgDeepTest.kt@@ -0,0 +1,210 @@+package com.vayunmathur.library.image.decoders++import android.graphics.Color+import android.graphics.Matrix+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.library.image.ImageRequest+import com.vayunmathur.library.image.Size+import kotlinx.coroutines.runBlocking+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertNotNull+import kotlin.test.assertNull+import kotlin.test.assertTrue++/**+ * Deep SVG coverage: transform matrices, style parsing, element families,+ * nested viewports and the full named-colour table. Matrix assertions use+ * Robolectric's real Matrix arithmetic; render assertions check bitmap+ * geometry (sizes), which is what callers depend on.+ */+@RunWith(RobolectricTestRunner::class)+class SvgDeepTest {++ private val ctx get() = ApplicationProvider.getApplicationContext<android.content.Context>()++ private fun svg(body: String, attrs: String = """width="100" height="80" viewBox="0 0 100 80"""") =+ """<svg xmlns="http://www.w3.org/2000/svg" $attrs>$body</svg>""".toByteArray()++ private fun decode(body: String, attrs: String = """width="100" height="80" viewBox="0 0 100 80"""") =+ runBlocking {+ SvgDecoder.decode(svg(body, attrs), ImageRequest.Builder(ctx).size(Size(60, 48)).build())+ }++ // ---- transforms ----++ @Test fun transformParsingBuildsRealMatrices() {+ val translate = parseTransform("translate(10 20)")+ assertNotNull(translate)+ val v = FloatArray(9)+ translate.getValues(v)+ assertEquals(10f, v[Matrix.MTRANS_X]); assertEquals(20f, v[Matrix.MTRANS_Y])++ val scale = parseTransform("scale(2)")+ assertNotNull(scale)+ scale.getValues(v)+ assertEquals(2f, v[Matrix.MSCALE_X]); assertEquals(2f, v[Matrix.MSCALE_Y])++ val scaleXY = parseTransform("scale(2 3)")+ assertNotNull(scaleXY)+ scaleXY.getValues(v)+ assertEquals(2f, v[Matrix.MSCALE_X]); assertEquals(3f, v[Matrix.MSCALE_Y])++ val matrix = parseTransform("matrix(1 0 0 1 5 6)")+ assertNotNull(matrix)+ matrix.getValues(v)+ assertEquals(5f, v[Matrix.MTRANS_X]); assertEquals(6f, v[Matrix.MTRANS_Y])++ val rotate = parseTransform("rotate(90)")+ assertNotNull(rotate)+ val skewX = parseTransform("skewX(45)")+ assertNotNull(skewX)+ skewX.getValues(v)+ assertEquals(1f, v[Matrix.MSKEW_X], 0.01f)+ val skewY = parseTransform("skewY(45)")+ assertNotNull(skewY)++ val combined = parseTransform("translate(5 5) scale(2)")+ assertNotNull(combined)+ combined.getValues(v)+ assertEquals(2f, v[Matrix.MSCALE_X])++ assertNull(parseTransform(null))+ assertNull(parseTransform(""))+ assertNull(parseTransform("bogus(1 2)"))+ assertNull(parseTransform("matrix(1 2)"))+ assertFalse(applyTransformOperation(Matrix(), "nope", listOf(1f)))+ assertTrue(applyTransformOperation(Matrix(), "rotate", listOf(30f, 10f, 10f)))+ assertFalse(applyTranslateTransform(Matrix(), emptyList()))+ assertFalse(applyScaleTransform(Matrix(), emptyList()))+ assertFalse(applySkewTransform(Matrix(), emptyList(), horizontal = true))+ }++ @Test fun styleAttributeParsing() {+ val styles = parseStyleAttribute("fill:#ff0000; stroke: blue ; opacity:0.5")+ assertEquals("#ff0000", styles["fill"])+ assertEquals("blue", styles["stroke"])+ assertEquals("0.5", styles["opacity"])+ assertEquals(emptyMap(), parseStyleAttribute(""))+ assertNull(parseStyleDeclaration(""))+ assertNull(parseStyleDeclaration("fill"))+ assertNull(parseStyleDeclaration(":red"))+ assertNull(parseStyleDeclaration("fill:"))+ assertEquals("fill" to "#fff", parseStyleDeclaration(" FILL : #fff "))+ assertEquals(listOf(1f, 2.5f, -3f), parseNumbersList("1, 2.5 -3"))+ assertEquals(emptyList(), parseNumbersList(" "))+ }++ // ---- element families through the real renderer ----++ @Test fun allShapeFamiliesRender() {+ val bmp = decode(+ """+ <ellipse cx="30" cy="24" rx="20" ry="10" fill="lightblue"/>+ <line x1="0" y1="0" x2="60" y2="48" stroke="darkgreen" stroke-width="2"/>+ <polyline points="0,48 30,10 60,48" fill="none" stroke="purple"/>+ <polygon points="10,10 50,10 30,40" fill="orange"/>+ <rect x="5" y="5" width="20" height="12" rx="4" ry="4" fill="teal"/>+ <circle cx="30" cy="24" r="6" fill="magenta"/>+ """.trimIndent(),+ )+ assertNotNull(bmp)+ assertEquals(60, bmp.width); assertEquals(48, bmp.height)+ }++ @Test fun transformsAndStylesRender() {+ val bmp = decode(+ """+ <g transform="translate(10 5) scale(1.5)" style="opacity:0.9">+ <rect x="0" y="0" width="20" height="10" fill="red" transform="rotate(15 10 5)"/>+ </g>+ <g transform="skewX(10)"><rect x="0" y="20" width="10" height="5" style="fill:#00ff00;stroke:none"/></g>+ <rect x="40" y="30" width="10" height="10" fill="gold" transform="matrix(1 0 0 1 2 3)"/>+ """.trimIndent(),+ )+ assertNotNull(bmp)+ }++ @Test fun nestedSvgViewportsRender() {+ val bmp = decode(+ """+ <svg x="5" y="5" width="40" height="30" viewBox="0 0 40 30">+ <rect x="0" y="0" width="40" height="30" fill="navy"/>+ </svg>+ <svg x="10" y="10" width="20" height="20">+ <circle cx="10" cy="10" r="5" fill="yellow"/>+ </svg>+ """.trimIndent(),+ )+ assertNotNull(bmp)+ }++ @Test fun hiddenAndCollapsedContentRendersBlankButValid() {+ val bmp = decode(+ """+ <rect x="0" y="0" width="10" height="10" fill="red" display="none"/>+ <g visibility="hidden"><rect x="0" y="0" width="10" height="10" fill="red"/></g>+ <rect x="20" y="20" width="5" height="5" fill="blue"/>+ """.trimIndent(),+ )+ assertNotNull(bmp)+ assertEquals(60, bmp.width)+ }++ @Test fun namedColorsCoverTheTable() {+ // A spread across the CSS named-colour table, including greys with+ // both spellings and the aqua/fuchsia aliases.+ for ((name, expected) in mapOf(+ "black" to Color.BLACK, "white" to Color.WHITE, "red" to Color.RED,+ "lime" to Color.GREEN, "green" to Color.GREEN, "blue" to Color.BLUE,+ "yellow" to Color.YELLOW, "aqua" to Color.CYAN, "cyan" to Color.CYAN,+ "fuchsia" to Color.MAGENTA, "magenta" to Color.MAGENTA,+ "gray" to Color.GRAY, "grey" to Color.GRAY,+ )) {+ assertEquals(expected, parseColorString(name), "named colour $name")+ }+ // Beyond the built-in table, Android's Color.parseColor name map is+ // the fallback (its darkgray is the darker Android grey, not CSS).+ assertEquals(Color.DKGRAY, parseColorString("darkgray"))+ assertEquals(Color.LTGRAY, parseColorString("lightgray"))+ assertEquals(Color.RED, parseColorString("RED"))+ assertEquals(0x7FFF0000, parseColorString("rgba(255,0,0,0.5)"))+ assertNull(parseColorString("#f00")) // 3-digit shorthand is not supported+ assertEquals(0xFFFF0000.toInt(), parseColorString("#ffff0000"))+ }++ @Test fun documentSizeFallsBackThroughAttributes() {+ // No explicit request size: the decoder's 512 default square, as the+ // existing suite pins. An explicit size is honoured exactly.+ val plain = runBlocking {+ SvgDecoder.decode(+ svg("""<rect width="10" height="10" fill="red"/>""", ""),+ ImageRequest.Builder(ctx).build(),+ )+ }+ assertNotNull(plain)+ assertEquals(512, plain.width)+ val sized = runBlocking {+ SvgDecoder.decode(+ svg("""<rect width="10" height="10" fill="red"/>""", """width="200" viewBox="0 0 100 50""""),+ ImageRequest.Builder(ctx).size(Size(200, 100)).build(),+ )+ }+ assertNotNull(sized)+ assertEquals(200, sized.width)+ assertEquals(100, sized.height)+ // fillMissingDimensions: missing height comes from the viewBox.+ val root = fillMissingDimensions(200f, 0f, ViewBox(0f, 0f, 100f, 50f))+ assertEquals(200f, root.docWidth)+ assertEquals(50f, root.docHeight)+ val both = fillMissingDimensions(0f, 0f, ViewBox(0f, 0f, 100f, 50f))+ assertEquals(100f, both.docWidth)+ assertEquals(50f, both.docHeight)+ val none = fillMissingDimensions(10f, 20f, null)+ assertEquals(10f, none.docWidth)+ }++}