我是 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}"
谢谢!