Skip to content

Instantly share code, notes, and snippets.

@JoachimR
Created December 4, 2017 09:44
Show Gist options
  • Save JoachimR/49f32d951007196d5f7e33e972c47e82 to your computer and use it in GitHub Desktop.
Save JoachimR/49f32d951007196d5f7e33e972c47e82 to your computer and use it in GitHub Desktop.
Android Espresso SwipeToRefreshLayout assert isRefreshing state
import android.support.annotation.IdRes
import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.assertion.ViewAssertions.matches
import android.support.test.espresso.matcher.ViewMatchers.withId
import android.support.v4.widget.SwipeRefreshLayout
import android.view.View
import org.hamcrest.Description
import org.hamcrest.Matchers.not
import org.hamcrest.TypeSafeMatcher
fun assertSwipeToRefreshState(@IdRes swipeToRefreshResId: Int,
stateShouldBeRefreshing: Boolean) {
onView(withId(swipeToRefreshResId))
.check(matches(
if (stateShouldBeRefreshing) {
isRefreshing()
} else {
not(isRefreshing())
}
))
}
private fun isRefreshing() = object : TypeSafeMatcher<View>() {
override fun describeTo(description: Description) {
description.appendText("is a SwipeRefreshLayout that is currently refreshing")
}
override fun matchesSafely(item: View?) =
(item as? SwipeRefreshLayout)?.isRefreshing ?: false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment