Set default parsing value for access & changeMode.

Set the default value for property access and changeMode according to
AccessForVehicleProperty.java and ChangeModeForVehicleProperty.java if
no specified parsing value from config file to override.

Test: atest FakeVhalConfigParserUnitTest
Bug: 243719616
Bug: 243811446
Change-Id: Iae18d40568d1ab46f284b390f43f92da2aa2f13b
diff --git a/service/Android.bp b/service/Android.bp
index 65e7f5b..41ecbb5 100644
--- a/service/Android.bp
+++ b/service/Android.bp
@@ -33,6 +33,7 @@
 
     srcs: [
         // add full source for all codes under p/s/Car to reduce unnecessary library allow listing.
+        ":IVehicleGeneratedJavaFiles",
         ":android.car.cluster.navigation-src",
         ":android.car.watchdoglib-src",
         ":cartelemetry-cardata-proto-srcs",
diff --git a/service/src/com/android/car/hal/fakevhal/FakeVhalConfigParser.java b/service/src/com/android/car/hal/fakevhal/FakeVhalConfigParser.java
index b727357..260b43e 100644
--- a/service/src/com/android/car/hal/fakevhal/FakeVhalConfigParser.java
+++ b/service/src/com/android/car/hal/fakevhal/FakeVhalConfigParser.java
@@ -18,6 +18,8 @@
 
 import android.annotation.Nullable;
 import android.car.builtin.util.Slogf;
+import android.hardware.automotive.vehicle.AccessForVehicleProperty;
+import android.hardware.automotive.vehicle.ChangeModeForVehicleProperty;
 import android.hardware.automotive.vehicle.PortLocationType;
 import android.hardware.automotive.vehicle.RawPropValues;
 import android.hardware.automotive.vehicle.VehicleArea;
@@ -300,6 +302,8 @@
 
         VehiclePropConfig vehiclePropConfig = new VehiclePropConfig();
         vehiclePropConfig.prop = VehicleProperty.INVALID;
+        boolean isAccessSet = false;
+        boolean isChangeModeSet = false;
         List<VehicleAreaConfig> areaConfigs = new ArrayList<>();
         RawPropValues rawPropValues = null;
         SparseArray<RawPropValues> defaultValuesByAreaId = new SparseArray<>();
@@ -324,9 +328,11 @@
                     break;
                 case JSON_FIELD_NAME_ACCESS:
                     vehiclePropConfig.access = parseIntValue(propertyObject, fieldName, errors);
+                    isAccessSet = true;
                     break;
                 case JSON_FIELD_NAME_CHANGE_MODE:
                     vehiclePropConfig.changeMode = parseIntValue(propertyObject, fieldName, errors);
+                    isChangeModeSet = true;
                     break;
                 case JSON_FIELD_NAME_CONFIG_ARRAY:
                     JSONArray configArray = propertyObject.optJSONArray(fieldName);
@@ -380,6 +386,24 @@
             return null;
         }
 
+        if (!isAccessSet) {
+            if (AccessForVehicleProperty.values.containsKey(vehiclePropConfig.prop)) {
+                vehiclePropConfig.access =
+                        AccessForVehicleProperty.values.get(vehiclePropConfig.prop);
+            } else {
+                errors.add("Access field is not set for this property: " + propertyObject);
+            }
+        }
+
+        if (!isChangeModeSet) {
+            if (ChangeModeForVehicleProperty.values.containsKey(vehiclePropConfig.prop)) {
+                vehiclePropConfig.changeMode = ChangeModeForVehicleProperty.values
+                        .get(vehiclePropConfig.prop);
+            } else {
+                errors.add("ChangeMode field is not set for this property: " + propertyObject);
+            }
+        }
+
         return new ConfigDeclaration(vehiclePropConfig, rawPropValues, defaultValuesByAreaId);
     }
 
diff --git a/tests/carservice_unit_test/src/com/android/car/hal/fakevhal/FakeVhalConfigParserUnitTest.java b/tests/carservice_unit_test/src/com/android/car/hal/fakevhal/FakeVhalConfigParserUnitTest.java
index 3ccd185..bca0c21 100644
--- a/tests/carservice_unit_test/src/com/android/car/hal/fakevhal/FakeVhalConfigParserUnitTest.java
+++ b/tests/carservice_unit_test/src/com/android/car/hal/fakevhal/FakeVhalConfigParserUnitTest.java
@@ -20,6 +20,8 @@
 
 import static org.junit.Assert.assertThrows;
 
+import android.hardware.automotive.vehicle.AccessForVehicleProperty;
+import android.hardware.automotive.vehicle.ChangeModeForVehicleProperty;
 import android.hardware.automotive.vehicle.RawPropValues;
 import android.hardware.automotive.vehicle.VehicleAreaConfig;
 import android.hardware.automotive.vehicle.VehicleAreaDoor;
@@ -139,7 +141,9 @@
 
     @Test
     public void testParsePropertyIdWithIntValue() throws Exception {
-        String jsonString = "{\"properties\": [{\"property\": 123}]}";
+        String jsonString = "{\"properties\": [{\"property\": 123,"
+                + "\"access\": \"VehiclePropertyAccess::READ_WRITE\","
+                + "\"changeMode\": \"VehiclePropertyChangeMode::STATIC\"}]}";
         File tempFile = createTempFileWithContent(jsonString);
 
         int propId = mFakeVhalConfigParser.parseJsonConfig(tempFile)
@@ -270,7 +274,9 @@
 
     @Test
     public void testParsePropertyIdFromConstantsMap() throws Exception {
-        String jsonString = "{\"properties\": [{\"property\": \"Constants::DOOR_1_LEFT\"}]}";
+        String jsonString = "{\"properties\": [{\"property\": \"Constants::DOOR_1_LEFT\","
+                + "\"access\": \"VehiclePropertyAccess::READ_WRITE\","
+                + "\"changeMode\": \"VehiclePropertyChangeMode::STATIC\"}]}";
         File tempFile = createTempFileWithContent(jsonString);
 
         int propId = mFakeVhalConfigParser.parseJsonConfig(tempFile).get(DOOR_1_LEFT).getConfig()
@@ -315,8 +321,10 @@
 
     @Test
     public void testParseFloatValueIsString() throws Exception {
-        String jsonString = "{\"properties\": [{\"property\": 123, \"minSampleRate\": "
-                + "\"VehicleUnit::FAHRENHEIT\"}]}";
+        String jsonString = "{\"properties\": [{\"property\": 123, "
+                + "\"access\": \"VehiclePropertyAccess::READ_WRITE\","
+                + "\"changeMode\": \"VehiclePropertyChangeMode::STATIC\","
+                + "\"minSampleRate\": \"VehicleUnit::FAHRENHEIT\"}]}";
         File tempFile = createTempFileWithContent(jsonString);
 
         float minSampleRate = mFakeVhalConfigParser.parseJsonConfig(tempFile).get(123).getConfig()
@@ -339,7 +347,10 @@
 
     @Test
     public void testParseFloatValueIsInt() throws Exception {
-        String jsonString = "{\"properties\": [{\"property\": 123, \"minSampleRate\": 456}]}";
+        String jsonString = "{\"properties\": [{\"property\": 123,"
+                + "\"access\": \"VehiclePropertyAccess::READ_WRITE\","
+                + "\"changeMode\": \"VehiclePropertyChangeMode::STATIC\","
+                + "\"minSampleRate\": 456}]}";
         File tempFile = createTempFileWithContent(jsonString);
 
         float minSampleRate = mFakeVhalConfigParser.parseJsonConfig(tempFile).get(123).getConfig()
@@ -349,28 +360,82 @@
     }
 
     @Test
-    public void testParseAccessField() throws Exception {
+    public void testParseAccessFieldOverride() throws Exception {
         String jsonString = "{\"properties\": [{\"property\": \"VehicleProperty::INFO_VIN\", "
-                + "\"access\": \"VehiclePropertyAccess::READ\"}]}";
+                + "\"access\": \"VehiclePropertyAccess::READ_WRITE\"}]}";
         File tempFile = createTempFileWithContent(jsonString);
 
         int access = mFakeVhalConfigParser.parseJsonConfig(tempFile).get(VehicleProperty.INFO_VIN)
                 .getConfig().access;
 
-        assertThat(access).isEqualTo(1);
+        assertThat(access).isEqualTo(VehiclePropertyAccess.READ_WRITE);
+        assertThat(access).isNotEqualTo(AccessForVehicleProperty.values
+                .get(VehicleProperty.INFO_VIN));
     }
 
     @Test
-    public void testParseChangeModeField() throws Exception {
+    public void testParseAccessFieldNotSpecifiedDefaultAccessValueExist() throws Exception {
+        String jsonString = "{\"properties\": [{\"property\": \"VehicleProperty::INFO_VIN\"}]}";
+        File tempFile = createTempFileWithContent(jsonString);
+
+        int access = mFakeVhalConfigParser.parseJsonConfig(tempFile).get(VehicleProperty.INFO_VIN)
+                .getConfig().access;
+
+        assertThat(access).isEqualTo(AccessForVehicleProperty.values.get(VehicleProperty.INFO_VIN));
+    }
+
+    @Test
+    public void testParseAccessFieldNotSpecifiedDefaultAccessValueNotExist() throws Exception {
+        String jsonString = "{\"properties\": [{\"property\": 123,"
+                + "\"changeMode\": \"VehiclePropertyChangeMode::STATIC\"}]}";
+        File tempFile = createTempFileWithContent(jsonString);
+
+        IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () ->
+                mFakeVhalConfigParser.parseJsonConfig(tempFile));
+
+        assertThat(thrown).hasMessageThat().contains("Access field is not set for this property");
+    }
+
+    @Test
+    public void testParseChangeModeFieldOverride() throws Exception {
         String jsonString = "{\"properties\": [{\"property\": "
                 + "\"VehicleProperty::PERF_VEHICLE_SPEED_DISPLAY\", "
-                + "\"changeMode\": \"VehiclePropertyChangeMode::CONTINUOUS\"}]}";
+                + "\"changeMode\": \"VehiclePropertyChangeMode::ON_CHANGE\"}]}";
         File tempFile = createTempFileWithContent(jsonString);
 
         int changeMode = mFakeVhalConfigParser.parseJsonConfig(tempFile)
                 .get(VehicleProperty.PERF_VEHICLE_SPEED_DISPLAY).getConfig().changeMode;
 
-        assertThat(changeMode).isEqualTo(2);
+        assertThat(changeMode).isEqualTo(VehiclePropertyChangeMode.ON_CHANGE);
+        assertThat(changeMode).isNotEqualTo(ChangeModeForVehicleProperty.values
+                .get(VehicleProperty.PERF_VEHICLE_SPEED_DISPLAY));
+    }
+
+    @Test
+    public void testParseChangeModeFieldNotSpecifiedDefaultChangeModeValueExist() throws Exception {
+        String jsonString = "{\"properties\": [{\"property\": "
+                + "\"VehicleProperty::PERF_VEHICLE_SPEED_DISPLAY\"}]}";
+        File tempFile = createTempFileWithContent(jsonString);
+
+        int changeMode = mFakeVhalConfigParser.parseJsonConfig(tempFile)
+                .get(VehicleProperty.PERF_VEHICLE_SPEED_DISPLAY).getConfig().changeMode;
+
+        assertThat(changeMode).isEqualTo(ChangeModeForVehicleProperty.values
+                .get(VehicleProperty.PERF_VEHICLE_SPEED_DISPLAY));
+    }
+
+    @Test
+    public void testParseChangeModeFieldNotSpecifiedDefaultChangeModeValueNotExist()
+            throws Exception {
+        String jsonString = "{\"properties\": [{\"property\": 123,"
+                + "\"access\": \"VehiclePropertyAccess::READ\"}]}";
+        File tempFile = createTempFileWithContent(jsonString);
+
+        IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () ->
+                mFakeVhalConfigParser.parseJsonConfig(tempFile));
+
+        assertThat(thrown).hasMessageThat().contains("ChangeMode field is not set for this "
+                + "property");
     }
 
     @Test
@@ -528,6 +593,7 @@
         File tempFile = createTempFileWithContent(jsonString);
         VehiclePropConfig vehiclePropConfig = new VehiclePropConfig();
         vehiclePropConfig.prop = 286261504;
+        vehiclePropConfig.access = AccessForVehicleProperty.values.get(286261504);
         VehicleAreaConfig vehicleAreaConfig = new VehicleAreaConfig();
         vehicleAreaConfig.areaId = 1;
         vehicleAreaConfig.minInt32Value = 0;
@@ -543,7 +609,7 @@
         ConfigDeclaration configDeclaration = mFakeVhalConfigParser.parseJsonConfig(tempFile)
                 .get(286261504);
 
-        assertThat(expectConfigDeclaration).isEqualTo(configDeclaration);
+        assertThat(configDeclaration).isEqualTo(expectConfigDeclaration);
     }
 
     @Test
@@ -631,6 +697,7 @@
         ConfigDeclaration configDeclaration = mFakeVhalConfigParser.parseJsonConfig(tempFile)
                 .get(VehicleProperty.WHEEL_TICK);
 
+        assertThat(expectConfigDeclaration.getConfig().changeMode).isEqualTo(0);
         assertThat(expectConfigDeclaration).isEqualTo(configDeclaration);
     }