Reputation: 188
I am learning UI testing with Espresso. I want to test scrolling of recycler view to bottom and only then load next page from view model and pass it recycler view.
I have following onScrollListener in my fragment:
private fun setupOnScrollListener() {
recyclerViewApi.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
val isRecyclerViewBottom = !recyclerView.canScrollVertically(1) &&
newState == RecyclerView.SCROLL_STATE_IDLE
if (isRecyclerViewBottom) {
downloadNextPage()
}
}
})
}
private fun downloadNextPage() {
showProgressBar(true)
viewModel.getNextMovies()
}
When I test it manually with Log.d()
it works great.
My question is: How to use Espresso (or maybe different API, if you know better than Espresso) to scroll recycler view to this state:
isRecyclerViewBottom = !recyclerView.canScrollVertically(1) && newState == RecyclerView.SCROLL_STATE_IDLE
,
so my downloadNextPage()
will be invoked and test function will pull more data.
My test function:
@Test
fun scrollToBottom_isNextPageLoaded(){
every { repository.getApiMovies(any(), any()) } returns
Flowable.just(Resource.success(moviesList1_5)) andThen
Flowable.just(Resource.success(moviesList1_10))
val scenario = launchFragmentInContainer<ApiFragment>(factory = fragmentsFactory)
//first 5 items are in view, so I go to the last item (index 4)
recyclerView.perform(scrollToPosition<ViewHolder>(4))
recyclerView.perform(swipeDown())
//Below doesn't make any difference
Thread.sleep(1000L)
verify(exactly = 2) { repo.getApiMovies(any(), any()) }
}
I use Robolectric, Mockk, Espresso. I have mocked here repository class, which is passed to constructor of ViewModelFactory, which is passed to constructor of the ApiFragment.
Message from JUnit:
java.lang.AssertionError: Verification failed: call 1 of 1: ApiRepository(repo#4).getApiMovies(any(), any())).
One matching call found, but needs at least 2 and at most 2 calls
Call: ApiRepository(repo#4).getApiMovies(Top Rated, 1)
It is not my first test function. Everything else works great. I just don't know how to make Espresso to go to bottom of recycler view and 'pull up' bottom edge of it to call downloadNextPage()
Upvotes: 1
Views: 1743
Reputation: 188
Ok. I have just found a sollution. I changed recyclerView.perform(swipeDown())
to recyclerView.perform(swipeUp())
.
Upvotes: 2