一. 自动测试
1. 一个问题
假设要实现这样的功能: 依次点击下面图片上的B1 B2 B3,该如何实现呢?
这就需要模拟点击触摸屏的事件,最好是命令行的这样可以写成脚本.
android 下有自动测试工具moneky,可以实现自动点击
adb shell monkey -p com.test 1000
但是它这个好像是随机点的,没有固定按键的功能(如果有人知道,怎么用monkey实现,请告诉我).
既然monkey解决不了问题,那就再找另一个方法
2. 查找触摸屏属于event几
手机是mt6575
在摸拟器中,触摸屏是event0,但在真实的机子上不一定是event0,所以需要先查看一下用命令getevent
-
sun@ubuntu:/tmp$ adb shell getevent
-
add device 1: /dev/input/event4
-
name: "AVRCP"
-
add device 2: /dev/input/event3
-
name: "mtk-tpd"
-
could not get driver version for /dev/input/mouse0, Not a typewriter
-
add device 3: /dev/input/event1
-
name: "hwmdata"
-
add device 4: /dev/input/event0
-
name: "ACCDET"
-
could not get driver version for /dev/input/mice, Not a typewriter
-
add device 5: /dev/input/event2
-
name: "mtk-kpd"
注: device号和event号不是一样的,这儿只关心event号
通过上面可以看到触摸屏name=mtk-tpd, 是event3
如果不放心,可以测试一下:
-
sun@ubuntu:/tmp$ adb shell cat /dev/input/event0
-
^C
-
sun@ubuntu:/tmp$ adb shell cat /dev/input/event1
-
^C
-
sun@ubuntu:/tmp$ adb shell cat /dev/input/event2
-
^C
-
sun@ubuntu:/tmp$ adb shell cat /dev/input/event3
-
??C??^CJ?C
-
sun@ubuntu:/tmp$ adb shell cat /dev/input/event4
-
^C
执行adb shell cat /dev/input/event0时,在屏幕上随便点击,发现只有event3上面有输出.
这样就确定了,手机上的触摸屏是event3
3.确定按键命令
执行命令
adb shell getevent /dev/input/event3
然后在屏幕上迅速的按一下,发现有如下输出:
-
sun@ubuntu:/work/cong/driver$ adb shell getevent /dev/input/event3
-
0001 014a 00000001
-
0003 0030 00000001
-
0003 0035 00000028
-
0003 0036 00000112
-
0003 0039 00000001
-
0000 0002 00000000
-
0000 0000 00000000
-
0003 0030 00000001
-
0003 0035 00000028
-
0003 0036 00000112
-
0003 0039 00000001
-
0000 0002 00000000
-
0000 0000 00000000
-
0001 014a 00000000
-
0000 0002 00000000
-
0000 0000 00000000
那么这些输出是什么意思呢?
这可以通过给getevent加上-d -l参数来查看
-d
: show HID descriptor
, if available
-l
: label event types
and names
in plain text
-
sun@ubuntu:/work/cong/driver$ adb shell getevent -d -l /dev/input/event3
-
EV_KEY BTN_TOUCH DOWN
-
EV_ABS ABS_MT_TOUCH_MAJOR 00000001
-
EV_ABS ABS_MT_POSITION_X 0000003e
-
EV_ABS ABS_MT_POSITION_Y 00000103
-
EV_ABS ABS_MT_TRACKING_ID 00000001
-
EV_SYN SYN_MT_REPORT 00000000
-
EV_SYN SYN_REPORT 00000000
-
EV_ABS ABS_MT_TOUCH_MAJOR 00000001
-
EV_ABS ABS_MT_POSITION_X 0000003e
-
EV_ABS ABS_MT_POSITION_Y 00000103
-
EV_ABS ABS_MT_TRACKING_ID 00000001
-
EV_SYN SYN_MT_REPORT 00000000
-
EV_SYN SYN_REPORT 00000000
-
EV_ABS ABS_MT_TOUCH_MAJOR 00000001
-
EV_ABS ABS_MT_POSITION_X 0000003e
-
EV_ABS ABS_MT_POSITION_Y 00000103
-
EV_ABS ABS_MT_TRACKING_ID 00000001
-
EV_SYN SYN_MT_REPORT 00000000
-
EV_SYN SYN_REPORT 00000000
-
EV_ABS ABS_MT_TOUCH_MAJOR 00000001
-
EV_ABS ABS_MT_POSITION_X 0000003e
-
EV_ABS ABS_MT_POSITION_Y 00000103
-
EV_ABS ABS_MT_TRACKING_ID 00000001
-
EV_SYN SYN_MT_REPORT 00000000
-
EV_SYN SYN_REPORT 00000000
-
EV_KEY BTN_TOUCH UP
-
EV_SYN SYN_MT_REPORT 00000000
-
EV_SYN SYN_REPORT 00000000
其实上面的 EV_KEY, EV_ABS等都是在内核的 include/linux/input.h 这个文件中定义的
-
#define EV_SYN 0x00
-
#define EV_KEY 0x01
-
#define EV_REL 0x02
-
#define EV_ABS 0x03
-
-
#define ABS_MT_TOUCH_MAJOR 0x30
-
#define ABS_MT_POSITION_X 0x35
-
#define ABS_MT_POSITION_Y 0x36
-
#define ABS_MT_TRACKING_ID 0x39
这样对应起来就是:
-
EV_KEY BTN_TOUCH DOWN
-
0001 014a 00000001
-
-
EV_ABS ABS_MT_TOUCH_MAJOR 00000001
-
EV_ABS ABS_MT_POSITION_X 0000003e
-
EV_ABS ABS_MT_POSITION_Y 000000ef
-
EV_ABS ABS_MT_TRACKING_ID 00000001
-
0003 0030 00000001
-
0003 0035 00000028
-
0003 0036 00000112
-
0003 0039 00000001
-
-
EV_SYN SYN_MT_REPORT 00000000
-
EV_SYN SYN_REPORT 00000000
-
0000 0002 00000000
-
0000 0000 00000000
-
-
EV_ABS ABS_MT_TOUCH_MAJOR 00000001
-
EV_ABS ABS_MT_POSITION_X 0000003e
-
EV_ABS ABS_MT_POSITION_Y 000000ef
-
EV_ABS ABS_MT_TRACKING_ID 00000001
-
0003 0030 00000001
-
0003 0035 00000028
-
0003 0036 00000112
-
0003 0039 00000001
-
-
EV_SYN SYN_MT_REPORT 00000000
-
EV_SYN SYN_REPORT 00000000
-
0000 0002 00000000
-
0000 0000 00000000
-
-
EV_KEY BTN_TOUCH UP
-
0001 014a 00000000
-
-
EV_SYN SYN_MT_REPORT 00000000
-
EV_SYN SYN_REPORT 00000000
-
0000 0002 00000000
-
0000 0000 00000000
发现上面是发送了两次X Y坐标,手点的有抖动嘛
4. 编写脚本
实现自动按键时,需要把重复发送XY坐标值去掉就可以了
无限循环:
-
#!/bin/sh
-
btn_press()
-
{
-
#button Down
-
adb shell sendevent /dev/input/event3 1 330 1
-
-
#MAJOR X Y ID
-
adb shell sendevent /dev/input/event3 3 48 1
-
adb shell sendevent /dev/input/event3 3 53 40
-
adb shell sendevent /dev/input/event3 3 54 250
-
adb shell sendevent /dev/input/event3 3 57 1
-
-
#SYN_MT_REPORT SYM
-
adb shell sendevent /dev/input/event3 0 2 0
-
adb shell sendevent /dev/input/event3 0 0 0
-
-
#button up
-
adb shell sendevent /dev/input/event3 1 330 0
-
adb shell sendevent /dev/input/event3 0 2 0
-
adb shell sendevent /dev/input/event3 0 0 0
-
}
-
while :
-
do
-
btn_press ;
-
done
注:
1. getevent命令如下
-
sun@ubuntu:/tmp$ adb shell getevent -h
-
Usage: getevent [-t] [-n] [-s switchmask] [-S] [-v [mask]] [-d] [-p] [-i] [-l] [-q] [-c count] [-r] [device]
-
-t: show time stamps
-
-n: don't print newlines
-
-s: print switch states for given bits
-
-S: print all switch states
-
-v: verbosity mask (errs=1, dev=2, name=4, info=8, vers=16, pos. events=32, props=64)
-
-d: show HID descriptor, if available
-
-p: show possible events (errs, dev, name, pos. events)
-
-i: show all device info and possible events
-
-l: label event types and names in plain text
-
-q: quiet (clear verbosity mask)
-
-c: print given number of events then exit
-
-r: print rate events are received
2. 获取屏幕按键坐标
Settings --> Developer Options --> Pointer location
然后在屏幕上方就可以看到X Y坐标了
参考文章:
http://forum.xda-developers.com/showthread.php?t=1875094
阅读(4351) | 评论(0) | 转发(0) |