老李推荐:第14章6节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-装备ViewServer-启动ViewServer

 

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

在获得ViewServer的运行状态后,如果ViewServer还没有启动的话,HierarchyViewer的setupViewServer的下一步就会去启动ViewServer。调用的方法是DeviceBridge.startViewServer,我们进去看看:

190     public static boolean startViewServer(IDevice device) {

191         return startViewServer(device, DEFAULT_SERVER_PORT);

192     }

代码14-6-1 DeviceBridge-startViewServer

传入的参数是ddmlib的Device类的一个实例,方法很简单,直接转发给startViewServer的另一个重载函数,并且增加多一个4939(DEFAULT_SERVER_PORT)的端口号作为参数:

193     public static boolean startViewServer(IDevice device, int port) {

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

195         try {

196             if (device.isOnline()) {

197                 device.executeShellCommand(buildStartServerShellCommand(port),

198                         new BooleanResultReader(result));

199             }

200         } catch (TimeoutException e) {

...

208         }

209         return result[0];

210     }

代码14-6-2 DeviceBridge-startViewServer重载

流程跟上一小节发送”adb shell service call window 3”来查询ViewServer一样,都是先去组建命令字串,然后通过Device实例把这个命令给发出去,只是命令不一样而已。这里我们看下发送的是什么命令:

229     private static String buildStartServerShellCommand(int port) {

230         return String.format("service call window %d i32 %d", SERVICE_CODE_START_SERVER, port); //$NON-NLS-1$

231     }

代码14-6-3 DeviceBridge - BuildStartServerShellCommand

其中 SERVICE_CODE_START_SERVER 全局变量是定义为1,所以可以看到最终组合的命令就是”service call window 1 i32 4939”最终结合Device的executeShellCommand命令就等于是发送”adb shell service call window 1 i32 4939”命令来启动ViewServer来监听4939端口了。这个命令我们在第13章也已经见识过了。

04-30 03:16