在我的Android项目中,我具有自定义匹配器:
fun withFontSizeValue(expectedValueSp: Float): Matcher<View> {
return object : BoundedMatcher<View, View>(View::class.java) {
public override fun matchesSafely(view: View): Boolean {
if (view !is TextView) {
return false
}
val textView = view as TextView
val actualSizelsPx = textView.textSize
//float actualSizeSp = actualSizelsPx / view.getResources().getDisplayMetrics().scaledDensity;
val actualSizeSp = AndroidUtil.px2Sp(view.getContext(), actualSizelsPx)
return java.lang.Float.compare(actualSizeSp, expectedValueSp) == 0
}
override fun describeTo(description: Description) {
description.appendText("with fontSize: ")
description.appendValue(expectedValueSp)
}
}
}
我的xml
<TextView
android:id="@+id/toolbaTitleTextView"
style="@style/textViewOneLine"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="@{title}"
android:textAllCaps="true"
android:textColor="@android:color/white"
android:textSize="@dimen/tool_bar_text_size"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/toolBar"
app:layout_constraintEnd_toEndOf="@+id/toolBar"
app:layout_constraintStart_toStartOf="@+id/toolBar"
app:layout_constraintTop_toTopOf="@+id/toolBar" />
并用于检查TextView的textSize
:
onView(withId(R.id.toolbaTitleTextView)).check(matches(withFontSizeValue(13f)))
好。很好。
但是我需要像这样使用smt:
onView(withId(R.id.toolbaTitleTextView)).check(matches(withFontSizeValue(R.dimen.tool_bar_text_size)))
在dimen.xml中
<dimen name="tool_bar_text_size">13sp</dimen>
如何进行此检查?由R.dimen.tool_bar_text_size