|
| 1 | +package com.amaze.filemanager.matcher |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.graphics.Bitmap |
| 5 | +import android.graphics.Canvas |
| 6 | +import android.graphics.drawable.Drawable |
| 7 | +import android.graphics.drawable.StateListDrawable |
| 8 | +import android.view.View |
| 9 | +import androidx.annotation.DrawableRes |
| 10 | +import androidx.appcompat.view.menu.ActionMenuItemView |
| 11 | +import androidx.test.espresso.matcher.BoundedMatcher |
| 12 | +import org.hamcrest.Description |
| 13 | +import org.hamcrest.Matcher |
| 14 | + |
| 15 | +/** |
| 16 | + * This matcher allows to select by [ActionMenuItemView] icon |
| 17 | + * |
| 18 | + * From https://stackoverflow.com/a/70108466/3124150 |
| 19 | + */ |
| 20 | +object ActionMenuIconMatcher { |
| 21 | + @JvmStatic |
| 22 | + fun withActionIconDrawable( |
| 23 | + @DrawableRes resourceId: Int, |
| 24 | + ): Matcher<View?> { |
| 25 | + return object : BoundedMatcher<View?, ActionMenuItemView>(ActionMenuItemView::class.java) { |
| 26 | + override fun describeTo(description: Description) { |
| 27 | + description.appendText("has image drawable resource $resourceId") |
| 28 | + } |
| 29 | + |
| 30 | + override fun matchesSafely(actionMenuItemView: ActionMenuItemView): Boolean { |
| 31 | + return sameBitmap( |
| 32 | + actionMenuItemView.context, |
| 33 | + actionMenuItemView.itemData.icon, |
| 34 | + resourceId, |
| 35 | + actionMenuItemView, |
| 36 | + ) |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @JvmStatic |
| 42 | + private fun sameBitmap( |
| 43 | + context: Context, |
| 44 | + drawable: Drawable?, |
| 45 | + resourceId: Int, |
| 46 | + view: View, |
| 47 | + ): Boolean { |
| 48 | + var drawable = drawable |
| 49 | + val otherDrawable: Drawable? = context.resources.getDrawable(resourceId) |
| 50 | + if (drawable == null || otherDrawable == null) { |
| 51 | + return false |
| 52 | + } |
| 53 | + |
| 54 | + if (drawable is StateListDrawable) { |
| 55 | + val getStateDrawableIndex = |
| 56 | + StateListDrawable::class.java.getMethod( |
| 57 | + "getStateDrawableIndex", |
| 58 | + IntArray::class.java, |
| 59 | + ) |
| 60 | + val getStateDrawable = |
| 61 | + StateListDrawable::class.java.getMethod( |
| 62 | + "getStateDrawable", |
| 63 | + Int::class.javaPrimitiveType, |
| 64 | + ) |
| 65 | + val index = getStateDrawableIndex.invoke(drawable, view.drawableState) |
| 66 | + drawable = getStateDrawable.invoke(drawable, index) as Drawable |
| 67 | + } |
| 68 | + |
| 69 | + val bitmap = getBitmapFromDrawable(context, drawable) |
| 70 | + val otherBitmap = getBitmapFromDrawable(context, otherDrawable) |
| 71 | + return bitmap.sameAs(otherBitmap) |
| 72 | + } |
| 73 | + |
| 74 | + @JvmStatic |
| 75 | + private fun getBitmapFromDrawable( |
| 76 | + context: Context?, |
| 77 | + drawable: Drawable, |
| 78 | + ): Bitmap { |
| 79 | + val bitmap: Bitmap = |
| 80 | + Bitmap.createBitmap( |
| 81 | + drawable.intrinsicWidth, |
| 82 | + drawable.intrinsicHeight, |
| 83 | + Bitmap.Config.ARGB_8888, |
| 84 | + ) |
| 85 | + val canvas = Canvas(bitmap) |
| 86 | + drawable.setBounds(0, 0, canvas.width, canvas.height) |
| 87 | + drawable.draw(canvas) |
| 88 | + return bitmap |
| 89 | + } |
| 90 | +} |
0 commit comments