Search in sources :

Example 81 with TypeSafeMatcher

use of org.hamcrest.TypeSafeMatcher in project double-espresso by JakeWharton.

the class ViewMatchers method hasSibling.

/**
 * Returns an {@link Matcher} that matches {@link View}s based on their siblings.<br>
 * <br>
 * 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).
 *
 * @param siblingMatcher a {@link Matcher} for the sibling of the view.
 */
public static Matcher<View> hasSibling(final Matcher<View> siblingMatcher) {
    checkNotNull(siblingMatcher);
    return new TypeSafeMatcher<View>() {

        @Override
        public void describeTo(Description description) {
            description.appendText("has sibling: ");
            siblingMatcher.describeTo(description);
        }

        @Override
        public boolean matchesSafely(View view) {
            ViewParent parent = view.getParent();
            if (!(parent instanceof ViewGroup)) {
                return false;
            }
            ViewGroup parentGroup = (ViewGroup) parent;
            for (int i = 0; i < parentGroup.getChildCount(); i++) {
                if (siblingMatcher.matches(parentGroup.getChildAt(i))) {
                    return true;
                }
            }
            return false;
        }
    };
}
Also used : TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) StringDescription(org.hamcrest.StringDescription) ViewParent(android.view.ViewParent) ViewGroup(android.view.ViewGroup) TextView(android.widget.TextView) View(android.view.View)

Example 82 with TypeSafeMatcher

use of org.hamcrest.TypeSafeMatcher in project double-espresso by JakeWharton.

the class ViewMatchers method withChild.

/**
 * A matcher that returns true if and only if the view's child is accepted by the provided
 * matcher.
 *
 * @param childMatcher the matcher to apply on the child views.
 */
public static Matcher<View> withChild(final Matcher<View> childMatcher) {
    checkNotNull(childMatcher);
    return new TypeSafeMatcher<View>() {

        @Override
        public void describeTo(Description description) {
            description.appendText("has child: ");
            childMatcher.describeTo(description);
        }

        @Override
        public boolean matchesSafely(View view) {
            if (!(view instanceof ViewGroup)) {
                return false;
            }
            ViewGroup group = (ViewGroup) view;
            for (int i = 0; i < group.getChildCount(); i++) {
                if (childMatcher.matches(group.getChildAt(i))) {
                    return true;
                }
            }
            return false;
        }
    };
}
Also used : TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) StringDescription(org.hamcrest.StringDescription) ViewGroup(android.view.ViewGroup) TextView(android.widget.TextView) View(android.view.View)

Example 83 with TypeSafeMatcher

use of org.hamcrest.TypeSafeMatcher in project double-espresso by JakeWharton.

the class RootMatchers method isDialog.

/**
 * Matches {@link Root}s that are dialogs (i.e. is not a window of the currently resumed
 * activity).
 */
public static Matcher<Root> isDialog() {
    return new TypeSafeMatcher<Root>() {

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

        @Override
        public boolean matchesSafely(Root root) {
            int type = root.getWindowLayoutParams().get().type;
            if ((type != WindowManager.LayoutParams.TYPE_BASE_APPLICATION && type < WindowManager.LayoutParams.LAST_APPLICATION_WINDOW)) {
                IBinder windowToken = root.getDecorView().getWindowToken();
                IBinder appToken = root.getDecorView().getApplicationWindowToken();
                if (windowToken == appToken) {
                    // therefore it must be a dialog box.
                    return true;
                }
            }
            return false;
        }
    };
}
Also used : IBinder(android.os.IBinder) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) Root(com.google.android.apps.common.testing.ui.espresso.Root)

Example 84 with TypeSafeMatcher

use of org.hamcrest.TypeSafeMatcher in project double-espresso by JakeWharton.

the class DataInteraction method displayingData.

private Matcher<View> displayingData(final Matcher<View> adapterMatcher, final Matcher<Object> dataMatcher, final AdapterViewProtocol adapterViewProtocol, final AdapterDataLoaderAction adapterDataLoaderAction) {
    checkNotNull(adapterMatcher);
    checkNotNull(dataMatcher);
    checkNotNull(adapterViewProtocol);
    return new TypeSafeMatcher<View>() {

        @Override
        public void describeTo(Description description) {
            description.appendText(" displaying data matching: ");
            dataMatcher.describeTo(description);
            description.appendText(" within adapter view matching: ");
            adapterMatcher.describeTo(description);
        }

        @SuppressWarnings("unchecked")
        @Override
        public boolean matchesSafely(View view) {
            ViewParent parent = view.getParent();
            while (parent != null && !(parent instanceof AdapterView)) {
                parent = parent.getParent();
            }
            if (parent != null && adapterMatcher.matches(parent)) {
                Optional<AdaptedData> data = adapterViewProtocol.getDataRenderedByView((AdapterView<? extends Adapter>) parent, view);
                if (data.isPresent()) {
                    return adapterDataLoaderAction.getAdaptedData().opaqueToken.equals(data.get().opaqueToken);
                }
            }
            return false;
        }
    };
}
Also used : TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) ViewParent(android.view.ViewParent) AdapterView(android.widget.AdapterView) AdaptedData(com.google.android.apps.common.testing.ui.espresso.action.AdapterViewProtocol.AdaptedData) Espresso.onView(com.google.android.apps.common.testing.ui.espresso.Espresso.onView) View(android.view.View) AdapterView(android.widget.AdapterView)

Example 85 with TypeSafeMatcher

use of org.hamcrest.TypeSafeMatcher in project flink by apache.

the class S3RecoverableFsDataOutputStreamTest method hasContent.

private static TypeSafeMatcher<TestMultipartUpload> hasContent(final Collection<byte[]> expectedContents) throws IOException {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    for (byte[] c : expectedContents) {
        stream.write(c);
    }
    byte[] expectedContent = stream.toByteArray();
    return new TypeSafeMatcher<TestMultipartUpload>() {

        @Override
        protected boolean matchesSafely(TestMultipartUpload testMultipartUpload) {
            return Arrays.equals(testMultipartUpload.getPublishedContents(), expectedContent);
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("a TestMultipartUpload with contents='").appendValue(expectedContent).appendText("'");
        }
    };
}
Also used : TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

TypeSafeMatcher (org.hamcrest.TypeSafeMatcher)121 Description (org.hamcrest.Description)118 View (android.view.View)60 ViewParent (android.view.ViewParent)43 ViewGroup (android.view.ViewGroup)40 Espresso.onView (android.support.test.espresso.Espresso.onView)26 Espresso.onView (androidx.test.espresso.Espresso.onView)11 TextView (android.widget.TextView)10 Test (org.junit.Test)9 Resources (android.content.res.Resources)8 ViewMatchers.withContentDescription (android.support.test.espresso.matcher.ViewMatchers.withContentDescription)8 ArrayList (java.util.ArrayList)6 JsonNode (org.codehaus.jackson.JsonNode)6 JsonParseException (org.neo4j.server.rest.domain.JsonParseException)6 HTTP (org.neo4j.test.server.HTTP)6 RecyclerView (androidx.recyclerview.widget.RecyclerView)5 ViewMatchers.withContentDescription (androidx.test.espresso.matcher.ViewMatchers.withContentDescription)5 List (java.util.List)5 StringDescription (org.hamcrest.StringDescription)5 AdapterView (android.widget.AdapterView)4