Daichi Hirono | 5bc41d1 | 2015-08-28 15:47:33 +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 | |
| 19 | import android.content.Context; |
| 20 | import android.hardware.usb.UsbDevice; |
Daichi Hirono | 5bc41d1 | 2015-08-28 15:47:33 +0900 | [diff] [blame] | 21 | import android.hardware.usb.UsbManager; |
Daichi Hirono | 2a9a433 | 2016-01-11 13:33:41 +0900 | [diff] [blame^] | 22 | import android.mtp.MtpConstants; |
| 23 | import android.mtp.MtpEvent; |
Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 24 | import android.os.CancellationSignal; |
| 25 | import android.os.OperationCanceledException; |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 26 | import android.os.SystemClock; |
Daichi Hirono | 5bc41d1 | 2015-08-28 15:47:33 +0900 | [diff] [blame] | 27 | import android.test.InstrumentationTestCase; |
| 28 | |
Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 29 | import java.io.IOException; |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 30 | import java.util.Arrays; |
Daichi Hirono | 99b5805 | 2015-12-03 18:09:16 +0900 | [diff] [blame] | 31 | import java.util.concurrent.Callable; |
| 32 | import java.util.concurrent.FutureTask; |
| 33 | import java.util.concurrent.TimeUnit; |
Daichi Hirono | 5bc41d1 | 2015-08-28 15:47:33 +0900 | [diff] [blame] | 34 | |
Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 35 | @RealDeviceTest |
Daichi Hirono | 5bc41d1 | 2015-08-28 15:47:33 +0900 | [diff] [blame] | 36 | public class MtpManagerTest extends InstrumentationTestCase { |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 37 | |
Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 38 | private static final int TIMEOUT_MS = 1000; |
| 39 | UsbManager mUsbManager; |
| 40 | MtpManager mManager; |
| 41 | UsbDevice mUsbDevice; |
| 42 | int mRequest; |
| 43 | |
| 44 | @Override |
| 45 | public void setUp() throws Exception { |
| 46 | mUsbManager = getContext().getSystemService(UsbManager.class); |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 47 | mManager = new MtpManager(getContext()); |
| 48 | mUsbDevice = TestUtil.setupMtpDevice(getInstrumentation(), mUsbManager, mManager); |
Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public void tearDown() throws IOException { |
| 53 | mManager.closeDevice(mUsbDevice.getDeviceId()); |
| 54 | } |
| 55 | |
Daichi Hirono | b255f58 | 2015-12-02 12:20:25 +0900 | [diff] [blame] | 56 | @Override |
| 57 | public TestResultInstrumentation getInstrumentation() { |
| 58 | return (TestResultInstrumentation) super.getInstrumentation(); |
| 59 | } |
| 60 | |
Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 61 | public void testCancelEvent() throws Exception { |
| 62 | final CancellationSignal signal = new CancellationSignal(); |
Daichi Hirono | 99b5805 | 2015-12-03 18:09:16 +0900 | [diff] [blame] | 63 | final FutureTask<Boolean> future = new FutureTask<Boolean>( |
| 64 | new Callable<Boolean>() { |
| 65 | @Override |
| 66 | public Boolean call() throws IOException { |
| 67 | try { |
Daichi Hirono | 4c406121 | 2015-12-24 12:43:12 +0900 | [diff] [blame] | 68 | while (true) { |
| 69 | mManager.readEvent(mUsbDevice.getDeviceId(), signal); |
| 70 | } |
Daichi Hirono | 99b5805 | 2015-12-03 18:09:16 +0900 | [diff] [blame] | 71 | } catch (OperationCanceledException exception) { |
| 72 | return true; |
| 73 | } |
| 74 | } |
| 75 | }); |
| 76 | final Thread thread = new Thread(future); |
Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 77 | thread.start(); |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 78 | SystemClock.sleep(TIMEOUT_MS); |
Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 79 | signal.cancel(); |
Daichi Hirono | 99b5805 | 2015-12-03 18:09:16 +0900 | [diff] [blame] | 80 | assertTrue(future.get(TIMEOUT_MS, TimeUnit.MILLISECONDS)); |
Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 81 | } |
| 82 | |
Daichi Hirono | 1d4779c | 2016-01-06 16:43:32 +0900 | [diff] [blame] | 83 | public void testOperationsSupported() { |
| 84 | final MtpDeviceRecord[] records = mManager.getDevices(); |
| 85 | assertEquals(1, records.length); |
| 86 | assertNotNull(records[0].operationsSupported); |
| 87 | getInstrumentation().show(Arrays.toString(records[0].operationsSupported)); |
| 88 | } |
| 89 | |
Daichi Hirono | 2a9a433 | 2016-01-11 13:33:41 +0900 | [diff] [blame^] | 90 | public void testEventObjectAdded() throws Exception { |
| 91 | while (true) { |
| 92 | getInstrumentation().show("Please take a photo by using connected MTP device."); |
| 93 | final CancellationSignal signal = new CancellationSignal(); |
| 94 | MtpEvent event = mManager.readEvent(mUsbDevice.getDeviceId(), signal); |
| 95 | if (event.getEventCode() != MtpConstants.EVENT_OBJECT_ADDED) { |
| 96 | continue; |
| 97 | } |
| 98 | assertTrue(event.getObjectHandle() != 0); |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | |
Daichi Hirono | 5bc41d1 | 2015-08-28 15:47:33 +0900 | [diff] [blame] | 103 | private Context getContext() { |
| 104 | return getInstrumentation().getContext(); |
| 105 | } |
| 106 | } |