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_DETECTION_NOT_SUPPORTED;
20 import static android.hardware.usb.UsbPortStatus.CONTAMINANT_PROTECTION_NONE;
21 import static android.hardware.usb.UsbPortStatus.DATA_ROLE_DEVICE;
22 import static android.hardware.usb.UsbPortStatus.DATA_ROLE_HOST;
23 import static android.hardware.usb.UsbPortStatus.DATA_ROLE_NONE;
24 import static android.hardware.usb.UsbPortStatus.MODE_NONE;
25 import static android.hardware.usb.UsbPortStatus.POWER_ROLE_NONE;
26 import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SINK;
27 import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SOURCE;
28 
29 import static com.google.common.truth.Truth.assertThat;
30 
31 import android.hardware.usb.flags.Flags;
32 import android.platform.test.annotations.RequiresFlagsEnabled;
33 import android.platform.test.flag.junit.CheckFlagsRule;
34 import android.platform.test.flag.junit.DeviceFlagsValueProvider;
35 
36 import com.google.testing.junit.testparameterinjector.TestParameter;
37 import com.google.testing.junit.testparameterinjector.TestParameterInjector;
38 
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 UsbPortStatusTest {
46 
47     @Rule
48     public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
49 
50     @Test
51     @RequiresFlagsEnabled(Flags.FLAG_ENABLE_IS_PD_COMPLIANT_API)
testIsPdCompliant( @estParameter boolean isSinkDeviceRoleSupported, @TestParameter boolean isSinkHostRoleSupported, @TestParameter boolean isSourceDeviceRoleSupported, @TestParameter boolean isSourceHostRoleSupported)52     public void testIsPdCompliant(
53             @TestParameter boolean isSinkDeviceRoleSupported,
54             @TestParameter boolean isSinkHostRoleSupported,
55             @TestParameter boolean isSourceDeviceRoleSupported,
56             @TestParameter boolean isSourceHostRoleSupported) {
57         int supportedRoleCombinations = getSupportedRoleCombinations(
58                 isSinkDeviceRoleSupported,
59                 isSinkHostRoleSupported,
60                 isSourceDeviceRoleSupported,
61                 isSourceHostRoleSupported);
62         UsbPortStatus usbPortStatus = new UsbPortStatus(
63                 MODE_NONE,
64                 POWER_ROLE_NONE,
65                 DATA_ROLE_NONE,
66                 supportedRoleCombinations,
67                 CONTAMINANT_PROTECTION_NONE,
68                 CONTAMINANT_DETECTION_NOT_SUPPORTED);
69         boolean expectedResult = isSinkDeviceRoleSupported
70                 && isSinkHostRoleSupported
71                 && isSourceDeviceRoleSupported
72                 && isSourceHostRoleSupported;
73 
74         assertThat(usbPortStatus.isPdCompliant()).isEqualTo(expectedResult);
75     }
76 
getSupportedRoleCombinations( boolean isSinkDeviceRoleSupported, boolean isSinkHostRoleSupported, boolean isSourceDeviceRoleSupported, boolean isSourceHostRoleSupported)77     private int getSupportedRoleCombinations(
78             boolean isSinkDeviceRoleSupported,
79             boolean isSinkHostRoleSupported,
80             boolean isSourceDeviceRoleSupported,
81             boolean isSourceHostRoleSupported) {
82         int result = UsbPort.combineRolesAsBit(POWER_ROLE_NONE, DATA_ROLE_NONE);
83 
84         if (isSinkDeviceRoleSupported) {
85             result |= UsbPort.combineRolesAsBit(POWER_ROLE_SINK, DATA_ROLE_DEVICE);
86         }
87         if (isSinkHostRoleSupported) {
88             result |= UsbPort.combineRolesAsBit(POWER_ROLE_SINK, DATA_ROLE_HOST);
89         }
90         if (isSourceDeviceRoleSupported) {
91             result |= UsbPort.combineRolesAsBit(POWER_ROLE_SOURCE, DATA_ROLE_DEVICE);
92         }
93         if (isSourceHostRoleSupported) {
94             result |= UsbPort.combineRolesAsBit(POWER_ROLE_SOURCE, DATA_ROLE_HOST);
95         }
96 
97         return result;
98     }
99 }
100