Skip to content

Commit

Permalink
Replace FakeImage with ColorImage.
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrtwhite committed Nov 19, 2024
1 parent cbb4b54 commit 379b869
Show file tree
Hide file tree
Showing 15 changed files with 238 additions and 32 deletions.
25 changes: 25 additions & 0 deletions coil-test/api/android/coil-test.api
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
public final class coil3/test/ColorImage : coil3/Image {
public static final field Black I
public static final field Blue I
public static final field Companion Lcoil3/test/ColorImage$Companion;
public static final field Green I
public static final field Red I
public static final field Transparent I
public static final field White I
public fun <init> ()V
public fun <init> (IIIJZ)V
public synthetic fun <init> (IIIJZILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun draw (Landroid/graphics/Canvas;)V
public fun equals (Ljava/lang/Object;)Z
public final fun getColor ()I
public fun getHeight ()I
public fun getShareable ()Z
public fun getSize ()J
public fun getWidth ()I
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class coil3/test/ColorImage$Companion {
}

public final class coil3/test/FakeImage : coil3/Image {
public fun <init> ()V
public fun <init> (IIJZI)V
Expand Down
35 changes: 35 additions & 0 deletions coil-test/api/coil-test.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,41 @@
// - Show declarations: true

// Library unique name: <io.coil-kt.coil3:coil-test>
final class coil3.test/ColorImage : coil3/Image { // coil3.test/ColorImage|null[0]
constructor <init>(kotlin/Int = ..., kotlin/Int = ..., kotlin/Int = ..., kotlin/Long = ..., kotlin/Boolean = ...) // coil3.test/ColorImage.<init>|<init>(kotlin.Int;kotlin.Int;kotlin.Int;kotlin.Long;kotlin.Boolean){}[0]

final val color // coil3.test/ColorImage.color|{}color[0]
final fun <get-color>(): kotlin/Int // coil3.test/ColorImage.color.<get-color>|<get-color>(){}[0]
final val height // coil3.test/ColorImage.height|{}height[0]
final fun <get-height>(): kotlin/Int // coil3.test/ColorImage.height.<get-height>|<get-height>(){}[0]
final val shareable // coil3.test/ColorImage.shareable|{}shareable[0]
final fun <get-shareable>(): kotlin/Boolean // coil3.test/ColorImage.shareable.<get-shareable>|<get-shareable>(){}[0]
final val size // coil3.test/ColorImage.size|{}size[0]
final fun <get-size>(): kotlin/Long // coil3.test/ColorImage.size.<get-size>|<get-size>(){}[0]
final val width // coil3.test/ColorImage.width|{}width[0]
final fun <get-width>(): kotlin/Int // coil3.test/ColorImage.width.<get-width>|<get-width>(){}[0]

final fun draw(org.jetbrains.skia/Canvas) // coil3.test/ColorImage.draw|draw(org.jetbrains.skia.Canvas){}[0]
final fun equals(kotlin/Any?): kotlin/Boolean // coil3.test/ColorImage.equals|equals(kotlin.Any?){}[0]
final fun hashCode(): kotlin/Int // coil3.test/ColorImage.hashCode|hashCode(){}[0]
final fun toString(): kotlin/String // coil3.test/ColorImage.toString|toString(){}[0]

final object Companion { // coil3.test/ColorImage.Companion|null[0]
final const val Black // coil3.test/ColorImage.Companion.Black|{}Black[0]
final fun <get-Black>(): kotlin/Int // coil3.test/ColorImage.Companion.Black.<get-Black>|<get-Black>(){}[0]
final const val Blue // coil3.test/ColorImage.Companion.Blue|{}Blue[0]
final fun <get-Blue>(): kotlin/Int // coil3.test/ColorImage.Companion.Blue.<get-Blue>|<get-Blue>(){}[0]
final const val Green // coil3.test/ColorImage.Companion.Green|{}Green[0]
final fun <get-Green>(): kotlin/Int // coil3.test/ColorImage.Companion.Green.<get-Green>|<get-Green>(){}[0]
final const val Red // coil3.test/ColorImage.Companion.Red|{}Red[0]
final fun <get-Red>(): kotlin/Int // coil3.test/ColorImage.Companion.Red.<get-Red>|<get-Red>(){}[0]
final const val Transparent // coil3.test/ColorImage.Companion.Transparent|{}Transparent[0]
final fun <get-Transparent>(): kotlin/Int // coil3.test/ColorImage.Companion.Transparent.<get-Transparent>|<get-Transparent>(){}[0]
final const val White // coil3.test/ColorImage.Companion.White|{}White[0]
final fun <get-White>(): kotlin/Int // coil3.test/ColorImage.Companion.White.<get-White>|<get-White>(){}[0]
}
}

final class coil3.test/FakeImage : coil3/Image { // coil3.test/FakeImage|null[0]
constructor <init>(kotlin/Int = ..., kotlin/Int = ..., kotlin/Long = ..., kotlin/Boolean = ..., kotlin/Int = ...) // coil3.test/FakeImage.<init>|<init>(kotlin.Int;kotlin.Int;kotlin.Long;kotlin.Boolean;kotlin.Int){}[0]

Expand Down
25 changes: 25 additions & 0 deletions coil-test/api/jvm/coil-test.api
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
public final class coil3/test/ColorImage : coil3/Image {
public static final field Black I
public static final field Blue I
public static final field Companion Lcoil3/test/ColorImage$Companion;
public static final field Green I
public static final field Red I
public static final field Transparent I
public static final field White I
public fun <init> ()V
public fun <init> (IIIJZ)V
public synthetic fun <init> (IIIJZILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun draw (Lorg/jetbrains/skia/Canvas;)V
public fun equals (Ljava/lang/Object;)Z
public final fun getColor ()I
public fun getHeight ()I
public fun getShareable ()Z
public fun getSize ()J
public fun getWidth ()I
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class coil3/test/ColorImage$Companion {
}

public final class coil3/test/FakeImage : coil3/Image {
public fun <init> ()V
public fun <init> (IIJZI)V
Expand Down
39 changes: 39 additions & 0 deletions coil-test/src/androidMain/kotlin/coil3/test/ColorImage.android.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package coil3.test

import android.graphics.Paint
import coil3.Canvas
import coil3.Image
import coil3.annotation.Poko

@Poko
actual class ColorImage actual constructor(
actual val color: Int,
actual override val width: Int,
actual override val height: Int,
actual override val size: Long,
actual override val shareable: Boolean,
) : Image {
private var lazyPaint: Paint? = null

actual override fun draw(canvas: Canvas) {
val paint = lazyPaint ?: run {
Paint()
.apply { color = this@ColorImage.color }
.also { lazyPaint = it }
}
if (width >= 0 && height >= 0) {
canvas.drawRect(0f, 0f, width.toFloat(), height.toFloat(), paint)
} else {
canvas.drawPaint(paint)
}
}

actual companion object {
actual const val Black = 0xFF000000.toInt()
actual const val White = 0xFFFFFFFF.toInt()
actual const val Transparent = 0x00000000.toInt()
actual const val Red = 0xFFFF0000.toInt()
actual const val Green = 0xFF00FF00.toInt()
actual const val Blue = 0xFF0000FF.toInt()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import coil3.Canvas
import coil3.Image
import coil3.annotation.Poko

@Deprecated(
message = "This use case is better fulfilled by ColorImage.",
replaceWith = ReplaceWith("ColorImage", ["coil3.test.ColorImage"]),
level = DeprecationLevel.WARNING,
)
@Poko
actual class FakeImage actual constructor(
actual override val width: Int,
Expand All @@ -21,7 +26,6 @@ actual class FakeImage actual constructor(
.apply { color = this@FakeImage.color }
.also { lazyPaint = it }
}

canvas.drawRect(0f, 0f, width.toFloat(), height.toFloat(), paint)
}
}
34 changes: 34 additions & 0 deletions coil-test/src/commonMain/kotlin/coil3/test/ColorImage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package coil3.test

import coil3.Canvas
import coil3.Image

/**
* An image that draws a [color].
*
* By default the image has no intrinsic size and will fill its canvas. Set [width] and [height]
* to a positive value to draw a square with those dimensions.
*/
expect class ColorImage(
color: Int = Black,
width: Int = -1,
height: Int = -1,
size: Long = 0,
shareable: Boolean = true,
) : Image {
val color: Int
override val size: Long
override val width: Int
override val height: Int
override val shareable: Boolean
override fun draw(canvas: Canvas)

companion object {
val Black: Int
val White: Int
val Transparent: Int
val Red: Int
val Green: Int
val Blue: Int
}
}
8 changes: 6 additions & 2 deletions coil-test/src/commonMain/kotlin/coil3/test/FakeImage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ package coil3.test

import coil3.Canvas
import coil3.Image
import coil3.test.internal.Black
import coil3.test.ColorImage.Companion.Black

/**
* A simple [Image] that draws a 100x100 black square by default.
*/
@Deprecated(
message = "This use case is better fulfilled by ColorImage.",
replaceWith = ReplaceWith("ColorImage(color, width, height, size, shareable)", ["coil3.test.ColorImage"]),
level = DeprecationLevel.WARNING,
)
expect class FakeImage(
width: Int = 100,
height: Int = 100,
Expand All @@ -19,6 +24,5 @@ expect class FakeImage(
override val size: Long
override val shareable: Boolean
val color: Int

override fun draw(canvas: Canvas)
}
3 changes: 0 additions & 3 deletions coil-test/src/commonMain/kotlin/coil3/test/internal/utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,3 @@ internal fun imageResultOf(
request = request,
dataSource = DataSource.MEMORY,
)

// Format: 0xALPHA_RED_GREEN_BLUE
internal const val Black = 0xFF_00_00_00.toInt()
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package coil3.test

import coil3.Canvas
import coil3.Image
import coil3.annotation.Poko
import org.jetbrains.skia.Paint
import org.jetbrains.skia.Rect

@Poko
actual class ColorImage actual constructor(
actual val color: Int,
actual override val width: Int,
actual override val height: Int,
actual override val size: Long,
actual override val shareable: Boolean,
) : 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@ColorImage.color }
.also { lazyPaint = it }
}
if (width >= 0 && height >= 0) {
val rect = lazyRect ?: run {
Rect.makeWH(width.toFloat(), height.toFloat())
.also { lazyRect = it }
}
canvas.drawRect(rect, paint)
} else {
canvas.drawPaint(paint)
}
}

actual companion object {
actual const val Black = 0xFF000000.toInt()
actual const val White = 0xFFFFFFFF.toInt()
actual const val Transparent = 0x00000000.toInt()
actual const val Red = 0xFFFF0000.toInt()
actual const val Green = 0xFF00FF00.toInt()
actual const val Blue = 0xFF0000FF.toInt()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import coil3.annotation.Poko
import org.jetbrains.skia.Paint
import org.jetbrains.skia.Rect

@Deprecated(
message = "This use case is better fulfilled by ColorImage.",
replaceWith = ReplaceWith("ColorImage(color, width, height, size, shareable)", ["coil3.test.ColorImage"]),
level = DeprecationLevel.WARNING,
)
@Poko
actual class FakeImage actual constructor(
actual override val width: Int,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package coil3.composescreenshot

import android.graphics.Color
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
Expand All @@ -17,12 +16,13 @@ import coil3.compose.AsyncImage
import coil3.compose.AsyncImagePreviewHandler
import coil3.compose.LocalAsyncImagePreviewHandler
import coil3.compose.rememberAsyncImagePainter
import coil3.test.FakeImage
import coil3.test.ColorImage
import coil3.test.ColorImage.Companion.Red

@OptIn(ExperimentalCoilApi::class)
class PreviewScreenshots {
private val previewHandler = AsyncImagePreviewHandler {
FakeImage(color = Color.RED)
ColorImage(Red, width = 100, height = 100)
}

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

import android.graphics.Color
import android.widget.ImageView
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.fillMaxSize
Expand All @@ -14,7 +13,8 @@ import coil3.compose.rememberAsyncImagePainter
import coil3.decode.ImageSource
import coil3.request.ImageRequest
import coil3.request.target
import coil3.test.FakeImage
import coil3.test.ColorImage
import coil3.test.ColorImage.Companion.Red
import coil3.test.FakeImageLoaderEngine
import kotlin.test.assertTrue
import okio.Buffer
Expand All @@ -38,7 +38,7 @@ class PaparazziTest {
fun imageView() {
val url = "https://www.example.com/image.jpg"
val engine = FakeImageLoaderEngine.Builder()
.intercept(url, FakeImage(color = Color.RED))
.intercept(url, ColorImage(Red, width = 100, height = 100))
.build()
val imageLoader = ImageLoader.Builder(paparazzi.context)
.components { add(engine) }
Expand All @@ -60,7 +60,7 @@ class PaparazziTest {
fun asyncImage() {
val url = "https://www.example.com/image.jpg"
val engine = FakeImageLoaderEngine.Builder()
.intercept(url, FakeImage(color = Color.RED))
.intercept(url, ColorImage(Red, width = 100, height = 100))
.build()
val imageLoader = ImageLoader.Builder(paparazzi.context)
.components { add(engine) }
Expand All @@ -81,7 +81,7 @@ class PaparazziTest {
fun rememberAsyncImagePainter() {
val url = "https://www.example.com/image.jpg"
val engine = FakeImageLoaderEngine.Builder()
.intercept(url, FakeImage(color = Color.RED))
.intercept(url, ColorImage(Red, width = 100, height = 100))
.build()
val imageLoader = ImageLoader.Builder(paparazzi.context)
.components { add(engine) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package coil3.roborazzi

import android.graphics.Color
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.ui.Modifier
Expand All @@ -10,7 +9,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.ColorImage
import coil3.test.ColorImage.Companion.Red
import coil3.test.FakeImageLoaderEngine
import coil3.test.utils.ComposeTestActivity
import coil3.test.utils.RobolectricTest
Expand Down Expand Up @@ -39,7 +39,7 @@ class RoborazziComposeTestAndroid : RobolectricTest() {
fun asyncImage() {
val url = "https://www.example.com/image.jpg"
val engine = FakeImageLoaderEngine.Builder()
.intercept(url, FakeImage(color = Color.RED))
.intercept(url, ColorImage(Red, width = 100, height = 100))
.build()
val imageLoader = ImageLoader.Builder(composeTestRule.activity)
.components { add(engine) }
Expand All @@ -60,7 +60,7 @@ class RoborazziComposeTestAndroid : RobolectricTest() {
fun rememberAsyncImagePainter() {
val url = "https://www.example.com/image.jpg"
val engine = FakeImageLoaderEngine.Builder()
.intercept(url, FakeImage(color = Color.RED))
.intercept(url, ColorImage(Red, width = 100, height = 100))
.build()
val imageLoader = ImageLoader.Builder(composeTestRule.activity)
.components { add(engine) }
Expand Down
Loading

0 comments on commit 379b869

Please sign in to comment.