我正在使用Espresso来测试我的应用程序,我需要在RecyclerView中测试拖放行为。不幸的是,我看不到任何拖拉()动作在浓缩咖啡,我错过了什么?
我没有找到任何Espresso
拖动选项。只有swipeUp()
, swipeDown()
, swipeLwft()
, swipeRight()
,在这种情况下可能没有用处。
此外,在谷歌组有一个问题没有答案:https://groups.google.com/forum/#!味精/android-test-kit-discuss/X3TDJBUT4Ho/OJAOkTOoxGcJ
虽然,也有Espresso MotionEvents
类与sendMovement()
方法,但我还没有尝试过。
使用以下代码:
public static void drag(Instrumentation inst, float fromX, float toX, float fromY,
float toY, int stepCount) {
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
float y = fromY;
float x = fromX;
float yStep = (toY - fromY) / stepCount;
float xStep = (toX - fromX) / stepCount;
MotionEvent event = MotionEvent.obtain(downTime, eventTime,
MotionEvent.ACTION_DOWN, x, y, 0);
inst.sendPointerSync(event);
for (int i = 0; i < stepCount; ++i) {
y += yStep;
x += xStep;
eventTime = SystemClock.uptimeMillis();
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, x, y, 0);
inst.sendPointerSync(event);
}
eventTime = SystemClock.uptimeMillis();
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x, y, 0);
inst.sendPointerSync(event);
inst.waitForIdleSync();
}
还可以阅读TouchUtils
(已从API 24弃用):https://developer.android.com/reference/android/test/TouchUtils.html
希望对大家有所帮助