Search in sources :

Example 1 with NoMatchingViewException

use of com.google.android.apps.common.testing.ui.espresso.NoMatchingViewException in project double-espresso by JakeWharton.

the class ViewFinderImplTest method testGetView_missing.

@UiThreadTest
public void testGetView_missing() {
    ViewFinder finder = new ViewFinderImpl(Matchers.<View>nullValue(), testViewProvider);
    try {
        finder.getView();
        fail("No children should pass that matcher!");
    } catch (NoMatchingViewException expected) {
    }
}
Also used : ViewFinder(com.google.android.apps.common.testing.ui.espresso.ViewFinder) NoMatchingViewException(com.google.android.apps.common.testing.ui.espresso.NoMatchingViewException) UiThreadTest(android.test.UiThreadTest)

Example 2 with NoMatchingViewException

use of com.google.android.apps.common.testing.ui.espresso.NoMatchingViewException 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 NoMatchingViewException

use of com.google.android.apps.common.testing.ui.espresso.NoMatchingViewException in project double-espresso by JakeWharton.

the class ViewFinderImpl method getView.

@Override
public View getView() throws AmbiguousViewMatcherException, NoMatchingViewException {
    checkMainThread();
    final Predicate<View> matcherPredicate = new MatcherPredicateAdapter<View>(checkNotNull(viewMatcher));
    View root = rootViewProvider.get();
    Iterator<View> matchedViewIterator = Iterables.filter(breadthFirstViewTraversal(root), matcherPredicate).iterator();
    View matchedView = null;
    while (matchedViewIterator.hasNext()) {
        if (matchedView != null) {
            // Ambiguous!
            throw new AmbiguousViewMatcherException.Builder().withViewMatcher(viewMatcher).withRootView(root).withView1(matchedView).withView2(matchedViewIterator.next()).withOtherAmbiguousViews(Iterators.toArray(matchedViewIterator, View.class)).build();
        } else {
            matchedView = matchedViewIterator.next();
        }
    }
    if (null == matchedView) {
        final Predicate<View> adapterViewPredicate = new MatcherPredicateAdapter<View>(ViewMatchers.isAssignableFrom(AdapterView.class));
        List<View> adapterViews = Lists.newArrayList(Iterables.filter(breadthFirstViewTraversal(root), adapterViewPredicate).iterator());
        if (adapterViews.isEmpty()) {
            throw new NoMatchingViewException.Builder().withViewMatcher(viewMatcher).withRootView(root).build();
        }
        String warning = String.format("\nIf the target view is not part of the view hierarchy, you " + "may need to use Espresso.onData to load it from one of the following AdapterViews:%s", Joiner.on("\n- ").join(adapterViews));
        throw new NoMatchingViewException.Builder().withViewMatcher(viewMatcher).withRootView(root).withAdapterViews(adapterViews).withAdapterViewWarning(Optional.of(warning)).build();
    } else {
        return matchedView;
    }
}
Also used : AdapterView(android.widget.AdapterView) NoMatchingViewException(com.google.android.apps.common.testing.ui.espresso.NoMatchingViewException) View(android.view.View) AdapterView(android.widget.AdapterView)

Aggregations

NoMatchingViewException (com.google.android.apps.common.testing.ui.espresso.NoMatchingViewException)3 View (android.view.View)2 UiThreadTest (android.test.UiThreadTest)1 AdapterView (android.widget.AdapterView)1 Espresso.onView (com.google.android.apps.common.testing.ui.espresso.Espresso.onView)1 ViewAssertion (com.google.android.apps.common.testing.ui.espresso.ViewAssertion)1 ViewFinder (com.google.android.apps.common.testing.ui.espresso.ViewFinder)1 AssertionFailedError (junit.framework.AssertionFailedError)1