3, MonkeyRunner进程启动
在测试脚本中,开始通常会调用MonkeyRunner的waitForConnection方法连接设备。
流程图如下,
MonkeyRunner.waitForConnection();
waitForConnection方法如下,
public static MonkeyDevice waitForConnection(PyObject[] args, String[] kws)
{
ArgParser ap = JythonUtils.createArgParser(args, kws);
Preconditions.checkNotNull(ap);
long timeoutMs;
try
{
double timeoutInSecs = JythonUtils.getFloat(ap, 0);
timeoutMs = (timeoutInSecs * 1000.0D);
}
catch (PyException e)
{
timeoutMs = 9223372036854775807L;
}
IChimpDevice device = chimpchat.waitForConnection(timeoutMs, ap.getString(1, ".*"));
MonkeyDevice chimpDevice = new MonkeyDevice(device);
return chimpDevice;
}
直接调用ChimpChat对象的waitForConnection方法,然后构造MonkeyDevice对象,
MonkeyDevice后面论述。
AdbBackend的waitForConnection方法如下,
public IChimpDevice waitForConnection(long timeoutMs, String deviceIdRegex)
{
do
{
IDevice device = findAttachedDevice(deviceIdRegex);
if ((device != null) && (device.getState() == IDevice.DeviceState.ONLINE))
{
IChimpDevice chimpDevice = new AdbChimpDevice(device);
this.devices.add(chimpDevice);
return chimpDevice;
}
try
{
Thread.sleep(200L);
}
catch (InterruptedException e)
{
LOG.log(Level.SEVERE, "Error sleeping", e);
}
timeoutMs -= 200L;
} while (timeoutMs > 0L);
return null;
}
该方法会做一个while循环一直等待找到一个跟用户提供的或者默认的序列号吻合的设备知道超时,
如果找到了该设备就会以该设备作为参数去构造一个AdbChimpDevice的实例对象并返回。
3.1 获取设备
findAttachedDevice方法如下,
private IDevice findAttachedDevice(String deviceIdRegex)
{
Pattern pattern = Pattern.compile(deviceIdRegex);
for (IDevice device : this.bridge.getDevices())
{
String serialNumber = device.getSerialNumber();
if (pattern.matcher(serialNumber).matches()) {
return device;
}
}
return null;
}
最后一直调用DeviceMonitor的getDevices方法,第三章中启动adb然后将设备都添加到列表中,在此直接返回该列表。
3.2 AdbChimpDevice对象
流程图如下,
AdbChimpDevice的构造方法如下,
public AdbChimpDevice(IDevice device)
{
this.device = device;
this.manager = createManager("127.0.0.1", 12345);
Preconditions.checkNotNull(this.manager);
}
createManager方法如下,
private ChimpManager createManager(String address, int port)
{
try
{
this.device.createForward(port, port);
}
•••
Socket monkeySocket;
try
{
monkeySocket = new Socket(addr, port);
}
•••
try
{
mm = new ChimpManager(monkeySocket);
}
•••
1, 调用Device的createForward的方法启动monkey进程
2, 创建连接到本机monkey转发端口的Socket对象, 根据该Socket对象构造ChimpManager实例。
最后AdbHelper的createForward方法如下,
public static void createForward(InetSocketAddress adbSockAddr, Device device, String localPortSpec, String remotePortSpec)
throws TimeoutException, AdbCommandRejectedException, IOException
{
SocketChannel adbChan = null;
try
{
adbChan = SocketChannel.open(adbSockAddr);
adbChan.configureBlocking(false);
byte[] request = formAdbRequest(String.format("host-serial:%1$s:forward:%2$s;%3$s", new Object[] { device.getSerialNumber(), localPortSpec, remotePortSpec }));
write(adbChan, request);
AdbResponse resp = readAdbResponse(adbChan, false);
if (!resp.okay)
{
Log.w("create-forward", "Error creating forward: " + resp.message);
throw new AdbCommandRejectedException(resp.message);
}
}
finally
{
if (adbChan != null) {
adbChan.close();
}
}
}
device类里面的createForward方法来把主机pc端本地的端口转发给目标机器端的monkey监听端口,
这样子做的好处是我们通过直接连接主机pc端的转发端口发送命令就会等同于通过网络连接上目标机器的monkey监听端口来发送monkey命令。