Loading packages/MtpDocumentsProvider/tests/AndroidManifest.xml +4 −2 Original line number Diff line number Diff line Loading @@ -17,6 +17,8 @@ <instrumentation android:name="com.android.mtp.TestResultInstrumentation" android:targetPackage="com.android.mtp" android:label="Tests for MtpDocumentsProvider" /> android:label="Tests for MtpDocumentsProvider with the UI for output." /> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.android.mtp" android:label="Tests for MtpDocumentsProvider." /> </manifest> packages/MtpDocumentsProvider/tests/src/com/android/mtp/MtpManagerTest.java 0 → 100644 +80 −0 Original line number Diff line number Diff line /* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.mtp; import android.content.Context; import android.hardware.usb.UsbDevice; import android.hardware.usb.UsbDeviceConnection; import android.hardware.usb.UsbManager; import android.test.InstrumentationTestCase; import java.util.HashMap; public class MtpManagerTest extends InstrumentationTestCase { @RealDeviceTest public void testBasic() throws Exception { final UsbDevice usbDevice = findDevice(); final MtpManager manager = new MtpManager(getContext()); manager.openDevice(usbDevice.getDeviceId()); waitForStorages(manager, usbDevice.getDeviceId()); manager.closeDevice(usbDevice.getDeviceId()); } private UsbDevice findDevice() throws InterruptedException { final UsbManager usbManager = getContext().getSystemService(UsbManager.class); while (true) { final HashMap<String,UsbDevice> devices = usbManager.getDeviceList(); if (devices.size() == 0) { show("Wait for devices."); Thread.sleep(1000); continue; } final UsbDevice device = devices.values().iterator().next(); 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)); } connection.close(); return device; } } private void waitForStorages(MtpManager manager, int deviceId) throws Exception { while (true) { if (manager.getRoots(deviceId).length == 0) { show("Wait for storages."); Thread.sleep(1000); continue; } return; } } private void show(String message) { if (!(getInstrumentation() instanceof TestResultInstrumentation)) { return; } ((TestResultInstrumentation) getInstrumentation()).show(message); } private Context getContext() { return getInstrumentation().getContext(); } } packages/MtpDocumentsProvider/tests/src/com/android/mtp/RealDeviceTest.java 0 → 100644 +23 −0 Original line number Diff line number Diff line /* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.mtp; import java.lang.annotation.ElementType; import java.lang.annotation.Target; @Target(ElementType.METHOD) @interface RealDeviceTest {} packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestResultInstrumentation.java +4 −0 Original line number Diff line number Diff line Loading @@ -40,6 +40,10 @@ public class TestResultInstrumentation extends InstrumentationTestRunner impleme mHasError = false; } void show(String message) { TestResultActivity.show(getContext(), " " + message); } private void show(String tag, Test test, Throwable t) { TestResultActivity.show( getContext(), Loading Loading
packages/MtpDocumentsProvider/tests/AndroidManifest.xml +4 −2 Original line number Diff line number Diff line Loading @@ -17,6 +17,8 @@ <instrumentation android:name="com.android.mtp.TestResultInstrumentation" android:targetPackage="com.android.mtp" android:label="Tests for MtpDocumentsProvider" /> android:label="Tests for MtpDocumentsProvider with the UI for output." /> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.android.mtp" android:label="Tests for MtpDocumentsProvider." /> </manifest>
packages/MtpDocumentsProvider/tests/src/com/android/mtp/MtpManagerTest.java 0 → 100644 +80 −0 Original line number Diff line number Diff line /* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.mtp; import android.content.Context; import android.hardware.usb.UsbDevice; import android.hardware.usb.UsbDeviceConnection; import android.hardware.usb.UsbManager; import android.test.InstrumentationTestCase; import java.util.HashMap; public class MtpManagerTest extends InstrumentationTestCase { @RealDeviceTest public void testBasic() throws Exception { final UsbDevice usbDevice = findDevice(); final MtpManager manager = new MtpManager(getContext()); manager.openDevice(usbDevice.getDeviceId()); waitForStorages(manager, usbDevice.getDeviceId()); manager.closeDevice(usbDevice.getDeviceId()); } private UsbDevice findDevice() throws InterruptedException { final UsbManager usbManager = getContext().getSystemService(UsbManager.class); while (true) { final HashMap<String,UsbDevice> devices = usbManager.getDeviceList(); if (devices.size() == 0) { show("Wait for devices."); Thread.sleep(1000); continue; } final UsbDevice device = devices.values().iterator().next(); 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)); } connection.close(); return device; } } private void waitForStorages(MtpManager manager, int deviceId) throws Exception { while (true) { if (manager.getRoots(deviceId).length == 0) { show("Wait for storages."); Thread.sleep(1000); continue; } return; } } private void show(String message) { if (!(getInstrumentation() instanceof TestResultInstrumentation)) { return; } ((TestResultInstrumentation) getInstrumentation()).show(message); } private Context getContext() { return getInstrumentation().getContext(); } }
packages/MtpDocumentsProvider/tests/src/com/android/mtp/RealDeviceTest.java 0 → 100644 +23 −0 Original line number Diff line number Diff line /* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.mtp; import java.lang.annotation.ElementType; import java.lang.annotation.Target; @Target(ElementType.METHOD) @interface RealDeviceTest {}
packages/MtpDocumentsProvider/tests/src/com/android/mtp/TestResultInstrumentation.java +4 −0 Original line number Diff line number Diff line Loading @@ -40,6 +40,10 @@ public class TestResultInstrumentation extends InstrumentationTestRunner impleme mHasError = false; } void show(String message) { TestResultActivity.show(getContext(), " " + message); } private void show(String tag, Test test, Throwable t) { TestResultActivity.show( getContext(), Loading