Merge "Polish MtpManagerTest."

This commit is contained in:
Daichi Hirono
2015-12-04 03:34:19 +00:00
committed by Android (Google) Code Review
3 changed files with 24 additions and 15 deletions

View File

@@ -24,6 +24,9 @@ import android.os.OperationCanceledException;
import android.test.InstrumentationTestCase;
import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
@RealDeviceTest
public class MtpManagerTest extends InstrumentationTestCase {
@@ -53,20 +56,23 @@ public class MtpManagerTest extends InstrumentationTestCase {
public void testCancelEvent() throws Exception {
final CancellationSignal signal = new CancellationSignal();
final Thread thread = new Thread() {
@Override
public void run() {
try {
mManager.readEvent(mUsbDevice.getDeviceId(), signal);
} catch (OperationCanceledException | IOException e) {
getInstrumentation().show(e.getMessage());
}
}
};
final FutureTask<Boolean> future = new FutureTask<Boolean>(
new Callable<Boolean>() {
@Override
public Boolean call() throws IOException {
try {
mManager.readEvent(mUsbDevice.getDeviceId(), signal);
return false;
} catch (OperationCanceledException exception) {
return true;
}
}
});
final Thread thread = new Thread(future);
thread.start();
Thread.sleep(TIMEOUT_MS);
signal.cancel();
thread.join(TIMEOUT_MS);
assertTrue(future.get(TIMEOUT_MS, TimeUnit.MILLISECONDS));
}
private Context getContext() {

View File

@@ -23,6 +23,10 @@ import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestListener;
/**
* Instrumentation that can show the test result in the TestResultActivity.
* It's useful when it runs testcases with a real USB device and could not use USB port for ADB.
*/
public class TestResultInstrumentation extends InstrumentationTestRunner implements TestListener {
private boolean mHasError = false;

View File

@@ -33,7 +33,7 @@ import junit.framework.Assert;
/**
* Static utility methods for testing.
*/
class TestUtil {
final class TestUtil {
private static final String ACTION_USB_PERMISSION =
"com.android.mtp.USB_PERMISSION";
@@ -42,12 +42,11 @@ class TestUtil {
/**
* Requests permission for a MTP device and returns the first MTP device that has at least one
* storage.
* @throws Exception
*/
static UsbDevice setupMtpDevice(
TestResultInstrumentation instrumentation,
UsbManager usbManager,
MtpManager manager) throws Exception {
MtpManager manager) throws InterruptedException, IOException {
for (int i = 0; i < 2; i++) {
final UsbDevice device = findMtpDevice(instrumentation, usbManager);
manager.openDevice(device.getDeviceId());
@@ -121,7 +120,7 @@ class TestUtil {
private static void waitForStorages(
TestResultInstrumentation instrumentation,
MtpManager manager,
int deviceId) throws Exception {
int deviceId) throws IOException, InterruptedException {
while (true) {
if (manager.getRoots(deviceId).length == 0) {
instrumentation.show("Wait for storages.");