这里是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 中查看文字大小?
答案 0 :(得分:1)
只需将您的尺寸(以像素为单位)转换为比例像素即可。所以,你的匹配器应该是这样的:
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);
}
};
}
答案 1 :(得分:0)
你可以使用这个类来获得字体大小的等效性
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))));
打勾的答案和这个答案之间的唯一区别是它使用内部方法来获取浮点值。