ViewMatchers


public final class ViewMatchers


A collection of hamcrest matchers that match Views.

Summary

Nested types

Enumerates the possible list of values for getVisibility.

Public methods

static void
<T> assertThat(T actual, Matcher<T> matcher)

A replacement for MatcherAssert.assertThat that renders View objects nicely.

static void
<T> assertThat(String message, T actual, Matcher<T> matcher)

A replacement for MatcherAssert.assertThat that renders View objects nicely.

static Matcher<View>

Returns a matcher that matches Views currently do not have focus.

static Matcher<View>
hasBackground(int drawableId)

Returns a matcher that matches android.view.View based on background resource Bitmap.

static Matcher<View>
hasChildCount(int childCount)

Matches a ViewGroup if it has exactly the specified number of children.

static Matcher<View>

Returns an Matcher that matches Views with any content description.

static Matcher<View>
hasDescendant(Matcher<View> descendantMatcher)

Returns a matcher that matches Views based on the presence of a descendant in its view hierarchy.

static Matcher<View>
hasErrorText(String expectedError)

Returns a matcher that matches EditText based on edit text error string value.

static Matcher<View>
hasErrorText(Matcher<String> stringMatcher)

Returns a matcher that matches EditText based on edit text error string value.

static Matcher<View>

Returns a matcher that matches Views currently have focus.

static Matcher<View>
hasImeAction(int imeAction)

Returns a matcher that matches views that support input methods (e.g.

static Matcher<View>
hasImeAction(Matcher<Integer> imeActionMatcher)

Returns a matcher that matches views that support input methods (e.g.

static Matcher<View>

Returns a matcher that matches TextViews that have links.

static Matcher<View>
hasMinimumChildCount(int minChildCount)

Matches a ViewGroup if it has at least the specified number of children.

static Matcher<View>
hasSibling(Matcher<View> siblingMatcher)

Returns an Matcher that matches Views based on their siblings.

static Matcher<View>
hasTextColor(int colorResId)

Returns a matcher that matches android.widget.TextView based on it's color.

static Matcher<View>

Matches Views based on instance or subclass of the provided class.

static Matcher<View>

Returns a matcher that accepts if and only if the view is a CompoundButton (or subtype of) and is in checked state.

static Matcher<View>

Returns a matcher that matches Views that are clickable.

static Matcher<View>

Returns a matcher which only accepts a view whose height and width fit perfectly within the currently displayed region of this view.

static Matcher<View>
isDescendantOfA(Matcher<View> ancestorMatcher)

Returns a matcher that matches Views based on the given ancestor type.

static Matcher<View>

Returns a matcher that matches Views that are currently displayed on the screen to the user.

static Matcher<View>
isDisplayingAtLeast(int areaPercentage)

Returns a matcher which accepts a view so long as a given percentage of that view's area is not obscured by any parent view and is thus visible to the user.

static Matcher<View>

Returns a matcher that matches Views that are enabled.

static Matcher<View>

Returns a matcher that matches Views that are focusable.

static Matcher<View>

Returns a matcher that matches Views that are focused.

static Matcher<View>

Returns a matcher that matches WebView if they are evaluating JavaScript.

static Matcher<View>

Returns a matcher that accepts if and only if the view is a CompoundButton (or subtype of) and is not in checked state.

static Matcher<View>

Returns a matcher that matches Views that are not clickable.

static Matcher<View>

Returns a matcher that matches Views that are not enabled.

static Matcher<View>

Returns a matcher that matches Views that are not focusable.

static Matcher<View>

Returns a matcher that matches Views that are not focused.

static Matcher<View>

Returns a matcher that matches Views that are not selected.

static Matcher<View>

Returns a matcher that matches root View.

static Matcher<View>

Returns a matcher that matches Views that are selected.

static Matcher<View>

Returns a matcher that matches views that support input methods.

static Matcher<View>
withAlpha(float alpha)

Matches Views with the specified alpha value.

static Matcher<View>
withChild(Matcher<View> childMatcher)

Returns true only if the view's child is accepted by the provided matcher.

static Matcher<View>
withClassName(Matcher<String> classNameMatcher)

Returns a matcher that matches Views with class name matching the given matcher.

static Matcher<View>

Returns an Matcher that matches Views based on content description property value.

static Matcher<View>
withContentDescription(int resourceId)

Returns a Matcher that matches Views based on content description property value.

static Matcher<View>

Returns an Matcher that matches Views based on content description's text value.

static Matcher<View>

Returns a matcher that matches Views that have "effective" visibility set to the given value.

static Matcher<View>
withHint(String hintText)

Returns a matcher that matches TextView based on it's hint property value.

static Matcher<View>
withHint(int resourceId)

Returns a matcher that matches a descendant of TextView that is displaying the hint associated with the given resource id.

static Matcher<View>
withHint(Matcher<String> stringMatcher)

Returns a matcher that matches TextViews based on hint property value.

static Matcher<View>
withId(int id)

A matcher that matches Views based on its resource id.

static Matcher<View>
withId(Matcher<Integer> integerMatcher)

Returns a matcher that matches Views based on resource ids.

static Matcher<View>
withInputType(int inputType)

Returns a matcher that matches android.text.InputType.

static Matcher<View>
withParent(Matcher<View> parentMatcher)

A matcher that accepts a view if and only if the view's parent is accepted by the provided matcher.

static Matcher<View>
withParentIndex(int index)

Returns a matcher that matches the child index inside the ViewParent.

static Matcher<View>

Returns a matcher that matches Views based on resource id names, (for instance, channel_avatar).

static Matcher<View>

Returns a matcher that matches Views based on resource id names, (for instance, channel_avatar).

static Matcher<View>
withSpinnerText(int resourceId)

Returns a matcher that matches a descendant of Spinner that is displaying the string of the selected item associated with the given resource id.

static Matcher<View>
withSpinnerText(Matcher<String> stringMatcher)

Returns a matcher that matches Spinner's based on toString value of the selected item.

static Matcher<View>

Returns a matcher that matches Spinner based on it's selected item's toString value.

static Matcher<View>
withSubstring(String substring)

Returns a matcher that matches TextView that contains the specific substring.

static Matcher<View>
withTagKey(int key)

Returns a matcher that matches View based on tag keys.

static Matcher<View>
withTagKey(int key, Matcher<Object> objectMatcher)

Returns a matcher that matches Views based on tag keys.

static Matcher<View>
withTagValue(Matcher<Object> tagValueMatcher)

Returns a matcher that matches Views based on tag property values.

static Matcher<View>
withText(int resourceId)

Returns a matcher that matches a descendant of TextView that is displaying the string associated with the given resource id.

static Matcher<View>
withText(Matcher<String> stringMatcher)

Returns a matcher that matches TextViews based on text property value.

static Matcher<View>

Returns a matcher that matches TextView based on its text property value.

Public methods

assertThat

public static void <T> assertThat(T actual, Matcher<T> matcher)

A replacement for MatcherAssert.assertThat that renders View objects nicely.

Parameters
T actual

the actual value.

Matcher<T> matcher

a matcher that accepts or rejects actual.

assertThat

public static void <T> assertThat(String message, T actual, Matcher<T> matcher)

A replacement for MatcherAssert.assertThat that renders View objects nicely.

Parameters
String message

the message to display.

T actual

the actual value.

Matcher<T> matcher

a matcher that accepts or rejects actual.

doesNotHaveFocus

public static Matcher<ViewdoesNotHaveFocus()

Returns a matcher that matches Views currently do not have focus.

hasBackground

public static Matcher<ViewhasBackground(int drawableId)

Returns a matcher that matches android.view.View based on background resource Bitmap.

See also
sameAs

hasChildCount

public static Matcher<ViewhasChildCount(int childCount)

Matches a ViewGroup if it has exactly the specified number of children.

hasContentDescription

public static Matcher<ViewhasContentDescription()

Returns an Matcher that matches Views with any content description.

hasDescendant

public static Matcher<ViewhasDescendant(Matcher<View> descendantMatcher)

Returns a matcher that matches Views based on the presence of a descendant in its view hierarchy.

Parameters
Matcher<View> descendantMatcher

the type of the descendant to match on

hasErrorText

public static Matcher<ViewhasErrorText(String expectedError)

Returns a matcher that matches EditText based on edit text error string value.

Note: Sugar for hasErrorText(is("string")).

hasErrorText

public static Matcher<ViewhasErrorText(Matcher<String> stringMatcher)

Returns a matcher that matches EditText based on edit text error string value.

Note: View's error property can be null, to match against it use hasErrorText(nullValue(String.class)

hasFocus

public static Matcher<ViewhasFocus()

Returns a matcher that matches Views currently have focus.

hasImeAction

public static Matcher<ViewhasImeAction(int imeAction)

Returns a matcher that matches views that support input methods (e.g. EditText) and have the specified IME action set in its EditorInfo.

Parameters
int imeAction

the IME action to match

hasImeAction

public static Matcher<ViewhasImeAction(Matcher<Integer> imeActionMatcher)

Returns a matcher that matches views that support input methods (e.g. EditText) and have the specified IME action set in its EditorInfo.

Parameters
Matcher<Integer> imeActionMatcher

a matcher for the IME action

hasLinks

public static Matcher<ViewhasLinks()

Returns a matcher that matches TextViews that have links.

hasMinimumChildCount

public static Matcher<ViewhasMinimumChildCount(int minChildCount)

Matches a ViewGroup if it has at least the specified number of children.

hasSibling

public static Matcher<ViewhasSibling(Matcher<View> siblingMatcher)

Returns an Matcher that matches Views based on their siblings.

This may be particularly useful when a view cannot be uniquely selected on properties such as text or R.id. For example: a call button is repeated several times in a contacts layout and the only way to differentiate the call button view is by what appears next to it (e.g. the unique name of the contact).

Parameters
Matcher<View> siblingMatcher

a Matcher for the sibling of the view.

hasTextColor

public static Matcher<ViewhasTextColor(int colorResId)

Returns a matcher that matches android.widget.TextView based on it's color.

isAssignableFrom

public static Matcher<ViewisAssignableFrom(Class<View> clazz)

Matches Views based on instance or subclass of the provided class.

Some versions of Hamcrest make the generic typing of this a nightmare, so we have a special case for our users.

isChecked

public static Matcher<ViewisChecked()

Returns a matcher that accepts if and only if the view is a CompoundButton (or subtype of) and is in checked state.

isClickable

public static Matcher<ViewisClickable()

Returns a matcher that matches Views that are clickable.

isCompletelyDisplayed

public static Matcher<ViewisCompletelyDisplayed()

Returns a matcher which only accepts a view whose height and width fit perfectly within the currently displayed region of this view.

There exist views (such as ScrollViews) whose height and width are larger then the physical device screen by design. Such views will never be completely displayed.

isDescendantOfA

public static Matcher<ViewisDescendantOfA(Matcher<View> ancestorMatcher)

Returns a matcher that matches Views based on the given ancestor type.

Parameters
Matcher<View> ancestorMatcher

the type of the ancestor to match on

isDisplayed

public static Matcher<ViewisDisplayed()

Returns a matcher that matches Views that are currently displayed on the screen to the user.

Note: isDisplayed will select views that are partially displayed (eg: the full height/width of the view is greater than the height/width of the visible rectangle). If you wish to ensure the entire rectangle this view draws is displayed to the user use isCompletelyDisplayed.

isDisplayingAtLeast

public static Matcher<ViewisDisplayingAtLeast(int areaPercentage)

Returns a matcher which accepts a view so long as a given percentage of that view's area is not obscured by any parent view and is thus visible to the user.

Parameters
int areaPercentage

an integer ranging from (0, 100] indicating how much percent of the surface area of the view must be shown to the user to be accepted.

isEnabled

public static Matcher<ViewisEnabled()

Returns a matcher that matches Views that are enabled.

isFocusable

public static Matcher<ViewisFocusable()

Returns a matcher that matches Views that are focusable.

isFocused

public static Matcher<ViewisFocused()

Returns a matcher that matches Views that are focused.

isJavascriptEnabled

public static Matcher<ViewisJavascriptEnabled()

Returns a matcher that matches WebView if they are evaluating JavaScript.

isNotChecked

public static Matcher<ViewisNotChecked()

Returns a matcher that accepts if and only if the view is a CompoundButton (or subtype of) and is not in checked state.

isNotClickable

public static Matcher<ViewisNotClickable()

Returns a matcher that matches Views that are not clickable.

isNotEnabled

public static Matcher<ViewisNotEnabled()

Returns a matcher that matches Views that are not enabled.

isNotFocusable

public static Matcher<ViewisNotFocusable()

Returns a matcher that matches Views that are not focusable.

isNotFocused

public static Matcher<ViewisNotFocused()

Returns a matcher that matches Views that are not focused.

isNotSelected

public static Matcher<ViewisNotSelected()

Returns a matcher that matches Views that are not selected.

isRoot

public static Matcher<ViewisRoot()

Returns a matcher that matches root View.

isSelected

public static Matcher<ViewisSelected()

Returns a matcher that matches Views that are selected.

supportsInputMethods

public static Matcher<ViewsupportsInputMethods()

Returns a matcher that matches views that support input methods.

withAlpha

public static Matcher<ViewwithAlpha(float alpha)

Matches Views with the specified alpha value.

withChild

public static Matcher<ViewwithChild(Matcher<View> childMatcher)

Returns true only if the view's child is accepted by the provided matcher.

Parameters
Matcher<View> childMatcher

the matcher to apply on the child views.

withClassName

public static Matcher<ViewwithClassName(Matcher<String> classNameMatcher)

Returns a matcher that matches Views with class name matching the given matcher.

withContentDescription

public static Matcher<ViewwithContentDescription(Matcher<CharSequence> charSequenceMatcher)

Returns an Matcher that matches Views based on content description property value.

Parameters
Matcher<CharSequence> charSequenceMatcher

a CharSequenceMatcher for the content description

withContentDescription

public static Matcher<ViewwithContentDescription(int resourceId)

Returns a Matcher that matches Views based on content description property value.

Parameters
int resourceId

the resource id of the content description to match on.

withContentDescription

public static Matcher<ViewwithContentDescription(String text)

Returns an Matcher that matches Views based on content description's text value.

Parameters
String text

the text to match on.

withEffectiveVisibility

public static Matcher<ViewwithEffectiveVisibility(ViewMatchers.Visibility visibility)

Returns a matcher that matches Views that have "effective" visibility set to the given value.

Effective visibility takes into account not only the view's visibility value, but also that of its ancestors. In case of View.VISIBLE, this means that the view and all of its ancestors have visibility=VISIBLE. In case of GONE and INVISIBLE, it's the opposite - any GONE or INVISIBLE parent will make all of its children have their effective visibility.

Note: Contrary to what the name may imply, view visibility does not directly translate into whether the view is displayed on screen (use isDisplayed for that). For example, the view and all of its ancestors can have visibility=VISIBLE, but the view may need to be scrolled to in order to be actually visible to the user. Unless you're specifically targeting the visibility value with your test, use isDisplayed.

withHint

public static Matcher<ViewwithHint(String hintText)

Returns a matcher that matches TextView based on it's hint property value.

Note: View's sugar for withHint(is("string")).

Parameters
String hintText

String with the hint text to match

withHint

public static Matcher<ViewwithHint(int resourceId)

Returns a matcher that matches a descendant of TextView that is displaying the hint associated with the given resource id.

Parameters
int resourceId

the string resource the text view is expected to have as a hint.

withHint

public static Matcher<ViewwithHint(Matcher<String> stringMatcher)

Returns a matcher that matches TextViews based on hint property value.

Note: View's hint property can be null, to match against it use withHint(nullValue(String.class)

Parameters
Matcher<String> stringMatcher

Matcher of String with text to match

withId

public static Matcher<ViewwithId(int id)

A matcher that matches Views based on its resource id.

Same as withId(is(int)) but attempts to look up resource name of the given id and use a R.id.myView style description with describeTo. If resource lookup is unavailable, at the time describeTo is invoked, this will print out a simple "with id: %d". If the lookup for a given id fails, "with id: %d (resource name not found)" will be returned as the description.

Note: Android resource ids are not guaranteed to be unique. You may have to pair this matcher with another one to guarantee a unique view selection.

Parameters
int id

the resource id.

withId

public static Matcher<ViewwithId(Matcher<Integer> integerMatcher)

Returns a matcher that matches Views based on resource ids. Note: Android resource ids are not guaranteed to be unique. You may have to pair this matcher with another one to guarantee a unique view selection.

Parameters
Matcher<Integer> integerMatcher

a Matcher for resource ids

withInputType

public static Matcher<ViewwithInputType(int inputType)

Returns a matcher that matches android.text.InputType.

withParent

public static Matcher<ViewwithParent(Matcher<View> parentMatcher)

A matcher that accepts a view if and only if the view's parent is accepted by the provided matcher.

Parameters
Matcher<View> parentMatcher

the matcher to apply on getParent.

withParentIndex

public static Matcher<ViewwithParentIndex(int index)

Returns a matcher that matches the child index inside the ViewParent.

withResourceName

public static Matcher<ViewwithResourceName(String name)

Returns a matcher that matches Views based on resource id names, (for instance, channel_avatar).

Parameters
String name

the resource id name

withResourceName

public static Matcher<ViewwithResourceName(Matcher<String> stringMatcher)

Returns a matcher that matches Views based on resource id names, (for instance, channel_avatar).

Parameters
Matcher<String> stringMatcher

a Matcher for resource id names

withSpinnerText

public static Matcher<ViewwithSpinnerText(int resourceId)

Returns a matcher that matches a descendant of Spinner that is displaying the string of the selected item associated with the given resource id.

Parameters
int resourceId

the resource id of the string resource the text view is expected to hold.

withSpinnerText

public static Matcher<ViewwithSpinnerText(Matcher<String> stringMatcher)

Returns a matcher that matches Spinner's based on toString value of the selected item.

Parameters
Matcher<String> stringMatcher

Matcher of String with text to match.

withSpinnerText

public static Matcher<ViewwithSpinnerText(String text)

Returns a matcher that matches Spinner based on it's selected item's toString value.

Note: Sugar for withSpinnerText(is("string")).

withSubstring

public static Matcher<ViewwithSubstring(String substring)

Returns a matcher that matches TextView that contains the specific substring.

Note: View's sugar for withText(containsString("string")).

Parameters
String substring

String that is expected to be contained

withTagKey

public static Matcher<ViewwithTagKey(int key)

Returns a matcher that matches View based on tag keys.

Parameters
int key

to match

withTagKey

public static Matcher<ViewwithTagKey(int key, Matcher<Object> objectMatcher)

Returns a matcher that matches Views based on tag keys.

Parameters
int key

to match

Matcher<Object> objectMatcher

Object to match

withTagValue

public static Matcher<ViewwithTagValue(Matcher<Object> tagValueMatcher)

Returns a matcher that matches Views based on tag property values.

Parameters
Matcher<Object> tagValueMatcher

a Matcher for the view's tag property value

withText

public static Matcher<ViewwithText(int resourceId)

Returns a matcher that matches a descendant of TextView that is displaying the string associated with the given resource id.

Parameters
int resourceId

the string resource the text view is expected to hold.

withText

public static Matcher<ViewwithText(Matcher<String> stringMatcher)

Returns a matcher that matches TextViews based on text property value.

Note: A View text property is never null. If you call setText with a null value it will still be "" (empty string). Do not use a null matcher.

Parameters
Matcher<String> stringMatcher

Matcher of String with text to match

withText

public static Matcher<ViewwithText(String text)

Returns a matcher that matches TextView based on its text property value.

Note: View's sugar for withText(is("string")).

Parameters
String text

String with the text to match