robotium源码分析 ClickView

转:http://blog.csdn.net/zqilu/article/details/17096201

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关于Click的方法有 ClickOnView()、ClickLongOnView()、ClickInList()、ClickonSceen()clickOnActionBarItem()这几个大类。

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


      通过源码我们知道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)                  //用于发送字符串


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值