blob: dfde27c7c889ca1364d839512a3e7dcde1403dd3 [file] [log] [blame]
Daichi Hirono5bc41d12015-08-28 15:47:33 +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
19import android.content.Context;
20import android.hardware.usb.UsbDevice;
Daichi Hirono5bc41d12015-08-28 15:47:33 +090021import android.hardware.usb.UsbManager;
Daichi Hirono2a9a4332016-01-11 13:33:41 +090022import android.mtp.MtpConstants;
23import android.mtp.MtpEvent;
Daichi Hirono0b494662015-09-10 20:38:15 +090024import android.os.CancellationSignal;
25import android.os.OperationCanceledException;
Daichi Hirono1d4779c2016-01-06 16:43:32 +090026import android.os.SystemClock;
Daichi Hirono5bc41d12015-08-28 15:47:33 +090027import android.test.InstrumentationTestCase;
28
Daichi Hirono0b494662015-09-10 20:38:15 +090029import java.io.IOException;
Daichi Hirono1d4779c2016-01-06 16:43:32 +090030import java.util.Arrays;
Daichi Hirono99b58052015-12-03 18:09:16 +090031import java.util.concurrent.Callable;
32import java.util.concurrent.FutureTask;
33import java.util.concurrent.TimeUnit;
Daichi Hirono5bc41d12015-08-28 15:47:33 +090034
Daichi Hirono0b494662015-09-10 20:38:15 +090035@RealDeviceTest
Daichi Hirono5bc41d12015-08-28 15:47:33 +090036public class MtpManagerTest extends InstrumentationTestCase {
Daichi Hironob255f582015-12-02 12:20:25 +090037
Daichi Hirono0b494662015-09-10 20:38:15 +090038 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 Hironob255f582015-12-02 12:20:25 +090047 mManager = new MtpManager(getContext());
48 mUsbDevice = TestUtil.setupMtpDevice(getInstrumentation(), mUsbManager, mManager);
Daichi Hirono0b494662015-09-10 20:38:15 +090049 }
50
51 @Override
52 public void tearDown() throws IOException {
53 mManager.closeDevice(mUsbDevice.getDeviceId());
54 }
55
Daichi Hironob255f582015-12-02 12:20:25 +090056 @Override
57 public TestResultInstrumentation getInstrumentation() {
58 return (TestResultInstrumentation) super.getInstrumentation();
59 }
60
Daichi Hirono0b494662015-09-10 20:38:15 +090061 public void testCancelEvent() throws Exception {
62 final CancellationSignal signal = new CancellationSignal();
Daichi Hirono99b58052015-12-03 18:09:16 +090063 final FutureTask<Boolean> future = new FutureTask<Boolean>(
64 new Callable<Boolean>() {
65 @Override
66 public Boolean call() throws IOException {
67 try {
Daichi Hirono4c4061212015-12-24 12:43:12 +090068 while (true) {
69 mManager.readEvent(mUsbDevice.getDeviceId(), signal);
70 }
Daichi Hirono99b58052015-12-03 18:09:16 +090071 } catch (OperationCanceledException exception) {
72 return true;
73 }
74 }
75 });
76 final Thread thread = new Thread(future);
Daichi Hirono0b494662015-09-10 20:38:15 +090077 thread.start();
Daichi Hirono1d4779c2016-01-06 16:43:32 +090078 SystemClock.sleep(TIMEOUT_MS);
Daichi Hirono0b494662015-09-10 20:38:15 +090079 signal.cancel();
Daichi Hirono99b58052015-12-03 18:09:16 +090080 assertTrue(future.get(TIMEOUT_MS, TimeUnit.MILLISECONDS));
Daichi Hirono0b494662015-09-10 20:38:15 +090081 }
82
Daichi Hirono1d4779c2016-01-06 16:43:32 +090083 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 Hirono2a9a4332016-01-11 13:33:41 +090090 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 Hirono5bc41d12015-08-28 15:47:33 +0900103 private Context getContext() {
104 return getInstrumentation().getContext();
105 }
106}