From eb433811f7d503fcdaea4362007e81eb42c22ef6 Mon Sep 17 00:00:00 2001 From: EmmanuelMess Date: Tue, 13 Jan 2026 11:51:39 -0300 Subject: [PATCH] Add documentation for ActionMenuIconMatcher --- .../matcher/ActionMenuIconMatcher.kt | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/app/src/androidTest/java/com/amaze/filemanager/matcher/ActionMenuIconMatcher.kt b/app/src/androidTest/java/com/amaze/filemanager/matcher/ActionMenuIconMatcher.kt index d29cd4e2e..2bd0ac7ca 100644 --- a/app/src/androidTest/java/com/amaze/filemanager/matcher/ActionMenuIconMatcher.kt +++ b/app/src/androidTest/java/com/amaze/filemanager/matcher/ActionMenuIconMatcher.kt @@ -18,6 +18,10 @@ import org.hamcrest.Matcher * From https://stackoverflow.com/a/70108466/3124150 */ object ActionMenuIconMatcher { + /** + * A [Matcher] that will match against an [ActionMenuItemView] view with a specific icon + * resource + */ @JvmStatic fun withActionIconDrawable( @DrawableRes resourceId: Int, @@ -28,9 +32,11 @@ object ActionMenuIconMatcher { } override fun matchesSafely(actionMenuItemView: ActionMenuItemView): Boolean { + val iconDrawable = actionMenuItemView.itemData.icon ?: return false + return sameBitmap( actionMenuItemView.context, - actionMenuItemView.itemData.icon, + iconDrawable, resourceId, actionMenuItemView, ) @@ -38,18 +44,18 @@ object ActionMenuIconMatcher { } } + /** + * Compares a [Drawable] against a resource id, returns if they are identical + */ @JvmStatic private fun sameBitmap( context: Context, - drawable: Drawable?, - resourceId: Int, + drawable: Drawable, + @DrawableRes resourceId: Int, view: View, ): Boolean { var drawable = drawable - val otherDrawable: Drawable? = context.resources.getDrawable(resourceId) - if (drawable == null || otherDrawable == null) { - return false - } + val otherDrawable: Drawable = context.resources.getDrawable(resourceId) ?: return false if (drawable is StateListDrawable) { val getStateDrawableIndex = @@ -66,16 +72,16 @@ object ActionMenuIconMatcher { drawable = getStateDrawable.invoke(drawable, index) as Drawable } - val bitmap = getBitmapFromDrawable(context, drawable) - val otherBitmap = getBitmapFromDrawable(context, otherDrawable) + val bitmap = getBitmapFromDrawable(drawable) + val otherBitmap = getBitmapFromDrawable(otherDrawable) return bitmap.sameAs(otherBitmap) } + /** + * Convert a [Bitmap] to a [Drawable] by drawing to a canvas + */ @JvmStatic - private fun getBitmapFromDrawable( - context: Context?, - drawable: Drawable, - ): Bitmap { + private fun getBitmapFromDrawable(drawable: Drawable): Bitmap { val bitmap: Bitmap = Bitmap.createBitmap( drawable.intrinsicWidth,