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

Commit 2e95181a authored by Daichi Hirono's avatar Daichi Hirono Committed by Android (Google) Code Review
Browse files

Merge "Add simple unit test for MtpManager which uses real MTP device."

parents c68b9df8 5bc41d18
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@

    <instrumentation android:name="com.android.mtp.TestResultInstrumentation"
        android:targetPackage="com.android.mtp"
        android:label="Tests for MtpDocumentsProvider" />

        android:label="Tests for MtpDocumentsProvider with the UI for output." />
    <instrumentation android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.android.mtp"
        android:label="Tests for MtpDocumentsProvider." />
</manifest>
+80 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.mtp;

import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbManager;
import android.test.InstrumentationTestCase;

import java.util.HashMap;

public class MtpManagerTest extends InstrumentationTestCase {
    @RealDeviceTest
    public void testBasic() throws Exception {
        final UsbDevice usbDevice = findDevice();
        final MtpManager manager = new MtpManager(getContext());
        manager.openDevice(usbDevice.getDeviceId());
        waitForStorages(manager, usbDevice.getDeviceId());
        manager.closeDevice(usbDevice.getDeviceId());
    }

    private UsbDevice findDevice() throws InterruptedException {
        final UsbManager usbManager = getContext().getSystemService(UsbManager.class);
        while (true) {
            final HashMap<String,UsbDevice> devices = usbManager.getDeviceList();
            if (devices.size() == 0) {
                show("Wait for devices.");
                Thread.sleep(1000);
                continue;
            }
            final UsbDevice device = devices.values().iterator().next();
            final UsbDeviceConnection connection = usbManager.openDevice(device);
            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.
                connection.claimInterface(device.getInterface(i), true);
                connection.releaseInterface(device.getInterface(i));
            }
            connection.close();
            return device;
        }
    }

    private void waitForStorages(MtpManager manager, int deviceId) throws Exception {
        while (true) {
            if (manager.getRoots(deviceId).length == 0) {
                show("Wait for storages.");
                Thread.sleep(1000);
                continue;
            }
            return;
        }
    }

    private void show(String message) {
        if (!(getInstrumentation() instanceof TestResultInstrumentation)) {
            return;
        }
        ((TestResultInstrumentation) getInstrumentation()).show(message);
    }

    private Context getContext() {
        return getInstrumentation().getContext();
    }
}
+23 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.mtp;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@interface RealDeviceTest {}
+4 −0
Original line number Diff line number Diff line
@@ -40,6 +40,10 @@ public class TestResultInstrumentation extends InstrumentationTestRunner impleme
        mHasError = false;
    }

    void show(String message) {
        TestResultActivity.show(getContext(), "    " + message);
    }

    private void show(String tag, Test test, Throwable t) {
        TestResultActivity.show(
                getContext(),