@@ -18,6 +18,10 @@ import org.hamcrest.Matcher
1818 * From https://stackoverflow.com/a/70108466/3124150
1919 */
2020object ActionMenuIconMatcher {
21+ /* *
22+ * A [Matcher] that will match against an [ActionMenuItemView] view with a specific icon
23+ * resource
24+ */
2125 @JvmStatic
2226 fun withActionIconDrawable (
2327 @DrawableRes resourceId : Int ,
@@ -28,28 +32,30 @@ object ActionMenuIconMatcher {
2832 }
2933
3034 override fun matchesSafely (actionMenuItemView : ActionMenuItemView ): Boolean {
35+ val iconDrawable = actionMenuItemView.itemData.icon ? : return false
36+
3137 return sameBitmap(
3238 actionMenuItemView.context,
33- actionMenuItemView.itemData.icon ,
39+ iconDrawable ,
3440 resourceId,
3541 actionMenuItemView,
3642 )
3743 }
3844 }
3945 }
4046
47+ /* *
48+ * Compares a [Drawable] against a resource id, returns if they are identical
49+ */
4150 @JvmStatic
4251 private fun sameBitmap (
4352 context : Context ,
44- drawable : Drawable ? ,
45- resourceId : Int ,
53+ drawable : Drawable ,
54+ @DrawableRes resourceId : Int ,
4655 view : View ,
4756 ): Boolean {
4857 var drawable = drawable
49- val otherDrawable: Drawable ? = context.resources.getDrawable(resourceId)
50- if (drawable == null || otherDrawable == null ) {
51- return false
52- }
58+ val otherDrawable: Drawable = context.resources.getDrawable(resourceId) ? : return false
5359
5460 if (drawable is StateListDrawable ) {
5561 val getStateDrawableIndex =
@@ -66,16 +72,16 @@ object ActionMenuIconMatcher {
6672 drawable = getStateDrawable.invoke(drawable, index) as Drawable
6773 }
6874
69- val bitmap = getBitmapFromDrawable(context, drawable)
70- val otherBitmap = getBitmapFromDrawable(context, otherDrawable)
75+ val bitmap = getBitmapFromDrawable(drawable)
76+ val otherBitmap = getBitmapFromDrawable(otherDrawable)
7177 return bitmap.sameAs(otherBitmap)
7278 }
7379
80+ /* *
81+ * Convert a [Bitmap] to a [Drawable] by drawing to a canvas
82+ */
7483 @JvmStatic
75- private fun getBitmapFromDrawable (
76- context : Context ? ,
77- drawable : Drawable ,
78- ): Bitmap {
84+ private fun getBitmapFromDrawable (drawable : Drawable ): Bitmap {
7985 val bitmap: Bitmap =
8086 Bitmap .createBitmap(
8187 drawable.intrinsicWidth,
0 commit comments