summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--packages/MtpDocumentsProvider/tests/src/com/android/mtp/MtpManagerTest.java28
-rw-r--r--packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestResultInstrumentation.java4
-rw-r--r--packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestUtil.java7
3 files changed, 24 insertions, 15 deletions
diff --git a/packages/MtpDocumentsProvider/tests/src/com/android/mtp/MtpManagerTest.java b/packages/MtpDocumentsProvider/tests/src/com/android/mtp/MtpManagerTest.java
index a045d06facf0..ed617e71aeb2 100644
--- a/packages/MtpDocumentsProvider/tests/src/com/android/mtp/MtpManagerTest.java
+++ b/packages/MtpDocumentsProvider/tests/src/com/android/mtp/MtpManagerTest.java
@@ -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() {
diff --git a/packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestResultInstrumentation.java b/packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestResultInstrumentation.java
index 3e64f9a2917a..4e4cf7833dd0 100644
--- a/packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestResultInstrumentation.java
+++ b/packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestResultInstrumentation.java
@@ -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;
diff --git a/packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestUtil.java b/packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestUtil.java
index e6c12cb9e10b..f91032147fff 100644
--- a/packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestUtil.java
+++ b/packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestUtil.java
@@ -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.");