使用Espresso检查EditText的字体大小,高度和宽度

时间:2017-11-20 18:20:47

标签: android android-edittext android-espresso

如何使用Espresso检查EditText的字体大小,高度和宽度?

目前我要使用的文字:

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

阅读文字:

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

2 个答案:

答案 0 :(得分:1)

您必须创建自己的自定义匹配器,因为默认情况下Espresso不支持任何这些匹配器。

幸运的是,这可以很容易地完成。看一下这个例子的字体大小:

public class FontSizeMatcher extends TypeSafeMatcher<View> {

    private final float expectedSize;

    public FontSizeMatcher(float expectedSize) {
        super(View.class);
        this.expectedSize = expectedSize;
    }

    @Override
    protected boolean matchesSafely(View target) {
        if (!(target instanceof TextView)){
            return false;
        }
        TextView targetEditText = (TextView) target;
        return targetEditText.getTextSize() == expectedSize;
    }


    @Override
    public void describeTo(Description description) {
        description.appendText("with fontSize: ");
        description.appendValue(expectedSize);
    }

}

然后像这样创建一个入口点:

public static Matcher<View> withFontSize(final float fontSize) {
    return new FontSizeMatcher(fontSize);
}

并像这样使用它:

onView(withId(R.id.editText1)).check(matches(withFontSize(36)));

宽度和宽度高度可以用类似的方式完成。

答案 1 :(得分:0)

视图大小匹配器

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);
    }
}

使用

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