jadb/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java

211 lines
7.3 KiB
Java
Raw Normal View History

package se.vidstige.jadb.test.integration;
2013-07-25 21:13:20 +02:00
import org.junit.*;
2016-07-26 11:20:42 +02:00
import org.junit.rules.TemporaryFolder;
import se.vidstige.jadb.*;
2013-07-25 21:13:20 +02:00
import java.io.*;
2017-03-20 13:17:38 +01:00
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
2016-03-02 21:06:19 +01:00
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
2016-03-02 21:06:19 +01:00
import static org.junit.Assert.*;
public class RealDeviceTestCases {
2013-07-25 21:13:20 +02:00
2016-07-26 11:20:42 +02:00
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder(); //Must be public
private JadbConnection jadb;
2016-07-26 11:20:42 +02:00
@BeforeClass
public static void tryToStartAdbServer() {
try {
new AdbServerLauncher(new Subprocess(), System.getenv()).launch();
2018-08-06 13:18:09 +02:00
} catch (IOException | InterruptedException e) {
System.out.println("Could not start adb-server");
}
}
@Before
public void connect() throws IOException {
try {
jadb = new JadbConnection();
jadb.getHostVersion();
} catch (Exception e) {
org.junit.Assume.assumeNoException(e);
}
}
@Test
public void testGetHostVersion() throws Exception {
jadb.getHostVersion();
}
@Test
public void testGetDevices() throws Exception {
List<JadbDevice> actual = jadb.getDevices();
2016-11-03 21:53:13 +01:00
Assert.assertNotNull(actual);
//Assert.assertEquals("emulator-5554", actual.get(0).getSerial());
}
2014-03-19 10:06:40 +01:00
@Test
public void testListFilesTwice() throws Exception {
2014-03-20 09:50:17 +01:00
JadbDevice any = jadb.getAnyDevice();
for (RemoteFile f : any.list("/")) {
System.out.println(f.getPath());
}
2016-03-02 21:06:19 +01:00
for (RemoteFile f : any.list("/")) {
System.out.println(f.getPath());
}
2014-03-19 10:06:40 +01:00
}
@Test
public void testPushFile() throws Exception {
2014-03-20 09:50:17 +01:00
JadbDevice any = jadb.getAnyDevice();
any.push(new File("README.md"), new RemoteFile("/sdcard/README.md"));
//second read on the same device
any.push(new File("README.md"), new RemoteFile("/sdcard/README.md"));
}
@Test(expected = JadbException.class)
public void testPushFileToInvalidPath() throws Exception {
JadbDevice any = jadb.getAnyDevice();
any.push(new File("README.md"), new RemoteFile("/no/such/directory/README.md"));
}
@Test
public void testPullFile() throws Exception {
JadbDevice any = jadb.getAnyDevice();
2016-07-26 11:20:42 +02:00
any.pull(new RemoteFile("/sdcard/README.md"), temporaryFolder.newFile("foobar.md"));
//second read on the same device
2016-07-26 11:20:42 +02:00
any.pull(new RemoteFile("/sdcard/README.md"), temporaryFolder.newFile("foobar.md"));
}
@Test(expected = JadbException.class)
public void testPullInvalidFile() throws Exception {
JadbDevice any = jadb.getAnyDevice();
2016-07-26 11:20:42 +02:00
any.pull(new RemoteFile("/file/does/not/exist"), temporaryFolder.newFile("xyz"));
}
2016-11-03 21:53:13 +01:00
@SuppressWarnings("deprecation")
@Test
public void testShellExecuteTwice() throws Exception {
2016-02-28 10:54:33 +01:00
JadbDevice any = jadb.getAnyDevice();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
any.executeShell(bout, "ls /");
any.executeShell(bout, "ls", "-la", "/");
byte[] buf = bout.toByteArray();
System.out.write(buf, 0, buf.length);
}
@Test
public void testShellProcessBuilderStart() throws Exception {
JadbDevice any = jadb.getAnyDevice();
Process process = any.shellProcessBuilder("ls /").start();
AtomicReference<String> stdout = new AtomicReference<>();
AtomicReference<String> stderr = new AtomicReference<>();
Thread thread1 = gobbler(process.getInputStream(), stdout);
Thread thread2 = gobbler(process.getErrorStream(), stderr);
thread1.start();
thread2.start();
process.waitFor();
thread1.join();
thread2.join();
System.out.println(stdout.get());
System.out.println(stderr.get());
}
private Thread gobbler(final InputStream stream, final AtomicReference<String> out) {
return new Thread(new Runnable() {
@Override
public void run() {
out.set(new Scanner(stream).useDelimiter("\\A").next());
}
});
}
@Test
public void testShellExecuteProcessRedirectToOutputStream() throws Exception {
JadbDevice any = jadb.getAnyDevice();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream err = new ByteArrayOutputStream();
Process process = any.shellProcessBuilder("ls /")
.redirectOutput(out)
.redirectError(err)
.start();
process.waitFor();
System.out.println(out.toString(StandardCharsets.UTF_8.name()));
System.out.println(err.toString(StandardCharsets.UTF_8.name()));
}
@Test
public void testShellExecuteProcessRedirectErrorStream() throws Exception {
JadbDevice any = jadb.getAnyDevice();
Process process = any.shellProcessBuilder("ls /").redirectErrorStream(true).start();
String stdout = new Scanner(process.getInputStream()).useDelimiter("\\A").next();
process.waitFor();
System.out.println(stdout);
}
@Test
public void testShellExecuteProcessDestroy() throws Exception {
JadbDevice anyDevice = jadb.getAnyDevice();
ExecutorService executor = Executors.newSingleThreadExecutor();
ShellProcess process = anyDevice.shellProcessBuilder("sleep 30").redirectErrorStream(true).useExecutor(executor).start();
process.destroy();
assertEquals(process.waitFor(), 9);
executor.shutdown();
assertTrue(executor.awaitTermination(5, TimeUnit.SECONDS));
2016-02-28 10:54:33 +01:00
}
@Test
public void testScreenshot() throws Exception {
JadbDevice any = jadb.getAnyDevice();
2018-08-06 13:22:55 +02:00
try (FileOutputStream outputStream = new FileOutputStream(temporaryFolder.newFile("screenshot.png"))) {
InputStream stdout = any.executeShell("screencap", "-p");
Stream.copy(stdout, outputStream);
}
}
2017-03-17 17:48:13 +01:00
2017-03-20 11:48:24 +01:00
/**
* This test requires emulator running on non-standard tcp port - this may be achieve by executing such command:
* ${ANDROID_HOME}/emulator -verbose -avd ${NAME} -ports 10000,10001
*
* @throws IOException
* @throws JadbException
* @throws ConnectionToRemoteDeviceException
*/
2017-03-17 17:48:13 +01:00
@Test
public void testConnectionToTcpDevice() throws IOException, JadbException, ConnectionToRemoteDeviceException {
2017-03-20 13:17:38 +01:00
jadb.connectToTcpDevice(new InetSocketAddress("127.0.0.1", 10001));
List<JadbDevice> devices = jadb.getDevices();
assertNotNull(devices);
assertFalse(devices.isEmpty());
2017-03-17 17:48:13 +01:00
}
2017-03-20 11:48:24 +01:00
/**
* @throws IOException
* @throws JadbException
* @throws ConnectionToRemoteDeviceException
* @see #testConnectionToTcpDevice()
2017-03-20 11:48:24 +01:00
*/
@Test
public void testDisconnectionToTcpDevice() throws IOException, JadbException, ConnectionToRemoteDeviceException {
testConnectionToTcpDevice();
2017-03-20 13:17:38 +01:00
jadb.disconnectFromTcpDevice(new InetSocketAddress("127.0.0.1", 10001));
2017-03-20 11:48:24 +01:00
jadb.getDevices();
List<JadbDevice> devices = jadb.getDevices();
assertNotNull(devices);
assertTrue(devices.isEmpty());
}
2013-07-25 21:13:20 +02:00
}