Material-components-android: [TextInputLayout] How to simulate clicks to drawables?

Created on 27 Aug 2020  路  7Comments  路  Source: material-components/material-components-android

Description: How to simulate a click onto TextInputLayout end drawable? Right now I use callOnClick to test button presses, and I don't see a corresponding method to simulate clicks to the End Drawable. I am using Robolectric as well - if that helps somehow.

Material Library version: 1.1.0

bug

All 7 comments

What behaviour are you trying to test with clicking the end drawable?

In this case when I click the end drawable, I clear the input field.

Unfortunately there is no easy way to do that.

  • Option 1)
    If you have something like:
textInput.setEndIconOnClickListener { v ->
   clearText();
}

You can directly call clearText() of your Fragment/Activity in your unit test and check if the EditText is empty afterwards. You can do the same thing if you have viewmodels.

  • Option 2)
    Use reflection to grab the internal CheckableImageButton endIconView and use Espresso to click it
    (obviously this can break with library updates)

I see. If I were to contribute a way to performClick on the various different drawables, would that have a good chance of approval? I'm asking since I'm not sure how flexible or not the team is about adding new methods, and what the guidelines are on that.

My bad, I completely forgot about this.
Using the internal ID is 100 times better than accessing the view with reflection

@pedronveloso You should follow this approach if it is ok for you

Grazie! This helps.
For reference on those that might find this thread, this is how I did it for pure Unit Test using Robolectric:

textViewLayout.findViewById<View>(R.id.text_input_end_icon) as CheckableImageButton).performClick()
Was this page helpful?
0 / 5 - 0 ratings