4

I have a RecyclerView with several CardView. I set background to RelativeLayout of cards in code like that:

layout.setBackground(ContextCompat.getDrawable(mContext, R.drawable.item_background_grey));

Cards with different drawables look like that
white_and_grey_cards

I want to check drawable with espresso test.

I am getting ViewInteraction like that:

ViewInteraction relativeLayout2 = onView(
                allOf(withId(R.id.last_interaction_entry_layout),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.tab_last_interaction_recyclerview),
                                        1),
                                0),
                        isDisplayed()));

It works okay, when I'm checking text inside. But when I'm checking background
relativeLayout2.check(matches(hasBackground(R.drawable.item_background_grey)));

It gives me following error:

androidx.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'has background with drawable ID: 2131165305' doesn't match the selected view.
Expected: has background with drawable ID: 2131165305
Got: "RelativeLayout{id=2131230886, res-name=last_interaction_entry_layout, visibility=VISIBLE, width=736, height=113, 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=android.widget.FrameLayout$LayoutParams@2242239, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}"

When I'm trying to check if there is any view with such background

onView(allOf(hasBackground(R.drawable.item_background)))
      .check(matches(isDisplayed()));

I get NoMatchingViewException.

Also, tried checking color directly like proposed here, here and there, got

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.drawable.ColorDrawable.getColor()' on a null object reference
Kirill Smirnov
  • 131
  • 2
  • 12

0 Answers0