Home » Android » Android testing, how to simulate multitouch (zoom in/out) using instruments?

Android testing, how to simulate multitouch (zoom in/out) using instruments?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I can simply simulate single touches – tap, swipe, hold etc in my tests, but completely stuck with simulating multi touch on HTС Desire with Android 2.2.

Could you please advise, how can I reproduce events chain to test multi-touches?

I think I need to use some tricky kind of MotionEvent like MASK or something like this, but have no idea how do this.

I have found here a dump of events of reproduced zooming:
http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2-part-3-understanding-touch-events/1775?tag=mantle_skin;content

 1. event ACTION_DOWN[#0(pid 0)=135,179]
 2. event ACTION_MOVE[#0(pid 0)=135,184]
 3. event ACTION_MOVE[#0(pid 0)=144,205]
 4. event ACTION_MOVE[#0(pid 0)=152,227]
 5. event ACTION_POINTER_DOWN(pid 1)[#0(pid 0)=153,230;#1(pid 1)=380,538]
 6. event ACTION_MOVE[#0(pid 0)=153,231;#1(pid 1)=380,538]
 7. event ACTION_MOVE[#0(pid 0)=155,236;#1(pid 1)=364,512]
 8. event ACTION_MOVE[#0(pid 0)=157,240;#1(pid 1)=350,498]
 9. event ACTION_MOVE[#0(pid 0)=158,245;#1(pid 1)=343,494]
 10. event ACTION_POINTER_UP(pid 0)[#0(pid 0)=158,247;#1(pid 1)=336,484]
 11. event ACTION_MOVE[#0(pid 1)=334,481]
 12. event ACTION_MOVE[#0(pid 1)=328,472]
 13. event ACTION_UP[#0(pid 1)=327,471]

Here is my issue:

  1. event ACTION_POINTER_DOWN(pid 1)[#0(pid 0)=153,230;#1(pid 1)=380,538]
  2. event ACTION_MOVE[#0**(pid 0)=153,231**;#1**(pid 1)=380,538**]

How can I generate events with 4 coordinates (pid 0 x0 y0 and pid 1 x1 y1)?

Looks like I need to find the way how to use following event:

public static MotionEvent obtain (long downTime, long eventTime, int action, int pointers, int[] pointerIds, PointerCoords[] pointerCoords, int metaState, float xPrecision, float yPrecision, int deviceId, int edgeFlags, int source, int flags)

Thanks to Dan for reply, I have tried this logic, but still encountering problems to add coordinates:

MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, 135, 179, 0);
            inst.sendPointerSync(event);
//                eventTime+=100;
            event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, 135, 184, 0);
            inst.sendPointerSync(event);
//                eventTime+=100;

            int pointerToMove = 1; // pointer IDs are zero-based
            event = MotionEvent.obtain(downTime, eventTime, (pointerToMove << MotionEvent.ACTION_POINTER_INDEX_SHIFT) +  MotionEvent.ACTION_POINTER_DOWN, 138, 189, 0);
            inst.sendPointerSync(event);


            event = MotionEvent.obtain(downTime, eventTime, (pointerToMove << MotionEvent.ACTION_POINTER_INDEX_SHIFT) + MotionEvent.ACTION_MOVE, 158, 220, 0);
            inst.sendPointerSync(event);

 //                eventTime+=100;
            event = MotionEvent.obtain(downTime, eventTime, (2 * 256) + MotionEvent.ACTION_MOVE, 138, 180, 0);
            inst.sendPointerSync(event);
 //                eventTime+=100;
            event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE,  135, 184, 0);

These events sequence are caught in my test stub and dumped like:

(14368): event ACTION_DOWN[#0(pid 0)=135,179]
(14368): event ACTION_MOVE[#0(pid 0)=135,184]
(14368): event ACTION_POINTER_DOWN(pid 1)[#0(pid 0)=138,189]
(14368): event ACTION_MOVE[#0(pid 0)=158,220]
(14368): event ACTION_MOVE[#0(pid 0)=138,180]
(14368): event ACTION_MOVE[#0(pid 0)=135,184]

Here you can see, that
(2 * 256) + MotionEvent.ACTION_MOVE
doesn’t change the pointer ID for event 🙁
And
pointerToMove << MotionEvent.ACTION_POINTER_INDEX_SHIFT
approach is not working for ACTION_POINTER_DOWN, may be I’m not allowed to use such way for POINTER_DOWN?

My issue is that I can’t generate 2 pair of coords for Pointer 0 and Pointer 1:

(14368): event ACTION_POINTER_DOWN(pid 1)[#0(pid 0)=138,189]

Here you can see, that using your logic I have added pid1 to the event, but it is still has no coordinates, cause x and y was associated with pid 0..

Thank you in advance.

Yahor

Still have no ideas how to implement it, did somebody ever send a multitouch event?

How to&Answers:

I believe you just need to indicate the pointer index in the ‘action’ parameter passed to MotionEvent.obtain. Specifically, the upper 8-bits of the action is the pointer index and the lower 8-bits is the action (e.g. MotionEvent.ACTION_MOVE). So, if you want to move the second pointer this should work:

int pointerToMove = 1; // pointer IDs are zero-based
event = MotionEvent.obtain(downTime, eventTime, (pointerToMove << MotionEvent.ACTION_POINTER_INDEX_SHIFT) + MotionEvent.ACTION_MOVE, x0, y0, 0);
inst.sendPointerSync(event);

-Dan

Answer:

I supposed this can help you..