本文介绍了使用Espresso检查EditText的字体大小,高度和宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

此刻我要使用的文本:

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

并阅读文字:

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

推荐答案

由于Espresso默认不支持任何这些匹配器,因此您将必须创建自己的自定义匹配器.

You will have to create your own custom matchers since Espresso does not support any of these matchers by default.

幸运的是,这很容易做到.看一下此示例中的字体大小:

Luckily this can be done quite easily. Take a look at this example for font size:

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

}

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

Then create an entrypoint like so:

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

并像这样使用它:

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

对于宽度&高度可以用类似的方式完成.

For width & height it can be done in a similar manner.

这篇关于使用Espresso检查EditText的字体大小,高度和宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 23:41