???????дAndroid???????????????????????????????????????????Android?汾????????????е?ú???????????????????汾??Android??????????в????????????????????????????????View?????????????robotium??click???????????

????View view = ... // ???????????????View?????

????solo.click(view); // ?????????

????????????????????е??????????????????????????????????????в????汾???????????????????(??????????汾)?????е?ú?????????????(??汾)??????????????????

??????????????????????????Android??????????????????????????????п???????????????????(????)??С?????????????????????????Щ????????????????????С?Щ????????????

????????Щ?????????????????????????????????????????????????????С – ?????dp??λ?????dp??λ?????е?????????????????????????????????????????????????????????android??????????????????????????????????????????????????????????????С????μ????????????

 ????????????????????????????????Щ??????Android??????????????????-??????????????????????????????????????????????????????16????????????????????????????????????? 16???? / 160 dpi?????????????(??2.5????);????????????????棬??????????16???? / 240 dpi??????????????(??1.7????)?????????????????????????????????????????о????????????????????Щ??

???????????????????????棬??????DPI?????С?????????????????????????????????????????????????????????????robotium?????instrumentation??????android??????????????????????????????????λ???x?? y????Robotium???????????????????????

????1. ??????????????View???????

????2. ???View. getLocationOnScreen?????????????????????????????????λ???????

????3. ???View.getWidth??View.getHeight?????????????С??

????4. ??????????????м?λ??????λ???????????????????????С????ó????????λ????????

????5. ???robotium??????λ???x??y?????????????android??????

????1: public void clickOnScreen(float x?? float y) {

????2: long downTime = SystemClock.uptimeMillis();

????3: long eventTime = SystemClock.uptimeMillis();

????4: MotionEvent event = MotionEvent.obtain(downTime?? eventTime??

????5: MotionEvent.ACTION_DOWN?? x?? y?? 0);

????6: MotionEvent event2 = MotionEvent.obtain(downTime?? eventTime??

????7: MotionEvent.ACTION_UP?? x?? y?? 0);

????8: try{

????9: inst.sendPointerSync(event);

????10: inst.sendPointerSync(event2);

????11: sleeper.sleep(MINISLEEP);

????12: }catch(SecurityException e){

????13: Assert.assertTrue("Click can not be completed!"?? false);

????14: }

????15: }

???????????е?????λ??????????????????п????????????Σ??????????????????????????λ???????

?????????????????????????????????????????dpi?????С???????????????????????????????????????

????1: // ???????????????View?????

????2: public void clickOnScreen(View view?? boolean longClick?? int time) {

????3: if(view == null)

????4: Assert.assertTrue("View is null and can therefore not be clicked!"?? false);

????5: int[] xy = new int[2];

????6:

????7: // ??????????????λ?? – ?????dpi?????С?????????λ??????????λ??

????8: view.getLocationOnScreen(xy);

????9:

????10: // ?????????С??????????????????е?λ??

????11: final int top = view.getTop();

????12: final int viewWidth = view.getWidth();

????13: final int viewHeight = view.getHeight();

????14: float x = xy[0] + (viewWidth / 2.0f);

????15: float y = xy[1] + (viewHeight / 2.0f);

????16:

????17: // ?????????????????????x?? y????????????????????

????18: Activity activity = activityUtils.getCurrentActivity();

????19: DisplayMetrics rdm = activity.getResources().getDisplayMetrics();

????20: DisplayMetrics wdm = new DisplayMetrics();

????21: activity.getWindowManager().getDefaultDisplay().getMetrics(wdm);

????22: x *= wdm.scaledDensity / rdm.scaledDensity;

????23: y *= wdm.scaledDensity / rdm.scaledDensity;

????24:

????25: // ???????Android??????

????26: if (longClick)

????27: clickLongOnScreen(x?? y?? time);

????28: else

????29: clickOnScreen(x?? y);

????30: }