Search code examples
androidandroid-recyclerviewandroid-espressoui-testing

Why test not fail when swipeRight on RecyclerView?


Here my xml with RecyclerView

<androidx.recyclerview.widget.RecyclerView
   android:id="@+id/tradersRecyclerView"
   android:layout_width="0dp"
   android:layout_height="0dp"
   android:visibility="@{handler.tradersList.size > 0 ? View.VISIBLE : View.GONE}"
   app:layout_constraintBottom_toBottomOf="parent"
   app:layout_constraintEnd_toEndOf="parent"
   app:layout_constraintStart_toStartOf="parent"
   app:layout_constraintTop_toBottomOf="@+id/tradersToolBar"        
   tools:listitem="@layout/trader_list_item" />

When swiping left on list's item than I show sub items (Resume, Stop)

Something like this:

enter image description here

The sub-items show only when swipe left. When swipe right nothing happens.

Here Espresso's test that check that swipeLeft is work

 @Test
 fun scroll_itemList_swipeLeft() {
    // scroll
    onView(withId(R.id.tradersRecyclerView))
                .perform(RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(checkItemCount));
    // swipe
    onView(withId(R.id.tradersRecyclerView))
                .perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(checkItemCount, swipeLeft()))
 }

Nice. The test passes.

Now I create a test that must fail when swipe right. Because when swipe right nothing happens

@Test
fun scroll_itemList_notSwipeRight() {
     // scroll
     onView(withId(R.id.tradersRecyclerView))
                .perform(RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(checkItemCount));
     // swipe
     onView(withId(R.id.tradersRecyclerView))
                .perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(checkItemCount, ViewActions.swipeRight()))
 }

but this test also success pass. Why? I need create test that check than swipe right nothing happens

P.S. Here work tests when swipe left and swipe right

@Test
fun itemList_swipeLeft_isDisplayedSwipeContainer() {
    onView(withId(R.id.tradersRecyclerView))
            .perform(RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(checkItemCount));
    onView(withId(R.id.tradersRecyclerView))
            .perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(checkItemCount, swipeLeft()))
    onView(withRecyclerView(R.id.tradersRecyclerView).atPositionOnView(checkItemCount, R.id.swipeContainer))
            .check(matches(isDisplayed()))
}

@Test
fun itemList_swipeRight_notDisplayedSwipeContainer() {
    onView(withId(R.id.tradersRecyclerView))
            .perform(scrollToPosition<RecyclerView.ViewHolder>(checkItemCount));
    onView(withId(R.id.tradersRecyclerView))
            .perform(actionOnItemAtPosition<RecyclerView.ViewHolder>(checkItemCount, ViewActions.swipeRight()))
    onView(withRecyclerView(R.id.tradersRecyclerView).atPositionOnView(checkItemCount, R.id.swipeContainer))
            .check(matches(not(isDisplayed())))
}

Solution

  • Add something like this to your testes (both of them):

    onView(withId(R.id.*your_resume_button_id*)).check(mathes(isDisplayed()))
    

    This will add a check on visibility for a view that should only be visible when swiping to left.

    So the first test will pass and the second will fail as this view won't be visible when swiping to right.

    Hope that helps.