Implement long-pressed and drag in android Automated Testing

Source: Internet
Author: User

During the android Application automation process, you may encounter scenarios where you need to press and drag long. For example, in a UC browser, you can press the icon in a navigation bar to make it movable, then, move it to another place and change the position of other navigation icons. In robotium, there is a drag (float fromX, float toX, float fromY, float toY, int stepCount) method, however, because the drag does not follow this step for a long time, the application cannot be moved, and such a common and simple operation cannot be completed.

The source code of the drag method is implemented 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 ){}

}

It can be seen that the MotionEvent ACTION_DOWN is used to simulate the screen press operation, ACTION_MOVE simulates the gesture to slide on the screen, ACTION_UP simulates the gesture to exit the screen, thus completing the entire drag process, in fact, all the clicking methods in robotium are done by simulating different gestures.

Therefore, to complete the long-and-drag operation, you only need to wait for a while after ACTION_DOWN to simulate the long-press operation.

/**

* You can drag a View to the position of another view to sort the news column and mobile phone reports.

* @ Param viewFrom start View

* @ Param viewTo endpoint View

* @ Throws Exception

*/

Public void clickLongAndDrag (View viewFrom, View viewTo) throws Exception {

// Obtain the absolute x and y coordinates on the mobile phone screen in the 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 to simulate long-pressed operations

EventTime = SystemClock. uptimeMillis () + 1000;

// Add the 10-point coordinate to xStop. The obtained View coordinate must be slightly adjusted based on the actual situation of the application.

Event = MotionEvent. obtain (downTime, eventTime, MotionEvent. ACTION_MOVE, xStop + 10f, yStop + 50f, 0 );

Inst. sendPointerSync (event );

EventTime = SystemClock. uptimeMillis () + 1000;

// Move the page again. If you do not do so, you cannot activate the state of the application to be tested. As a result, the View moves and then returns 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

}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.