测试双击事件

时间:2012-10-09 15:30:57

标签: android junit

我想知道是否可以模拟doulbe tap事件。我试过了

TouchUtils.clickView(...) 

连续两次,但没有奏效。我也试着用这种方式:

    long downTime = SystemClock.uptimeMillis();
    long eventTime = SystemClock.uptimeMillis();

    eventTime +=200;
    downTime +=200;

    Instrumentation instrumentation = getInstrumentation();

    MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, mainview.getWidth()/2, mainview.getHeight()/2, 0);
    instrumentation.sendPointerSync(event);

    event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, mainview.getWidth()/2, mainview.getHeight()/2, 0);
    instrumentation.sendPointerSync(event);

    downTime = SystemClock.uptimeMillis();
    eventTime = SystemClock.uptimeMillis();

    eventTime +=200;
    downTime += 200;

    event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, mainview.getWidth()/2, mainview.getHeight()/2, 0);
    instrumentation.sendPointerSync(event);

    event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, mainview.getWidth()/2, mainview.getHeight()/2, 0);
    instrumentation.sendPointerSync(event);

但事件仍未触发。

1 个答案:

答案 0 :(得分:0)

查看the tests covering the onDoubleTap gesture detector,您需要使用TouchUtils.tapView