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.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 * Unit tests lib for {@link android.hardware.usb.UsbManager}. 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 * Counter for tracking UsbOperation operations. 50 */ 51 private static final AtomicInteger sUsbOperationCount = new AtomicInteger(); 52 UsbManagerTestLib(Context context)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 getCurrentFunctions()61 private long getCurrentFunctions() { 62 return mUsbManagerMock.getCurrentFunctions(); 63 } 64 setCurrentFunctions(long functions)65 private void setCurrentFunctions(long functions) { 66 mUsbManagerMock.setCurrentFunctions(functions); 67 } 68 getCurrentFunctionsSys()69 private long getCurrentFunctionsSys() { 70 return mUsbManagerSys.getCurrentFunctions(); 71 } 72 setCurrentFunctionsSys(long functions)73 private void setCurrentFunctionsSys(long functions) { 74 mUsbManagerSys.setCurrentFunctions(functions); 75 } 76 testSetGetCurrentFunctions_Matched(long functions)77 private void testSetGetCurrentFunctions_Matched(long functions) { 78 setCurrentFunctions(functions); 79 assertEquals("CurrentFunctions mismatched: ", functions, getCurrentFunctions()); 80 } 81 testGetCurrentFunctionsMock_Matched(long functions)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 testSetCurrentFunctionsMock_Matched(long functions)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 testGetCurrentFunctionsSysEx()103 public void testGetCurrentFunctionsSysEx() throws Exception { 104 getCurrentFunctionsSys(); 105 } 106 testSetCurrentFunctionsSysEx(long functions)107 public void testSetCurrentFunctionsSysEx(long functions) throws Exception { 108 setCurrentFunctionsSys(functions); 109 } 110 testGetCurrentFunctionsEx()111 public void testGetCurrentFunctionsEx() throws Exception { 112 getCurrentFunctions(); 113 114 verify(mMockUsbService).getCurrentFunctions(); 115 } 116 testSetCurrentFunctionsEx(long functions)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 testGetCurrentFunctions_shouldMatched()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 testGetCurrentFunctionsMock_Matched(UsbManager.FUNCTION_NCM); 131 } 132 testSetCurrentFunctions_shouldMatched()133 public void testSetCurrentFunctions_shouldMatched() { 134 testSetCurrentFunctionsMock_Matched(UsbManager.FUNCTION_NONE); 135 testSetCurrentFunctionsMock_Matched(UsbManager.FUNCTION_MTP); 136 testSetCurrentFunctionsMock_Matched(UsbManager.FUNCTION_PTP); 137 testSetCurrentFunctionsMock_Matched(UsbManager.FUNCTION_MIDI); 138 testSetCurrentFunctionsMock_Matched(UsbManager.FUNCTION_RNDIS); 139 testSetCurrentFunctionsMock_Matched(UsbManager.FUNCTION_NCM); 140 } 141 } 142