Android espresso 如何验证是否选择了文本

Android espresso 如何验证是否选择了文本,android-espresso,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,

我需要验证是否选择了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
}
当您选择文本视图时,
被选中
将从false变为true


有没有一种内置的方法可以在浓缩咖啡中实现这一点?

我想,我需要一个自定义匹配器来实现这一点,下面是代码:

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");
        }
    };
}
public静态匹配器isTextSelected(){
返回新的TypeSafeMatcher(){
@凌驾
公共布尔匹配安全(视图){
如果(!(文本视图的视图实例)){
返回false;
}
return(view.isSelected();
}
@凌驾
公共无效说明(说明){
description.appendText(“已选择=真”);
}
};
}

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

作为一项改进:您可以返回新的BoundedMatcher(TextView.class){…},而不是返回TypeSafeMatcher。优点是MatchesFely现在有一个TextView参数而不是视图,类型检查由BoundedMatcher本身处理。