Created
December 4, 2017 09:44
-
-
Save JoachimR/49f32d951007196d5f7e33e972c47e82 to your computer and use it in GitHub Desktop.
Android Espresso SwipeToRefreshLayout assert isRefreshing state
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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