summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Daichi Hirono <hirono@google.com> 2016-01-11 08:00:35 +0000
committer Android (Google) Code Review <android-gerrit@google.com> 2016-01-11 08:00:35 +0000
commitdf7a1d69eab05f643bc7ec8bc43e93b441a5b7ad (patch)
tree6df968f7874d52fce382ff996fe341a5b2694316
parent5eeefba37c8ef7fe6fed3cfc37d9f18f9d6e34b2 (diff)
parentab03cb1b469940ab672850e0d2de3c78025260d3 (diff)
Merge "Clean up TestUtil."
-rw-r--r--packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestUtil.java52
1 files changed, 18 insertions, 34 deletions
diff --git a/packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestUtil.java b/packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestUtil.java
index 2935267d9f07..ffcc0882e019 100644
--- a/packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestUtil.java
+++ b/packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestUtil.java
@@ -42,9 +42,8 @@ final class TestUtil {
UsbManager usbManager,
MtpManager manager) {
while (true) {
- final UsbDevice device = findMtpDevice(instrumentation, usbManager, manager);
try {
- manager.openDevice(device.getDeviceId());
+ final UsbDevice device = findMtpDevice(usbManager, manager);
waitForStorages(instrumentation, manager, device.getDeviceId());
return device;
} catch (IOException exp) {
@@ -59,41 +58,26 @@ final class TestUtil {
}
private static UsbDevice findMtpDevice(
- TestResultInstrumentation instrumentation,
UsbManager usbManager,
- MtpManager manager) {
- while (true) {
- final HashMap<String,UsbDevice> devices = usbManager.getDeviceList();
- if (devices.size() == 0) {
- instrumentation.show("Wait for devices.");
- SystemClock.sleep(1000);
- continue;
- }
- final UsbDevice device = devices.values().iterator().next();
- try {
- manager.openDevice(device.getDeviceId());
- } catch (IOException e) {
- // Maybe other application is using the device.
- // Force to obtain ownership of the device so that we can use the device next call
- // of findMtpDevice.
- instrumentation.show("Tries to get ownership of MTP device.");
- final UsbDeviceConnection connection = usbManager.openDevice(device);
- if (connection == null) {
- Assert.fail("Cannot open USB connection.");
- return null;
- }
- 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();
- SystemClock.sleep(1000);
- continue;
+ MtpManager manager) throws IOException {
+ final HashMap<String,UsbDevice> devices = usbManager.getDeviceList();
+ if (devices.size() == 0) {
+ throw new IOException("Device not found.");
+ }
+ final UsbDevice device = devices.values().iterator().next();
+ // Tries to get ownership of the device in case that another application use it.
+ if (usbManager.hasPermission(device)) {
+ 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));
}
- return device;
+ connection.close();
}
+ manager.openDevice(device.getDeviceId());
+ return device;
}
private static void waitForStorages(