Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate a fling gesture in Android ActivityInstrumentationTestCase2

I have an Android view that I am detecting a fling gesture on to perform an operation, and I want to write some tests to verify that the gesture is working correctly. I have tried TouchUtils.dragViewTo and TouchUtils.drag (with a very small number of steps) but neither of these seem to trigger the event.

Is there a way to simulate the fling gesture?

like image 369
Kevin Avatar asked Oct 22 '10 11:10

Kevin


1 Answers

Ok, this is a really old question, but posting a solution which worked for me, may help others who come searching for this

int[] xy = new int[2];
View v = currentActivity.getCurrentFocus();
v.getLocationOnScreen(xy);
final int viewWidth = v.getWidth();
final int viewHeight = v.getHeight();
final float x = xy[0] + (viewWidth / 2.0f);
float fromY = xy[1] + (viewHeight / 2.0f);
int screenWidth = currentActivity.getWindowManager().getDefaultDisplay().getWidth();    
//Drag from centre of screen to Leftmost edge of display
TouchUtils.drag(this, (screenWidth - 1), x,  fromY, fromY , 5); //Vary the last parameter to sdjust speed of fling
like image 111
Royston Pinto Avatar answered Nov 15 '22 10:11

Royston Pinto