Skip to content

Commit

Permalink
Fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrtwhite committed Dec 8, 2023
1 parent 5570933 commit 914a618
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
import androidx.core.graphics.drawable.toBitmap
import coil3.BitmapImage
import coil3.EventListener
import coil3.ImageLoader
import coil3.compose.AsyncImagePainter.State
import coil3.compose.core.test.R
import coil3.drawable
import coil3.networkObserverEnabled
import coil3.request.CachePolicy
import coil3.request.ErrorResult
Expand Down Expand Up @@ -697,7 +696,7 @@ class AsyncImagePainterTest {
}

private fun assertLoadedBitmapSize(width: Dp, height: Dp, requestNumber: Int = 0) {
val bitmap = (requestTracker.results[requestNumber] as SuccessResult).image.drawable.toBitmap()
val bitmap = ((requestTracker.results[requestNumber] as SuccessResult).image as BitmapImage).bitmap
assertEquals(bitmap.width, width.toPx())
assertEquals(bitmap.height, height.toPx())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@ import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
import androidx.core.graphics.drawable.toBitmap
import androidx.core.graphics.drawable.toDrawable
import coil3.BitmapImage
import coil3.ImageLoader
import coil3.asCoilImage
import coil3.compose.AsyncImagePainter.State
import coil3.compose.core.test.R
import coil3.decode.DecodeUtils
import coil3.drawable
import coil3.fetch.FetchResult
import coil3.fetch.Fetcher
import coil3.memory.MemoryCache
Expand Down Expand Up @@ -722,7 +721,7 @@ class AsyncImageTest {
private fun assertLoadedBitmapSize(width: Int, height: Int, requestNumber: Int = 0) {
val result = requestTracker.results[requestNumber]
assertIs<SuccessResult>(result)
val bitmap = result.image.drawable.toBitmap()
val bitmap = (result.image as BitmapImage).bitmap
assertContains((width - 1)..(width + 1), bitmap.width)
assertContains((height - 1)..(height + 1), bitmap.height)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.os.Build.VERSION.SDK_INT
import coil3.BitmapImage
import coil3.Extras
import coil3.ImageLoader
import coil3.drawable
import coil3.fetch.SourceFetchResult
import coil3.request.Options
import coil3.request.allowRgb565
Expand Down Expand Up @@ -45,7 +45,7 @@ class BitmapFactoryDecoderTest {
)

assertTrue(result.isSampled)
val drawable = assertIs<BitmapDrawable>(result.image.drawable)
val drawable = assertIs<BitmapDrawable>(result.image.asDrawable(context.resources))
assertEquals(Size(100, 125), drawable.bitmap.size)
assertEquals(Bitmap.Config.ARGB_8888, drawable.bitmap.config)
}
Expand All @@ -59,7 +59,7 @@ class BitmapFactoryDecoderTest {
)

assertTrue(result.isSampled)
val drawable = assertIs<BitmapDrawable>(result.image.drawable)
val drawable = assertIs<BitmapDrawable>(result.image.asDrawable(context.resources))
assertEquals(Size(80, 100), drawable.bitmap.size)
assertEquals(Bitmap.Config.ARGB_8888, drawable.bitmap.config)
}
Expand All @@ -73,7 +73,7 @@ class BitmapFactoryDecoderTest {
)

assertTrue(result.isSampled)
val drawable = assertIs<BitmapDrawable>(result.image.drawable)
val drawable = assertIs<BitmapDrawable>(result.image.asDrawable(context.resources))
assertEquals(Size(100, 125), drawable.bitmap.size)
assertEquals(Bitmap.Config.ARGB_8888, drawable.bitmap.config)
}
Expand All @@ -96,7 +96,7 @@ class BitmapFactoryDecoderTest {
)

assertTrue(result.isSampled)
val drawable = assertIs<BitmapDrawable>(result.image.drawable)
val drawable = assertIs<BitmapDrawable>(result.image.asDrawable(context.resources))
assertEquals(Size(600, 750), drawable.bitmap.size)
}

Expand Down Expand Up @@ -314,7 +314,7 @@ class BitmapFactoryDecoderTest {
val result = decode("16_bit.png", Size(250, 250))

assertTrue(result.isSampled)
val drawable = assertIs<BitmapDrawable>(result.image.drawable)
val drawable = assertIs<BitmapDrawable>(result.image.asDrawable(context.resources))
assertEquals(Size(250, 250), drawable.bitmap.size)

val expectedConfig = if (SDK_INT >= 26) Bitmap.Config.RGBA_F16 else Bitmap.Config.ARGB_8888
Expand Down Expand Up @@ -354,13 +354,13 @@ class BitmapFactoryDecoderTest {
size: Size,
scale: Scale = Scale.FILL,
factory: BitmapFactoryDecoder.Factory = decoderFactory,
): Bitmap = assertIs<BitmapDrawable>(decode(assetName, size, scale, factory).image.drawable).bitmap
): Bitmap = assertIs<BitmapImage>(decode(assetName, size, scale, factory).image).bitmap

private suspend fun decodeBitmap(
assetName: String,
options: Options,
factory: BitmapFactoryDecoder.Factory = decoderFactory,
): Bitmap = assertIs<BitmapDrawable>(decode(assetName, options, factory).image.drawable).bitmap
): Bitmap = assertIs<BitmapImage>(decode(assetName, options, factory).image).bitmap

private suspend fun decode(
assetName: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package coil3.fetch

import android.content.ContentResolver.SCHEME_ANDROID_RESOURCE
import android.graphics.drawable.BitmapDrawable
import android.os.Build.VERSION.SDK_INT
import androidx.core.graphics.drawable.toBitmap
import coil3.BitmapImage
import coil3.ImageLoader
import coil3.core.test.R
import coil3.drawable
import coil3.map.ResourceIntMapper
import coil3.map.ResourceUriMapper
import coil3.request.Options
Expand Down Expand Up @@ -49,7 +48,7 @@ class ResourceUriFetcherTest {
val result = fetcherFactory.create(uri, options, ImageLoader(context))?.fetch()

assertIs<ImageFetchResult>(result)
assertIs<BitmapDrawable>(result.image.drawable)
assertIs<BitmapImage>(result.image)
assertTrue(result.isSampled)
}

Expand Down Expand Up @@ -79,7 +78,7 @@ class ResourceUriFetcherTest {
val result = fetcherFactory.create(uri, options, ImageLoader(context))?.fetch()

assertIs<ImageFetchResult>(result)
assertIs<BitmapDrawable>(result.image.drawable)
assertIs<BitmapImage>(result.image)
assertTrue(result.isSampled)
}

Expand All @@ -94,7 +93,7 @@ class ResourceUriFetcherTest {
fetcherFactory.create(uri, options, imageLoader)?.fetch()
}
val expected = activity.getDrawableCompat(R.drawable.ic_tinted_vector).toBitmap()
val actual = (result as ImageFetchResult).image.drawable.toBitmap()
val actual = ((result as ImageFetchResult).image as BitmapImage).bitmap
actual.assertIsSimilarTo(expected)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import android.graphics.PorterDuffXfermode
import android.graphics.RectF
import android.graphics.drawable.Animatable
import android.os.Build.VERSION.SDK_INT
import androidx.core.graphics.drawable.toBitmap
import coil3.BitmapImage
import coil3.ImageLoader
import coil3.decode.GifDecoder
import coil3.decode.ImageDecoderDecoder
import coil3.drawable
import coil3.request.CachePolicy
import coil3.request.ImageRequest
import coil3.request.SuccessResult
Expand Down Expand Up @@ -69,7 +68,7 @@ class AnimatedAndNormalTransformationTest {
assertIs<SuccessResult>(actual)
// Make sure this is still an animated result (has not been flattened to
// apply CircleCropTransformation).
assertIs<Animatable>(actual.image.drawable)
assertIs<Animatable>(actual.image.asDrawable(context.resources))
}

@Test
Expand All @@ -81,8 +80,8 @@ class AnimatedAndNormalTransformationTest {
val actual = imageLoader.execute(imageRequest)
assertIs<SuccessResult>(actual)
// Make sure this is not an animated result.
assertIsNot<Animatable>(actual.image.drawable)
actual.image.drawable.toBitmap().assertIsSimilarTo(expected)
assertIsNot<Animatable>(actual.image.asDrawable(context.resources))
(actual.image as BitmapImage).bitmap.assertIsSimilarTo(expected)
}

class AnimatedCircleTransformation : AnimatedTransformation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package coil3.transform
import android.content.ContentResolver.SCHEME_FILE
import android.graphics.Bitmap
import android.os.Build.VERSION.SDK_INT
import androidx.core.graphics.drawable.toBitmap
import coil3.BitmapImage
import coil3.ImageLoader
import coil3.decode.GifDecoder
import coil3.decode.ImageDecoderDecoder
import coil3.drawable
import coil3.request.ImageRequest
import coil3.request.SuccessResult
import coil3.request.animatedTransformation
Expand Down Expand Up @@ -52,7 +51,7 @@ class AnimatedTransformationTest {
val actual = imageLoader.execute(imageRequest)
val expected = context.decodeBitmapAsset("animated_gif_rounded.png")
assertIs<SuccessResult>(actual)
actual.image.drawable.toBitmap().assertIsSimilarTo(expected, threshold = 0.98)
(actual.image as BitmapImage).bitmap.assertIsSimilarTo(expected, threshold = 0.98)
}

@Test
Expand All @@ -72,7 +71,7 @@ class AnimatedTransformationTest {
val actual = imageLoader.execute(imageRequest)
val expected = context.decodeBitmapAsset("animated_heif_rounded.png")
assertIs<SuccessResult>(actual)
actual.image.drawable.toBitmap().assertIsSimilarTo(expected, threshold = 0.98)
(actual.image as BitmapImage).bitmap.assertIsSimilarTo(expected, threshold = 0.98)
}

@Test
Expand All @@ -89,6 +88,6 @@ class AnimatedTransformationTest {
val actual = imageLoader.execute(imageRequest)
val expected = context.decodeBitmapAsset("animated_webp_rounded.png")
assertIs<SuccessResult>(actual)
actual.image.drawable.toBitmap().assertIsSimilarTo(expected, threshold = 0.98)
(actual.image as BitmapImage).bitmap.assertIsSimilarTo(expected, threshold = 0.98)
}
}
15 changes: 7 additions & 8 deletions coil-svg/src/androidTest/java/coil3/decode/SvgDecoderTest.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package coil3.decode

import android.graphics.drawable.BitmapDrawable
import coil3.BitmapImage
import coil3.ImageLoader
import coil3.drawable
import coil3.fetch.SourceFetchResult
import coil3.request.Options
import coil3.size.Dimension
Expand Down Expand Up @@ -101,10 +100,10 @@ class SvgDecoderTest {
)

assertTrue(result.isSampled)
val drawable = assertIs<BitmapDrawable>(result.image.drawable)
val image = assertIs<BitmapImage>(result.image)

val expected = context.decodeBitmapAsset("coil_logo.png")
drawable.bitmap.assertIsSimilarTo(expected)
image.bitmap.assertIsSimilarTo(expected)
}

@Test
Expand All @@ -124,10 +123,10 @@ class SvgDecoderTest {
)

assertTrue(result.isSampled)
val drawable = assertIs<BitmapDrawable>(result.image.drawable)
val image = assertIs<BitmapImage>(result.image)

val expected = context.decodeBitmapAsset("instacart_logo.png")
drawable.bitmap.assertIsSimilarTo(expected)
image.bitmap.assertIsSimilarTo(expected)
}

/** Regression test: https://github.com/coil-kt/coil/issues/1246 */
Expand All @@ -149,10 +148,10 @@ class SvgDecoderTest {
)

assertTrue(result.isSampled)
val drawable = assertIs<BitmapDrawable>(result.image.drawable)
val image = assertIs<BitmapImage>(result.image)

val expected = context.decodeBitmapAsset("coil_logo.png")
drawable.bitmap.assertIsSimilarTo(expected)
image.bitmap.assertIsSimilarTo(expected)
}

private fun BufferedSource.asSourceResult(
Expand Down

0 comments on commit 914a618

Please sign in to comment.