Android Espresso can't type in TYPE_TEXT_VARIATION_NORMAL?

4

I have a TextInputEditText view in my android application, and I am setting it up programmatically (via Anko and Kotlin) like this:

            textInputEditText {
                id = R.id.text_input_id
                inputType = EditorInfo.TYPE_TEXT_VARIATION_NORMAL
                hint = resources.getText(R.string.text_hint)
            }

Then in an Espresso test I call this line:

onView(withId(R.id.text_input_id)).perform(typeText(textToType))

Which fails with this error:

Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints:
((is displayed on the screen to the user) and (supports input methods or is assignable from class: class android.widget.SearchView))
Target view: "TextInputEditText{id=2131623954, res-name=text_input_id, visibility=VISIBLE, width=862, height=118, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=, input-type=0, ime-target=false, has-links=false}"

I've discovered that if I switch it from this:

/**
 * Default variation of {@link #TYPE_CLASS_TEXT}: plain old normal text.
 */
public static final int TYPE_TEXT_VARIATION_NORMAL = 0x00000000;

To this:

/**
 * Variation of {@link #TYPE_CLASS_TEXT}: entering a password, which should
 * be visible to the user.
 */
public static final int TYPE_TEXT_VARIATION_VISIBLE_PASSWORD = 0x00000090;

It suddenly magically works! But that doesn't make much sense. The normal text sounds like the right one to use until Espresso blows up in my face. Is this a bug with Espresso, or something I'm not understanding?

java
android
android-espresso
asked on Stack Overflow Mar 23, 2017 by CorayThan

2 Answers

1

My guess is that this is happening inside the typeText call. See the list of constraints in TypeTextAction.getConstraints(). The logic is essentially:

isDisplayed() &&
hasFocus() &&
( supportsInputMethods() || isAssignableFrom(SearchView.class) )

My first guess is that it's not passing the supportsInputMethods() constraint. You can put a break point inside ViewMatchers.supportsInputMethods() -> matchesSafely() and see for yourself.

answered on Stack Overflow May 2, 2017 by tir38
0

You can use EditorInfo.TYPE_CLASS_TEXT instead of EditorInfo.TYPE_TEXT_VARIATION_NORMAL to make Espresso work.

TYPE_TEXT_VARIATION_NORMAL is causing supportsInputMethods() ViewMatcher (that typeText() is using internally) to fail for some reason.

TYPE_CLASS_TEXT will give similar input type as TYPE_TEXT_VARIATION_NORMAL.


User contributions licensed under CC BY-SA 3.0