Search in sources :

Example 1 with ViewAssertion

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);
            }
        }
    };
}
Also used : ViewAssertion(com.google.android.apps.common.testing.ui.espresso.ViewAssertion) Optional(com.google.common.base.Optional) StringDescription(org.hamcrest.StringDescription)

Example 2 with ViewAssertion

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);
    }
}
Also used : ViewAssertion(com.google.android.apps.common.testing.ui.espresso.ViewAssertion) NoMatchingViewException(com.google.android.apps.common.testing.ui.espresso.NoMatchingViewException) AssertionFailedError(junit.framework.AssertionFailedError) Espresso.onView(com.google.android.apps.common.testing.ui.espresso.Espresso.onView) View(android.view.View)

Example 3 with ViewAssertion

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);
            }
        }
    };
}
Also used : ViewAssertion(com.google.android.apps.common.testing.ui.espresso.ViewAssertion) Optional(com.google.common.base.Optional) ArrayList(java.util.ArrayList) AssertionFailedError(junit.framework.AssertionFailedError) View(android.view.View) Predicate(com.google.common.base.Predicate)

Aggregations

ViewAssertion (com.google.android.apps.common.testing.ui.espresso.ViewAssertion)3 View (android.view.View)2 Optional (com.google.common.base.Optional)2 AssertionFailedError (junit.framework.AssertionFailedError)2 Espresso.onView (com.google.android.apps.common.testing.ui.espresso.Espresso.onView)1 NoMatchingViewException (com.google.android.apps.common.testing.ui.espresso.NoMatchingViewException)1 Predicate (com.google.common.base.Predicate)1 ArrayList (java.util.ArrayList)1 StringDescription (org.hamcrest.StringDescription)1