blob: 5ceab01328b02f8555f3b5d4be8020cd778992b2 [file] [log] [blame]
Daichi Hironob255f582015-12-02 12:20:25 +09001/*
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
Daichi Hironob255f582015-12-02 12:20:25 +090019import android.hardware.usb.UsbDevice;
20import android.hardware.usb.UsbDeviceConnection;
21import android.hardware.usb.UsbManager;
Daichi Hirono0f325372016-02-21 15:50:30 +090022import android.mtp.MtpConstants;
Daichi Hirono1d4779c2016-01-06 16:43:32 +090023import android.os.SystemClock;
Daichi Hironob255f582015-12-02 12:20:25 +090024
Daichi Hironof578fa22016-02-19 18:05:42 +090025import java.io.FileNotFoundException;
Daichi Hironob255f582015-12-02 12:20:25 +090026import java.io.IOException;
27import java.util.HashMap;
Daichi Hirono1d4779c2016-01-06 16:43:32 +090028import java.util.Objects;
29
Daichi Hironob255f582015-12-02 12:20:25 +090030/**
31 * Static utility methods for testing.
32 */
Daichi Hirono99b58052015-12-03 18:09:16 +090033final class TestUtil {
Daichi Hironob255f582015-12-02 12:20:25 +090034 private TestUtil() {}
35
Daichi Hirono0f325372016-02-21 15:50:30 +090036 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 Hirono61ba9232016-02-26 12:58:39 +090040 MtpConstants.OPERATION_DELETE_OBJECT,
Daichi Hirono0f325372016-02-21 15:50:30 +090041 };
42
Daichi Hironob255f582015-12-02 12:20:25 +090043 /**
44 * Requests permission for a MTP device and returns the first MTP device that has at least one
45 * storage.
Daichi Hironob255f582015-12-02 12:20:25 +090046 */
47 static UsbDevice setupMtpDevice(
48 TestResultInstrumentation instrumentation,
49 UsbManager usbManager,
Daichi Hirono1d4779c2016-01-06 16:43:32 +090050 MtpManager manager) {
51 while (true) {
Daichi Hironob255f582015-12-02 12:20:25 +090052 try {
Daichi Hironoab03cb12016-01-11 15:47:21 +090053 final UsbDevice device = findMtpDevice(usbManager, manager);
Daichi Hironob255f582015-12-02 12:20:25 +090054 waitForStorages(instrumentation, manager, device.getDeviceId());
55 return device;
56 } catch (IOException exp) {
Daichi Hirono1d4779c2016-01-06 16:43:32 +090057 instrumentation.show(Objects.toString(exp.getMessage()));
58 SystemClock.sleep(1000);
Daichi Hironob255f582015-12-02 12:20:25 +090059 // 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 Hironob255f582015-12-02 12:20:25 +090065 }
66
Daichi Hironof578fa22016-02-19 18:05:42 +090067 static void addTestDevice(MtpDatabase database) throws FileNotFoundException {
68 database.getMapper().startAddingDocuments(null);
69 database.getMapper().putDeviceDocument(new MtpDeviceRecord(
Daichi Hirono0f325372016-02-21 15:50:30 +090070 0, "Device", "device_key", /* opened is */ true, new MtpRoot[0],
71 OPERATIONS_SUPPORTED, null));
Daichi Hironof578fa22016-02-19 18:05:42 +090072 database.getMapper().stopAddingDocuments(null);
73 }
74
75 static void addTestStorage(MtpDatabase database, String parentId) throws FileNotFoundException {
76 database.getMapper().startAddingDocuments(parentId);
Daichi Hirono0f325372016-02-21 15:50:30 +090077 database.getMapper().putStorageDocuments(parentId, OPERATIONS_SUPPORTED, new MtpRoot[] {
Daichi Hironof578fa22016-02-19 18:05:42 +090078 new MtpRoot(0, 100, "Storage", 1024, 1024, ""),
79 });
80 database.getMapper().stopAddingDocuments(parentId);
81 }
82
Daichi Hironob255f582015-12-02 12:20:25 +090083 private static UsbDevice findMtpDevice(
Daichi Hironoaf5ea382016-01-07 18:24:59 +090084 UsbManager usbManager,
Daichi Hironoab03cb12016-01-11 15:47:21 +090085 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 Hironob255f582015-12-02 12:20:25 +090089 }
Daichi Hironoab03cb12016-01-11 15:47:21 +090090 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 Hironob255f582015-12-02 12:20:25 +0900104 }
105
Daichi Hironob255f582015-12-02 12:20:25 +0900106 private static void waitForStorages(
107 TestResultInstrumentation instrumentation,
108 MtpManager manager,
Daichi Hirono1d4779c2016-01-06 16:43:32 +0900109 int deviceId) throws IOException {
Daichi Hironob255f582015-12-02 12:20:25 +0900110 while (true) {
Daichi Hirono20754c52015-12-15 18:52:26 +0900111 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 Hironob255f582015-12-02 12:20:25 +0900122 instrumentation.show("Wait for storages.");
Daichi Hirono1d4779c2016-01-06 16:43:32 +0900123 SystemClock.sleep(1000);
Daichi Hironob255f582015-12-02 12:20:25 +0900124 continue;
125 }
126 return;
127 }
128 }
129}