Search in sources :

Example 41 with TypeSafeMatcher

use of org.hamcrest.TypeSafeMatcher in project MultiType by drakeet.

the class RecyclerViewMatcher method atPositionOnView.

public Matcher<View> atPositionOnView(final int position, final int targetViewId) {
    return new TypeSafeMatcher<View>() {

        Resources resources = null;

        View childView;

        public void describeTo(Description description) {
            String idDescription = Integer.toString(recyclerViewId);
            if (this.resources != null) {
                try {
                    idDescription = this.resources.getResourceName(recyclerViewId);
                } catch (Resources.NotFoundException var4) {
                    idDescription = String.format("%s (resource name not found)", recyclerViewId);
                }
            }
            description.appendText("with id: " + idDescription);
        }

        public boolean matchesSafely(View view) {
            this.resources = view.getResources();
            if (childView == null) {
                RecyclerView recyclerView = view.getRootView().findViewById(recyclerViewId);
                if (recyclerView != null && recyclerView.getId() == recyclerViewId) {
                    childView = recyclerView.findViewHolderForAdapterPosition(position).itemView;
                } else {
                    return false;
                }
            }
            if (targetViewId == -1) {
                return view == childView;
            } else {
                View targetView = childView.findViewById(targetViewId);
                return view == targetView;
            }
        }
    };
}
Also used : TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) RecyclerView(android.support.v7.widget.RecyclerView) Resources(android.content.res.Resources) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View)

Example 42 with TypeSafeMatcher

use of org.hamcrest.TypeSafeMatcher in project kickmaterial by byoutline.

the class CustomMatchers method withErrorSet.

public static Matcher<View> withErrorSet(@NonNull final String expected) {
    if (expected == null) {
        throw new AssertionError("Null string passed");
    }
    return new TypeSafeMatcher<View>() {

        @Override
        public boolean matchesSafely(View view) {
            if (!(view instanceof EditText)) {
                return false;
            }
            EditText editText = (EditText) view;
            CharSequence error = editText.getError();
            if (error == null) {
                return false;
            }
            return expected.equals(error.toString());
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("view should have error: ").appendValue(expected).appendText(" set");
        }
    };
}
Also used : EditText(android.widget.EditText) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) View(android.view.View)

Example 43 with TypeSafeMatcher

use of org.hamcrest.TypeSafeMatcher in project material-components-android by material-components.

the class TestUtilsMatchers method isActionViewOf.

/**
   * Returns a matcher that matches the action view of the specified menu item.
   *
   * @param menu The menu
   * @param id The ID of the menu item
   */
public static Matcher<View> isActionViewOf(@NonNull final Menu menu, @IdRes final int id) {
    return new TypeSafeMatcher<View>() {

        private Resources resources;

        @Override
        protected boolean matchesSafely(View view) {
            resources = view.getResources();
            MenuItemImpl item = (MenuItemImpl) menu.findItem(id);
            return item != null && item.getActionView() == view;
        }

        @Override
        public void describeTo(Description description) {
            String name;
            if (resources != null) {
                name = resources.getResourceName(id);
            } else {
                name = Integer.toString(id);
            }
            description.appendText("is action view of menu item " + name);
        }
    };
}
Also used : MenuItemImpl(android.support.v7.view.menu.MenuItemImpl) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) Resources(android.content.res.Resources) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView)

Example 44 with TypeSafeMatcher

use of org.hamcrest.TypeSafeMatcher in project material-components-android by material-components.

the class TestUtilsMatchers method isChildOfA.

/**
   * Returns a matcher that matches {@link View}s based on the given parent type.
   *
   * @param parentMatcher the type of the parent to match on
   */
public static Matcher<View> isChildOfA(final Matcher<View> parentMatcher) {
    return new TypeSafeMatcher<View>() {

        @Override
        public void describeTo(Description description) {
            description.appendText("is child of a: ");
            parentMatcher.describeTo(description);
        }

        @Override
        public boolean matchesSafely(View view) {
            final ViewParent viewParent = view.getParent();
            if (!(viewParent instanceof View)) {
                return false;
            }
            if (parentMatcher.matches(viewParent)) {
                return true;
            }
            return false;
        }
    };
}
Also used : TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) ViewParent(android.view.ViewParent) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView)

Example 45 with TypeSafeMatcher

use of org.hamcrest.TypeSafeMatcher in project neo4j by neo4j.

the class QueryResultsSerializationTest method restContainsNestedDeleted.

/**
     * This matcher is hardcoded to check for a list containing one deleted node and one map with a deleted node mapped to the key `someKey`.
     */
public static Matcher<? super Response> restContainsNestedDeleted() {
    return new TypeSafeMatcher<Response>() {

        @Override
        protected boolean matchesSafely(HTTP.Response response) {
            try {
                JsonNode list = TransactionMatchers.getJsonNodeWithName(response, "rest").iterator().next();
                assertThat(list.get(0).get("metadata").get("deleted").asBoolean(), equalTo(Boolean.TRUE));
                assertThat(list.get(1).get("someKey").get("metadata").get("deleted").asBoolean(), equalTo(Boolean.TRUE));
                return true;
            } catch (JsonParseException e) {
                return false;
            }
        }

        @Override
        public void describeTo(Description description) {
        }
    };
}
Also used : Response(org.neo4j.test.server.HTTP.Response) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) JsonNode(org.codehaus.jackson.JsonNode) JsonParseException(org.neo4j.server.rest.domain.JsonParseException)

Aggregations

TypeSafeMatcher (org.hamcrest.TypeSafeMatcher)82 Description (org.hamcrest.Description)79 View (android.view.View)44 ViewParent (android.view.ViewParent)33 ViewGroup (android.view.ViewGroup)30 Espresso.onView (android.support.test.espresso.Espresso.onView)25 TextView (android.widget.TextView)9 Test (org.junit.Test)9 ViewMatchers.withContentDescription (android.support.test.espresso.matcher.ViewMatchers.withContentDescription)7 JsonNode (org.codehaus.jackson.JsonNode)6 JsonParseException (org.neo4j.server.rest.domain.JsonParseException)6 HTTP (org.neo4j.test.server.HTTP)6 Resources (android.content.res.Resources)5 StringDescription (org.hamcrest.StringDescription)5 AdapterView (android.widget.AdapterView)4 RecyclerView (android.support.v7.widget.RecyclerView)3 Adapter (android.widget.Adapter)3 Espresso.onView (com.google.android.apps.common.testing.ui.espresso.Espresso.onView)3 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3