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 com.android.car.audio; 18 19 import static com.android.car.audio.CarActivationVolumeConfig.ACTIVATION_VOLUME_ON_SOURCE_CHANGED; 20 21 import static org.junit.Assert.assertThrows; 22 23 import android.car.test.AbstractExpectableTestCase; 24 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 import org.mockito.junit.MockitoJUnitRunner; 28 29 @RunWith(MockitoJUnitRunner.class) 30 public final class CarActivationVolumeConfigTest extends AbstractExpectableTestCase { 31 32 private static final int MIN_ACTIVATION_GAIN_INDEX_PERCENTAGE = 10; 33 private static final int MAX_ACTIVATION_GAIN_INDEX_PERCENTAGE = 90; 34 private static final int ACTIVATION_VOLUME_INVOCATION_TYPE = 35 CarActivationVolumeConfig.ACTIVATION_VOLUME_ON_BOOT; 36 37 private static final CarActivationVolumeConfig CAR_ACTIVATION_VOLUME_CONFIG = 38 new CarActivationVolumeConfig(ACTIVATION_VOLUME_INVOCATION_TYPE, 39 MIN_ACTIVATION_GAIN_INDEX_PERCENTAGE, MAX_ACTIVATION_GAIN_INDEX_PERCENTAGE); 40 41 @Test construct_withMinActivationVolumePercentageHigherThanMax()42 public void construct_withMinActivationVolumePercentageHigherThanMax() { 43 IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, 44 () -> new CarActivationVolumeConfig(ACTIVATION_VOLUME_INVOCATION_TYPE, 45 MAX_ACTIVATION_GAIN_INDEX_PERCENTAGE, 46 MIN_ACTIVATION_GAIN_INDEX_PERCENTAGE)); 47 48 expectWithMessage("Exception for min activation volume percentage higher than max") 49 .that(thrown).hasMessageThat().contains("can not be higher than"); 50 } 51 52 @Test getInvocationType()53 public void getInvocationType() { 54 expectWithMessage("Activation volume invocation type") 55 .that(CAR_ACTIVATION_VOLUME_CONFIG.getInvocationType()) 56 .isEqualTo(ACTIVATION_VOLUME_INVOCATION_TYPE); 57 } 58 59 @Test getMinActivationVolumePercentage()60 public void getMinActivationVolumePercentage() { 61 expectWithMessage("Min activation volume percentage") 62 .that(CAR_ACTIVATION_VOLUME_CONFIG.getMinActivationVolumePercentage()) 63 .isEqualTo(MIN_ACTIVATION_GAIN_INDEX_PERCENTAGE); 64 } 65 66 @Test getMaxActivationVolumePercentage()67 public void getMaxActivationVolumePercentage() { 68 expectWithMessage("Max activation volume percentage") 69 .that(CAR_ACTIVATION_VOLUME_CONFIG.getMaxActivationVolumePercentage()) 70 .isEqualTo(MAX_ACTIVATION_GAIN_INDEX_PERCENTAGE); 71 } 72 73 @Test equals_withSameObject()74 public void equals_withSameObject() { 75 CarActivationVolumeConfig config = 76 new CarActivationVolumeConfig(CAR_ACTIVATION_VOLUME_CONFIG.getInvocationType(), 77 CAR_ACTIVATION_VOLUME_CONFIG.getMinActivationVolumePercentage(), 78 CAR_ACTIVATION_VOLUME_CONFIG.getMaxActivationVolumePercentage()); 79 80 expectWithMessage("Car volume activation") 81 .that(config.equals(CAR_ACTIVATION_VOLUME_CONFIG)).isTrue(); 82 } 83 84 @Test equals_withNullObject()85 public void equals_withNullObject() { 86 expectWithMessage("Car volume activation with null object") 87 .that(CAR_ACTIVATION_VOLUME_CONFIG.equals(null)).isFalse(); 88 } 89 90 @Test equals_withDifferentObject()91 public void equals_withDifferentObject() { 92 expectWithMessage("Car volume activation with different object") 93 .that(CAR_ACTIVATION_VOLUME_CONFIG.equals(new Object())).isFalse(); 94 } 95 96 @Test equals_withDifferentConfiguration()97 public void equals_withDifferentConfiguration() { 98 CarActivationVolumeConfig testActivationConfig = 99 new CarActivationVolumeConfig(ACTIVATION_VOLUME_ON_SOURCE_CHANGED, 100 MIN_ACTIVATION_GAIN_INDEX_PERCENTAGE, MAX_ACTIVATION_GAIN_INDEX_PERCENTAGE); 101 102 expectWithMessage("Car volume activation different configuration") 103 .that(testActivationConfig.equals(CAR_ACTIVATION_VOLUME_CONFIG)).isFalse(); 104 } 105 106 @Test hashCode_withSameConfiguration()107 public void hashCode_withSameConfiguration() { 108 CarActivationVolumeConfig testActivationConfig = 109 new CarActivationVolumeConfig(ACTIVATION_VOLUME_INVOCATION_TYPE, 110 MIN_ACTIVATION_GAIN_INDEX_PERCENTAGE, MAX_ACTIVATION_GAIN_INDEX_PERCENTAGE); 111 112 expectWithMessage("Hash result for same configuration") 113 .that(testActivationConfig.hashCode() 114 == CAR_ACTIVATION_VOLUME_CONFIG.hashCode()).isTrue(); 115 } 116 } 117