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

Commit ab03cb1b authored by Daichi Hirono's avatar Daichi Hirono
Browse files

Clean up TestUtil.

 * Integrate two while loops waiting for valid device.
 * Try to open a device just after getting an device ownership so that
   other applicaitons do not steal device ownership before TestUtil
   opens a device.

Change-Id: Ia273cfb2a47fe630efd8c54b22d6ef5823a402b8
parent 263ce2b5
Loading
Loading
Loading
Loading
+18 −34
Original line number Diff line number Diff line
@@ -42,9 +42,8 @@ final class TestUtil {
            UsbManager usbManager,
            MtpManager manager) {
        while (true) {
            final UsbDevice device = findMtpDevice(instrumentation, usbManager, manager);
            try {
                manager.openDevice(device.getDeviceId());
                final UsbDevice device = findMtpDevice(usbManager, manager);
                waitForStorages(instrumentation, manager, device.getDeviceId());
                return device;
            } catch (IOException exp) {
@@ -59,29 +58,16 @@ final class TestUtil {
    }

    private static UsbDevice findMtpDevice(
            TestResultInstrumentation instrumentation,
            UsbManager usbManager,
            MtpManager manager) {
        while (true) {
            MtpManager manager) throws IOException {
        final HashMap<String,UsbDevice> devices = usbManager.getDeviceList();
        if (devices.size() == 0) {
                instrumentation.show("Wait for devices.");
                SystemClock.sleep(1000);
                continue;
            throw new IOException("Device not found.");
        }
        final UsbDevice device = devices.values().iterator().next();
            try {
                manager.openDevice(device.getDeviceId());
            } catch (IOException e) {
                // Maybe other application is using the device.
                // Force to obtain ownership of the device so that we can use the device next call
                // of findMtpDevice.
                instrumentation.show("Tries to get ownership of MTP device.");
        // Tries to get ownership of the device in case that another application use it.
        if (usbManager.hasPermission(device)) {
            final UsbDeviceConnection connection = usbManager.openDevice(device);
                if (connection == null) {
                    Assert.fail("Cannot open USB connection.");
                    return null;
                }
            for (int i = 0; i < device.getInterfaceCount(); i++) {
                // Since the test runs real environment, we need to call claim interface with
                // force = true to rob interfaces from other applications.
@@ -89,12 +75,10 @@ final class TestUtil {
                connection.releaseInterface(device.getInterface(i));
            }
            connection.close();
                SystemClock.sleep(1000);
                continue;
        }
        manager.openDevice(device.getDeviceId());
        return device;
    }
    }

    private static void waitForStorages(
            TestResultInstrumentation instrumentation,