Teleben
Teleben

Reputation: 33

Espresso swipe gesture test fails on android emulator but passes on real device

I have been using my phone to run instrumented android tests for a long time. Today I thought I would switch to the emulator, but I found that some tests that pass on my phone do not pass on the emulator -- because the swipe gesture does not do the same thing on the emulator as it does on my phone (Xperia Z5 Compact, Android 7.1.1). I am also running the same version of Android in the emulator.

When run on the emulator the ViewActions.swipeLeft() method seems to start the swipe too far to the right and cause that little wavy animation that happens when you drag from the very edge of the screen at an end of a ViewPager. The swipe works totally fine on the phone and emulator when done manually. Hence, I think it must be something to do with Espresso. I can imagine how perhaps the issue could be the swipe listener in a RecyclerView inside a swipe-able ViewPager. But it works fine on the emulator and phone and tests pass on my phone.

This is my swipe handling code, which is added to a RecyclerView inside a ViewPager tab (on the last tab).

fun attachSingleSwipeActionToRecyclerView(
    direction: Int = ItemTouchHelper.LEFT,
    recyclerView: RecyclerView,
    swipeAction: RecyclerView.ViewHolder.() -> Unit
): ItemTouchHelper {
    val simpleCallback = object : ItemTouchHelper.SimpleCallback(0, direction) {
        override fun onMove(
            recyclerView: RecyclerView,
            viewHolder: RecyclerView.ViewHolder,
            target: RecyclerView.ViewHolder
        ): Boolean {
            return false
        }

        override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
            swipeAction(viewHolder)
        }
    }

    val itemTouchHelper = ItemTouchHelper(simpleCallback)
    itemTouchHelper.attachToRecyclerView(recyclerView)
    return itemTouchHelper
}

In my test I am simply calling ViewActions.swipeLeft() on the first RecyclerView item on the screen.

I have tried creating my own start point for the Espresso swipe, even when I made it start at the very center, rather than far right side, nothing changed, including the end of ViewPager wave animation

GeneralSwipeAction(
    Swipe.FAST,
    GeneralLocation.CENTER,
    GeneralLocation.CENTER_LEFT,
    Press.FINGER
)

What could be different about the emulator that causes the espresso swipe to act differently than on a real device?

Upvotes: 2

Views: 1377

Answers (1)

Aaron
Aaron

Reputation: 3894

At times, GeneralSwipeAction can become unreliable because of its calculation varies on different screen size or density, so it may not be suitable for ViewPager in your case. Instead, you could try to scroll with ViewPagerActions:

// Moves ViewPager to the right be one page.
onView(your_view_pager).perform(ViewPagerActions.scrollRight());

// Moves ViewPager to a specific page.
onView(your_view_pager).perform(ViewPagerActions.scrollToPage(page_number));

Upvotes: 4

Related Questions