Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 99b58052 authored by Daichi Hirono's avatar Daichi Hirono
Browse files

Polish MtpManagerTest.

Change-Id: Ibadd209ee1cb752f0e55694d0f1f631c558f1370
parent 56e086f5
Loading
Loading
Loading
Loading
+17 −11
Original line number Diff line number Diff line
@@ -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() {
        final FutureTask<Boolean> future = new FutureTask<Boolean>(
                new Callable<Boolean>() {
                    @Override
            public void run() {
                    public Boolean call() throws IOException {
                        try {
                            mManager.readEvent(mUsbDevice.getDeviceId(), signal);
                } catch (OperationCanceledException | IOException e) {
                    getInstrumentation().show(e.getMessage());
                            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() {
+4 −0
Original line number Diff line number Diff line
@@ -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;

+3 −4
Original line number Diff line number Diff line
@@ -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.");