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.os.Parcel;
22 
23 import org.junit.Test;
24 
25 import java.util.List;
26 
27 /**
28  * Unit tests for {@link AreaIdConfig}
29  */
30 public class AreaIdConfigTest {
31     private static final int ACCESS = 1;
32     private static final int AREA_ID = 99;
33     private static final int MAX_VALUE = 10;
34     private static final int MIN_VALUE = 1;
35     private static final List<Integer> SUPPORTED_ENUM_VALUES = List.of(2, 3, 4);
36     private static final AreaIdConfig<Integer> AREA_ID_CONFIG = new AreaIdConfig.Builder<Integer>(
37             ACCESS, AREA_ID).setMaxValue(MAX_VALUE).setMinValue(MIN_VALUE).setSupportedEnumValues(
38             SUPPORTED_ENUM_VALUES).setSupportVariableUpdateRate(true).build();
39 
40     @Test
getAccess_returnsExpectedValue()41     public void getAccess_returnsExpectedValue() {
42         assertThat(AREA_ID_CONFIG.getAccess()).isEqualTo(ACCESS);
43     }
44 
45     @Test
getAreaId_returnsExpectedValue()46     public void getAreaId_returnsExpectedValue() {
47         assertThat(AREA_ID_CONFIG.getAreaId()).isEqualTo(AREA_ID);
48     }
49 
50     @Test
getMinValue_returnsExpectedValue()51     public void getMinValue_returnsExpectedValue() {
52         assertThat(AREA_ID_CONFIG.getMinValue()).isEqualTo(MIN_VALUE);
53     }
54 
55     @Test
getMinValue_returnsNullIfNotSet()56     public void getMinValue_returnsNullIfNotSet() {
57         assertThat(new AreaIdConfig.Builder<Long>(ACCESS, AREA_ID).build().getMinValue()).isNull();
58     }
59 
60     @Test
getMaxValue_returnsExpectedValue()61     public void getMaxValue_returnsExpectedValue() {
62         assertThat(AREA_ID_CONFIG.getMaxValue()).isEqualTo(MAX_VALUE);
63     }
64 
65     @Test
getMaxValue_returnsNullIfNotSet()66     public void getMaxValue_returnsNullIfNotSet() {
67         assertThat(new AreaIdConfig.Builder<Long>(ACCESS, AREA_ID).build().getMaxValue()).isNull();
68     }
69 
70     @Test
getSupportedEnumValues_returnsExpectedValue()71     public void getSupportedEnumValues_returnsExpectedValue() {
72         assertThat(AREA_ID_CONFIG.getSupportedEnumValues()).containsExactlyElementsIn(
73                 SUPPORTED_ENUM_VALUES);
74     }
75 
76     @Test
getSupportedEnumValues_returnsEmptyListIfNoSet()77     public void getSupportedEnumValues_returnsEmptyListIfNoSet() {
78         assertThat(new AreaIdConfig.Builder<Long>(
79                 ACCESS, AREA_ID).build().getSupportedEnumValues()).isEmpty();
80     }
81 
82     @Test
describeContents_returnsExpectedValue()83     public void describeContents_returnsExpectedValue() {
84         assertThat(AREA_ID_CONFIG.describeContents()).isEqualTo(0);
85     }
86 
87     @Test
isVariableUpdateRateSupported()88     public void isVariableUpdateRateSupported() {
89         assertThat(AREA_ID_CONFIG.isVariableUpdateRateSupported()).isTrue();
90     }
91 
92     @Test
isVariableUpdateRateSupported_defaultFalse()93     public void isVariableUpdateRateSupported_defaultFalse() {
94         assertThat(new AreaIdConfig.Builder<Long>(AREA_ID).build().isVariableUpdateRateSupported())
95                 .isFalse();
96     }
97 
98     @Test
writeToParcel_writesCorrectly()99     public void writeToParcel_writesCorrectly() {
100         Parcel parcel = Parcel.obtain();
101         AREA_ID_CONFIG.writeToParcel(parcel, /*flags=*/0);
102 
103         // After you're done with writing, you need to reset the parcel for reading.
104         parcel.setDataPosition(0);
105 
106         AreaIdConfig<Object> areaIdConfig = AreaIdConfig.CREATOR.createFromParcel(parcel);
107         assertThat((Integer) areaIdConfig.getMaxValue()).isEqualTo(MAX_VALUE);
108         assertThat((Integer) areaIdConfig.getMinValue()).isEqualTo(MIN_VALUE);
109         assertThat(
110                 (List<?>) areaIdConfig.getSupportedEnumValues()).containsExactlyElementsIn(
111                 SUPPORTED_ENUM_VALUES);
112         assertThat(areaIdConfig.isVariableUpdateRateSupported()).isTrue();
113     }
114 
115     @Test
toString_doesNotReturnNull()116     public void toString_doesNotReturnNull() {
117         assertThat(AREA_ID_CONFIG.toString()).isNotNull();
118     }
119 
120     @Test
creator_newArrayWorksCorrectly()121     public void creator_newArrayWorksCorrectly() {
122         AreaIdConfig<Object>[] areaIdConfigs = AreaIdConfig.CREATOR.newArray(2);
123         assertThat(areaIdConfigs).hasLength(2);
124         assertThat(areaIdConfigs[0]).isNull();
125         assertThat(areaIdConfigs[1]).isNull();
126     }
127 }
128 
129