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
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.
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.
CheckableImageButton endIconView and use Espresso to click itI 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.
The endIcon has the id text_input_end_icon.
Check the internal test. You can do something similar:
https://github.com/material-components/material-components-android/blob/34df6d91f1164ec981616346e7b675c1e69d4134/tests/javatests/com/google/android/material/textfield/TextInputLayoutIconsTest.java#L415-L431
If you are using the app:endIconMode="clear_text" you can do something like:
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()