robotium源码分析 ClickView

来源:互联网 发布:杜兰特本赛季数据统计 编辑:程序博客网 时间:2024/05/16 09:15

robotium关于Click的方法有 ClickOnView()、ClickLongOnView()、ClickInList()、ClickonSceen()clickOnActionBarItem()这几个大类。

而ClickonScreen(float x,float y)源码为:

       public void clickOnScreen(float x, float y) {
boolean successfull = false;

               Instrumentation inst;
int retry = 0;
while(!successfull && retry < 10) {
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
MotionEvent event = MotionEvent.obtain(downTime, eventTime,
MotionEvent.ACTION_DOWN, x, y, 0);
MotionEvent event2 = MotionEvent.obtain(downTime, eventTime,
MotionEvent.ACTION_UP, x, y, 0);
try{
inst.sendPointerSync(event);
inst.sendPointerSync(event2);
successfull = true;
sleeper.sleep(MINISLEEP);
}catch(SecurityException e){
dialogUtils.hideSoftKeyboard(null, false, true);
retry++;
}
}
if(!successfull) {
Assert.assertTrue("Click can not be completed!", false);
}
}

      通过源码我们知道Robotium点击View就是调用Instrumentation类方法中的sendPointSync(event)(模拟鼠标点击),而clickOnView则是通过view的id或者index获取到View,然后通过View的getLocationOnScreen()方法和View的宽和高得到该View的坐标,然后调用clickOnScreen()方法。View坐标获取的代码为:

                 view.getLocationOnScreen(xy);
final int viewWidth = view.getWidth();
final int viewHeight = view.getHeight();
final float x = xy[0] + (viewWidth / 2.0f);
float y = xy[1] + (viewHeight / 2.0f);

clickLongOnView则是在两个event之间加上间隔时间。

clickOnActionBarItem()则是调用Instrumentation类方法中的invokeMenuActionSync(Activity activity,int id);方法。


drag()方法也是类似于click 只是多了个move 事件;

sendCharacterSync(int keyCode)           //用于发送指定KeyCode的按键

sendKeyDownUpSync(int key)               //用于发送指定KeyCode的按键

sendPointerSync(MotionEvent event)    //用于模拟Touch

sendStringSync(String text)                  //用于发送字符串


原创粉丝点击