MtpManagerTest.java revision b255f58904d7a2aa64ba9f03ed0ede3759fd03c5
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.os.CancellationSignal;
23import android.os.OperationCanceledException;
24import android.test.InstrumentationTestCase;
25
26import java.io.IOException;
27
28@RealDeviceTest
29public class MtpManagerTest extends InstrumentationTestCase {
30
31    private static final int TIMEOUT_MS = 1000;
32    UsbManager mUsbManager;
33    MtpManager mManager;
34    UsbDevice mUsbDevice;
35    int mRequest;
36
37    @Override
38    public void setUp() throws Exception {
39        mUsbManager = getContext().getSystemService(UsbManager.class);
40        mManager = new MtpManager(getContext());
41        mUsbDevice = TestUtil.setupMtpDevice(getInstrumentation(), mUsbManager, mManager);
42    }
43
44    @Override
45    public void tearDown() throws IOException {
46        mManager.closeDevice(mUsbDevice.getDeviceId());
47    }
48
49    @Override
50    public TestResultInstrumentation getInstrumentation() {
51        return (TestResultInstrumentation) super.getInstrumentation();
52    }
53
54    public void testCancelEvent() throws Exception {
55        final CancellationSignal signal = new CancellationSignal();
56        final Thread thread = new Thread() {
57            @Override
58            public void run() {
59                try {
60                    mManager.readEvent(mUsbDevice.getDeviceId(), signal);
61                } catch (OperationCanceledException | IOException e) {
62                    getInstrumentation().show(e.getMessage());
63                }
64            }
65        };
66        thread.start();
67        Thread.sleep(TIMEOUT_MS);
68        signal.cancel();
69        thread.join(TIMEOUT_MS);
70    }
71
72    private Context getContext() {
73        return getInstrumentation().getContext();
74    }
75}
76