use of com.google.android.apps.common.testing.ui.espresso.ViewAssertion in project double-espresso by JakeWharton.
the class ViewAssertions method matches.
/**
* Returns a generic {@link ViewAssertion} that asserts that a view exists in the view hierarchy
* and is matched by the given view matcher.
*/
public static ViewAssertion matches(final Matcher<? super View> viewMatcher) {
checkNotNull(viewMatcher);
return new ViewAssertion() {
@Override
public void check(Optional<View> view, Optional<NoMatchingViewException> noViewException) {
StringDescription description = new StringDescription();
description.appendText("'");
viewMatcher.describeTo(description);
if (noViewException.isPresent()) {
description.appendText(String.format("' check could not be performed because view '%s' was not found.\n", viewMatcher));
Log.e(TAG, description.toString());
throw noViewException.get();
} else {
// TODO(user): ideally, we should append the matcher used to find the view
// This can be done in DefaultFailureHandler (just like we currently to with
// PerformException)
description.appendText("' doesn't match the selected view.");
assertThat(description.toString(), view.get(), viewMatcher);
}
}
};
}
use of com.google.android.apps.common.testing.ui.espresso.ViewAssertion in project double-espresso by JakeWharton.
the class DefaultFailureHandlerTest method testCustomAssertionError.
public void testCustomAssertionError() {
try {
onView(isRoot()).check(new ViewAssertion() {
@Override
public void check(Optional<View> view, Optional<NoMatchingViewException> noViewFoundException) {
assertFalse(true);
}
});
fail("Previous call expected to fail");
} catch (AssertionFailedError e) {
assertFailureStackContainsThisClass(e);
}
}
use of com.google.android.apps.common.testing.ui.espresso.ViewAssertion in project double-espresso by JakeWharton.
the class ViewAssertions method selectedDescendantsMatch.
/**
* Returns a generic {@link ViewAssertion} that asserts that the descendant views selected by the
* selector match the specified matcher.
*
* Example: onView(rootView).check(selectedDescendantsMatch(
* not(isAssignableFrom(TextView.class)), hasContentDescription()));
*/
public static ViewAssertion selectedDescendantsMatch(final Matcher<View> selector, final Matcher<View> matcher) {
return new ViewAssertion() {
@SuppressWarnings("unchecked")
@Override
public void check(Optional<View> view, Optional<NoMatchingViewException> noViewException) {
Preconditions.checkArgument(view.isPresent());
View rootView = view.get();
final Predicate<View> viewPredicate = new Predicate<View>() {
@Override
public boolean apply(View input) {
return selector.matches(input);
}
};
Iterator<View> selectedViewIterator = Iterables.filter(breadthFirstViewTraversal(rootView), viewPredicate).iterator();
List<View> nonMatchingViews = new ArrayList<View>();
while (selectedViewIterator.hasNext()) {
View selectedView = selectedViewIterator.next();
if (!matcher.matches(selectedView)) {
nonMatchingViews.add(selectedView);
}
}
if (nonMatchingViews.size() > 0) {
String errorMessage = HumanReadables.getViewHierarchyErrorMessage(rootView, Optional.of(nonMatchingViews), String.format("At least one view did not match the required matcher: %s", matcher), Optional.of("****DOES NOT MATCH****"));
throw new AssertionFailedError(errorMessage);
}
}
};
}
Aggregations