Skip to content

Commit

Permalink
Add tolerance to Picasso tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbanes committed Mar 12, 2021
1 parent ff89179 commit c11e0e9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
4 changes: 2 additions & 2 deletions imageloading-testutils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ dependencies {

implementation Libs.AndroidX.coreKtx

implementation Libs.AndroidX.Compose.runtime
implementation Libs.AndroidX.Compose.foundation
implementation Libs.AndroidX.Compose.ui

implementation Libs.Kotlin.stdlib
Expand All @@ -82,6 +80,8 @@ dependencies {
api Libs.OkHttp.mockWebServer
implementation Libs.OkHttp.okhttp

implementation Libs.truth

implementation Libs.Coroutines.test

implementation Libs.AndroidX.Compose.test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ package dev.chrisbanes.accompanist.imageloading.test

import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.graphics.toPixelMap
import org.junit.Assert.assertEquals
import com.google.common.truth.Truth.assertThat

/**
* Assert that all of the pixels in this image as of the [expected] color.
*/
fun ImageBitmap.assertPixels(expected: Color) {
val expectedArgb = expected.toArgb()
fun ImageBitmap.assertPixels(expected: Color, tolerance: Float = 0.0001f) {
toPixelMap().buffer.forEach { pixel ->
assertEquals(expectedArgb, pixel)
val color = Color(pixel)
assertThat(color.red).isWithin(tolerance).of(expected.red)
assertThat(color.green).isWithin(tolerance).of(expected.green)
assertThat(color.blue).isWithin(tolerance).of(expected.blue)
assertThat(color.alpha).isWithin(tolerance).of(expected.alpha)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import java.util.concurrent.TimeUnit

/**
* This 4% tolerance is to work around scaling issues, which causes very slight color differences
* when using Picasso.
*/
private const val PixelTolerance = 0.04f

@LargeTest
@RunWith(JUnit4::class)
class PicassoTest {
Expand Down Expand Up @@ -129,7 +135,7 @@ class PicassoTest {
.assertHeightIsEqualTo(128.dp)
.assertIsDisplayed()
.captureToImage()
.assertPixels(Color.Red)
.assertPixels(Color.Red, PixelTolerance)
}

@OptIn(ExperimentalCoroutinesApi::class)
Expand Down Expand Up @@ -158,7 +164,7 @@ class PicassoTest {
.assertHeightIsEqualTo(128.dp)
.assertIsDisplayed()
.captureToImage()
.assertPixels(Color.Red)
.assertPixels(Color.Red, PixelTolerance)

// Now switch the data URI to the blue drawable
data = server.url("/blue")
Expand All @@ -172,7 +178,7 @@ class PicassoTest {
.assertHeightIsEqualTo(128.dp)
.assertIsDisplayed()
.captureToImage()
.assertPixels(Color.Blue)
.assertPixels(Color.Blue, PixelTolerance)
}

@OptIn(ExperimentalCoroutinesApi::class)
Expand Down Expand Up @@ -256,7 +262,7 @@ class PicassoTest {
composeTestRule.onNodeWithTag(TestTags.Image)
.assertIsDisplayed()
.captureToImage()
.assertPixels(Color.Red)
.assertPixels(Color.Red, PixelTolerance)
}

@SdkSuppress(minSdkVersion = 26) // captureToImage
Expand Down Expand Up @@ -292,7 +298,7 @@ class PicassoTest {
composeTestRule.onNodeWithTag(TestTags.Image)
.assertIsDisplayed()
.captureToImage()
.assertPixels(Color.Red)
.assertPixels(Color.Red, PixelTolerance)
}

@Test
Expand Down Expand Up @@ -407,7 +413,7 @@ class PicassoTest {
composeTestRule.onNodeWithTag(TestTags.Image)
.assertIsDisplayed()
.captureToImage()
.assertPixels(Color.Cyan)
.assertPixels(Color.Cyan, PixelTolerance)
}

@Test
Expand Down Expand Up @@ -480,7 +486,7 @@ class PicassoTest {
composeTestRule.onNodeWithTag(TestTags.Image)
.assertIsDisplayed()
.captureToImage()
.assertPixels(Color.Red)
.assertPixels(Color.Red, PixelTolerance)
}

@Test(expected = IllegalArgumentException::class)
Expand Down

0 comments on commit c11e0e9

Please sign in to comment.