• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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.media;
18 
19 import static android.media.AudioAttributes.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE;
20 import static android.media.AudioAttributes.USAGE_ASSISTANT;
21 import static android.media.AudioAttributes.USAGE_MEDIA;
22 
23 import static org.junit.Assert.assertThrows;
24 
25 import android.car.feature.Flags;
26 import android.car.test.AbstractExpectableTestCase;
27 import android.media.AudioAttributes;
28 import android.os.Parcel;
29 import android.platform.test.flag.junit.SetFlagsRule;
30 
31 import org.junit.Rule;
32 import org.junit.Test;
33 
34 import java.util.List;
35 
36 public final class CarAudioZoneConfigInfoUnitTest extends AbstractExpectableTestCase {
37 
38     private static final int TEST_PARCEL_FLAGS = 0;
39     private static final int TEST_ZONE_ID = 1;
40     private static final int TEST_CONFIG_ID = 0;
41     private static final int TEST_CONFIG_ID_2 = 1;
42     private static final String TEST_CONFIG_NAME = "zone 1 config 0";
43 
44     private static final int TEST_PRIMARY_GROUP_ID = 7;
45     private static final String TEST_GROUP_NAME = "3";
46     private static final int TEST_MAX_GAIN_INDEX = 9_005;
47     private static final int TEST_MIN_GAIN_INDEX = 0;
48     private static final int TEST_MAX_ACTIVATION_GAIN_INDEX = 8_005;
49     private static final int TEST_MIN_ACTIVATION_GAIN_INDEX = 1_000;
50 
51     private static final AudioAttributes TEST_MEDIA_AUDIO_ATTRIBUTE =
52             new AudioAttributes.Builder().setUsage(USAGE_MEDIA).build();
53     private static final AudioAttributes TEST_NAVIGATION_AUDIO_ATTRIBUTE =
54             new AudioAttributes.Builder().setUsage(USAGE_ASSISTANCE_NAVIGATION_GUIDANCE).build();
55     private static final AudioAttributes TEST_ASSISTANT_AUDIO_ATTRIBUTE =
56             new AudioAttributes.Builder().setUsage(USAGE_ASSISTANT).build();
57     private static final List<AudioAttributes> TEST_AUDIO_ATTRIBUTES = List.of(
58             TEST_MEDIA_AUDIO_ATTRIBUTE, TEST_NAVIGATION_AUDIO_ATTRIBUTE,
59             TEST_ASSISTANT_AUDIO_ATTRIBUTE);
60     private static final CarVolumeGroupInfo TEST_VOLUME_INFO =
61             new CarVolumeGroupInfo.Builder(TEST_GROUP_NAME, TEST_ZONE_ID, TEST_PRIMARY_GROUP_ID)
62                     .setMaxVolumeGainIndex(TEST_MAX_GAIN_INDEX)
63                     .setMinVolumeGainIndex(TEST_MIN_GAIN_INDEX)
64                     .setMaxActivationVolumeGainIndex(TEST_MAX_ACTIVATION_GAIN_INDEX)
65                     .setMinActivationVolumeGainIndex(TEST_MIN_ACTIVATION_GAIN_INDEX)
66                     .setAudioAttributes(TEST_AUDIO_ATTRIBUTES).build();
67     private static final boolean TEST_ACTIVE_STATUS = true;
68     private static final boolean TEST_SELECTED_STATUS = false;
69     private static final boolean TEST_DEFAULT_STATUS = true;
70     private static final CarAudioZoneConfigInfo TEST_ZONE_CONFIG_INFO =
71             new CarAudioZoneConfigInfo(TEST_CONFIG_NAME, List.of(TEST_VOLUME_INFO),
72                     TEST_ZONE_ID, TEST_CONFIG_ID, TEST_ACTIVE_STATUS, TEST_SELECTED_STATUS,
73                     TEST_DEFAULT_STATUS);
74 
75     @Rule
76     public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
77 
78     @Test
constructor_withNullName_fails()79     public void constructor_withNullName_fails() {
80         NullPointerException thrown = assertThrows(NullPointerException.class, () ->
81                 new CarAudioZoneConfigInfo(/* name= */ null, TEST_ZONE_ID, TEST_CONFIG_ID)
82         );
83 
84         expectWithMessage("Null zone configuration info name exception")
85                 .that(thrown).hasMessageThat().contains("Zone configuration name");
86     }
87 
88     @Test
constructor_withNullVolumeGroups_fails()89     public void constructor_withNullVolumeGroups_fails() {
90         NullPointerException thrown = assertThrows(NullPointerException.class, () ->
91                 new CarAudioZoneConfigInfo(TEST_CONFIG_NAME, /* groups= */ null , TEST_ZONE_ID,
92                         TEST_CONFIG_ID, /* isActive= */ true, /* isSelected= */ false,
93                         /* isDefault= */ false)
94         );
95 
96         expectWithMessage("Null zone configuration info volumes exception")
97                 .that(thrown).hasMessageThat().contains("Zone configuration volume groups");
98     }
99 
100     @Test
getZoneId()101     public void getZoneId() {
102         expectWithMessage("Zone id of zone configuration info")
103                 .that(TEST_ZONE_CONFIG_INFO.getZoneId()).isEqualTo(TEST_ZONE_ID);
104     }
105 
106     @Test
getConfigId()107     public void getConfigId() {
108         expectWithMessage("Config id of zone configuration info")
109                 .that(TEST_ZONE_CONFIG_INFO.getConfigId()).isEqualTo(TEST_CONFIG_ID);
110     }
111 
112     @Test
getName()113     public void getName() {
114         expectWithMessage("Config name of zone configuration info")
115                 .that(TEST_ZONE_CONFIG_INFO.getName()).isEqualTo(TEST_CONFIG_NAME);
116     }
117 
118     @Test
isActive()119     public void isActive() {
120         mSetFlagsRule.enableFlags(Flags.FLAG_CAR_AUDIO_DYNAMIC_DEVICES);
121 
122         expectWithMessage("Config active status")
123                 .that(TEST_ZONE_CONFIG_INFO.isActive()).isEqualTo(TEST_ACTIVE_STATUS);
124     }
125 
126     @Test
isSelected()127     public void isSelected() {
128         mSetFlagsRule.enableFlags(Flags.FLAG_CAR_AUDIO_DYNAMIC_DEVICES);
129 
130         expectWithMessage("Config selected status")
131                 .that(TEST_ZONE_CONFIG_INFO.isSelected()).isEqualTo(TEST_SELECTED_STATUS);
132     }
133 
134     @Test
isDefault()135     public void isDefault() {
136         mSetFlagsRule.enableFlags(Flags.FLAG_CAR_AUDIO_DYNAMIC_DEVICES);
137 
138         expectWithMessage("Config default indicator").that(TEST_ZONE_CONFIG_INFO.isDefault())
139                 .isEqualTo(TEST_DEFAULT_STATUS);
140     }
141 
142     @Test
getConfigVolumeGroups()143     public void getConfigVolumeGroups() {
144         mSetFlagsRule.enableFlags(Flags.FLAG_CAR_AUDIO_DYNAMIC_DEVICES);
145 
146         expectWithMessage("Config volume groups").that(
147                 TEST_ZONE_CONFIG_INFO.getConfigVolumeGroups()).containsExactly(TEST_VOLUME_INFO);
148     }
149 
150     @Test
writeToParcel_createFromParcel()151     public void writeToParcel_createFromParcel() {
152         Parcel parcel = Parcel.obtain();
153 
154         TEST_ZONE_CONFIG_INFO.writeToParcel(parcel, TEST_PARCEL_FLAGS);
155         parcel.setDataPosition(/* pos= */ 0);
156 
157         expectWithMessage("Zone configuration info created from parcel")
158                 .that(CarAudioZoneConfigInfo.CREATOR.createFromParcel(parcel))
159                 .isEqualTo(TEST_ZONE_CONFIG_INFO);
160     }
161 
162     @Test
newArray()163     public void newArray() {
164         CarAudioZoneConfigInfo[] infos = CarAudioZoneConfigInfo.CREATOR.newArray(/* size= */ 3);
165 
166         expectWithMessage("Zone configuration infos size").that(infos).hasLength(3);
167     }
168 
169     @Test
equals_forSameContent()170     public void equals_forSameContent() {
171         mSetFlagsRule.disableFlags(Flags.FLAG_CAR_AUDIO_DYNAMIC_DEVICES);
172         CarAudioZoneConfigInfo infoWithSameContent = new CarAudioZoneConfigInfo(TEST_CONFIG_NAME,
173                 TEST_ZONE_ID, TEST_CONFIG_ID);
174 
175         expectWithMessage("Zone configuration info with same content")
176                 .that(infoWithSameContent).isEqualTo(TEST_ZONE_CONFIG_INFO);
177     }
178 
179     @Test
equals_forSameContent_withDynamicFlagEnabled()180     public void equals_forSameContent_withDynamicFlagEnabled() {
181         mSetFlagsRule.enableFlags(Flags.FLAG_CAR_AUDIO_DYNAMIC_DEVICES);
182         CarAudioZoneConfigInfo infoWithSameContent = new CarAudioZoneConfigInfo(TEST_CONFIG_NAME,
183                 List.of(TEST_VOLUME_INFO), TEST_ZONE_ID, TEST_CONFIG_ID, TEST_ACTIVE_STATUS,
184                 TEST_SELECTED_STATUS, TEST_DEFAULT_STATUS);
185 
186         expectWithMessage("Zone config info with same content and dynamic flags enabled")
187                 .that(infoWithSameContent).isEqualTo(TEST_ZONE_CONFIG_INFO);
188     }
189 
190     @Test
equals_forDifferentContent_withDynamicFlagEnabled()191     public void equals_forDifferentContent_withDynamicFlagEnabled() {
192         mSetFlagsRule.enableFlags(Flags.FLAG_CAR_AUDIO_DYNAMIC_DEVICES);
193         CarAudioZoneConfigInfo infoWithSameContent = new CarAudioZoneConfigInfo(TEST_CONFIG_NAME,
194                 List.of(TEST_VOLUME_INFO), TEST_ZONE_ID, TEST_CONFIG_ID_2, TEST_ACTIVE_STATUS,
195                 TEST_SELECTED_STATUS, TEST_DEFAULT_STATUS);
196 
197         expectWithMessage("Zone config info with same content and dynamic flags enabled")
198                 .that(infoWithSameContent).isNotEqualTo(TEST_ZONE_CONFIG_INFO);
199     }
200 
201     @Test
equals_forNull()202     public void equals_forNull() {
203         expectWithMessage("Zone configuration info null content")
204                 .that(TEST_ZONE_CONFIG_INFO.equals(null)).isFalse();
205     }
206 
207     @Test
hasSameConfigInfo_forSameContent()208     public void hasSameConfigInfo_forSameContent() {
209         mSetFlagsRule.enableFlags(Flags.FLAG_CAR_AUDIO_DYNAMIC_DEVICES);
210         CarAudioZoneConfigInfo infoWithSameContent = new CarAudioZoneConfigInfo(TEST_CONFIG_NAME,
211                 TEST_ZONE_ID, TEST_CONFIG_ID);
212 
213         expectWithMessage("Zone configuration with same info")
214                 .that(infoWithSameContent.hasSameConfigInfo(TEST_ZONE_CONFIG_INFO)).isTrue();
215     }
216 
217     @Test
hasSameConfigInfo_withNullInfo()218     public void hasSameConfigInfo_withNullInfo() {
219         mSetFlagsRule.enableFlags(Flags.FLAG_CAR_AUDIO_DYNAMIC_DEVICES);
220         CarAudioZoneConfigInfo infoWithSameContent = new CarAudioZoneConfigInfo(TEST_CONFIG_NAME,
221                 TEST_ZONE_ID, TEST_CONFIG_ID);
222 
223         NullPointerException thrown = assertThrows(NullPointerException.class,
224                 () -> infoWithSameContent.hasSameConfigInfo(null));
225 
226         expectWithMessage("Zone configuration exception")
227                 .that(thrown).hasMessageThat().contains("Car audio zone info");
228     }
229 
230     @Test
hashCode_forSameContent()231     public void hashCode_forSameContent() {
232         mSetFlagsRule.disableFlags(Flags.FLAG_CAR_AUDIO_DYNAMIC_DEVICES);
233         CarAudioZoneConfigInfo infoWithSameContent = new CarAudioZoneConfigInfo(TEST_CONFIG_NAME,
234                 TEST_ZONE_ID, TEST_CONFIG_ID);
235 
236         expectWithMessage("Zone Configuration info hash with same content")
237                 .that(infoWithSameContent.hashCode()).isEqualTo(TEST_ZONE_CONFIG_INFO.hashCode());
238     }
239 }
240