android應用自動化過程中,會遇見需要長按並拖動的情境,例如類似UC瀏覽器中,長按某個導航中的表徵圖,使其處於可移動狀態,然後再將其移動到另一個地方,與其它互動中心圖示換個位置,在robotium中有個drag(float fromX, float toX, float fromY, float toY,int stepCount)方法,但由於drag沒有長按這個步驟,因此不能使應用處於可移動的狀態,是沒法完成這樣的常見而簡單的操作的。
drag方法源碼實現如下:
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) {}
}
可以看出其實是通過MotionEvent的ACTION_DOWN類比螢幕按下操作,ACTION_MOVE類比手勢在螢幕上滑動,ACTION_UP類比手勢離開螢幕,從而完成整個拖動過程,而且其實robotium中的各種點擊類方法也都是通過類比不同的手勢完成的。
因此要想完成長按並拖動的操作,只要在ACTION_DOWN之後,停留一段時間即可類比長按操作。
/**
* 實現將一個視圖拖動到另一個視圖所在的位置,用於實現快訊欄目、手機報的排序
* @param viewFrom 起始View
* @param viewTo 終點View
* @throws Exception
*/
public void clickLongAndDrag(View viewFrom,View viewTo) throws Exception {
//獲得視圖View中手機螢幕上的絕對x、y座標
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);
//延遲一秒,類比長按操作
eventTime = SystemClock.uptimeMillis() + 1000;
//xStop加了10點座標,獲得的View座標需根據應用實際情況稍做一點調整
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, xStop+10f, yStop+50f, 0);
inst.sendPointerSync(event);
eventTime = SystemClock.uptimeMillis() + 1000;
//又再小小移動了一次,不這麼做的話可以無法啟用被測應用狀態,導致View移動後又回複到原來位置
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
}
}