如何验证是否选择了文本

时间:2014-05-01 21:45:51

标签: android-espresso

我需要验证是否选择了TextView。这是对象:

TextView{
    id=2131230879,
    res-name=title,
    visibility=VISIBLE,
    width=101,
    height=144,
    has-focus=false,
    has-focusable=false,
    has-window-focus=true,
    is-clickable=false,
    is-enabled=true,
    is-focused=false,
    is-focusable=false,
    is-layout-requested=false,
    is-selected=true,
    root-is-layout-requested=false, 
    has-input-connection=false, 
    x=71.0, 
    y=0.0, 
    text=Size, 
    input-type=0, 
    ime-target=false
}
选择文本视图时,

is-selected会从false更改为true。

在Espresso中有内置的方法吗?

1 个答案:

答案 0 :(得分:2)

想通了,我需要一个自定义匹配器,这里是代码:

public static Matcher<View> isTextSelected() {
    return new TypeSafeMatcher<View>() {

        @Override
        public boolean matchesSafely(View view) {
            if (!(view instanceof TextView)) {
                return false;
            }
            return (view).isSelected();
        }
        @Override
        public void describeTo(Description description) {
            description.appendText("is-selected=true");
        }
    };
}

然后在视图上调用匹配器:onView(withText("XYZ")).check(matches(isTextSelected()));