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

Commit 3a6212ae authored by Daichi Hirono's avatar Daichi Hirono
Browse files

Clean MtpManagerTest code.

 * Use try-with-resource block to ensure close auto-closeable stream.
 * Define local variables just above the place where they are used.

BUG=None

Change-Id: I9d6c952ebac096c51567a4a787b2bc9fff6502de
parent 537427bd
Loading
Loading
Loading
Loading
+14 −16
Original line number Diff line number Diff line
@@ -101,9 +101,6 @@ public class MtpManagerTest extends InstrumentationTestCase {
    }

    public void testCreateDocumentAndGetPartialObject() throws Exception {
        final ParcelFileDescriptor[] fds = ParcelFileDescriptor.createPipe();
        final ParcelFileDescriptor.AutoCloseOutputStream stream =
                new ParcelFileDescriptor.AutoCloseOutputStream(fds[1]);
        int storageId = 0;
        for (final MtpDeviceRecord record : mManager.getDevices()) {
            if (record.deviceId == mUsbDevice.getDeviceId()) {
@@ -112,6 +109,7 @@ public class MtpManagerTest extends InstrumentationTestCase {
            }
        }
        assertTrue("Valid storage not found.", storageId != 0);

        final String testFileName = "MtpManagerTest_testFile.txt";
        for (final int handle : mManager.getObjectHandles(
                mUsbDevice.getDeviceId(), storageId, MtpManager.OBJECT_HANDLE_ROOT_CHILDREN)) {
@@ -121,11 +119,14 @@ public class MtpManagerTest extends InstrumentationTestCase {
                break;
            }
        }

        final ParcelFileDescriptor[] fds = ParcelFileDescriptor.createPipe();
        final byte[] expectedBytes = "Hello Android!".getBytes("ascii");
        final int objectHandle;
        try {
        try (final ParcelFileDescriptor.AutoCloseOutputStream stream =
                new ParcelFileDescriptor.AutoCloseOutputStream(fds[1])) {
            stream.write(expectedBytes);
            objectHandle = mManager.createDocument(
        }
        final int objectHandle = mManager.createDocument(
                mUsbDevice.getDeviceId(),
                new MtpObjectInfo.Builder()
                        .setStorageId(storageId)
@@ -134,9 +135,6 @@ public class MtpManagerTest extends InstrumentationTestCase {
                        .setFormat(MtpConstants.FORMAT_TEXT)
                        .build(),
                fds[0]);
        } finally {
            stream.close();
        }
        final byte[] bytes = new byte[100];
        assertEquals(5, mManager.getPartialObject(
                mUsbDevice.getDeviceId(), objectHandle, 0, 5, bytes));