nicky kumari
nicky kumari

Reputation: 61

How to write Robolectric unit test to test swipe left of a recycler view item

*I am writing unit test case to test swipe left of a recyclerview item. I tried with this code. But this is not working

Code Snippet:


float x = 0.0f;
float y = 0.0f;

int metaState = 0;
MotionEvent event = MotionEvent.obtain(
        downTime,
        eventTime,
        MotionEvent.ACTION_DOWN,
        x,
        y,
        metaState
);

MotionEvent moveEvent = MotionEvent.obtain(
        downTime,
        eventTime,
        MotionEvent.ACTION_MOVE,
        30,
        y,
        metaState
);
MotionEvent upEvent = MotionEvent.obtain(
        downTime,
        eventTime,
        MotionEvent.ACTION_UP,
        x,
        y,
        metaState
);

recylerView.findViewHolderForAdapterPosition(0).itemView.findViewById(R.id.regular_card).dispatchTouchEvent(event);
recylerView.findViewHolderForAdapterPosition(0).itemView.findViewById(R.id.regular_card).dispatchTouchEvent(moveEvent);
recylerView.findViewHolderForAdapterPosition(0).itemView.findViewById(R.id.regular_card).dispatchTouchEvent(upEvent);*

Upvotes: 2

Views: 563

Answers (1)

Oleksandr Albul
Oleksandr Albul

Reputation: 1741

This extension functions could help you:

fun ViewGroup.performSwipeToLeft(target: View) {
    this.performSwipe(target, distanceX = -this.width * .5f, distanceY = 0f)
}
fun ViewGroup.performSwipeToRight(target: View) {
    this.performSwipe(target, distanceX = +this.width * .5f, distanceY = 0f)
}
fun ViewGroup.performSwipeToTop(target: View) {
    this.performSwipe(target, distanceX = 0f, distanceY = -this.height * .5f)
}
fun ViewGroup.performSwipeToBottom(target: View) {
    this.performSwipe(target, distanceX = 0f, distanceY = +this.width * .5f)
}

fun ViewGroup.performSwipe(target: View, distanceX: Float, distanceY: Float) {
    val parentCoords = intArrayOf(0, 0)
    this.getLocationInWindow(parentCoords)

    val childCoords = intArrayOf(0, 0)
    target.getLocationInWindow(childCoords)

    val initGlobalX = childCoords[0].toFloat() + 1f
    val initGlobalY = childCoords[1].toFloat() + 1f

    val initLocalX = (childCoords[0] - parentCoords[0]).toFloat() + 1f
    val initLocalY = (childCoords[1] - parentCoords[1]).toFloat() + 1f

    val downTime = SystemClock.uptimeMillis()
    var eventTime = SystemClock.uptimeMillis()

    this.dispatchTouchEvent(
        MotionEvent.obtain(
            downTime,
            eventTime,
            MotionEvent.ACTION_DOWN,
            initGlobalX,
            initGlobalY,
            0
        ).apply {
            setLocation(initLocalX, initLocalY)
            source = InputDevice.SOURCE_TOUCHSCREEN
        }
    )

    val steps = 20
    var i = 0
    while (i in 0..steps) {
        val globalX = initGlobalX + i * distanceX / steps
        val globalY = initGlobalY + i * distanceY / steps
        val localX = initLocalX + i * distanceX / steps
        val localY = initLocalY + i * distanceY / steps
        if (globalX <= 10f || globalY <= 10f) {
            break
        }
        this.dispatchTouchEvent(
            MotionEvent.obtain(
                downTime,
                ++eventTime,
                MotionEvent.ACTION_MOVE,
                globalX,
                globalY,
                0
            ).apply {
                setLocation(localX, localY)
                source = InputDevice.SOURCE_TOUCHSCREEN
            }
        )
        i++
    }

    this.dispatchTouchEvent(
        MotionEvent.obtain(
            downTime,
            ++eventTime,
            MotionEvent.ACTION_UP,
            initGlobalX + i * distanceX,
            initGlobalY + i * distanceY,
            0
        ).apply {
            setLocation(initLocalX + i * distanceX, initLocalY + i * distanceY)
            source = InputDevice.SOURCE_TOUCHSCREEN
        }
    )
}

In your case you could use this as follows:

recylerView.performSwipeToLeft(recyclerView.getChildAt(0))

Also swipe animation could be tied with SystemClock.uptimeMillis(). So you need to add @LooperMode(LooperMode.Mode.PAUSED) before your test, and use ShadowSystemClock.advanceBy In my case it was:

@Test
    @LooperMode(LooperMode.Mode.PAUSED)
    fun swipeItem_itemDeleted() {
        ......
        listView.performSwipeToLeft(listView.getChildAt(1))
        while (!ShadowLooper.getShadowMainLooper().isIdle) {
            ShadowSystemClock.advanceBy(Duration.ofMillis(5))
            try {
                ShadowLooper.runMainLooperOneTask()
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

        assertEquals(... )
    }

Upvotes: 1

Related Questions