2016-07-29 18:51:52 +02:00
|
|
|
package se.vidstige.jadb.test.integration;
|
2013-07-25 21:13:20 +02:00
|
|
|
|
2022-06-11 18:14:23 +02:00
|
|
|
import org.junit.*;
|
2016-07-26 11:20:42 +02:00
|
|
|
import org.junit.rules.TemporaryFolder;
|
2016-03-28 21:09:00 +02:00
|
|
|
import se.vidstige.jadb.*;
|
2013-07-25 21:13:20 +02:00
|
|
|
|
2022-06-11 18:14:23 +02:00
|
|
|
import java.io.*;
|
2017-03-20 13:17:38 +01:00
|
|
|
import java.net.InetSocketAddress;
|
2022-06-11 18:14:23 +02:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2016-03-02 21:06:19 +01:00
|
|
|
import java.util.List;
|
2022-06-11 18:14:23 +02:00
|
|
|
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
|
|
|
|
2022-06-11 18:14:23 +02:00
|
|
|
import static org.junit.Assert.*;
|
2017-03-20 11:18:56 +01:00
|
|
|
|
2014-03-20 10:20:22 +01:00
|
|
|
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
|
2022-06-11 18:14:23 +02:00
|
|
|
private JadbConnection jadb;
|
2016-07-26 11:20:42 +02:00
|
|
|
|
2016-03-28 21:09:00 +02:00
|
|
|
@BeforeClass
|
2016-03-28 21:15:27 +02:00
|
|
|
public static void tryToStartAdbServer() {
|
|
|
|
try {
|
2016-09-10 09:13:29 +02:00
|
|
|
new AdbServerLauncher(new Subprocess(), System.getenv()).launch();
|
2018-08-06 13:18:09 +02:00
|
|
|
} catch (IOException | InterruptedException e) {
|
2016-03-28 21:15:27 +02:00
|
|
|
System.out.println("Could not start adb-server");
|
|
|
|
}
|
2016-03-28 21:09:00 +02:00
|
|
|
}
|
|
|
|
|
2016-03-28 16:06:44 +02:00
|
|
|
@Before
|
|
|
|
public void connect() throws IOException {
|
|
|
|
try {
|
|
|
|
jadb = new JadbConnection();
|
|
|
|
jadb.getHostVersion();
|
|
|
|
} catch (Exception e) {
|
|
|
|
org.junit.Assume.assumeNoException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 09:13:30 +01:00
|
|
|
@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);
|
2016-03-20 09:13:30 +01:00
|
|
|
//Assert.assertEquals("emulator-5554", actual.get(0).getSerial());
|
|
|
|
}
|
2014-03-19 10:06:40 +01:00
|
|
|
|
|
|
|
@Test
|
2016-03-20 09:13:30 +01:00
|
|
|
public void testListFilesTwice() throws Exception {
|
2014-03-20 09:50:17 +01:00
|
|
|
JadbDevice any = jadb.getAnyDevice();
|
2016-03-20 09:13:30 +01:00
|
|
|
for (RemoteFile f : any.list("/")) {
|
2014-03-20 11:37:04 +01:00
|
|
|
System.out.println(f.getPath());
|
2014-03-19 17:19:05 +01:00
|
|
|
}
|
2016-03-02 21:06:19 +01:00
|
|
|
|
2016-03-20 09:13:30 +01:00
|
|
|
for (RemoteFile f : any.list("/")) {
|
2016-02-28 03:04:59 +01:00
|
|
|
System.out.println(f.getPath());
|
|
|
|
}
|
2014-03-19 10:06:40 +01:00
|
|
|
}
|
2014-03-19 21:15:40 +01:00
|
|
|
|
|
|
|
@Test
|
2016-03-20 09:13:30 +01:00
|
|
|
public void testPushFile() throws Exception {
|
2014-03-20 09:50:17 +01:00
|
|
|
JadbDevice any = jadb.getAnyDevice();
|
2014-03-20 11:37:04 +01:00
|
|
|
any.push(new File("README.md"), new RemoteFile("/sdcard/README.md"));
|
2016-02-28 03:04:59 +01:00
|
|
|
//second read on the same device
|
|
|
|
any.push(new File("README.md"), new RemoteFile("/sdcard/README.md"));
|
2014-03-19 21:15:40 +01:00
|
|
|
}
|
2014-03-20 10:10:35 +01:00
|
|
|
|
2014-03-20 21:25:52 +01:00
|
|
|
@Test(expected = JadbException.class)
|
2016-03-20 09:13:30 +01:00
|
|
|
public void testPushFileToInvalidPath() throws Exception {
|
2014-03-20 21:25:52 +01:00
|
|
|
JadbDevice any = jadb.getAnyDevice();
|
|
|
|
any.push(new File("README.md"), new RemoteFile("/no/such/directory/README.md"));
|
|
|
|
}
|
|
|
|
|
2014-03-20 10:10:35 +01:00
|
|
|
@Test
|
2016-03-20 09:13:30 +01:00
|
|
|
public void testPullFile() throws Exception {
|
2014-03-20 10:10:35 +01:00
|
|
|
JadbDevice any = jadb.getAnyDevice();
|
2016-07-26 11:20:42 +02:00
|
|
|
any.pull(new RemoteFile("/sdcard/README.md"), temporaryFolder.newFile("foobar.md"));
|
2016-02-28 03:04:59 +01:00
|
|
|
//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"));
|
2014-03-20 10:10:35 +01:00
|
|
|
}
|
2014-03-20 21:31:25 +01:00
|
|
|
|
|
|
|
@Test(expected = JadbException.class)
|
2016-03-20 09:13:30 +01:00
|
|
|
public void testPullInvalidFile() throws Exception {
|
2014-03-20 21:31:25 +01:00
|
|
|
JadbDevice any = jadb.getAnyDevice();
|
2016-07-26 11:20:42 +02:00
|
|
|
any.pull(new RemoteFile("/file/does/not/exist"), temporaryFolder.newFile("xyz"));
|
2014-03-20 21:31:25 +01:00
|
|
|
}
|
2016-02-28 03:04:59 +01:00
|
|
|
|
2016-11-03 21:53:13 +01:00
|
|
|
@SuppressWarnings("deprecation")
|
2022-06-11 18:14:23 +02:00
|
|
|
@Test
|
2016-03-20 09:13:30 +01:00
|
|
|
public void testShellExecuteTwice() throws Exception {
|
2016-02-28 10:54:33 +01:00
|
|
|
JadbDevice any = jadb.getAnyDevice();
|
2017-01-28 17:41:24 +01:00
|
|
|
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
|
|
|
any.executeShell(bout, "ls /");
|
|
|
|
any.executeShell(bout, "ls", "-la", "/");
|
2022-06-11 18:14:23 +02:00
|
|
|
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
|
|
|
}
|
2016-03-28 09:41:53 +02: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"))) {
|
2016-04-27 20:28:21 +02:00
|
|
|
InputStream stdout = any.executeShell("screencap", "-p");
|
|
|
|
Stream.copy(stdout, outputStream);
|
|
|
|
}
|
2016-03-28 09:41:53 +02:00
|
|
|
}
|
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));
|
2017-03-20 11:18:56 +01:00
|
|
|
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
|
2022-06-11 18:14:23 +02:00
|
|
|
* @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
|
|
|
}
|