1 /*
2  * Copyright (C) 2024 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 android.hardware.usb;
18 
19 import static android.hardware.usb.UsbPortStatus.CONTAMINANT_PROTECTION_NONE;
20 import static android.hardware.usb.UsbPortStatus.MODE_NONE;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26 
27 import android.hardware.usb.flags.Flags;
28 import android.os.RemoteException;
29 import android.platform.test.annotations.RequiresFlagsEnabled;
30 import android.platform.test.flag.junit.CheckFlagsRule;
31 import android.platform.test.flag.junit.DeviceFlagsValueProvider;
32 
33 import androidx.test.InstrumentationRegistry;
34 
35 import com.google.testing.junit.testparameterinjector.TestParameter;
36 import com.google.testing.junit.testparameterinjector.TestParameterInjector;
37 
38 import org.junit.Before;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 
43 /** Tests for {@link android.hardware.usb.UsbPortStatus} */
44 @RunWith(TestParameterInjector.class)
45 public class UsbPortTest {
46 
47     private IUsbManager mMockUsbService;
48 
49     @Rule
50     public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
51 
52     private UsbManager mUsbManager;
53 
54     @Before
setUp()55     public void setUp() throws Exception {
56         mMockUsbService = mock(IUsbManager.class);
57         mUsbManager = new UsbManager(InstrumentationRegistry.getContext(), mMockUsbService);
58     }
59 
60     @Test
61     @RequiresFlagsEnabled(Flags.FLAG_ENABLE_IS_MODE_CHANGE_SUPPORTED_API)
testIsModeSupported(@estParameter boolean isModeChangeSupported)62     public void testIsModeSupported(@TestParameter boolean isModeChangeSupported)
63             throws RemoteException {
64         String testPortId = "port-1";
65         when(mMockUsbService.isModeChangeSupported(testPortId)).thenReturn(isModeChangeSupported);
66         UsbPort usbPort = new UsbPort(
67                 mUsbManager,
68                 testPortId,
69                 MODE_NONE,
70                 CONTAMINANT_PROTECTION_NONE,
71                 false /* supportsEnableContaminantPresenceProtection= */ ,
72                 false /* supportsEnableContaminantPresenceDetection= */);
73 
74         assertThat(usbPort.isModeChangeSupported()).isEqualTo(isModeChangeSupported);
75     }
76 }
77