Search in sources :

Example 1 with TypefaceEditText

use of com.waz.zclient.ui.text.TypefaceEditText in project wire-android by wireapp.

the class CustomMatchers method guidedEditTextWithId.

/**
     * Typing with regular {@link android.support.test.espresso.matcher.ViewMatchers#withId(int)} matchers won't work on a
     * GuidedEditText (GET) because the GET hides the EditText which Espresso needs. This matcher searches for an edit
     * text under a grandparent with the given id that has the type GET
     * @param id the id of the GuidedEditText which we want to type (or perform other actions) into
     * @return an Espresso compatible Matcher<View> for use in onView();
     */
public static Matcher<View> guidedEditTextWithId(final int id) {
    return new TypeSafeMatcher<View>() {

        Resources resources = null;

        @Override
        public void describeTo(Description description) {
            String idDescription = Integer.toString(id);
            if (resources != null) {
                try {
                    idDescription = resources.getResourceName(id);
                } catch (Resources.NotFoundException e) {
                    // No big deal, will just use the int value.
                    idDescription = String.format("%s (resource name not found)", id);
                }
            }
            description.appendText("with id: " + idDescription);
        }

        @Override
        public boolean matchesSafely(View view) {
            resources = view.getResources();
            GuidedEditText grandParent = getGrandParent(view);
            return grandParent != null && id == grandParent.getId() && view instanceof TypefaceEditText;
        }

        private GuidedEditText getGrandParent(View view) {
            ViewParent parent = view.getParent();
            if (!(parent instanceof FrameLayout)) {
                return null;
            }
            ViewParent grandParent = parent.getParent();
            if (!(grandParent instanceof GuidedEditText)) {
                return null;
            }
            return (GuidedEditText) grandParent;
        }
    };
}
Also used : TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) ViewParent(android.view.ViewParent) TypefaceEditText(com.waz.zclient.ui.text.TypefaceEditText) FrameLayout(android.widget.FrameLayout) Resources(android.content.res.Resources) GuidedEditText(com.waz.zclient.pages.main.profile.views.GuidedEditText) View(android.view.View)

Aggregations

Resources (android.content.res.Resources)1 View (android.view.View)1 ViewParent (android.view.ViewParent)1 FrameLayout (android.widget.FrameLayout)1 GuidedEditText (com.waz.zclient.pages.main.profile.views.GuidedEditText)1 TypefaceEditText (com.waz.zclient.ui.text.TypefaceEditText)1 Description (org.hamcrest.Description)1 TypeSafeMatcher (org.hamcrest.TypeSafeMatcher)1