MtpManagerTest.java revision 2a9a43369b4717bcf6b372f6798f72e80e938e30
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 17package com.android.mtp; 18 19import android.content.Context; 20import android.hardware.usb.UsbDevice; 21import android.hardware.usb.UsbManager; 22import android.mtp.MtpConstants; 23import android.mtp.MtpEvent; 24import android.os.CancellationSignal; 25import android.os.OperationCanceledException; 26import android.os.SystemClock; 27import android.test.InstrumentationTestCase; 28 29import java.io.IOException; 30import java.util.Arrays; 31import java.util.concurrent.Callable; 32import java.util.concurrent.FutureTask; 33import java.util.concurrent.TimeUnit; 34 35@RealDeviceTest 36public class MtpManagerTest extends InstrumentationTestCase { 37 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); 47 mManager = new MtpManager(getContext()); 48 mUsbDevice = TestUtil.setupMtpDevice(getInstrumentation(), mUsbManager, mManager); 49 } 50 51 @Override 52 public void tearDown() throws IOException { 53 mManager.closeDevice(mUsbDevice.getDeviceId()); 54 } 55 56 @Override 57 public TestResultInstrumentation getInstrumentation() { 58 return (TestResultInstrumentation) super.getInstrumentation(); 59 } 60 61 public void testCancelEvent() throws Exception { 62 final CancellationSignal signal = new CancellationSignal(); 63 final FutureTask<Boolean> future = new FutureTask<Boolean>( 64 new Callable<Boolean>() { 65 @Override 66 public Boolean call() throws IOException { 67 try { 68 while (true) { 69 mManager.readEvent(mUsbDevice.getDeviceId(), signal); 70 } 71 } catch (OperationCanceledException exception) { 72 return true; 73 } 74 } 75 }); 76 final Thread thread = new Thread(future); 77 thread.start(); 78 SystemClock.sleep(TIMEOUT_MS); 79 signal.cancel(); 80 assertTrue(future.get(TIMEOUT_MS, TimeUnit.MILLISECONDS)); 81 } 82 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 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 103 private Context getContext() { 104 return getInstrumentation().getContext(); 105 } 106} 107