1

I am developing an Android application using Kotlin. I am adding instrumented tests to my application. I am having a problem testing the background of textview if it's right drawable set in XML resource.

I am setting the background of the text view programmatically like this.

when (type) {
            ApplicationController.EVENT_TYPE_FUTURE -> {
                detailsViewHolder.tvStatus.setBackgroundResource(R.drawable.background_future_event)
            }

            ApplicationController.EVENT_TYPE_PAST -> {
                detailsViewHolder.tvStatus.setBackgroundResource(R.drawable.background_past_event)
            }

            ApplicationController.EVENT_TYPE_CURRENT -> {
                detailsViewHolder.tvStatus.setBackgroundResource(R.drawable.background_current_event)
            }
        }

In the expresso, I want to assert that the text view is set with the right XML resource. How can I do that?

Ankur_009
  • 3,823
  • 4
  • 31
  • 47
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372

1 Answers1

2

You could use one of the Espresso's view matchers ViewMatchers.hasBackground:

onView(withId(R.id.tvStatus)).check(matches(hasBackground(R.drawable.background_future_event)))

Although the matcher is in beta, but I hope it works in your case, otherwise you may have to create a custom matcher.

Aaron
  • 3,764
  • 2
  • 8
  • 25