add unit test for getState

This commit is contained in:
Giemsa 2016-10-27 01:47:05 +09:00 committed by Samuel Carlsson
parent 4486bd0a7a
commit 86ccd4ab42
3 changed files with 48 additions and 4 deletions

View File

@ -86,6 +86,31 @@ class AdbProtocolHandler implements Runnable {
shell(shellCommand, output, input);
output.close();
return;
} else if ("host:get-state".equals(command)) {
// TODO: Check so that exactly one device is selected.
AdbDeviceResponder device = responder.getDevices().get(0);
output.writeBytes("OKAY");
send(output, device.getType());
} else if (command.startsWith("host-serial:")) {
String[] strs = command.split(":",0);
if (strs.length != 3) {
throw new ProtocolException("Invalid command: " + command);
}
String serial = strs[1];
boolean found = false;
output.writeBytes("OKAY");
for (AdbDeviceResponder d : responder.getDevices()) {
if (d.getSerial().equals(serial)) {
send(output, d.getType());
found = true;
break;
}
}
if (!found) {
send(output, "unknown");
}
} else {
throw new ProtocolException("Unknown command: " + command);
}
@ -146,4 +171,4 @@ class AdbProtocolHandler implements Runnable {
writer.writeBytes(getCommandLength(response));
writer.writeBytes(response);
}
}
}

View File

@ -47,7 +47,11 @@ public class FakeAdbServer implements AdbResponder {
}
public void add(String serial) {
devices.add(new DeviceResponder(serial));
devices.add(new DeviceResponder(serial, "device"));
}
public void add(String serial, String type) {
devices.add(new DeviceResponder(serial, type));
}
public void verifyExpectations() {
@ -89,11 +93,13 @@ public class FakeAdbServer implements AdbResponder {
private class DeviceResponder implements AdbDeviceResponder {
private final String serial;
private final String type;
private List<FileExpectation> fileExpectations = new ArrayList<FileExpectation>();
private List<ShellExpectation> shellExpectations = new ArrayList<ShellExpectation>();
private DeviceResponder(String serial) {
private DeviceResponder(String serial, String type) {
this.serial = serial;
this.type = type;
}
@Override
@ -103,7 +109,7 @@ public class FakeAdbServer implements AdbResponder {
@Override
public String getType() {
return "device";
return type;
}
@Override

View File

@ -45,6 +45,19 @@ public class MockedTestCases {
Assert.assertEquals("serial-123", devices.get(0).getSerial());
}
@Test
public void testGetDeviceState() throws Exception {
server.add("serial-1", "offline");
server.add("serial-2", "device");
server.add("serial-3", "unknown");
server.add("serial-4", "foobar");
List<JadbDevice> devices = connection.getDevices();
Assert.assertEquals(JadbDevice.State.Offline, devices.get(0).getState());
Assert.assertEquals(JadbDevice.State.Device, devices.get(1).getState());
Assert.assertEquals(JadbDevice.State.Unknown, devices.get(2).getState());
Assert.assertEquals(JadbDevice.State.Unknown, devices.get(3).getState());
}
@Test
public void testListNoDevices() throws Exception {
List<JadbDevice> devices = connection.getDevices();