Android在自动化测试的过程中经常会涉及到注入event的场景,通过参考网上资料大概分为两种:
1. 通过instrumentation注入。
查看instrumentation 的源码会发现,会有很多send开头的方法:
随便选取其中的一个方法:
public void sendPointerSync(MotionEvent event) {
validateNotAppThread();
if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) == 0) {
event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
}
InputManager.getInstance().injectInputEvent(event,
InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
不难看出其中其实是调用了 InputManager 类来实现事件输入,这里不具体展开,补充一个简单的例子。
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
上面例子输入了一个 KEYCODE_BACK 返回键操作。