MtpManagerTest.java revision 5bc41d18544d983d913bd42e067bc8b7db0378cd
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.mtp;
18
19import android.content.Context;
20import android.hardware.usb.UsbDevice;
21import android.hardware.usb.UsbDeviceConnection;
22import android.hardware.usb.UsbManager;
23import android.test.InstrumentationTestCase;
24
25import java.util.HashMap;
26
27public class MtpManagerTest extends InstrumentationTestCase {
28    @RealDeviceTest
29    public void testBasic() throws Exception {
30        final UsbDevice usbDevice = findDevice();
31        final MtpManager manager = new MtpManager(getContext());
32        manager.openDevice(usbDevice.getDeviceId());
33        waitForStorages(manager, usbDevice.getDeviceId());
34        manager.closeDevice(usbDevice.getDeviceId());
35    }
36
37    private UsbDevice findDevice() throws InterruptedException {
38        final UsbManager usbManager = getContext().getSystemService(UsbManager.class);
39        while (true) {
40            final HashMap<String,UsbDevice> devices = usbManager.getDeviceList();
41            if (devices.size() == 0) {
42                show("Wait for devices.");
43                Thread.sleep(1000);
44                continue;
45            }
46            final UsbDevice device = devices.values().iterator().next();
47            final UsbDeviceConnection connection = usbManager.openDevice(device);
48            for (int i = 0; i < device.getInterfaceCount(); i++) {
49                // Since the test runs real environment, we need to call claim interface with
50                // force = true to rob interfaces from other applications.
51                connection.claimInterface(device.getInterface(i), true);
52                connection.releaseInterface(device.getInterface(i));
53            }
54            connection.close();
55            return device;
56        }
57    }
58
59    private void waitForStorages(MtpManager manager, int deviceId) throws Exception {
60        while (true) {
61            if (manager.getRoots(deviceId).length == 0) {
62                show("Wait for storages.");
63                Thread.sleep(1000);
64                continue;
65            }
66            return;
67        }
68    }
69
70    private void show(String message) {
71        if (!(getInstrumentation() instanceof TestResultInstrumentation)) {
72            return;
73        }
74        ((TestResultInstrumentation) getInstrumentation()).show(message);
75    }
76
77    private Context getContext() {
78        return getInstrumentation().getContext();
79    }
80}
81