Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2978127
  • 博文数量: 685
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 5303
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-19 14:17
个人简介

文章分类

全部博文(685)

文章存档

2015年(116)

2014年(569)

分类: Android平台

2014-09-20 14:49:22

原文地址:http://blog.chinaunix.net/uid-26009923-id-3839259.html

一. 自动测试
1. 一个问题
假设要实现这样的功能:  依次点击下面图片上的B1 B2 B3,该如何实现呢?

这就需要模拟点击触摸屏的事件,最好是命令行的这样可以写成脚本.
android 下有自动测试工具moneky,可以实现自动点击
adb shell monkey -p com.test 1000
但是它这个好像是随机点的,没有固定按键的功能(如果有人知道,怎么用monkey实现,请告诉我).
既然monkey解决不了问题,那就再找另一个方法
2. 查找触摸屏属于event几
手机mt6575
在摸拟器中,触摸屏是event0,但在真实的机子上不一定是event0,所以需要先查看一下用命令getevent
  1. sun@ubuntu:/tmp$ adb shell getevent
  2. add device 1: /dev/input/event4
  3.   name: "AVRCP"
  4. add device 2: /dev/input/event3
  5.   name: "mtk-tpd"
  6. could not get driver version for /dev/input/mouse0, Not a typewriter
  7. add device 3: /dev/input/event1
  8.   name: "hwmdata"
  9. add device 4: /dev/input/event0
  10.   name: "ACCDET"
  11. could not get driver version for /dev/input/mice, Not a typewriter
  12. add device 5: /dev/input/event2
  13.   name: "mtk-kpd"
注: device号和event号不是一样的,这儿只关心event号
通过上面可以看到触摸屏name=mtk-tpd, 是event3
如果不放心,可以测试一下:
  1. sun@ubuntu:/tmp$ adb shell cat /dev/input/event0 
  2. ^C
  3. sun@ubuntu:/tmp$ adb shell cat /dev/input/event1
  4. ^C
  5. sun@ubuntu:/tmp$ adb shell cat /dev/input/event2
  6. ^C
  7. sun@ubuntu:/tmp$ adb shell cat /dev/input/event3
  8. ??C??^CJ?C
  9. sun@ubuntu:/tmp$ adb shell cat /dev/input/event4
  10. ^C
执行adb shell cat /dev/input/event0时,在屏幕上随便点击,发现只有event3上面有输出.
这样就确定了,手机上的触摸屏是event3
3.确定按键命令
执行命令 adb shell getevent /dev/input/event3
然后在屏幕上迅速的按一下,发现有如下输出:
  1. sun@ubuntu:/work/cong/driver$ adb shell getevent /dev/input/event3
  2. 0001 014a 00000001
  3. 0003 0030 00000001
  4. 0003 0035 00000028
  5. 0003 0036 00000112
  6. 0003 0039 00000001
  7. 0000 0002 00000000
  8. 0000 0000 00000000
  9. 0003 0030 00000001
  10. 0003 0035 00000028
  11. 0003 0036 00000112
  12. 0003 0039 00000001
  13. 0000 0002 00000000
  14. 0000 0000 00000000
  15. 0001 014a 00000000
  16. 0000 0002 00000000
  17. 0000 0000 00000000
那么这些输出是什么意思呢?
这可以通过给getevent加上-d -l参数来查看
-d: show HID descriptor, if available
-l: label event types and names in plain text
  1. sun@ubuntu:/work/cong/driver$ adb shell getevent --/dev/input/event3
  2. EV_KEY BTN_TOUCH DOWN 
  3. EV_ABS ABS_MT_TOUCH_MAJOR 00000001 
  4. EV_ABS ABS_MT_POSITION_X 0000003e 
  5. EV_ABS ABS_MT_POSITION_Y 00000103 
  6. EV_ABS ABS_MT_TRACKING_ID 00000001 
  7. EV_SYN SYN_MT_REPORT 00000000 
  8. EV_SYN SYN_REPORT 00000000 
  9. EV_ABS ABS_MT_TOUCH_MAJOR 00000001 
  10. EV_ABS ABS_MT_POSITION_X 0000003e 
  11. EV_ABS ABS_MT_POSITION_Y 00000103 
  12. EV_ABS ABS_MT_TRACKING_ID 00000001 
  13. EV_SYN SYN_MT_REPORT 00000000 
  14. EV_SYN SYN_REPORT 00000000 
  15. EV_ABS ABS_MT_TOUCH_MAJOR 00000001 
  16. EV_ABS ABS_MT_POSITION_X 0000003e 
  17. EV_ABS ABS_MT_POSITION_Y 00000103 
  18. EV_ABS ABS_MT_TRACKING_ID 00000001 
  19. EV_SYN SYN_MT_REPORT 00000000 
  20. EV_SYN SYN_REPORT 00000000 
  21. EV_ABS ABS_MT_TOUCH_MAJOR 00000001 
  22. EV_ABS ABS_MT_POSITION_X 0000003e 
  23. EV_ABS ABS_MT_POSITION_Y 00000103 
  24. EV_ABS ABS_MT_TRACKING_ID 00000001 
  25. EV_SYN SYN_MT_REPORT 00000000 
  26. EV_SYN SYN_REPORT 00000000 
  27. EV_KEY BTN_TOUCH UP 
  28. EV_SYN SYN_MT_REPORT 00000000 
  29. EV_SYN SYN_REPORT 00000000
其实上面的 EV_KEY, EV_ABS等都是在内核的 include/linux/input.h 这个文件中定义的
  1. #define EV_SYN 0x00
  2. #define EV_KEY 0x01
  3. #define EV_REL 0x02
  4. #define EV_ABS 0x03

  5. #define ABS_MT_TOUCH_MAJOR 0x30 
  6. #define ABS_MT_POSITION_X 0x35 
  7. #define ABS_MT_POSITION_Y 0x36 
  8. #define ABS_MT_TRACKING_ID 0x39
这样对应起来就是:
  1. EV_KEY BTN_TOUCH DOWN 
  2. 0001 014a 00000001

  3. EV_ABS ABS_MT_TOUCH_MAJOR 00000001 
  4. EV_ABS ABS_MT_POSITION_X 0000003e 
  5. EV_ABS ABS_MT_POSITION_Y 000000ef 
  6. EV_ABS ABS_MT_TRACKING_ID 00000001 
  7. 0003 0030 00000001
  8. 0003 0035 00000028
  9. 0003 0036 00000112
  10. 0003 0039 00000001

  11. EV_SYN SYN_MT_REPORT 00000000 
  12. EV_SYN SYN_REPORT 00000000 
  13. 0000 0002 00000000
  14. 0000 0000 00000000

  15. EV_ABS ABS_MT_TOUCH_MAJOR 00000001 
  16. EV_ABS ABS_MT_POSITION_X 0000003e 
  17. EV_ABS ABS_MT_POSITION_Y 000000ef 
  18. EV_ABS ABS_MT_TRACKING_ID 00000001
  19. 0003 0030 00000001
  20. 0003 0035 00000028
  21. 0003 0036 00000112
  22. 0003 0039 00000001

  23. EV_SYN SYN_MT_REPORT 00000000 
  24. EV_SYN SYN_REPORT 00000000 
  25. 0000 0002 00000000
  26. 0000 0000 00000000

  27. EV_KEY BTN_TOUCH UP 
  28. 0001 014a 00000000

  29. EV_SYN SYN_MT_REPORT 00000000 
  30. EV_SYN SYN_REPORT 00000000 
  31. 0000 0002 00000000
  32. 0000 0000 00000000
发现上面是发送了两次X Y坐标,手点的有抖动嘛
4. 编写脚本 
实现自动按键时,需要把重复发送XY坐标值去掉就可以了
无限循环:
  1. #!/bin/sh
  2. btn_press()
  3. {
  4.     #button Down
  5.     adb shell sendevent /dev/input/event3 1 330 1

  6.     #MAJOR X Y ID
  7.     adb shell sendevent /dev/input/event3 3 48 1
  8.     adb shell sendevent /dev/input/event3 3 53 40
  9.     adb shell sendevent /dev/input/event3 3 54 250
  10.     adb shell sendevent /dev/input/event3 3 57 1

  11.     #SYN_MT_REPORT SYM
  12.     adb shell sendevent /dev/input/event3 0 2 0
  13.     adb shell sendevent /dev/input/event3 0 0 0

  14.     #button up
  15.     adb shell sendevent /dev/input/event3 1 330 0
  16.     adb shell sendevent /dev/input/event3 0 2 0
  17.     adb shell sendevent /dev/input/event3 0 0 0
  18. }
  19. while :
  20. do
  21.     btn_press ;
  22. done
注:
1. getevent命令如下

  1. sun@ubuntu:/tmp$ adb shell getevent -
  2. Usage: getevent [-t] [-n] [-s switchmask] [-S] [-[mask]] [-d] [-p] [-i] [-l] [-q] [-c count] [-r] [device]
  3.     -t: show time stamps
  4.     -n: don't print newlines
  5.     -s: print switch states for given bits
  6.     -S: print all switch states
  7.     -v: verbosity mask (errs=1, dev=2, name=4, info=8, vers=16, pos. events=32, props=64)
  8.     -d: show HID descriptor, if available
  9.     -p: show possible events (errs, dev, name, pos. events)
  10.     -i: show all device info and possible events
  11.     -l: label event types and names in plain text
  12.     -q: quiet (clear verbosity mask)
  13.     -c: print given number of events then exit
  14.     -r: print rate events are received
2. 获取屏幕按键坐标
Settings --> Developer Options --> Pointer location
然后在屏幕上方就可以看到X Y坐标了
参考文章:
http://forum.xda-developers.com/showthread.php?t=1875094
阅读(876) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~