Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify BitmapComparator #435

Merged
merged 2 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ jobs:
arch: x86
profile: pixel_2
disable-animations: true
script: ./gradlew connectedCheck
script: ./gradlew :sample:connectedCheck
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import androidx.annotation.DrawableRes
import android.view.View
import com.adevinta.android.barista.internal.util.BitmapComparator
import com.adevinta.android.barista.internal.util.DrawableToBitmapConverter
import org.hamcrest.Description
import org.hamcrest.TypeSafeMatcher
Expand Down Expand Up @@ -57,7 +56,7 @@ class BackgroundMatcher private constructor(@DrawableRes private val expectedDra
private fun hasSameBitmap(view: View, expectedDrawable: Drawable?): Boolean {
val viewBitmap = DrawableToBitmapConverter.getBitmap(view.background)
val expectedBitmap = DrawableToBitmapConverter.getBitmap(expectedDrawable)
return BitmapComparator.compare(viewBitmap, expectedBitmap)
return viewBitmap.sameAs(expectedBitmap)
}

private fun isSameColorDrawable(viewDrawable: ColorDrawable, expectedDrawable: ColorDrawable) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import android.widget.ImageView
import androidx.annotation.DrawableRes
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.graphics.drawable.DrawableCompat
import com.adevinta.android.barista.internal.util.BitmapComparator
import com.adevinta.android.barista.internal.util.DrawableToBitmapConverter
import com.google.android.material.button.MaterialButton
import org.hamcrest.Description
Expand Down Expand Up @@ -56,7 +55,7 @@ class DrawableMatcher private constructor(@DrawableRes private val expectedDrawa
resourceName = resources.getResourceEntryName(expectedDrawableRes)

val viewBitmap = DrawableToBitmapConverter.getBitmap(drawable)
return target.getExpectedBitmap()?.let { BitmapComparator.compare(viewBitmap, it) } ?: false
return target.getExpectedBitmap()?.let { viewBitmap.sameAs(it) } ?: false
}

override fun describeTo(description: Description) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.adevinta.android.barista.internal.util
package com.adevinta.android.barista.sample

import android.content.Context
import android.graphics.BitmapFactory
import androidx.test.core.app.ApplicationProvider
import com.adevinta.android.barista.test.R
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Assert
import org.junit.Test

class BitmapComparatorTest {
Expand All @@ -15,18 +13,19 @@ class BitmapComparatorTest {
val aBitmap = BitmapFactory.decodeResource(ApplicationProvider.getApplicationContext<Context>().resources, R.drawable.ic_barista)
val theSameBitmap = BitmapFactory.decodeResource(ApplicationProvider.getApplicationContext<Context>().resources, R.drawable.ic_barista)

val result = BitmapComparator.compare(aBitmap, theSameBitmap)
val result = aBitmap.sameAs(theSameBitmap)

assertTrue(result)
Assert.assertTrue(result)
}

@Test
fun returnFalseWhenComparingDifferentDrawables() {
val aBitmap = BitmapFactory.decodeResource(ApplicationProvider.getApplicationContext<Context>().resources, R.drawable.ic_barista)
val aDifferentBitmap = BitmapFactory.decodeResource(ApplicationProvider.getApplicationContext<Context>().resources, R.drawable.ic_launcher)
val aDifferentBitmap =
BitmapFactory.decodeResource(ApplicationProvider.getApplicationContext<Context>().resources, R.drawable.ic_action_menu)

val result = BitmapComparator.compare(aBitmap, aDifferentBitmap)
val result = aBitmap.sameAs(aDifferentBitmap)

assertFalse(result)
Assert.assertFalse(result)
}
}