1

我是 Espresso 测试和 Android 的新手。我正在尝试测试文本旁边是否显示了正确的图标。该图标设置为:

public void setLabelTextIcon(@DrawableRes int iconResId) {
    txtLabel.setCompoundDrawablesRelativeWithIntrinsicBounds(iconResId, 0,0,0);
}

我在网上找到了这个https://gist.github.com/frankiesardo/7490059但它对我不起作用。由于我缺乏背景知识,我无法更改其工作的代码。目前我尝试

    onView(withId(R.id.blue_triple_stripe_txtLabel)).check(matches(withActionIconDrawable(R.drawable.ic_date_grey)));

而 withActionIconDrawable() 是

public static Matcher<View> withActionIconDrawable(@DrawableRes final int resourceId) {
    return new BoundedMatcher<View, ActionMenuItemView>(ActionMenuItemView.class) {
        @Override
        public void describeTo(final Description description) {
            description.appendText("has image drawable resource " + resourceId);
        }

        @Override
        public boolean matchesSafely(final ActionMenuItemView actionMenuItemView) {
            return sameBitmap(actionMenuItemView.getContext(), actionMenuItemView.getItemData().getIcon(), resourceId);
        }
    };
}

我得到的错误是

androidx.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'has image drawable resource 2131230871' doesn't match the selected view.
Expected: has image drawable resource 2131230871
Got: "AppCompatTextView{id=2131296335, res-name=blue_triple_stripe_txtLabel, visibility=VISIBLE, width=342, height=72, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@4d24b35, tag=null, root-is-layout-requested=false, has-input-connection=false, x=206.0, y=18.0, text=2019-04-12, input-type=0, ime-target=false, has-links=false}"

谢谢!

4

1 回答 1

0

这在 Kotlin 中对我有用

fun withDrawableTextView(@DrawableRes id: Int) = object : TypeSafeMatcher<View>() {
   override fun describeTo(description: Description) {
       description.appendText("ImageView with drawable same as drawable with id $id")
    }

   override fun matchesSafely(tv: View?): Boolean {
       if (tv is TextView) {
           if (tv.requestFocusFromTouch()) 
               for (d in tv.compoundDrawables) 
                   if (d != null) {
                      val context = tv.context
                      if (sameBitmap(tv.getContext(), d, id)) {
                          return true
                      }
                   }
       }
      return false
    } 
}

声明sameBitmap与此处相同:

private fun sameBitmap(context: Context, drawable: Drawable, resourceId: Int): Boolean {
   var drawable: Drawable? = drawable
   var otherDrawable: Drawable = context.getResources().getDrawable(resourceId)
   if (drawable == null || otherDrawable == null) {
      return false
   }
   if (drawable is StateListDrawable && otherDrawable is StateListDrawable) {
      drawable = drawable.current
      otherDrawable = otherDrawable.current
   }
   if (drawable is BitmapDrawable) {
      val bitmap: Bitmap = (drawable as BitmapDrawable).getBitmap()
      val otherBitmap: Bitmap = (otherDrawable as BitmapDrawable).getBitmap()
      return bitmap.sameAs(otherBitmap)
  }
  return false
}

将其与以下代码一起使用:

  onView(withId(R.id.edit_txt_password)).check(matches(withDrawableTextView(R.drawable.view_password_xxxdp)))
于 2021-07-07T14:26:46.513 回答