Skip to content

Commit

Permalink
Fix FakeImage not rendering on Android.
Browse files Browse the repository at this point in the history
# Conflicts:
#	internal/test-paparazzi/src/test/snapshots/images/coil3.paparazzi_PaparazziTest_imageView.png
  • Loading branch information
colinrtwhite committed Nov 19, 2024
1 parent e488b62 commit 5a57c47
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,7 @@ class ImageRequest private constructor(
extras = extras,
)

@Deprecated(
level = DeprecationLevel.HIDDEN,
message = "Kept for binary compatibility."
)
@Deprecated("Kept for binary compatibility.", level = DeprecationLevel.HIDDEN)
fun copy(
fileSystem: FileSystem = this.fileSystem,
interceptorCoroutineContext: CoroutineContext = this.interceptorCoroutineContext,
Expand Down
11 changes: 10 additions & 1 deletion coil-test/src/androidMain/kotlin/coil3/test/FakeImage.android.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package coil3.test

import android.graphics.Paint
import coil3.Canvas
import coil3.Image
import coil3.annotation.Poko
Expand All @@ -12,7 +13,15 @@ actual class FakeImage actual constructor(
actual override val shareable: Boolean,
actual val color: Int,
) : Image {
private var lazyPaint: Paint? = null

actual override fun draw(canvas: Canvas) {
canvas.drawColor(color)
val paint = lazyPaint ?: run {
Paint()
.apply { color = this@FakeImage.color }
.also { lazyPaint = it }
}

canvas.drawRect(0f, 0f, width.toFloat(), height.toFloat(), paint)
}
}
2 changes: 1 addition & 1 deletion coil-test/src/commonMain/kotlin/coil3/test/FakeImage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ expect class FakeImage(
height: Int = 100,
size: Long = 4L * width * height,
shareable: Boolean = true,
color: Int = 0x000000,
color: Int = 0xFF000000.toInt(),
) : Image {
override val width: Int
override val height: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import coil3.Canvas
import coil3.Image
import coil3.annotation.Poko
import org.jetbrains.skia.Paint
import org.jetbrains.skia.Rect

@Poko
actual class FakeImage actual constructor(
Expand All @@ -14,13 +15,18 @@ actual class FakeImage actual constructor(
actual val color: Int,
) : Image {
private var lazyPaint: Paint? = null
private var lazyRect: Rect? = null

actual override fun draw(canvas: Canvas) {
val paint = lazyPaint ?: run {
Paint()
.apply { color = this@FakeImage.color }
.also { lazyPaint = it }
}
canvas.drawPaint(paint)
val rect = lazyRect ?: run {
Rect.makeWH(width.toFloat(), height.toFloat())
.also { lazyRect = it }
}
canvas.drawRect(rect, paint)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package coil3.composescreenshot

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
Expand All @@ -14,19 +13,16 @@ import androidx.compose.ui.tooling.preview.Devices
import androidx.compose.ui.tooling.preview.Preview
import coil3.ImageLoader
import coil3.annotation.ExperimentalCoilApi
import coil3.asImage
import coil3.compose.AsyncImage
import coil3.compose.AsyncImagePreviewHandler
import coil3.compose.LocalAsyncImagePreviewHandler
import coil3.compose.rememberAsyncImagePainter
import coil3.test.FakeImage

@OptIn(ExperimentalCoilApi::class)
class PreviewScreenshots {
private val previewHandler = AsyncImagePreviewHandler {
object : ColorDrawable(Color.RED) {
override fun getIntrinsicWidth() = 100
override fun getIntrinsicHeight() = 100
}.asImage()
FakeImage(color = Color.RED)
}

@Preview(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package coil3.paparazzi

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.widget.ImageView
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.fillMaxSize
Expand All @@ -15,8 +14,8 @@ import coil3.compose.rememberAsyncImagePainter
import coil3.decode.ImageSource
import coil3.request.ImageRequest
import coil3.request.target
import coil3.test.FakeImage
import coil3.test.FakeImageLoaderEngine
import coil3.test.intercept
import kotlin.test.assertTrue
import okio.Buffer
import okio.FileSystem
Expand All @@ -38,12 +37,8 @@ class PaparazziTest {
@Test
fun imageView() {
val url = "https://www.example.com/image.jpg"
val drawable = object : ColorDrawable(Color.RED) {
override fun getIntrinsicWidth() = 100
override fun getIntrinsicHeight() = 100
}
val engine = FakeImageLoaderEngine.Builder()
.intercept(url, drawable)
.intercept(url, FakeImage(color = Color.RED))
.build()
val imageLoader = ImageLoader.Builder(paparazzi.context)
.components { add(engine) }
Expand All @@ -64,12 +59,8 @@ class PaparazziTest {
@Test
fun asyncImage() {
val url = "https://www.example.com/image.jpg"
val drawable = object : ColorDrawable(Color.RED) {
override fun getIntrinsicWidth() = 100
override fun getIntrinsicHeight() = 100
}
val engine = FakeImageLoaderEngine.Builder()
.intercept(url, drawable)
.intercept(url, FakeImage(color = Color.RED))
.build()
val imageLoader = ImageLoader.Builder(paparazzi.context)
.components { add(engine) }
Expand All @@ -89,12 +80,8 @@ class PaparazziTest {
@Test
fun rememberAsyncImagePainter() {
val url = "https://www.example.com/image.jpg"
val drawable = object : ColorDrawable(Color.RED) {
override fun getIntrinsicWidth() = 100
override fun getIntrinsicHeight() = 100
}
val engine = FakeImageLoaderEngine.Builder()
.intercept(url, drawable)
.intercept(url, FakeImage(color = Color.RED))
.build()
val imageLoader = ImageLoader.Builder(paparazzi.context)
.components { add(engine) }
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package coil3.roborazzi

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.ui.Modifier
Expand All @@ -11,8 +10,8 @@ import androidx.compose.ui.test.onRoot
import coil3.ImageLoader
import coil3.compose.AsyncImage
import coil3.compose.rememberAsyncImagePainter
import coil3.test.FakeImage
import coil3.test.FakeImageLoaderEngine
import coil3.test.intercept
import coil3.test.utils.ComposeTestActivity
import coil3.test.utils.RobolectricTest
import com.github.takahirom.roborazzi.RoborazziRule
Expand All @@ -39,12 +38,8 @@ class RoborazziComposeTestAndroid : RobolectricTest() {
@Test
fun asyncImage() {
val url = "https://www.example.com/image.jpg"
val drawable = object : ColorDrawable(Color.RED) {
override fun getIntrinsicWidth() = 100
override fun getIntrinsicHeight() = 100
}
val engine = FakeImageLoaderEngine.Builder()
.intercept(url, drawable)
.intercept(url, FakeImage(color = Color.RED))
.build()
val imageLoader = ImageLoader.Builder(composeTestRule.activity)
.components { add(engine) }
Expand All @@ -64,12 +59,8 @@ class RoborazziComposeTestAndroid : RobolectricTest() {
@Test
fun rememberAsyncImagePainter() {
val url = "https://www.example.com/image.jpg"
val drawable = object : ColorDrawable(Color.RED) {
override fun getIntrinsicWidth() = 100
override fun getIntrinsicHeight() = 100
}
val engine = FakeImageLoaderEngine.Builder()
.intercept(url, drawable)
.intercept(url, FakeImage(color = Color.RED))
.build()
val imageLoader = ImageLoader.Builder(composeTestRule.activity)
.components { add(engine) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package coil3.roborazzi

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.widget.ImageView
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.matcher.ViewMatchers.isRoot
import androidx.test.ext.junit.rules.activityScenarioRule
import coil3.ImageLoader
import coil3.request.ImageRequest
import coil3.request.target
import coil3.test.FakeImage
import coil3.test.FakeImageLoaderEngine
import coil3.test.intercept
import coil3.test.utils.RobolectricTest
import coil3.test.utils.ViewTestActivity
import coil3.test.utils.activity
Expand All @@ -37,12 +36,8 @@ class RoborazziViewTest : RobolectricTest() {
@Test
fun imageView() {
val url = "https://www.example.com/image.jpg"
val drawable = object : ColorDrawable(Color.RED) {
override fun getIntrinsicWidth() = 100
override fun getIntrinsicHeight() = 100
}
val engine = FakeImageLoaderEngine.Builder()
.intercept(url, drawable)
.intercept(url, FakeImage(color = Color.RED))
.build()
val activity = activityRule.scenario.activity
val imageLoader = ImageLoader.Builder(activity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ class RoborazziComposeTestJvm {
val image = FakeImage(
width = 100,
height = 100,
color = 0xFFFF0000.toInt()
color = org.jetbrains.skia.Color.RED,
)

val engine = FakeImageLoaderEngine.Builder()
.intercept(url, image.toBitmap().asImage())
.intercept(url, image)
.build()

val imageLoader = ImageLoader.Builder(PlatformContext.INSTANCE)
Expand Down Expand Up @@ -63,7 +63,7 @@ class RoborazziComposeTestJvm {
val image = FakeImage(
width = 100,
height = 100,
color = 0xFFFF0000.toInt()
color = org.jetbrains.skia.Color.RED,
)
val engine = FakeImageLoaderEngine.Builder()
.intercept(url, image.toBitmap().asImage())
Expand Down

0 comments on commit 5a57c47

Please sign in to comment.