Tulip
Tulip

Reputation: 51

How to verify if text is selected

I need to verify if a TextView is selected. Here is the object:

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 changes from false to true when you select the textview.

Is there a built-in way to do it in Espresso?

Upvotes: 3

Views: 1479

Answers (1)

Tulip
Tulip

Reputation: 51

Figured out, I needed a custom matcher to do so, here's the code:

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

Then call the matcher on a view: onView(withText("XYZ")).check(matches(isTextSelected()));

Upvotes: 2

Related Questions