In the Android application automation process, you'll see a scene that needs to be long pressed and dragged, such as a UC browser, long pressed on a navigation icon to make it removable, and then move it to another place, swapping it with other navigation icons, with a drag in the Robotium (float FromX, float ToX, float fromY, float toy,int stepcount) method, but because the drag does not have a long press this step, therefore cannot make the application in the movable state, cannot complete such common and simple operation.
Drag method source code implementation is as follows:
public void drag (float FromX, float toX, float fromY, float ToY,
int Stepcount) {
Long downtime = Systemclock.uptimemillis ();
Long eventtime = Systemclock.uptimemillis ();
Float y = FromY;
float x = FromX;
float Ystep = (toy-fromy)/stepcount;
float Xstep = (TOX-FROMX)/stepcount;
Motionevent event = Motionevent.obtain (Downtime, Eventtime,motionevent.action_down, FromX, FromY, 0);
try {
Inst.sendpointersync (event);
catch (SecurityException ignored) {}
for (int i = 0; i < Stepcount; ++i) {
Y + + ystep;
x + + Xstep;
Eventtime = Systemclock.uptimemillis ();
event = Motionevent.obtain (Downtime, eventtime,motionevent.action_move, x, y, 0);
try {
Inst.sendpointersync (event);
catch (SecurityException ignored) {}
}
Eventtime = Systemclock.uptimemillis ();
event = Motionevent.obtain (Downtime, eventtime, Motionevent.action_up,tox, ToY, 0);
try {
Inst.sendpointersync (event);
catch (SecurityException ignored) {}
}
Can be seen in fact, through the Motionevent action_down analog screen operation, action_move simulation gestures on the screen, action_up analog gesture left the screen, thus completing the entire drag process, And in fact the various click-Class methods in Robotium are also done by simulating different gestures.
Therefore, in order to complete the long press and drag operation, as long as the Action_down, stay for a period of time can simulate long press operation.
/**
* Implementation to drag a view to the location of another view, to achieve the newsletter column, mobile newspaper sorting
* @param viewfrom Start view
* @param viewto End View
* @throws Exception
*/
public void Clicklonganddrag (View viewfrom,view viewto) throws Exception {
Get the absolute x and Y coordinates on the phone screen of view
Final int[] location = new INT[2];
Final int[] Location2 = new int[2];
Viewfrom.getlocationonscreen (location);
Viewto.getlocationonscreen (Location2);
float xstart=location[0];
float ystart=location[1];
float xstop=location2[0];
float ystop=location2[1];
LOG.I (TAG, "Xstart:" +string.valueof (Xstart));
LOG.I (TAG, "Ystart:" +string.valueof (Ystart));
LOG.I (TAG, "Xstop:" +string.valueof (Xstop));
LOG.I (TAG, "Ystop:" +string.valueof (Ystop));
Long downtime = Systemclock.uptimemillis ();
Long eventtime = Systemclock.uptimemillis ();
try{
Motionevent event = Motionevent.obtain (Downtime, eventtime, Motionevent.action_down, xstart+10f, yStart+10f, 0);
Inst.sendpointersync (event);
event = Motionevent.obtain (Downtime, eventtime, Motionevent.action_move, xstart+10f+1.0f, ystart+10f+1.0f, 0);
Inst.sendpointersync (event);
Thread.Sleep (1000);
Delay one second, simulate long press operation
Eventtime = Systemclock.uptimemillis () + 1000;
Xstop added 10 point coordinates, the view coordinates obtained need to make a little adjustment according to the application actual situation
event = Motionevent.obtain (Downtime, eventtime, Motionevent.action_move, xstop+10f, ystop+50f, 0);
Inst.sendpointersync (event);
Eventtime = Systemclock.uptimemillis () + 1000;
Moved a little bit again, not to do so can not activate the applied state of the test, causing the view to move back to the original position
event = Motionevent.obtain (Downtime, eventtime, Motionevent.action_move, xstop+10f, ystop+10f, 0);
Inst.sendpointersync (event);
Eventtime = Systemclock.uptimemillis () + 1000;
event = Motionevent.obtain (Downtime, eventtime, Motionevent.action_up, xstop+10f, ystop+10f, 0);
Inst.sendpointersync (event);
}catch (Exception ignored) {
Handle Exceptions if necessary
}
}