安卓Espresso无法输入TYPE_TEXT_VARIATION_NORMAL?

4
我在我的安卓应用中有一个TextInputEditText视图,并通过Anko和Kotlin进行编程设置,代码如下:
            textInputEditText {
                id = R.id.text_input_id
                inputType = EditorInfo.TYPE_TEXT_VARIATION_NORMAL
                hint = resources.getText(R.string.text_hint)
            }

在 Espresso 测试中,我调用了以下这行代码:
onView(withId(R.id.text_input_id)).perform(typeText(textToType))

这会导致以下错误:

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}"

我发现如果我把它从这个位置切换到:

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

转换为:

/**
 * 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;

它突然神奇地工作了!但这并没有太多意义。正常的文本听起来像是正确的,直到Espresso在我的脸上爆炸。这是Espresso的错误,还是我没有理解?

2个回答

1
我的猜测是这个问题出现在typeText调用内部。请参考TypeTextAction.getConstraints()中的约束列表。逻辑本质上是:
isDisplayed() &&
hasFocus() &&
( supportsInputMethods() || isAssignableFrom(SearchView.class) )

我猜测问题在于它没有通过supportsInputMethods()约束条件。你可以在ViewMatchers.supportsInputMethods() -> matchesSafely()中设置断点,亲自查看一下。

0

您可以使用EditorInfo.TYPE_CLASS_TEXT代替EditorInfo.TYPE_TEXT_VARIATION_NORMAL来使Espresso正常工作。

TYPE_TEXT_VARIATION_NORMAL会导致supportsInputMethods() ViewMatcher(内部使用的typeText())出现故障。

TYPE_CLASS_TEXT将提供与TYPE_TEXT_VARIATION_NORMAL类似的输入类型。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接