Currently I am struggling a little with the Unit test framework…
what I am trying to do
- I need to simulate 2 clicks on the screen (100,100) and (400,400) at a small duration difference.
- I need to simulate a longPressClick on the screen Let’s say ( 200, 200 )
- Upon User click the native code runs and performs pixel manipulation on Bitmap.
- This test is going to run for multiple pair-sets of points, for analysing the runtime performance of the system
Here’s where I am stuck
- I am using activityInstrumentationTestCase2 and touchUtils for the user click events.
- TouchUtils.longClickView(InstrumentationTestCase test, View v) works fine ; I’m able to detect the long pressing event propely , But test-case finishes even before the calculation / rendering is complete in my UI thread ; how do I stop the test from exiting in this case ?
- How do i simulate 2 / 3 user clicks @ particular location on screen ? cause TouchUtils.clickView(InstrumentationTestCase test, View v) would only simulate the user click in the center of the screen … How to do it properly ?
These are the things I have tried and seems I’m missing out something:
- TouchUtils.longClickView(InstrumentationTestCase test, View v) works fine…for creating longClickView .. Even I was able to create longClickView() at particular screen location by introducing the timedelay between ACTION_DOWN and ACTION_UP event.. please refer to the code attached
- I was able to achieve the user clicking event at particular screen location , But I faced a strange issue .. When I am displatching the MotionEvent(100,100) from the test-case.. The framework would always add “-76” in the Y event .. not sure why there was this deviation … I worked around the issue by adding 76 to my input data (100,176) for time being .. did anyone face a similar issue ?
- Even seems with this approach timing is very critical .. as If i place more delay between ACTION_DOWN and ACTION_UP , the event is detected as longClickPress … and if I put a little less … the “second” single click events ( ACTION_DOWN + ACTION_UP ) gets detected as DoubleTapEvent ..
What should be the right timing combination for ACTION_UP and ACTION_DOWN .. for a single user click event simulation .. ????????
@Test
public void testClick(){
List<Points> pointSequence = new ArrayList<Points>();
Log.d(TAG, "FirClick Start Timing : " + SystemClock.uptimeMillis());
pointSequence.add(new Points(100f,176f));
pointSequence.add(new Points(100f,176f));
singleClickSimulation(pointSequence,false);
}
private void singleClickSimulation(List<Points> pointSequence, Boolean addDelay) {
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
// NOTE : If I do not place this then the event is detected as doubleTap.
eventTime += 100;
Instrumentation inst = getInstrumentation();
MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, pointSequence.get(0).getX(), pointSequence.get(0).getY(), 0);
inst.sendPointerSync(event);
//eventTime = SystemClock.uptimeMillis();
pointSequence.remove(0);
//This delay I have added just to test; whether there is enough time for pixel manipulation or not, actually it would be used only at the end of the run of all the test cases for single user click
if(addDelay){
eventTime = SystemClock.uptimeMillis() + 3000;
}
eventTime += 25;
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, pointSequence.get(0).getX(), pointSequence.get(0).getY(), 0);
inst.sendPointerSync(event);
pointSequence.remove(0);
}
Partial Answer :
still to find what’s causing the 76px deviation when creating the user click events.
I have been able to simuate the single user click event with 50ms difference propely.
And I had to space 2 clicks by atleast 300ms ; so that “SingleTapConfirmed” can fire on its own for the GeastureDetector class..