I am new to Espresso tests and Android. I am trying to test if the correct Icon is shown next to the text. The icon is set with:
public void setLabelTextIcon(@DrawableRes int iconResId) {
txtLabel.setCompoundDrawablesRelativeWithIntrinsicBounds(iconResId, 0,0,0);
}
I found this online https://gist.github.com/frankiesardo/7490059 but it is not working for me. Due to my lack of background-knowledge I'm not able to change the code that it works. At the moment I try
onView(withId(R.id.blue_triple_stripe_txtLabel)).check(matches(withActionIconDrawable(R.drawable.ic_date_grey)));
and the withActionIconDrawable() is
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);
}
};
}
The Error I get is
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}"
Thanks!