• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.server.usblib;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.mockito.ArgumentMatchers.eq;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24 
25 import android.content.Context;
26 import android.hardware.usb.UsbManager;
27 import android.os.RemoteException;
28 import android.util.Log;
29 
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 
33 /**
34  * Unit tests lib for {@link android.hardware.usb.UsbManager}.
35  */
36 public class UsbManagerTestLib {
37     private static final String TAG = UsbManagerTestLib.class.getSimpleName();
38 
39     private Context mContext;
40 
41     private UsbManager mUsbManagerSys;
42     private UsbManager mUsbManagerMock;
43     @Mock private android.hardware.usb.IUsbManager mMockUsbService;
44 
UsbManagerTestLib(Context context)45     public UsbManagerTestLib(Context context) {
46         MockitoAnnotations.initMocks(this);
47         mContext = context;
48 
49         assertNotNull(mUsbManagerSys = mContext.getSystemService(UsbManager.class));
50         assertNotNull(mUsbManagerMock = new UsbManager(mContext, mMockUsbService));
51     }
52 
getCurrentFunctions()53     private long getCurrentFunctions() {
54         return mUsbManagerMock.getCurrentFunctions();
55     }
56 
setCurrentFunctions(long functions)57     private void setCurrentFunctions(long functions) {
58         mUsbManagerMock.setCurrentFunctions(functions);
59     }
60 
getCurrentFunctionsSys()61     private long getCurrentFunctionsSys() {
62         return mUsbManagerSys.getCurrentFunctions();
63     }
64 
setCurrentFunctionsSys(long functions)65     private void setCurrentFunctionsSys(long functions) {
66         mUsbManagerSys.setCurrentFunctions(functions);
67     }
68 
testSetGetCurrentFunctions_Matched(long functions)69     private void testSetGetCurrentFunctions_Matched(long functions) {
70         setCurrentFunctions(functions);
71         assertEquals("CurrentFunctions mismatched: ", functions, getCurrentFunctions());
72     }
73 
testGetCurrentFunctionsMock_Matched(long functions)74     private void testGetCurrentFunctionsMock_Matched(long functions) {
75         try {
76             when(mMockUsbService.getCurrentFunctions()).thenReturn(functions);
77 
78             assertEquals("CurrentFunctions mismatched: ", functions, getCurrentFunctions());
79         } catch (RemoteException remEx) {
80             Log.w(TAG, "RemoteException");
81         }
82     }
83 
testSetCurrentFunctionsMock_Matched(long functions)84     private void testSetCurrentFunctionsMock_Matched(long functions) {
85         try {
86             setCurrentFunctions(functions);
87 
88             verify(mMockUsbService).setCurrentFunctions(eq(functions));
89         } catch (RemoteException remEx) {
90             Log.w(TAG, "RemoteException");
91         }
92     }
93 
testGetCurrentFunctionsSysEx()94     public void testGetCurrentFunctionsSysEx() throws Exception {
95         getCurrentFunctionsSys();
96     }
97 
testSetCurrentFunctionsSysEx(long functions)98     public void testSetCurrentFunctionsSysEx(long functions) throws Exception {
99         setCurrentFunctionsSys(functions);
100     }
101 
testGetCurrentFunctionsEx()102     public void testGetCurrentFunctionsEx() throws Exception {
103         getCurrentFunctions();
104 
105         verify(mMockUsbService).getCurrentFunctions();
106     }
107 
testSetCurrentFunctionsEx(long functions)108     public void testSetCurrentFunctionsEx(long functions) throws Exception {
109         setCurrentFunctions(functions);
110 
111         verify(mMockUsbService).setCurrentFunctions(eq(functions));
112     }
113 
testGetCurrentFunctions_shouldMatched()114     public void testGetCurrentFunctions_shouldMatched() {
115         testGetCurrentFunctionsMock_Matched(UsbManager.FUNCTION_NONE);
116         testGetCurrentFunctionsMock_Matched(UsbManager.FUNCTION_MTP);
117         testGetCurrentFunctionsMock_Matched(UsbManager.FUNCTION_PTP);
118         testGetCurrentFunctionsMock_Matched(UsbManager.FUNCTION_MIDI);
119         testGetCurrentFunctionsMock_Matched(UsbManager.FUNCTION_RNDIS);
120     }
121 
testSetCurrentFunctions_shouldMatched()122     public void testSetCurrentFunctions_shouldMatched() {
123         testSetCurrentFunctionsMock_Matched(UsbManager.FUNCTION_NONE);
124         testSetCurrentFunctionsMock_Matched(UsbManager.FUNCTION_MTP);
125         testSetCurrentFunctionsMock_Matched(UsbManager.FUNCTION_PTP);
126         testSetCurrentFunctionsMock_Matched(UsbManager.FUNCTION_MIDI);
127         testSetCurrentFunctionsMock_Matched(UsbManager.FUNCTION_RNDIS);
128     }
129 }
130