在 Espresso 测试中断言 ProgressBar 的进度
Assert progress of ProgressBar in Espresso test
我有一个 ProgressBar
,它会随着我的用户完成一系列操作而填写。
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:indeterminate="false"
android:max="100"
android:progress="50"
... />
我想用 JUnit 和 Espresso 编写一个测试,它在不同的步骤断言 progress
属性的值。像这样...
onView(withId(R.id.progressBar)).check(matches(withProgress(50)));
似乎不存在与 withProgress
方法类似的东西。断言进度的方法是什么?
Espresso 似乎不允许 check
使用 ViewInteraction
的进度属性。虽然,可以直接从视图中恢复进度条视图并使用 assertThat
.
断言其值
@Test
public void ExampleAssertProgressBar(){
ProgressBar progressBar = activity.getActivity().findViewById(R.id.progressBar);
int progress = progressBar.getProgress();
assertThat(progress, equalTo(50));
}
由于这非常简单,我认为这是可行的方法。
以防万一有人需要有关如何操作的其他示例或需要断言 MaxProgress、颜色和进度。我的答案是在 Kotlin 中,我从这个 Java 答案中分叉出来:.
fun matchesProgressBarTintAndMaxProgress(
expectedResourceId: ColorStateList?,
maxExpected: Int,
progressExpected: Int?
): Matcher<View?>? {
return object : BoundedMatcher<View?, View>(View::class.java) {
var actualColor: ColorStateList? = null
var actualMaxProgress = 0
var actualProgress = 0
var message: String? = null
override fun matchesSafely(item: View): Boolean {
actualColor = (item as ProgressBar).progressTintList
actualMaxProgress = item.max
actualProgress = item.progress
return actualColor == expectedResourceId && actualMaxProgress == maxExpected && actualProgress == progressExpected
}
override fun describeTo(description: Description) {
message = ("Tint color match: ${actualColor == expectedResourceId} "
+ "MaxProgress match: ${actualMaxProgress == maxExpected} "
+ "ActualProgress match: ${actualProgress == progressExpected}")
description.appendText(message)
}
}
}
使用方法:
onView(withId(R.id.progressBar)).check(
matches(
matchesProgressBarTintAndMaxProgress(
ColorStateList.valueOf(
ContextCompat.getColor(
context,
R.color.red
)
),
100,
50
)
)
)
我有一个 ProgressBar
,它会随着我的用户完成一系列操作而填写。
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:indeterminate="false"
android:max="100"
android:progress="50"
... />
我想用 JUnit 和 Espresso 编写一个测试,它在不同的步骤断言 progress
属性的值。像这样...
onView(withId(R.id.progressBar)).check(matches(withProgress(50)));
似乎不存在与 withProgress
方法类似的东西。断言进度的方法是什么?
Espresso 似乎不允许 check
使用 ViewInteraction
的进度属性。虽然,可以直接从视图中恢复进度条视图并使用 assertThat
.
@Test
public void ExampleAssertProgressBar(){
ProgressBar progressBar = activity.getActivity().findViewById(R.id.progressBar);
int progress = progressBar.getProgress();
assertThat(progress, equalTo(50));
}
由于这非常简单,我认为这是可行的方法。
以防万一有人需要有关如何操作的其他示例或需要断言 MaxProgress、颜色和进度。我的答案是在 Kotlin 中,我从这个 Java 答案中分叉出来:
fun matchesProgressBarTintAndMaxProgress(
expectedResourceId: ColorStateList?,
maxExpected: Int,
progressExpected: Int?
): Matcher<View?>? {
return object : BoundedMatcher<View?, View>(View::class.java) {
var actualColor: ColorStateList? = null
var actualMaxProgress = 0
var actualProgress = 0
var message: String? = null
override fun matchesSafely(item: View): Boolean {
actualColor = (item as ProgressBar).progressTintList
actualMaxProgress = item.max
actualProgress = item.progress
return actualColor == expectedResourceId && actualMaxProgress == maxExpected && actualProgress == progressExpected
}
override fun describeTo(description: Description) {
message = ("Tint color match: ${actualColor == expectedResourceId} "
+ "MaxProgress match: ${actualMaxProgress == maxExpected} "
+ "ActualProgress match: ${actualProgress == progressExpected}")
description.appendText(message)
}
}
}
使用方法:
onView(withId(R.id.progressBar)).check(
matches(
matchesProgressBarTintAndMaxProgress(
ColorStateList.valueOf(
ContextCompat.getColor(
context,
R.color.red
)
),
100,
50
)
)
)