Android.Espresso: 如何在SP中查看TextView的文字大小?

Android.Espresso: How check text size of TextView in SP?

这里 xml:

<TextView
            android:id="@+id/toolbaTitleTextView"
            style="@style/textViewOneLine"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:textAllCaps="true"
            android:textColor="@android:color/black"
            android:textSize="13sp"          
           />

我需要编写 Espresso 测试来检查 TextView 的字体大小(在 SP 中)= 13。

所以这里测试一下:

@Test
    public void toolBarTextSize() {
        onView(withId(R.id.toolbaTitleTextView)).check(matches(withFontSize(13)));
    }

这里是匹配器:

public static Matcher<View> withFontSize(final float expectedSize) {
        return new BoundedMatcher<View, View>(View.class) {

            @Override
            public 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);
            }
        };
    }

但是我得到了错误,因为大小 像素

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with fontSize: <13.0F>' doesn't match the selected view.
Expected: with fontSize: <13.0F>
Got: "AppCompatTextView{id=2131296624, res-name=toolbaTitleTextView, visibility=VISIBLE, width=1080, height=168, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.support.constraint.ConstraintLayout$LayoutParams@13f0b, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=Sign in, input-type=0, ime-target=false, has-links=false}"

但我需要 SP。如何检查 SP?

中的文字大小

只需将您的大小(以像素为单位)转换为比例像素即可。所以,你的匹配器应该是这样的:

public static Matcher<View> withFontSize(final float expectedSize) {
    return new BoundedMatcher<View, View>(View.class) {

        @Override
        public boolean matchesSafely(View target) {
            if (!(target instanceof TextView)) {
                return false;
            }
            TextView targetEditText = (TextView) target;
            float pixels = targetEditText.getTextSize();
            float actualSize = pixels / target.getResources().getDisplayMetrics().scaledDensity;
            return Float.compare(actualSize, expectedSize) == 0;
        }

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

您可以使用此 class 来获得等效的字体大小

class FontSizeMatcher extends TypeSafeMatcher<View> {

    private final float mExpectedFontSize;
    private float actualFontSize;

    public FontSizeMatcher(float fontSize) {
        super(View.class);
        mExpectedFontSize = fontSize;
    }

    @Override
    protected boolean matchesSafely(View item) {

        if (!(item instanceof TextView)) {
            return false;
        }

        actualFontSize = ((TextView) item).getTextSize();
        return mExpectedFontSize == actualFontSize;
    }

    @Override
    public void describeTo(Description description) {
        description.appendText("Text Size did not match " + mExpectedFontSize + " was " + actualFontSize);
    }
}

要使用它,

onView(withId(R.id.btn)).check(matches(new FontSizeMatcher(InstrumentationRegistry.getInstrumentation().getTargetContext().getResources().getDimension(R.dimen.font_test))));

打勾的答案和这个唯一的区别是它使用内部方法来获取浮点值。