coil-kt / coil

Image loading for Android and Compose Multiplatform.

Home Page:https://coil-kt.github.io/coil/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support a FakeImageLoader for typical Preview use cases

yschimke opened this issue · comments

The https://coil-kt.github.io/coil/image_loaders/#testing docs suggest implementing a custom ImageLoader.

But there could be some useful defaults, like loading DrawableRes, or mapping a url to a test image.

https://github.com/google/horologist/blob/fdc4fa6b4bb4387c2ec7804a9c5eb9c67f909764/tiles/src/androidTest/java/com/google/android/horologist/tiles/FakeImageLoader.kt

It would be nice to have a Test or Fake ImageLoader that implemented sensible defaults for situations like Previews.

Would this be accepted as a PR?

I agree, having a coil-fakeimageloader artifact (or similar) would be useful. I've been a bit cautious to add a 1st party artifact for it since it would require ongoing support vs. providing a recipe that can be copied + modified.

I'm going on holiday until the end of July so let's circle back on this then!

@yschimke Took an initial stab at this here. It's not finished, but let me know if you have any feedback!

I've ended up with this for testing smaller components that use Coil but won't explicitly see an Image Loader. Is this in scope?

    public inline fun ImageLoader.override(function: () -> Unit) {
        Coil.setImageLoader(this)
        try {
            function()
        } finally {
            Coil::class.java.getDeclaredField("imageLoader").apply {
                isAccessible = true
            }.set(null, null)
        }
    }

Or some other way to achieve this?