如何使用Espresso检查EditText的字体大小,高度和宽度?
目前我要使用的文字:
onView(withId(R.id.editText1)).perform(clearText(), typeText("Amr"));
并阅读文字:
onView(withId(R.id.editText1)).check(matches(withText("Amr")));
您必须创建自己的自定义匹配器,因为默认情况下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)));
对于宽度和高度,可以以类似的方式完成。
匹配视图大小
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)));