Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a MotionEvent to zoom?

I have an instrumentation that I want to use to test my application, but I can't seem to get the pinch zoom to simulate properly. Here is my current code. The gist of it is this: push down both finger 1 and finger 2, move them both closer to one another, then release them both.

private void performZoomTest(int numUpdates) {
    long downTime = SystemClock.uptimeMillis();
    long eventTime = SystemClock.uptimeMillis();    

    float x1 = 0;
    float y1 = 0;

    Display main = activity.getWindowManager().getDefaultDisplay();

    float x2 = main.getWidth();
    float y2 = main.getHeight();

    float xstep = x2 / (2 * numUpdates);
    float ystep = y2 / (2 * numUpdates);

    int id1 = 0 << MotionEvent.ACTION_POINTER_ID_SHIFT;
    int id2 = 1 << MotionEvent.ACTION_POINTER_ID_SHIFT;

    MotionEvent event = 
        MotionEvent.obtain(downTime, eventTime, 
                   MotionEvent.ACTION_DOWN + id1,
                   x1, y1, 0);
    sendPointerSync(event);
    event = 
        MotionEvent.obtain(downTime, eventTime, 
                   MotionEvent.ACTION_DOWN + id2,
                   x2, y2, 0);
    sendPointerSync(event);

    waitForIdleSync();

    for (int i = 0; i < numUpdates; i++) {
        eventTime = SystemClock.uptimeMillis();

        Log.i("",Integer.toString(i));
        x1 += xstep;
        y1 += ystep;

        x2 -= xstep;
        y2 -= ystep;

        event = 
            MotionEvent.obtain(downTime, eventTime, 
                       MotionEvent.ACTION_MOVE + id1,
                       x1, y1, 0);
        Log.i("id1", Integer.toString(MotionEvent.ACTION_MOVE + id1));
        sendPointerSync(event);
        event = 
            MotionEvent.obtain(downTime, eventTime, 
                       MotionEvent.ACTION_MOVE + id2, 
                       x2, y2, 0);
        Log.i("id1", Integer.toString(MotionEvent.ACTION_MOVE + id2));
        sendPointerSync(event);

        waitForIdleSync();
    } 

    eventTime = SystemClock.uptimeMillis();

    event = 
        MotionEvent.obtain(downTime, eventTime, 
                   MotionEvent.ACTION_UP + id1, 
                   x1, y1, 0);
    sendPointerSync(event);
    event = 
        MotionEvent.obtain(downTime, eventTime, 
                   MotionEvent.ACTION_UP + id2, 
                   x2, y2, 0);
    sendPointerSync(event);

    waitForIdleSync();
}

The result seems to be that the application treats the two events as being distinct, and doesn't register them as a single event. Is there a better way of doing this?

like image 639
alexgolec Avatar asked Nov 14 '22 21:11

alexgolec


1 Answers

take a look here: https://stackoverflow.com/questions/522312/best-practices-for-unit-testing-android-apps

I think multitouch unit testing is not possible with the standard Android framework.

like image 102
stk Avatar answered Dec 10 '22 07:12

stk