Reputation: 3539
I'm currently implementing a gesture detector for multi-touch events on Android. For testing the behaviour of the detector, i want to send MotionEvents into the detector and check his actions.
The problem i am currently having is that i can only create MotionEvents by calling one of the existing MotionEvent.obtain()
methods, but it seems these methods do not allow me to set the pointer id for an event. I.e. i can only create single-touch events.
Does anyone know if it's possible to do this somehow? I could use another datastructure for input to the detector, but if possible i want to avoid this and stick with the MotionEvent class.
Upvotes: 1
Views: 3665
Reputation: 1336
I've ended up setting my test project API level to 9 while the main project is still API 7, so I can use the new MotionEvent.obtain with PointerCoords[] on my tests. It's working fine.
Upvotes: 1
Reputation: 3539
With Android SDK 2.3 there is a new method, as already mentioned by f20k, which fixes the problem.
But i'm currently stuck with api level 7 (2.1). My solution was to add a layer between my touch handling code and the actual events i get from Android, simply by converting to my own MotionEvent class (i named it TouchInfo). Furthermore, this allowed me to implement the whole gesture handling on the JVM which allowed me to drive the code by TDD. I could have done it with Android JUnit tests too, but developing the code on the JVM allowed me to use things like junit4, hamcrest and mockito.
A third way would be to use robolectric if you don't want to add the layer.
Upvotes: 0
Reputation: 3106
Have you tried:
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)
which is the third obtain( ) method listed in MotionEvent page
You can specify:
Upvotes: 1