如何使用espress编写用于recyclerview拖放的测试用例

问题描述 投票:1回答:1

我正在为Recyclerview编写测试用例。我在recyclerview中受拖放测试的困扰。我尝试了某种方式。但是无法为recyclerview编写拖放测试。

在recyclerview中,我有拖动按钮。使用该按钮,我将项目拖放到其他位置。

我的代码从下往上滑动

onView(withId(R.id.recyclerView)).perform(
                    RecyclerViewActions.actionOnItemAtPosition(0, new GeneralSwipeAction(
                            Swipe.SLOW, GeneralLocation.BOTTOM_CENTER, GeneralLocation.TOP_CENTER,
                            Press.FINGER)));

此代码在recyclerview中滑动。

所以我正在这样尝试:

onView(withId(R.id.recyclerView)).perform(
                RecyclerViewActions.actionOnItemAtPosition(0,
                        MyViewAction.clickChildViewWithId(R.id.img_drag)),new GeneralSwipeAction(
                        Swipe.SLOW, GeneralLocation.TOP_RIGHT, GeneralLocation.CENTER_RIGHT,
                        Press.FINGER));

但这对我来说不是锻炼。请让我有任何想法来测试拖放功能。

android testing android-recyclerview android-espresso
1个回答
0
投票

您编写的代码首先单击拖动手柄,然后从第一个项目的右上方滑动到其中心右方。如果要拖放项目,则需要几个自定义CoordinatesProvider

  1. [RecyclerViewCoordinatesProvider将在RecyclerView中找到项目的坐标:
class RecyclerViewCoordinatesProvider(private val position: Int, private val childItemCoordinatesProvider: CoordinatesProvider) : CoordinatesProvider {
    override fun calculateCoordinates(view: View): FloatArray {
        return childItemCoordinatesProvider.calculateCoordinates((view as RecyclerView).layoutManager!!.findViewByPosition(position)!!)
    }
}
  1. [ChildViewCoordinatesProvider将找到具有给定id的孩子的坐标:
class ChildViewCoordinatesProvider(private val childViewId: Int, private val insideChildViewCoordinatesProvider: CoordinatesProvider) : CoordinatesProvider {
    override fun calculateCoordinates(view: View): FloatArray {
        return insideChildViewCoordinatesProvider.calculateCoordinates(view.findViewById<View>(childViewId))
    }
}
  1. 最后,我们将创建新的自定义常规位置,该位置将在View的上方和下方提供坐标(我假设我们正在LinearLayoutManager中重新排序):
enum class CustomGeneralLocation : CoordinatesProvider {
    UNDER_RIGHT {
        override fun calculateCoordinates(view: View): FloatArray {
            val screenLocation = IntArray(2)
            view.getLocationOnScreen(screenLocation)
            return floatArrayOf(screenLocation[0] + view.width - 1f, screenLocation[1] + view.height * 1.5f)
        }
    },
    ABOVE_RIGHT {
        override fun calculateCoordinates(view: View): FloatArray {
            val screenLocation = IntArray(2)
            view.getLocationOnScreen(screenLocation)
            return floatArrayOf(screenLocation[0] + view.width - 1f, screenLocation[1] - view.height * 0.5f)
        }

    }
}

现在,如果要拖动项目from的原始位置toRecyclerView的ID为server_list且拖动手柄的ID为drag_handle的目标位置,请使用以下方法:

private fun reorder(from: Int, to: Int) {
        onView(withId(R.id.server_list)).perform(GeneralSwipeAction(
                Swipe.SLOW,
                RecyclerViewCoordinatesProvider(from, ChildViewCoordinatesProvider(R.id.drag_handle, GeneralLocation.CENTER)),
                RecyclerViewCoordinatesProvider(to, if (from < to) CustomGeneralLocation.UNDER_RIGHT else CustomGeneralLocation.ABOVE_RIGHT),
                Press.FINGER))
    }
© www.soinside.com 2019 - 2024. All rights reserved.