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.oem; 18 19 import static android.car.feature.Flags.FLAG_CAR_AUDIO_DYNAMIC_DEVICES; 20 import static android.car.oem.CarAudioFeaturesInfo.AUDIO_FEATURE_ISOLATED_DEVICE_FOCUS; 21 import static android.car.oem.CarAudioFeaturesInfo.AUDIO_FEATURE_NO_FEATURE; 22 import static android.car.oem.CarAudioFeaturesInfo.AUDIO_FEATURE_FADE_MANAGER_CONFIGS; 23 24 import static org.junit.Assert.assertThrows; 25 26 import android.car.test.AbstractExpectableTestCase; 27 import android.os.Parcel; 28 import android.platform.test.flag.junit.SetFlagsRule; 29 30 import org.junit.Rule; 31 import org.junit.Test; 32 33 public final class CarAudioFeaturesInfoUnitTest extends AbstractExpectableTestCase { 34 35 private static final int TEST_PARCEL_FLAGS = 0; 36 @Rule 37 public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); 38 39 @Test build_withDynamicDeviceFeature()40 public void build_withDynamicDeviceFeature() { 41 mSetFlagsRule.enableFlags(FLAG_CAR_AUDIO_DYNAMIC_DEVICES); 42 CarAudioFeaturesInfo info = new CarAudioFeaturesInfo.Builder( 43 AUDIO_FEATURE_ISOLATED_DEVICE_FOCUS).build(); 44 45 expectWithMessage("Car audio dynamic devices feature with dynamic device only") 46 .that(info.isAudioFeatureEnabled(AUDIO_FEATURE_ISOLATED_DEVICE_FOCUS)).isTrue(); 47 expectWithMessage("Car fade manager feature with dynamic device only") 48 .that(info.isAudioFeatureEnabled(AUDIO_FEATURE_FADE_MANAGER_CONFIGS)) 49 .isFalse(); 50 } 51 52 @Test build_withFadeManagerFeature()53 public void build_withFadeManagerFeature() { 54 mSetFlagsRule.enableFlags(FLAG_CAR_AUDIO_DYNAMIC_DEVICES); 55 CarAudioFeaturesInfo info = new CarAudioFeaturesInfo.Builder( 56 AUDIO_FEATURE_FADE_MANAGER_CONFIGS).build(); 57 58 expectWithMessage("Car audio dynamic devices feature with fade manager only") 59 .that(info.isAudioFeatureEnabled(AUDIO_FEATURE_ISOLATED_DEVICE_FOCUS)).isFalse(); 60 expectWithMessage("Car fade manager feature with fade manager device only") 61 .that(info.isAudioFeatureEnabled(AUDIO_FEATURE_FADE_MANAGER_CONFIGS)) 62 .isTrue(); 63 } 64 65 @Test build_withNoFeatures()66 public void build_withNoFeatures() { 67 mSetFlagsRule.enableFlags(FLAG_CAR_AUDIO_DYNAMIC_DEVICES); 68 CarAudioFeaturesInfo info = new CarAudioFeaturesInfo.Builder( 69 AUDIO_FEATURE_NO_FEATURE).build(); 70 71 expectWithMessage("Car audio dynamic devices feature with no features") 72 .that(info.isAudioFeatureEnabled(AUDIO_FEATURE_ISOLATED_DEVICE_FOCUS)).isFalse(); 73 expectWithMessage("Car fade manager feature with no features") 74 .that(info.isAudioFeatureEnabled(AUDIO_FEATURE_FADE_MANAGER_CONFIGS)) 75 .isFalse(); 76 } 77 78 @Test build_afterReUse_fails()79 public void build_afterReUse_fails() { 80 mSetFlagsRule.enableFlags(FLAG_CAR_AUDIO_DYNAMIC_DEVICES); 81 CarAudioFeaturesInfo.Builder builder = new CarAudioFeaturesInfo.Builder( 82 AUDIO_FEATURE_NO_FEATURE); 83 builder.build(); 84 85 IllegalStateException thrown = assertThrows(IllegalStateException.class, builder::build); 86 87 expectWithMessage("Re-using build in builder exception").that(thrown).hasMessageThat() 88 .contains("should not be reused"); 89 } 90 91 @Test build_withInvalidFeature_fails()92 public void build_withInvalidFeature_fails() { 93 int invalidAudioFeature = -1; 94 mSetFlagsRule.enableFlags(FLAG_CAR_AUDIO_DYNAMIC_DEVICES); 95 IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, 96 () -> new CarAudioFeaturesInfo.Builder(invalidAudioFeature)); 97 98 expectWithMessage("Invalid audio feature exception") 99 .that(thrown).hasMessageThat().contains("invalid"); 100 } 101 102 @Test addAudioFeatures_withDynamicDevicesFeature()103 public void addAudioFeatures_withDynamicDevicesFeature() { 104 mSetFlagsRule.enableFlags(FLAG_CAR_AUDIO_DYNAMIC_DEVICES); 105 CarAudioFeaturesInfo info = new CarAudioFeaturesInfo.Builder(AUDIO_FEATURE_NO_FEATURE) 106 .addAudioFeature(AUDIO_FEATURE_ISOLATED_DEVICE_FOCUS).build(); 107 108 expectWithMessage("Car audio focus feature with added focus feature only") 109 .that(info.isAudioFeatureEnabled(AUDIO_FEATURE_ISOLATED_DEVICE_FOCUS)).isTrue(); 110 expectWithMessage("Car audio fade manager feature with added focus feature only") 111 .that(info.isAudioFeatureEnabled(AUDIO_FEATURE_FADE_MANAGER_CONFIGS)) 112 .isFalse(); 113 } 114 115 @Test addAudioFeatures_withFadeManagerFeature()116 public void addAudioFeatures_withFadeManagerFeature() { 117 mSetFlagsRule.enableFlags(FLAG_CAR_AUDIO_DYNAMIC_DEVICES); 118 CarAudioFeaturesInfo info = new CarAudioFeaturesInfo.Builder(AUDIO_FEATURE_NO_FEATURE) 119 .addAudioFeature(AUDIO_FEATURE_FADE_MANAGER_CONFIGS).build(); 120 121 expectWithMessage("Car audio focus feature with added fade manager feature") 122 .that(info.isAudioFeatureEnabled(AUDIO_FEATURE_ISOLATED_DEVICE_FOCUS)).isFalse(); 123 expectWithMessage("Car audio fade manager feature with added fade manager feature") 124 .that(info.isAudioFeatureEnabled(AUDIO_FEATURE_FADE_MANAGER_CONFIGS)) 125 .isTrue(); 126 } 127 128 @Test addAudioFeatures_afterReUse_fails()129 public void addAudioFeatures_afterReUse_fails() { 130 mSetFlagsRule.enableFlags(FLAG_CAR_AUDIO_DYNAMIC_DEVICES); 131 CarAudioFeaturesInfo.Builder builder = new CarAudioFeaturesInfo.Builder( 132 AUDIO_FEATURE_NO_FEATURE); 133 builder.build(); 134 135 IllegalStateException thrown = assertThrows(IllegalStateException.class, 136 () -> builder.addAudioFeature(AUDIO_FEATURE_FADE_MANAGER_CONFIGS)); 137 138 expectWithMessage("Re-using add audio feature in builder exception") 139 .that(thrown).hasMessageThat().contains("should not be reused"); 140 } 141 142 @Test writeToParcel_readFromParcel_andFocusFeature()143 public void writeToParcel_readFromParcel_andFocusFeature() { 144 mSetFlagsRule.enableFlags(FLAG_CAR_AUDIO_DYNAMIC_DEVICES); 145 CarAudioFeaturesInfo info = new CarAudioFeaturesInfo.Builder( 146 AUDIO_FEATURE_ISOLATED_DEVICE_FOCUS).build(); 147 Parcel parcel = Parcel.obtain(); 148 149 info.writeToParcel(parcel, TEST_PARCEL_FLAGS); 150 151 parcel.setDataPosition(/* pos= */ 0); 152 CarAudioFeaturesInfo createdInfo = CarAudioFeaturesInfo.CREATOR.createFromParcel(parcel); 153 expectWithMessage("Car audio device feature with focus feature and crated from parcel") 154 .that(createdInfo).isEqualTo(info); 155 expectWithMessage("Car focus feature with focus feature and created from parcel") 156 .that(info.isAudioFeatureEnabled(AUDIO_FEATURE_ISOLATED_DEVICE_FOCUS)).isTrue(); 157 expectWithMessage("Car fade manager feature with focus feature and created from parcel") 158 .that(info.isAudioFeatureEnabled(AUDIO_FEATURE_FADE_MANAGER_CONFIGS)) 159 .isFalse(); 160 } 161 162 @Test writeToParcel_readFromParcel_andFadeManagerFeature()163 public void writeToParcel_readFromParcel_andFadeManagerFeature() { 164 mSetFlagsRule.enableFlags(FLAG_CAR_AUDIO_DYNAMIC_DEVICES); 165 CarAudioFeaturesInfo info = new CarAudioFeaturesInfo.Builder( 166 AUDIO_FEATURE_FADE_MANAGER_CONFIGS).build(); 167 Parcel parcel = Parcel.obtain(); 168 169 info.writeToParcel(parcel, TEST_PARCEL_FLAGS); 170 171 parcel.setDataPosition(/* pos= */ 0); 172 CarAudioFeaturesInfo createdInfo = CarAudioFeaturesInfo.CREATOR.createFromParcel(parcel); 173 expectWithMessage("Car audio device feature with fade manager feature created from parcel") 174 .that(createdInfo).isEqualTo(info); 175 expectWithMessage("Car focus feature with fade manager feature and created from parcel") 176 .that(info.isAudioFeatureEnabled(AUDIO_FEATURE_ISOLATED_DEVICE_FOCUS)).isFalse(); 177 expectWithMessage("Car fade manager feature with fade manager feature" 178 + "and created from parcel") 179 .that(info.isAudioFeatureEnabled(AUDIO_FEATURE_FADE_MANAGER_CONFIGS)) 180 .isTrue(); 181 } 182 183 @Test equals_withFadeManagerFeature()184 public void equals_withFadeManagerFeature() { 185 mSetFlagsRule.enableFlags(FLAG_CAR_AUDIO_DYNAMIC_DEVICES); 186 CarAudioFeaturesInfo info1 = new CarAudioFeaturesInfo.Builder( 187 AUDIO_FEATURE_FADE_MANAGER_CONFIGS).build(); 188 CarAudioFeaturesInfo info2 = new CarAudioFeaturesInfo.Builder( 189 AUDIO_FEATURE_FADE_MANAGER_CONFIGS).build(); 190 191 expectWithMessage("Fade manager configuration features").that(info1).isEqualTo(info2); 192 } 193 194 @Test equals_withFocusFeature()195 public void equals_withFocusFeature() { 196 mSetFlagsRule.enableFlags(FLAG_CAR_AUDIO_DYNAMIC_DEVICES); 197 CarAudioFeaturesInfo info1 = new CarAudioFeaturesInfo.Builder( 198 AUDIO_FEATURE_ISOLATED_DEVICE_FOCUS).build(); 199 CarAudioFeaturesInfo info2 = new CarAudioFeaturesInfo.Builder( 200 AUDIO_FEATURE_ISOLATED_DEVICE_FOCUS).build(); 201 202 expectWithMessage("Isolated focus features").that(info1).isEqualTo(info2); 203 } 204 205 @Test equals_withDifferentFeatures()206 public void equals_withDifferentFeatures() { 207 mSetFlagsRule.enableFlags(FLAG_CAR_AUDIO_DYNAMIC_DEVICES); 208 CarAudioFeaturesInfo info1 = new CarAudioFeaturesInfo.Builder( 209 AUDIO_FEATURE_ISOLATED_DEVICE_FOCUS).build(); 210 CarAudioFeaturesInfo info2 = new CarAudioFeaturesInfo.Builder( 211 AUDIO_FEATURE_FADE_MANAGER_CONFIGS).build(); 212 213 expectWithMessage("Car audio features").that(info1).isNotEqualTo(info2); 214 } 215 216 @Test hashCode_withFocusFeature()217 public void hashCode_withFocusFeature() { 218 mSetFlagsRule.enableFlags(FLAG_CAR_AUDIO_DYNAMIC_DEVICES); 219 CarAudioFeaturesInfo info1 = new CarAudioFeaturesInfo.Builder( 220 AUDIO_FEATURE_ISOLATED_DEVICE_FOCUS).build(); 221 CarAudioFeaturesInfo info2 = new CarAudioFeaturesInfo.Builder( 222 AUDIO_FEATURE_ISOLATED_DEVICE_FOCUS).build(); 223 224 expectWithMessage("Isolated focus features hash").that(info1.hashCode()) 225 .isEqualTo(info2.hashCode()); 226 } 227 228 @Test hashCode_withFadeManagerFeature()229 public void hashCode_withFadeManagerFeature() { 230 mSetFlagsRule.enableFlags(FLAG_CAR_AUDIO_DYNAMIC_DEVICES); 231 CarAudioFeaturesInfo info1 = new CarAudioFeaturesInfo.Builder( 232 AUDIO_FEATURE_FADE_MANAGER_CONFIGS).build(); 233 CarAudioFeaturesInfo info2 = new CarAudioFeaturesInfo.Builder( 234 AUDIO_FEATURE_FADE_MANAGER_CONFIGS).build(); 235 236 expectWithMessage("Fade manager features hash").that(info1.hashCode()) 237 .isEqualTo(info2.hashCode()); 238 } 239 240 @Test toString_withFocusFeature()241 public void toString_withFocusFeature() { 242 mSetFlagsRule.enableFlags(FLAG_CAR_AUDIO_DYNAMIC_DEVICES); 243 CarAudioFeaturesInfo info = new CarAudioFeaturesInfo.Builder( 244 AUDIO_FEATURE_ISOLATED_DEVICE_FOCUS).build(); 245 246 expectWithMessage("Isolated focus features string").that(info.toString()) 247 .contains("ISOLATED_DEVICE_FOCUS"); 248 } 249 250 @Test toString_withFadeManagerFeature()251 public void toString_withFadeManagerFeature() { 252 mSetFlagsRule.enableFlags(FLAG_CAR_AUDIO_DYNAMIC_DEVICES); 253 CarAudioFeaturesInfo info = new CarAudioFeaturesInfo.Builder( 254 AUDIO_FEATURE_FADE_MANAGER_CONFIGS).build(); 255 256 expectWithMessage("Fade manager feature string").that(info.toString()) 257 .contains("FADE_MANAGER_CONFIGS"); 258 } 259 260 @Test toString_withNoFeature()261 public void toString_withNoFeature() { 262 mSetFlagsRule.enableFlags(FLAG_CAR_AUDIO_DYNAMIC_DEVICES); 263 CarAudioFeaturesInfo info = new CarAudioFeaturesInfo.Builder( 264 AUDIO_FEATURE_NO_FEATURE).build(); 265 266 expectWithMessage("Empty audio feature").that(info.toString()) 267 .contains("NONE"); 268 } 269 } 270