diff --git a/library/src/main/java/com/schibsted/spain/barista/internal/matcher/DrawableMatcher.kt b/library/src/main/java/com/schibsted/spain/barista/internal/matcher/DrawableMatcher.kt index 91b0d7bfe..043c2dab5 100644 --- a/library/src/main/java/com/schibsted/spain/barista/internal/matcher/DrawableMatcher.kt +++ b/library/src/main/java/com/schibsted/spain/barista/internal/matcher/DrawableMatcher.kt @@ -1,8 +1,13 @@ package com.schibsted.spain.barista.internal.matcher -import androidx.annotation.DrawableRes +import android.graphics.Bitmap +import android.graphics.Color +import android.graphics.drawable.Drawable import android.view.View import android.widget.ImageView +import androidx.annotation.DrawableRes +import androidx.core.graphics.drawable.DrawableCompat +import com.google.android.material.button.MaterialButton import com.schibsted.spain.barista.internal.util.BitmapComparator import com.schibsted.spain.barista.internal.util.DrawableToBitmapConverter import org.hamcrest.Description @@ -31,30 +36,26 @@ class DrawableMatcher private constructor(@DrawableRes private val expectedDrawa private var resourceName: String? = null override fun matchesSafely(target: View): Boolean { - if (target !is ImageView) { + val context = target.context + if (target !is ImageView && target !is MaterialButton) { return false } - val imageView = target + val drawable = target.getTargetDrawable() if (expectedDrawableRes == EMPTY) { - return imageView.drawable == null + return drawable == null } if (expectedDrawableRes == ANY) { - return imageView.drawable != null + return drawable != null } - if (imageView.drawable == null) { + if (drawable == null) { return false } - val resources = target.context.resources - val expectedDrawable = resources.getDrawable(expectedDrawableRes) - resourceName = resources.getResourceEntryName(expectedDrawableRes) + val resources = context.resources - if (expectedDrawable == null) { - return false - } + resourceName = resources.getResourceEntryName(expectedDrawableRes) - val viewBitmap = DrawableToBitmapConverter.getBitmap(imageView.drawable) - val expectedBitmap = DrawableToBitmapConverter.getBitmap(expectedDrawable) - return BitmapComparator.compare(viewBitmap, expectedBitmap) + val viewBitmap = DrawableToBitmapConverter.getBitmap(drawable) + return target.getExpectedBitmap()?.let { BitmapComparator.compare(viewBitmap, it) } ?: false } override fun describeTo(description: Description) { @@ -66,4 +67,28 @@ class DrawableMatcher private constructor(@DrawableRes private val expectedDrawa description.appendText("]") } } -} \ No newline at end of file + + private fun View.getTargetDrawable(): Drawable? { + return when (this) { + is MaterialButton -> this.icon + is ImageView -> this.drawable + else -> error("View not supported: $this") + } + } + + private fun View.getExpectedBitmap(): Bitmap? { + return resources.getDrawable(expectedDrawableRes)?.let { drawable -> + when (this) { + is MaterialButton -> DrawableToBitmapConverter.getBitmap(setTargetDrawableTint(drawable)) + is ImageView -> DrawableToBitmapConverter.getBitmap(drawable) + else -> error("") + } + } + } + + private fun MaterialButton.setTargetDrawableTint(drawable: Drawable): Drawable { + DrawableCompat.setTint(drawable, this.iconTint.getColorForState(icon.state, Color.BLACK)) + if (iconTintMode != null) DrawableCompat.setTintMode(drawable, iconTintMode) + return drawable + } +} diff --git a/sample/src/androidTest/java/com/schibsted/spain/barista/sample/AssertionsTest.java b/sample/src/androidTest/java/com/schibsted/spain/barista/sample/AssertionsTest.java index 40ba9db48..5c201cbd7 100644 --- a/sample/src/androidTest/java/com/schibsted/spain/barista/sample/AssertionsTest.java +++ b/sample/src/androidTest/java/com/schibsted/spain/barista/sample/AssertionsTest.java @@ -259,6 +259,11 @@ public void checkDrawable_withId() throws Exception { assertHasDrawable(R.id.image_view, R.drawable.ic_barista); } + @Test + public void checkMaterialButtonDrawable_withId() throws Exception { + assertHasDrawable(R.id.material_button_view, R.drawable.ic_barista); + } + @Test public void checkVectorDrawable_withId() throws Exception { assertHasDrawable(R.id.vector_image_view, R.drawable.barista_logo_vector); diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index b41db86f1..ab99bf0f5 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -2,6 +2,7 @@ @@ -155,4 +156,11 @@ android:layout_width="48dp" android:layout_height="48dp" /> + +