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.media.FadeManagerConfiguration.FADE_STATE_DISABLED; 20 21 import static org.junit.Assert.assertThrows; 22 23 import android.car.test.AbstractExpectableTestCase; 24 import android.media.FadeManagerConfiguration; 25 import android.os.Parcel; 26 27 import org.junit.Test; 28 29 public final class CarAudioFadeConfigurationUnitTest extends AbstractExpectableTestCase { 30 31 private static final int TEST_PARCEL_FLAGS = 0; 32 private static final String TEST_NAME_AGGRESSIVE_FADING = "aggressive fading"; 33 private static final String TEST_NAME_DISABLED_FADING = "disabled fading"; 34 private static final String TEST_NAME_RELAXED_FADING = "relaxed fading"; 35 private static final FadeManagerConfiguration TEST_FADE_MANAGER_CONFIG_DISABLED = 36 new FadeManagerConfiguration.Builder().setFadeState(FADE_STATE_DISABLED).build(); 37 private static final FadeManagerConfiguration TEST_FADE_MANAGER_CONFIG_ENABLED = 38 new FadeManagerConfiguration.Builder().build(); 39 40 @Test build()41 public void build() { 42 CarAudioFadeConfiguration carAudioFadeConfiguration = 43 new CarAudioFadeConfiguration.Builder(TEST_FADE_MANAGER_CONFIG_ENABLED) 44 .setName(TEST_NAME_AGGRESSIVE_FADING).build(); 45 46 expectWithMessage("Fade manager configuration") 47 .that(carAudioFadeConfiguration.getFadeManagerConfiguration()) 48 .isEqualTo(TEST_FADE_MANAGER_CONFIG_ENABLED); 49 expectWithMessage("Car Audio Fade configuration name") 50 .that(carAudioFadeConfiguration.getName()).isEqualTo(TEST_NAME_AGGRESSIVE_FADING); 51 } 52 53 @Test builder_withNullFadeManagerConfiguration_fails()54 public void builder_withNullFadeManagerConfiguration_fails() { 55 56 NullPointerException thrown = assertThrows(NullPointerException.class, 57 () -> new CarAudioFadeConfiguration.Builder(/* fadeManagerConfiguration= */ null) 58 ); 59 60 expectWithMessage("Null fade manager configuration exception") 61 .that(thrown).hasMessageThat().contains("configuration can not be null"); 62 } 63 64 @Test builder_withNullName_fails()65 public void builder_withNullName_fails() { 66 67 NullPointerException thrown = assertThrows(NullPointerException.class, 68 () -> new CarAudioFadeConfiguration.Builder(TEST_FADE_MANAGER_CONFIG_ENABLED) 69 .setName(/* name= */ null) 70 ); 71 72 expectWithMessage("Null name exception") 73 .that(thrown).hasMessageThat().contains("Name can not"); 74 } 75 76 @Test builder_withReuse_fails()77 public void builder_withReuse_fails() { 78 CarAudioFadeConfiguration.Builder builder = 79 new CarAudioFadeConfiguration.Builder(TEST_FADE_MANAGER_CONFIG_ENABLED); 80 builder.build(); 81 82 IllegalStateException thrown = assertThrows(IllegalStateException.class, () -> 83 builder.build() 84 ); 85 86 expectWithMessage("Reuse builder exception") 87 .that(thrown).hasMessageThat().contains("should not be reused"); 88 } 89 90 91 @Test writeToParcel_thenCreateFromParcel()92 public void writeToParcel_thenCreateFromParcel() { 93 Parcel parcel = Parcel.obtain(); 94 95 CarAudioFadeConfiguration carAudioFadeConfiguration = createAndWriteToParcel(parcel); 96 97 expectWithMessage("Car audio fade configuration from parcel") 98 .that(CarAudioFadeConfiguration.CREATOR.createFromParcel(parcel)) 99 .isEqualTo(carAudioFadeConfiguration); 100 } 101 102 @Test equals_withDuplicate()103 public void equals_withDuplicate() { 104 CarAudioFadeConfiguration result = 105 new CarAudioFadeConfiguration.Builder(TEST_FADE_MANAGER_CONFIG_ENABLED).build(); 106 CarAudioFadeConfiguration duplResult = 107 new CarAudioFadeConfiguration.Builder(TEST_FADE_MANAGER_CONFIG_ENABLED).build(); 108 109 expectWithMessage("Duplicate car audio fade configuration").that(result) 110 .isEqualTo(duplResult); 111 } 112 113 @Test equals_withDifferentFadeManagerConfiguration()114 public void equals_withDifferentFadeManagerConfiguration() { 115 CarAudioFadeConfiguration result1 = 116 new CarAudioFadeConfiguration.Builder(TEST_FADE_MANAGER_CONFIG_ENABLED) 117 .setName(TEST_NAME_RELAXED_FADING).build(); 118 CarAudioFadeConfiguration result2 = 119 new CarAudioFadeConfiguration.Builder(TEST_FADE_MANAGER_CONFIG_DISABLED) 120 .setName(TEST_NAME_DISABLED_FADING).build(); 121 122 expectWithMessage("Car audio fade configuration with different fade configs").that(result1) 123 .isNotEqualTo(result2); 124 } 125 126 @Test hashCode_withDuplicate_equals()127 public void hashCode_withDuplicate_equals() { 128 CarAudioFadeConfiguration result = 129 new CarAudioFadeConfiguration.Builder(TEST_FADE_MANAGER_CONFIG_ENABLED).build(); 130 CarAudioFadeConfiguration duplResult = 131 new CarAudioFadeConfiguration.Builder(TEST_FADE_MANAGER_CONFIG_ENABLED).build(); 132 133 expectWithMessage("Duplicate hash code").that(result.hashCode()) 134 .isEqualTo(duplResult.hashCode()); 135 } 136 137 createAndWriteToParcel(Parcel parcel)138 private CarAudioFadeConfiguration createAndWriteToParcel(Parcel parcel) { 139 CarAudioFadeConfiguration result = 140 new CarAudioFadeConfiguration.Builder(TEST_FADE_MANAGER_CONFIG_ENABLED) 141 .setName(TEST_NAME_RELAXED_FADING).build(); 142 143 result.writeToParcel(parcel, TEST_PARCEL_FLAGS); 144 parcel.setDataPosition(/* pos= */ 0); 145 return result; 146 } 147 } 148