Check the font size, height, and width of the EditText with Espresso

Viewed 2776

How could I check the font size, height, and width of an EditText with Espresso?

At the moment to sect the text I use:

onView(withId(R.id.editText1)).perform(clearText(), typeText("Amr"));

And to read the text:

onView(withId(R.id.editText1)).check(matches(withText("Amr")));
2 Answers

Matcher for view size

public class ViewSizeMatcher extends TypeSafeMatcher<View> {
    private final int expectedWith;
    private final int expectedHeight;

    public ViewSizeMatcher(int expectedWith, int expectedHeight) {
        super(View.class);
        this.expectedWith = expectedWith;
        this.expectedHeight = expectedHeight;
    }

    @Override
    protected boolean matchesSafely(View target) {
        int targetWidth = target.getWidth();
        int targetHeight = target.getHeight();

        return targetWidth == expectedWith && targetHeight == expectedHeight;
    }

    @Override
    public void describeTo(Description description) {
        description.appendText("with SizeMatcher: ");
        description.appendValue(expectedWith + "x" + expectedHeight);
    }
}

using

onView(withId(R.id.editText1)).check(matches(new ViewSizeMatcher(300, 250)));
Related