mirror of
https://github.com/revanced/jadb.git
synced 2024-11-19 10:39:23 +01:00
Refactor: Adding dummy test for getprop function.
- The FakeServer can now return a string as stdout.
This commit is contained in:
parent
9c83f0320c
commit
5b66a90c3b
@ -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;
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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<String, String> 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();
|
||||
|
Loading…
Reference in New Issue
Block a user