1 package android.hardware.automotive.vehicle;
2 
3 import static com.google.common.truth.Truth.assertWithMessage;
4 
5 import static org.junit.Assert.fail;
6 
7 import androidx.test.filters.SmallTest;
8 
9 import org.junit.Test;
10 import org.junit.runner.RunWith;
11 import org.junit.runners.JUnit4;
12 
13 import java.lang.reflect.Field;
14 import java.lang.reflect.Modifier;
15 import java.util.Map;
16 
17 @RunWith(JUnit4.class)
18 public class VehiclePropertyAnnotationJavaTest {
19 
doesAnnotationMapContainsAllProps(Map<Integer, Integer> annotationMap)20     private boolean doesAnnotationMapContainsAllProps(Map<Integer, Integer> annotationMap) {
21         for (Field field : VehicleProperty.class.getDeclaredFields()) {
22             int modifiers = field.getModifiers();
23             try {
24                 if (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)
25                         && Modifier.isPublic(modifiers) && field.getType().equals(int.class)) {
26                     int propId = field.getInt(/* obj= */ null);
27                     if (propId == VehicleProperty.INVALID) {
28                         // Skip INVALID_PROP.
29                         continue;
30                     }
31                     if (annotationMap.get(propId) == null) {
32                         return false;
33                     }
34                 }
35             } catch (IllegalAccessException e) {
36                 throw new IllegalStateException(
37                         "Cannot access a member for VehicleProperty.class", e);
38             }
39         }
40         return true;
41     }
42 
43     @Test
44     @SmallTest
testChangeMode()45     public void testChangeMode() {
46         assertWithMessage("Outdated annotation-generated AIDL files. Please run "
47                 + "generate_annotation_enums.py to update.")
48                 .that(doesAnnotationMapContainsAllProps(ChangeModeForVehicleProperty.values))
49                 .isTrue();
50     }
51 
52     @Test
53     @SmallTest
testAccess()54     public void testAccess() {
55         assertWithMessage("Outdated annotation-generated AIDL files. Please run "
56                 + "generate_annotation_enums.py to update.")
57                 .that(doesAnnotationMapContainsAllProps(AccessForVehicleProperty.values))
58                 .isTrue();
59     }
60 }