老李推荐:第14章5节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-装备ViewServer-查询ViewServer运行状态

 

poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标。如果对课程感兴趣,请大家咨询qq:908821478,咨询电话010-84505200。

上一小节我们描述了HierarchyViewer是如何组建ADB协议命令来实现ViewServer的端口转发的,在端口转发设置好后,下一个要做的事情就是去检测目标设备端ViewServer线程是否已经启动起来了。我们进入setupViewServer调用的DeviceBridge的isViewServerRunning方法:

165     public static boolean isViewServerRunning(IDevice device) {

166         final boolean[] result = new boolean[1];

167         try {

168             if (device.isOnline()) {

169                 device.executeShellCommand(buildIsServerRunningShellCommand(),

170                         new BooleanResultReader(result));

171                 if (!result[0]) {

172                     ViewServerInfo serverInfo = loadViewServerInfo(device);

173                     if (serverInfo != null && serverInfo.protocolVersion > 2) {

174                         result[0] = true;

175                     }

176                 }

177             }

178         } catch (TimeoutException e) {

...

187         }

188         return result[0];

189     }

代码14-5-1 DeviceBridge - isViewServerRunning

关键代码是上面的169行,通过Device类的实例来往ADB服务器发送相应的命令来检测ViewServer是否已经在运行。

device.executeShellCommand在前面章节已经分析过了,就是用来发送”adb shell”命令的。

我们看下buildIsServerRunningShellCommand方法,看这个命令是如何组织起来的:

235     private static String buildIsServerRunningShellCommand() {  236         return String.format("service call window %d", SERVICE_CODE_IS_SERVER_RUNNING);

237     }  

代码14-5-2 DeviceBridge - buildIsServerRunningShellCommand

而全局变量 SERVICE_CODE_IS_SERVER_RUNNING 的定义是:

48     private static final int DEFAULT_SERVER_PORT = 4939;

49     // These codes must match the auto-generated codes in IWindowManager.java

50     // See IWindowManager.aidl as well

51     private static final int SERVICE_CODE_START_SERVER = 1;

52     private static final int SERVICE_CODE_STOP_SERVER = 2;

53     private static final int SERVICE_CODE_IS_SERVER_RUNNING = 3;

代码14-5-3 DeviceBridge - 全局变量示例

236行整出来的这一串不就是”service call window 3”嘛。所以结合device.sendShellCommand,事实上就是往设备发送了命令”adb shell service call window 3”,上一章我们才用它来查询ViewServer的运行状态了!

04-30 03:16