Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 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 | |
| 17 | package com.android.mtp; |
| 18 | |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 19 | import android.hardware.usb.UsbDevice; |
| 20 | import android.hardware.usb.UsbDeviceConnection; |
| 21 | import android.hardware.usb.UsbManager; |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 22 | import android.mtp.MtpConstants; |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 23 | import android.os.SystemClock; |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 24 | |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 25 | import java.io.FileNotFoundException; |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 26 | import java.io.IOException; |
| 27 | import java.util.HashMap; |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 28 | import java.util.Objects; |
| 29 | |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 30 | /** |
| 31 | * Static utility methods for testing. |
| 32 | */ |
Daichi Hirono | 99b5805 | 2015-12-03 18:09:16 +0900 | [diff] [blame] | 33 | final class TestUtil { |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 34 | private TestUtil() {} |
| 35 | |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 36 | static final int[] OPERATIONS_SUPPORTED = new int[] { |
| 37 | MtpConstants.OPERATION_GET_PARTIAL_OBJECT, |
| 38 | MtpConstants.OPERATION_SEND_OBJECT, |
| 39 | MtpConstants.OPERATION_SEND_OBJECT_INFO, |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 40 | MtpConstants.OPERATION_DELETE_OBJECT, |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 41 | }; |
| 42 | |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 43 | /** |
| 44 | * Requests permission for a MTP device and returns the first MTP device that has at least one |
| 45 | * storage. |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 46 | */ |
| 47 | static UsbDevice setupMtpDevice( |
| 48 | TestResultInstrumentation instrumentation, |
| 49 | UsbManager usbManager, |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 50 | MtpManager manager) { |
| 51 | while (true) { |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 52 | try { |
Daichi Hirono | ab03cb1 | 2016-01-11 15:47:21 +0900 | [diff] [blame] | 53 | final UsbDevice device = findMtpDevice(usbManager, manager); |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 54 | waitForStorages(instrumentation, manager, device.getDeviceId()); |
| 55 | return device; |
| 56 | } catch (IOException exp) { |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 57 | instrumentation.show(Objects.toString(exp.getMessage())); |
| 58 | SystemClock.sleep(1000); |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 59 | // When the MTP device is Android, and it changes the USB device type from |
| 60 | // "Charging" to "MTP", the device ID will be updated. We need to find a device |
| 61 | // again. |
| 62 | continue; |
| 63 | } |
| 64 | } |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 65 | } |
| 66 | |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 67 | static void addTestDevice(MtpDatabase database) throws FileNotFoundException { |
| 68 | database.getMapper().startAddingDocuments(null); |
| 69 | database.getMapper().putDeviceDocument(new MtpDeviceRecord( |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 70 | 0, "Device", "device_key", /* opened is */ true, new MtpRoot[0], |
| 71 | OPERATIONS_SUPPORTED, null)); |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 72 | database.getMapper().stopAddingDocuments(null); |
| 73 | } |
| 74 | |
| 75 | static void addTestStorage(MtpDatabase database, String parentId) throws FileNotFoundException { |
| 76 | database.getMapper().startAddingDocuments(parentId); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 77 | database.getMapper().putStorageDocuments(parentId, OPERATIONS_SUPPORTED, new MtpRoot[] { |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 78 | new MtpRoot(0, 100, "Storage", 1024, 1024, ""), |
| 79 | }); |
| 80 | database.getMapper().stopAddingDocuments(parentId); |
| 81 | } |
| 82 | |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 83 | private static UsbDevice findMtpDevice( |
Daichi Hirono | af5ea38 | 2016-01-07 18:24:59 +0900 | [diff] [blame] | 84 | UsbManager usbManager, |
Daichi Hirono | ab03cb1 | 2016-01-11 15:47:21 +0900 | [diff] [blame] | 85 | MtpManager manager) throws IOException { |
| 86 | final HashMap<String,UsbDevice> devices = usbManager.getDeviceList(); |
| 87 | if (devices.size() == 0) { |
| 88 | throw new IOException("Device not found."); |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 89 | } |
Daichi Hirono | ab03cb1 | 2016-01-11 15:47:21 +0900 | [diff] [blame] | 90 | final UsbDevice device = devices.values().iterator().next(); |
| 91 | // Tries to get ownership of the device in case that another application use it. |
| 92 | if (usbManager.hasPermission(device)) { |
| 93 | final UsbDeviceConnection connection = usbManager.openDevice(device); |
| 94 | for (int i = 0; i < device.getInterfaceCount(); i++) { |
| 95 | // Since the test runs real environment, we need to call claim interface with |
| 96 | // force = true to rob interfaces from other applications. |
| 97 | connection.claimInterface(device.getInterface(i), true); |
| 98 | connection.releaseInterface(device.getInterface(i)); |
| 99 | } |
| 100 | connection.close(); |
| 101 | } |
| 102 | manager.openDevice(device.getDeviceId()); |
| 103 | return device; |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 104 | } |
| 105 | |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 106 | private static void waitForStorages( |
| 107 | TestResultInstrumentation instrumentation, |
| 108 | MtpManager manager, |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 109 | int deviceId) throws IOException { |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 110 | while (true) { |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 111 | MtpDeviceRecord device = null; |
| 112 | for (final MtpDeviceRecord deviceCandidate : manager.getDevices()) { |
| 113 | if (deviceCandidate.deviceId == deviceId) { |
| 114 | device = deviceCandidate; |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | if (device == null) { |
| 119 | throw new IOException("Device was detached."); |
| 120 | } |
| 121 | if (device.roots.length == 0) { |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 122 | instrumentation.show("Wait for storages."); |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 123 | SystemClock.sleep(1000); |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 124 | continue; |
| 125 | } |
| 126 | return; |
| 127 | } |
| 128 | } |
| 129 | } |