关闭

Robotium 跨进程(底层设备事件注入)

发表于:2015-8-18 10:06

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:fresh    来源:51Testing软件测试网采编

  Android跨进程注入最大的问题在于Android上层对跨进程注入事件的权限限制,那么能不能绕开上层直接操作底层设备呢
  答案是肯定的。
  传统方法:
  1、Monkey Server 利用Monkey进程跨进程注入事件的权限
  2、利用Uiautomator:Android提供了接口返回Uiautomation实例,在Robotium Case中可直接使用
  To get an instance of UiAutomation, call Instrumentation.getUiAutomation()
  UiAutomation mUiAutomation = getInstrumentation.getUiAutomation();
  3、利用adb注入事件
  4、抛开以上曲线救国的方法,这里重点要讲的是以下内容:
  直接往linux底层/dev/input/event*写事件
  该部分需要用C来完成,ndk-build编译成.so供上层调用
  Like this:
  C:
  JNIEXPORT void JNICALL Java_com_example_jnidemo_MainActivity_inject(JNIEnv* env, jobject thiz, uint16_t type, uint16_t code, int32_t value)
  {
  struct uinput_event event;
  int fd_kb = open("/dev/input/event21",O_RDWR);
  memset(&event, 0, sizeof(event));
  event.type = type;
  event.code = code;
  event.value = value;
  write(fd_kb, &event, sizeof(event));
  }
  Android:
  public native String inject(int type,int code,int value);
  这儿有一个坑,不同手机分管事件的设备不一样,比如上面的event21再其他手机上可能就不是这个了
  解决方法:遍历dev/input下所有设备往里面写数据(有点小暴力)
  Ok,事件注入到这就算没问题了,为了方便我们可以再在上层写一个Server程序供程序供Robotium Case中调用
  当然这就涉及到IPC通信
  Android IPC通信实现方式大概分为:广播、aidl等,封装完了剩下的就是Case中调用了(这里就不讲了相信大家都会)
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号