1

I have a ProgressBar which fills in as my user progresses through a sequence of actions.

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:indeterminate="false"
    android:max="100"
    android:progress="50"
    ... />

I would like write a test with JUnit and Espresso which asserts the value of the progress attributes at different steps. Something like this...

onView(withId(R.id.progressBar)).check(matches(withProgress(50)));

Nothing similar to the withProgress method seems to exist. What would be a way to assert the progress?

Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73

2 Answers2

3

It would seem Espresso does not allow to check the progress attribute using a ViewInteraction. Although, it is possible to recover the progress bar view directly from the view and to assert its value using assertThat.

@Test
public void ExampleAssertProgressBar(){
    ProgressBar progressBar = activity.getActivity().findViewById(R.id.progressBar);
    int progress = progressBar.getProgress();
    assertThat(progress, equalTo(50));
}

Since this is pretty straightforward, I assume it is the way to go.

Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73
0

In case anyone needs other example on how to do it or need to assert MaxProgress, color and Progress. My answer is in Kotlin, I forked from this Java answer: https://stackoverflow.com/a/58964772/11793333.

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

To use it:

            onView(withId(R.id.progressBar)).check(
                matches(
                    matchesProgressBarTintAndMaxProgress(
                        ColorStateList.valueOf(
                            ContextCompat.getColor(
                                context,
                                R.color.red
                            )
                        ),
                        100,
                        50
                    )
                )
            )
Henrique Vasconcellos
  • 1,144
  • 1
  • 8
  • 13