Skip to content

Commit

Permalink
Add isFocused() in ViewMatchers
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 307416619
  • Loading branch information
copybara-androidxtest committed Apr 20, 2020
1 parent fef67ad commit 5899e07
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ public static Matcher<View> isFocusable() {
return new IsFocusableMatcher();
}

/** Returns a matcher that matches {@link View Views} that are focused. */
public static Matcher<View> isFocused() {
return new IsFocusedMatcher();
}

/** Returns a matcher that matches {@link View Views} currently have focus. */
public static Matcher<View> hasFocus() {
return new HasFocusMatcher();
Expand Down Expand Up @@ -901,6 +906,21 @@ public boolean matchesSafely(View view) {
}
}

static final class IsFocusedMatcher extends TypeSafeMatcher<View> {
@RemoteMsgConstructor
private IsFocusedMatcher() {}

@Override
public void describeTo(Description description) {
description.appendText("is focused");
}

@Override
public boolean matchesSafely(View view) {
return view.isFocused();
}
}

static final class HasFocusMatcher extends TypeSafeMatcher<View> {
@RemoteMsgConstructor
private HasFocusMatcher() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import static androidx.test.espresso.matcher.ViewMatchers.isDescendantOfA;
import static androidx.test.espresso.matcher.ViewMatchers.isEnabled;
import static androidx.test.espresso.matcher.ViewMatchers.isFocusable;
import static androidx.test.espresso.matcher.ViewMatchers.isFocused;
import static androidx.test.espresso.matcher.ViewMatchers.isNotChecked;
import static androidx.test.espresso.matcher.ViewMatchers.isRoot;
import static androidx.test.espresso.matcher.ViewMatchers.isSelected;
Expand Down Expand Up @@ -495,6 +496,18 @@ public void isFocusableTest() {
assertFalse(isFocusable().matches(notFocusable));
}

@Test
public void isFocusedTest() {
View focused = new View(context);
focused.setFocusable(true);
focused.setFocusableInTouchMode(true);
focused.requestFocus();
View notFocused = new View(context);
assertTrue(focused.isFocused());
assertTrue(isFocused().matches(focused));
assertFalse(isFocused().matches(notFocused));
}

@Test
public void isSelectedTest() {
View selected = new View(context);
Expand Down

0 comments on commit 5899e07

Please sign in to comment.