TestUtil.java revision 99b58052f85c18272e63047b471edfd8089c09d3
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.app.PendingIntent;
20import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.IntentFilter;
24import android.hardware.usb.UsbDevice;
25import android.hardware.usb.UsbDeviceConnection;
26import android.hardware.usb.UsbManager;
27
28import java.io.IOException;
29import java.util.HashMap;
30import java.util.concurrent.CountDownLatch;
31import junit.framework.Assert;
32
33/**
34 * Static utility methods for testing.
35 */
36final class TestUtil {
37    private static final String ACTION_USB_PERMISSION =
38            "com.android.mtp.USB_PERMISSION";
39
40    private TestUtil() {}
41
42    /**
43     * Requests permission for a MTP device and returns the first MTP device that has at least one
44     * storage.
45     */
46    static UsbDevice setupMtpDevice(
47            TestResultInstrumentation instrumentation,
48            UsbManager usbManager,
49            MtpManager manager) throws InterruptedException, IOException {
50        for (int i = 0; i < 2; i++) {
51            final UsbDevice device = findMtpDevice(instrumentation, usbManager);
52            manager.openDevice(device.getDeviceId());
53            try {
54                waitForStorages(instrumentation, manager, device.getDeviceId());
55                return device;
56            } catch (IOException exp) {
57                // When the MTP device is Android, and it changes the USB device type from
58                // "Charging" to "MTP", the device ID will be updated. We need to find a device
59                // again.
60                continue;
61            }
62        }
63        throw new IOException("Failed to obtain MTP devices");
64    }
65
66    private static UsbDevice findMtpDevice(
67            TestResultInstrumentation instrumentation,
68            UsbManager usbManager) throws InterruptedException {
69        while (true) {
70            final HashMap<String,UsbDevice> devices = usbManager.getDeviceList();
71            if (devices.size() == 0) {
72                instrumentation.show("Wait for devices.");
73                Thread.sleep(1000);
74                continue;
75            }
76            final UsbDevice device = devices.values().iterator().next();
77            requestPermission(instrumentation, usbManager, device);
78            final UsbDeviceConnection connection = usbManager.openDevice(device);
79            if (connection == null) {
80                Assert.fail("Cannot open USB connection.");
81                return null;
82            }
83            for (int i = 0; i < device.getInterfaceCount(); i++) {
84                // Since the test runs real environment, we need to call claim interface with
85                // force = true to rob interfaces from other applications.
86                connection.claimInterface(device.getInterface(i), true);
87                connection.releaseInterface(device.getInterface(i));
88            }
89            connection.close();
90            return device;
91        }
92    }
93
94    private static void requestPermission(
95            final TestResultInstrumentation instrumentation,
96            UsbManager usbManager,
97            UsbDevice device) throws InterruptedException {
98        if (usbManager.hasPermission(device)) {
99            return;
100        }
101        final CountDownLatch latch = new CountDownLatch(1);
102        final BroadcastReceiver receiver = new BroadcastReceiver() {
103            @Override
104            public void onReceive(Context context, Intent intent) {
105                latch.countDown();
106                instrumentation.getTargetContext().unregisterReceiver(this);
107            }
108        };
109        instrumentation.getTargetContext().registerReceiver(
110                receiver, new IntentFilter(ACTION_USB_PERMISSION));
111        usbManager.requestPermission(device, PendingIntent.getBroadcast(
112                instrumentation.getTargetContext(),
113                0 /* requstCode */,
114                new Intent(ACTION_USB_PERMISSION),
115                0 /* flags */));
116        latch.await();
117        Assert.assertTrue(usbManager.hasPermission(device));
118    }
119
120    private static void waitForStorages(
121            TestResultInstrumentation instrumentation,
122            MtpManager manager,
123            int deviceId) throws IOException, InterruptedException {
124        while (true) {
125            if (manager.getRoots(deviceId).length == 0) {
126                instrumentation.show("Wait for storages.");
127                Thread.sleep(1000);
128                continue;
129            }
130            return;
131        }
132    }
133}
134