diff --git a/src/se/vidstige/jadb/server/AdbDeviceResponder.java b/src/se/vidstige/jadb/server/AdbDeviceResponder.java index 27b2421..459632c 100644 --- a/src/se/vidstige/jadb/server/AdbDeviceResponder.java +++ b/src/se/vidstige/jadb/server/AdbDeviceResponder.java @@ -4,6 +4,8 @@ import se.vidstige.jadb.JadbException; import se.vidstige.jadb.RemoteFile; import java.io.ByteArrayOutputStream; +import java.io.DataInput; +import java.io.DataOutputStream; import java.io.IOException; /** @@ -16,5 +18,5 @@ public interface AdbDeviceResponder { void filePushed(RemoteFile path, int mode, ByteArrayOutputStream buffer) throws JadbException; void filePulled(RemoteFile path, ByteArrayOutputStream buffer) throws JadbException, IOException; - void shell(String command) throws IOException; + void shell(String command, DataOutputStream stdout, DataInput stdin) throws IOException; } diff --git a/src/se/vidstige/jadb/server/AdbProtocolHandler.java b/src/se/vidstige/jadb/server/AdbProtocolHandler.java index 52c94ba..a661a87 100644 --- a/src/se/vidstige/jadb/server/AdbProtocolHandler.java +++ b/src/se/vidstige/jadb/server/AdbProtocolHandler.java @@ -81,8 +81,11 @@ class AdbProtocolHandler implements Runnable { sync.send("FAIL", e.getMessage()); } } else if (command.startsWith("shell:")) { - shell(command.substring("shell:".length())); + String shellCommand = command.substring("shell:".length()); output.writeBytes("OKAY"); + shell(shellCommand, output, input); + output.close(); + return; } else { throw new ProtocolException("Unknown command: " + command); } @@ -94,8 +97,8 @@ class AdbProtocolHandler implements Runnable { } } - private void shell(String command) throws IOException { - selected.shell(command); + private void shell(String command, DataOutputStream stdout, DataInput stdin) throws IOException { + selected.shell(command, stdout, stdin); } private int readInt(DataInput input) throws IOException { diff --git a/test/se/vidstige/jadb/test/fakes/FakeAdbServer.java b/test/se/vidstige/jadb/test/fakes/FakeAdbServer.java index 3cf3f32..fc6b39c 100644 --- a/test/se/vidstige/jadb/test/fakes/FakeAdbServer.java +++ b/test/se/vidstige/jadb/test/fakes/FakeAdbServer.java @@ -7,6 +7,8 @@ import se.vidstige.jadb.server.AdbResponder; import se.vidstige.jadb.server.AdbServer; import java.io.ByteArrayOutputStream; +import java.io.DataInput; +import java.io.DataOutputStream; import java.io.IOException; import java.net.ProtocolException; import java.nio.charset.Charset; @@ -76,8 +78,8 @@ public class FakeAdbServer implements AdbResponder { return findBySerial(serial).expectPull(path); } - public void expectShell(String serial, String commands) { - findBySerial(serial).expectShell(commands); + public DeviceResponder.ShellExpectation expectShell(String serial, String commands) { + return findBySerial(serial).expectShell(commands); } @Override @@ -131,11 +133,11 @@ public class FakeAdbServer implements AdbResponder { } @Override - public void shell(String command) throws IOException { + public void shell(String command, DataOutputStream stdout, DataInput stdin) throws IOException { for (ShellExpectation se : shellExpectations) { if (se.matches(command)) { shellExpectations.remove(se); - se.throwIfFail(); + se.writeOutputTo(stdout); return; } } @@ -192,6 +194,7 @@ public class FakeAdbServer implements AdbResponder { public class ShellExpectation { private final String command; + private byte[] stdout; public ShellExpectation(String command) { this.command = command; @@ -201,8 +204,12 @@ public class FakeAdbServer implements AdbResponder { return command.equals(this.command); } - public void throwIfFail() { + public void returns(String stdout) { + this.stdout = stdout.getBytes(Charset.forName("utf-8")); + } + public void writeOutputTo(DataOutputStream stdout) throws IOException { + stdout.write(this.stdout); } } @@ -220,7 +227,7 @@ public class FakeAdbServer implements AdbResponder { public ShellExpectation expectShell(String command) { ShellExpectation expectation = new ShellExpectation(command); - shellExpectations.add(new ShellExpectation(command)); + shellExpectations.add(expectation); return expectation; } } diff --git a/test/se/vidstige/jadb/test/unit/MockedTestCases.java b/test/se/vidstige/jadb/test/unit/MockedTestCases.java index f311818..ed01a9c 100644 --- a/test/se/vidstige/jadb/test/unit/MockedTestCases.java +++ b/test/se/vidstige/jadb/test/unit/MockedTestCases.java @@ -17,6 +17,7 @@ import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.List; +import java.util.Map; public class MockedTestCases { @@ -93,6 +94,15 @@ public class MockedTestCases { device.executeShell("ls", "space file"); } + @Test + public void testGetProps() throws Exception { + server.add("serial-123"); + server.expectShell("serial-123", "getprop").returns("[] = nope\nx\n("); + JadbDevice device = connection.getDevices().get(0); + Map x = device.getprop(); + Assert.assertEquals(0, x.size()); + } + private long parseDate(String date) throws ParseException { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); return dateFormat.parse(date).getTime();