Skip to content

Commit

Permalink
Tech: Add material button drawable matcher (#383)
Browse files Browse the repository at this point in the history
Co-authored-by: Estibaliz Torralbo <[email protected]>
  • Loading branch information
etorralbo and Estibaliz Torralbo authored Mar 2, 2021
1 parent 87cd5a7 commit 84f4555
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -66,4 +67,28 @@ class DrawableMatcher private constructor(@DrawableRes private val expectedDrawa
description.appendText("]")
}
}
}

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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
>

Expand Down Expand Up @@ -155,4 +156,11 @@
android:layout_width="48dp"
android:layout_height="48dp"
/>

<com.google.android.material.button.MaterialButton
android:id="@+id/material_button_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:icon="@drawable/ic_barista"
/>
</LinearLayout>

0 comments on commit 84f4555

Please sign in to comment.