| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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.Binder; |
| 28 |
import android.os.RemoteException; |
| 29 |
import android.util.Log; |
| 30 |
|
| 31 |
import org.mockito.Mock; |
| 32 |
import org.mockito.MockitoAnnotations; |
| 33 |
|
| 34 |
import java.util.concurrent.atomic.AtomicInteger; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class UsbManagerTestLib { |
| 40 |
private static final String TAG = UsbManagerTestLib.class.getSimpleName(); |
| 41 |
|
| 42 |
private Context mContext; |
| 43 |
|
| 44 |
private UsbManager mUsbManagerSys; |
| 45 |
private UsbManager mUsbManagerMock; |
| 46 |
@Mock private android.hardware.usb.IUsbManager mMockUsbService; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
private static final AtomicInteger sUsbOperationCount = new AtomicInteger(); |
| 52 |
|
| 53 |
public UsbManagerTestLib(Context context) { |
| 54 |
MockitoAnnotations.initMocks(this); |
| 55 |
mContext = context; |
| 56 |
|
| 57 |
assertNotNull(mUsbManagerSys = mContext.getSystemService(UsbManager.class)); |
| 58 |
assertNotNull(mUsbManagerMock = new UsbManager(mContext, mMockUsbService)); |
| 59 |
} |
| 60 |
|
| 61 |
private long getCurrentFunctions() { |
| 62 |
return mUsbManagerMock.getCurrentFunctions(); |
| 63 |
} |
| 64 |
|
| 65 |
private void setCurrentFunctions(long functions) { |
| 66 |
mUsbManagerMock.setCurrentFunctions(functions); |
| 67 |
} |
| 68 |
|
| 69 |
private long getCurrentFunctionsSys() { |
| 70 |
return mUsbManagerSys.getCurrentFunctions(); |
| 71 |
} |
| 72 |
|
| 73 |
private void setCurrentFunctionsSys(long functions) { |
| 74 |
mUsbManagerSys.setCurrentFunctions(functions); |
| 75 |
} |
| 76 |
|
| 77 |
private void testSetGetCurrentFunctions_Matched(long functions) { |
| 78 |
setCurrentFunctions(functions); |
| 79 |
assertEquals("CurrentFunctions mismatched: ", functions, getCurrentFunctions()); |
| 80 |
} |
| 81 |
|
| 82 |
private void testGetCurrentFunctionsMock_Matched(long functions) { |
| 83 |
try { |
| 84 |
when(mMockUsbService.getCurrentFunctions()).thenReturn(functions); |
| 85 |
|
| 86 |
assertEquals("CurrentFunctions mismatched: ", functions, getCurrentFunctions()); |
| 87 |
} catch (RemoteException remEx) { |
| 88 |
Log.w(TAG, "RemoteException"); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
private void testSetCurrentFunctionsMock_Matched(long functions) { |
| 93 |
int operationId = sUsbOperationCount.incrementAndGet() + Binder.getCallingUid(); |
| 94 |
try { |
| 95 |
setCurrentFunctions(functions); |
| 96 |
|
| 97 |
verify(mMockUsbService).setCurrentFunctions(eq(functions), operationId); |
| 98 |
} catch (RemoteException remEx) { |
| 99 |
Log.w(TAG, "RemoteException"); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
public void testGetCurrentFunctionsSysEx() throws Exception { |
| 104 |
getCurrentFunctionsSys(); |
| 105 |
} |
| 106 |
|
| 107 |
public void testSetCurrentFunctionsSysEx(long functions) throws Exception { |
| 108 |
setCurrentFunctionsSys(functions); |
| 109 |
} |
| 110 |
|
| 111 |
public void testGetCurrentFunctionsEx() throws Exception { |
| 112 |
getCurrentFunctions(); |
| 113 |
|
| 114 |
verify(mMockUsbService).getCurrentFunctions(); |
| 115 |
} |
| 116 |
|
| 117 |
public void testSetCurrentFunctionsEx(long functions) throws Exception { |
| 118 |
int operationId = sUsbOperationCount.incrementAndGet() + Binder.getCallingUid(); |
| 119 |
setCurrentFunctions(functions); |
| 120 |
|
| 121 |
verify(mMockUsbService).setCurrentFunctions(eq(functions), operationId); |
| 122 |
} |
| 123 |
|
| 124 |
public void testGetCurrentFunctions_shouldMatched() { |
| 125 |
testGetCurrentFunctionsMock_Matched(UsbManager.FUNCTION_NONE); |
| 126 |
testGetCurrentFunctionsMock_Matched(UsbManager.FUNCTION_MTP); |
| 127 |
testGetCurrentFunctionsMock_Matched(UsbManager.FUNCTION_PTP); |
| 128 |
testGetCurrentFunctionsMock_Matched(UsbManager.FUNCTION_MIDI); |
| 129 |
testGetCurrentFunctionsMock_Matched(UsbManager.FUNCTION_RNDIS); |
| 130 |
} |
| 131 |
|
| 132 |
public void testSetCurrentFunctions_shouldMatched() { |
| 133 |
testSetCurrentFunctionsMock_Matched(UsbManager.FUNCTION_NONE); |
| 134 |
testSetCurrentFunctionsMock_Matched(UsbManager.FUNCTION_MTP); |
| 135 |
testSetCurrentFunctionsMock_Matched(UsbManager.FUNCTION_PTP); |
| 136 |
testSetCurrentFunctionsMock_Matched(UsbManager.FUNCTION_MIDI); |
| 137 |
testSetCurrentFunctionsMock_Matched(UsbManager.FUNCTION_RNDIS); |
| 138 |
} |
| 139 |
} |