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.car.hardware.property; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.car.test.AbstractExpectableTestCase; 22 import android.os.Parcel; 23 24 import org.junit.Test; 25 26 import java.util.List; 27 28 /** 29 * Unit tests for {@link AreaIdConfig} 30 */ 31 public class AreaIdConfigTest extends AbstractExpectableTestCase { 32 private static final int ACCESS = 1; 33 private static final int AREA_ID = 99; 34 private static final int MAX_VALUE = 10; 35 private static final int MIN_VALUE = 1; 36 private static final List<Integer> SUPPORTED_ENUM_VALUES = List.of(2, 3, 4); 37 private static final AreaIdConfig<Integer> AREA_ID_CONFIG = new AreaIdConfig.Builder<Integer>( 38 ACCESS, AREA_ID).setMaxValue(MAX_VALUE).setMinValue(MIN_VALUE).setSupportedEnumValues( 39 SUPPORTED_ENUM_VALUES).setSupportVariableUpdateRate(true) 40 .setHasMinSupportedValue(true).setHasMaxSupportedValue(true) 41 .setHasSupportedValuesList(true).build(); 42 43 @Test getAccess_returnsExpectedValue()44 public void getAccess_returnsExpectedValue() { 45 assertThat(AREA_ID_CONFIG.getAccess()).isEqualTo(ACCESS); 46 } 47 48 @Test getAreaId_returnsExpectedValue()49 public void getAreaId_returnsExpectedValue() { 50 assertThat(AREA_ID_CONFIG.getAreaId()).isEqualTo(AREA_ID); 51 } 52 53 @Test getMinValue_returnsExpectedValue()54 public void getMinValue_returnsExpectedValue() { 55 assertThat(AREA_ID_CONFIG.getMinValue()).isEqualTo(MIN_VALUE); 56 } 57 58 @Test getMinValue_returnsNullIfNotSet()59 public void getMinValue_returnsNullIfNotSet() { 60 assertThat(new AreaIdConfig.Builder<Long>(ACCESS, AREA_ID).build().getMinValue()).isNull(); 61 } 62 63 @Test getMaxValue_returnsExpectedValue()64 public void getMaxValue_returnsExpectedValue() { 65 assertThat(AREA_ID_CONFIG.getMaxValue()).isEqualTo(MAX_VALUE); 66 } 67 68 @Test getMaxValue_returnsNullIfNotSet()69 public void getMaxValue_returnsNullIfNotSet() { 70 assertThat(new AreaIdConfig.Builder<Long>(ACCESS, AREA_ID).build().getMaxValue()).isNull(); 71 } 72 73 @Test getSupportedEnumValues_returnsExpectedValue()74 public void getSupportedEnumValues_returnsExpectedValue() { 75 assertThat(AREA_ID_CONFIG.getSupportedEnumValues()).containsExactlyElementsIn( 76 SUPPORTED_ENUM_VALUES); 77 } 78 79 @Test getSupportedEnumValues_returnsEmptyListIfNoSet()80 public void getSupportedEnumValues_returnsEmptyListIfNoSet() { 81 assertThat(new AreaIdConfig.Builder<Long>( 82 ACCESS, AREA_ID).build().getSupportedEnumValues()).isEmpty(); 83 } 84 85 @Test describeContents_returnsExpectedValue()86 public void describeContents_returnsExpectedValue() { 87 assertThat(AREA_ID_CONFIG.describeContents()).isEqualTo(0); 88 } 89 90 @Test isVariableUpdateRateSupported()91 public void isVariableUpdateRateSupported() { 92 assertThat(AREA_ID_CONFIG.isVariableUpdateRateSupported()).isTrue(); 93 } 94 95 @Test isVariableUpdateRateSupported_defaultFalse()96 public void isVariableUpdateRateSupported_defaultFalse() { 97 assertThat(new AreaIdConfig.Builder<Long>(AREA_ID).build().isVariableUpdateRateSupported()) 98 .isFalse(); 99 } 100 101 @Test writeToParcel_writesCorrectly()102 public void writeToParcel_writesCorrectly() { 103 Parcel parcel = Parcel.obtain(); 104 AREA_ID_CONFIG.writeToParcel(parcel, /*flags=*/0); 105 106 // After you're done with writing, you need to reset the parcel for reading. 107 parcel.setDataPosition(0); 108 109 AreaIdConfig<Object> areaIdConfig = AreaIdConfig.CREATOR.createFromParcel(parcel); 110 expectThat((Integer) areaIdConfig.getMaxValue()).isEqualTo(MAX_VALUE); 111 expectThat((Integer) areaIdConfig.getMinValue()).isEqualTo(MIN_VALUE); 112 expectThat( 113 (List<?>) areaIdConfig.getSupportedEnumValues()).containsExactlyElementsIn( 114 SUPPORTED_ENUM_VALUES); 115 expectThat(areaIdConfig.isVariableUpdateRateSupported()).isTrue(); 116 expectThat(areaIdConfig.hasMinSupportedValue()).isTrue(); 117 expectThat(areaIdConfig.hasMaxSupportedValue()).isTrue(); 118 expectThat(areaIdConfig.hasSupportedValuesList()).isTrue(); 119 } 120 121 @Test testSetMinSupportedValue()122 public void testSetMinSupportedValue() { 123 var areaIdConfig = new AreaIdConfig.Builder<Integer>(ACCESS, AREA_ID) 124 .setMinValue(MIN_VALUE).build(); 125 126 expectThat(areaIdConfig.getMinValue()).isEqualTo(MIN_VALUE); 127 expectThat(areaIdConfig.hasMinSupportedValue()).isTrue(); 128 expectThat(areaIdConfig.hasMaxSupportedValue()).isFalse(); 129 } 130 131 @Test testSetMaxSupportedValue()132 public void testSetMaxSupportedValue() { 133 var areaIdConfig = new AreaIdConfig.Builder<Integer>(ACCESS, AREA_ID) 134 .setMaxValue(MAX_VALUE).build(); 135 136 expectThat(areaIdConfig.getMaxValue()).isEqualTo(MAX_VALUE); 137 expectThat(areaIdConfig.hasMaxSupportedValue()).isTrue(); 138 expectThat(areaIdConfig.hasMinSupportedValue()).isFalse(); 139 } 140 141 @Test testSetSupportedEnumValues()142 public void testSetSupportedEnumValues() { 143 var areaIdConfig = new AreaIdConfig.Builder<Integer>(ACCESS, AREA_ID) 144 .setSupportedEnumValues(SUPPORTED_ENUM_VALUES).build(); 145 146 expectThat(areaIdConfig.getSupportedEnumValues()).containsExactlyElementsIn( 147 SUPPORTED_ENUM_VALUES); 148 expectThat(areaIdConfig.hasSupportedValuesList()).isTrue(); 149 } 150 151 @Test toString_doesNotReturnNull()152 public void toString_doesNotReturnNull() { 153 assertThat(AREA_ID_CONFIG.toString()).isNotNull(); 154 } 155 156 @Test creator_newArrayWorksCorrectly()157 public void creator_newArrayWorksCorrectly() { 158 AreaIdConfig<Object>[] areaIdConfigs = AreaIdConfig.CREATOR.newArray(2); 159 assertThat(areaIdConfigs).hasLength(2); 160 assertThat(areaIdConfigs[0]).isNull(); 161 assertThat(areaIdConfigs[1]).isNull(); 162 } 163 } 164 165