1 /* 2 * Copyright (C) 2022 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.media.CarAudioManager.PRIMARY_AUDIO_ZONE; 20 import static android.media.AudioAttributes.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE; 21 import static android.media.AudioAttributes.USAGE_ASSISTANT; 22 import static android.media.AudioAttributes.USAGE_MEDIA; 23 24 import static org.junit.Assert.assertThrows; 25 26 import android.car.media.CarVolumeGroupInfo; 27 import android.car.test.AbstractExpectableTestCase; 28 import android.media.AudioAttributes; 29 import android.os.Parcel; 30 import android.telephony.TelephonyManager; 31 32 import org.junit.Test; 33 34 import java.util.List; 35 36 public final class OemCarAudioVolumeRequestUnitTest extends AbstractExpectableTestCase { 37 38 private static final int TEST_PRIMARY_GROUP_ID = 7; 39 private static final int TEST_SECONDARY_GROUP_ID = 9; 40 private static final String TEST_GROUP_NAME = "3"; 41 private static final int TEST_MAX_GAIN_INDEX = 9_000; 42 private static final int TEST_MIN_GAIN_INDEX = 0; 43 private static final int TEST_MAX_ACTIVATION_GAIN_INDEX = 8_000; 44 private static final int TEST_MIN_ACTIVATION_GAIN_INDEX = 1_000; 45 private static final int TEST_PARCEL_FLAGS = 0; 46 47 private static final CarVolumeGroupInfo TEST_VOLUME_INFO = 48 new CarVolumeGroupInfo.Builder(TEST_GROUP_NAME, PRIMARY_AUDIO_ZONE, 49 TEST_PRIMARY_GROUP_ID).setMaxVolumeGainIndex(TEST_MAX_GAIN_INDEX) 50 .setMinVolumeGainIndex(TEST_MIN_GAIN_INDEX) 51 .setMaxActivationVolumeGainIndex(TEST_MAX_ACTIVATION_GAIN_INDEX) 52 .setMinActivationVolumeGainIndex(TEST_MIN_ACTIVATION_GAIN_INDEX).build(); 53 private static final CarVolumeGroupInfo TEST_VOLUME_INFO_2 = 54 new CarVolumeGroupInfo.Builder(TEST_GROUP_NAME, PRIMARY_AUDIO_ZONE, 55 TEST_SECONDARY_GROUP_ID).setMaxVolumeGainIndex(TEST_MAX_GAIN_INDEX) 56 .setMinVolumeGainIndex(TEST_MIN_GAIN_INDEX) 57 .setMaxActivationVolumeGainIndex(TEST_MAX_ACTIVATION_GAIN_INDEX) 58 .setMinActivationVolumeGainIndex(TEST_MIN_ACTIVATION_GAIN_INDEX).build(); 59 60 private static final AudioAttributes TEST_MEDIA_ATTRIBUTE = 61 new AudioAttributes.Builder().setUsage(USAGE_MEDIA).build(); 62 private static final AudioAttributes TEST_NAVIGATION_ATTRIBUTE = 63 new AudioAttributes.Builder().setUsage(USAGE_ASSISTANCE_NAVIGATION_GUIDANCE).build(); 64 private static final AudioAttributes TEST_ASSISTANT_ATTRIBUTE = 65 new AudioAttributes.Builder().setUsage(USAGE_ASSISTANT).build(); 66 67 private static final OemCarAudioVolumeRequest TEST_VOLUME_REQUEST = 68 new OemCarAudioVolumeRequest.Builder(PRIMARY_AUDIO_ZONE) 69 .addActivePlaybackAttributes(TEST_MEDIA_ATTRIBUTE) 70 .addDuckedAudioAttributes(TEST_NAVIGATION_ATTRIBUTE) 71 .addCarVolumeGroupInfos(TEST_VOLUME_INFO).build(); 72 73 private final OemCarAudioVolumeRequest.Builder mTestVolumeRequestBuilder = 74 new OemCarAudioVolumeRequest.Builder(PRIMARY_AUDIO_ZONE); 75 76 @Test build()77 public void build() { 78 OemCarAudioVolumeRequest request = new OemCarAudioVolumeRequest.Builder( 79 PRIMARY_AUDIO_ZONE).build(); 80 81 expectWithMessage("Car volume request build info zone id") 82 .that(request.getAudioZoneId()).isEqualTo(PRIMARY_AUDIO_ZONE); 83 } 84 85 @Test setActivePlaybackAttributes()86 public void setActivePlaybackAttributes() { 87 OemCarAudioVolumeRequest request = mTestVolumeRequestBuilder 88 .setActivePlaybackAttributes(List.of(TEST_ASSISTANT_ATTRIBUTE)).build(); 89 90 expectWithMessage("Car volume request build active attributes") 91 .that(request.getActivePlaybackAttributes()) 92 .containsExactly(TEST_ASSISTANT_ATTRIBUTE); 93 } 94 95 @Test addActivePlaybackAttributes()96 public void addActivePlaybackAttributes() { 97 OemCarAudioVolumeRequest request = mTestVolumeRequestBuilder 98 .addActivePlaybackAttributes(TEST_MEDIA_ATTRIBUTE).build(); 99 100 expectWithMessage("Car volume request build active attribute") 101 .that(request.getActivePlaybackAttributes()).containsExactly(TEST_MEDIA_ATTRIBUTE); 102 } 103 104 @Test setDuckedAudioAttributes()105 public void setDuckedAudioAttributes() { 106 OemCarAudioVolumeRequest request = mTestVolumeRequestBuilder 107 .setDuckedAudioAttributes(List.of(TEST_NAVIGATION_ATTRIBUTE)).build(); 108 109 expectWithMessage("Car volume request build ducked attributes") 110 .that(request.getDuckedAudioAttributes()) 111 .containsExactly(TEST_NAVIGATION_ATTRIBUTE); 112 } 113 114 @Test addDuckedAudioAttributes()115 public void addDuckedAudioAttributes() { 116 OemCarAudioVolumeRequest request = mTestVolumeRequestBuilder 117 .addDuckedAudioAttributes(TEST_MEDIA_ATTRIBUTE).build(); 118 119 expectWithMessage("Car volume request build ducked attribute") 120 .that(request.getDuckedAudioAttributes()).containsExactly(TEST_MEDIA_ATTRIBUTE); 121 } 122 123 @Test setCarVolumeGroupInfos()124 public void setCarVolumeGroupInfos() { 125 OemCarAudioVolumeRequest request = mTestVolumeRequestBuilder 126 .setCarVolumeGroupInfos(List.of(TEST_VOLUME_INFO)).build(); 127 128 expectWithMessage("Car volume request build volumes infos") 129 .that(request.getCarVolumeGroupInfos()).containsExactly(TEST_VOLUME_INFO); 130 } 131 132 @Test addCarVolumeGroupInfos()133 public void addCarVolumeGroupInfos() { 134 OemCarAudioVolumeRequest request = mTestVolumeRequestBuilder 135 .addCarVolumeGroupInfos(TEST_VOLUME_INFO_2).build(); 136 137 expectWithMessage("Car volume request build volume info") 138 .that(request.getCarVolumeGroupInfos()).containsExactly(TEST_VOLUME_INFO_2); 139 } 140 141 @Test setCallState()142 public void setCallState() { 143 OemCarAudioVolumeRequest request = mTestVolumeRequestBuilder 144 .setCallState(TelephonyManager.CALL_STATE_OFFHOOK).build(); 145 146 expectWithMessage("Car volume request build call state") 147 .that(request.getCallState()).isEqualTo(TelephonyManager.CALL_STATE_OFFHOOK); 148 } 149 150 @Test builder_withReuse_fails()151 public void builder_withReuse_fails() { 152 mTestVolumeRequestBuilder.build(); 153 154 IllegalStateException thrown = assertThrows(IllegalStateException.class, () -> 155 mTestVolumeRequestBuilder.build() 156 ); 157 158 expectWithMessage("Reuse request builder exception") 159 .that(thrown).hasMessageThat().contains("should not be reused"); 160 } 161 162 @Test writeToParcel()163 public void writeToParcel() { 164 Parcel parcel = Parcel.obtain(); 165 166 TEST_VOLUME_REQUEST.writeToParcel(parcel, TEST_PARCEL_FLAGS); 167 parcel.setDataPosition(/* pos= */ 0); 168 169 expectWithMessage("Car volume request from parcel") 170 .that(OemCarAudioVolumeRequest.CREATOR.createFromParcel(parcel)) 171 .isEqualTo(TEST_VOLUME_REQUEST); 172 } 173 174 @Test newArray()175 public void newArray() { 176 OemCarAudioVolumeRequest[] requests = 177 OemCarAudioVolumeRequest.CREATOR.newArray(/* size= */ 7); 178 179 expectWithMessage("Car volume request size").that(requests).hasLength(7); 180 } 181 182 @Test equals_forSameContent()183 public void equals_forSameContent() { 184 OemCarAudioVolumeRequest request = 185 new OemCarAudioVolumeRequest.Builder(TEST_VOLUME_REQUEST).build(); 186 187 expectWithMessage("Car volume request with same content") 188 .that(request).isEqualTo(TEST_VOLUME_REQUEST); 189 } 190 191 @Test equals_forNull()192 public void equals_forNull() { 193 OemCarAudioVolumeRequest request = 194 new OemCarAudioVolumeRequest.Builder(TEST_VOLUME_REQUEST).build(); 195 196 expectWithMessage("Car volume request null content") 197 .that(request.equals(null)).isFalse(); 198 } 199 200 @Test hashCode_forSameContent()201 public void hashCode_forSameContent() { 202 OemCarAudioVolumeRequest request = 203 new OemCarAudioVolumeRequest.Builder(TEST_VOLUME_REQUEST).build(); 204 205 expectWithMessage("Car volume request hash with same content") 206 .that(request.hashCode()).isEqualTo(TEST_VOLUME_REQUEST.hashCode()); 207 } 208 209 @Test toString_forContent()210 public void toString_forContent() { 211 OemCarAudioVolumeRequest request = 212 new OemCarAudioVolumeRequest.Builder(TEST_VOLUME_REQUEST).build(); 213 214 expectWithMessage("Car volume request zone id") 215 .that(request.toString()).contains(Integer.toString(PRIMARY_AUDIO_ZONE)); 216 } 217 218 @Test describeContents()219 public void describeContents() { 220 OemCarAudioVolumeRequest request = 221 new OemCarAudioVolumeRequest.Builder(TEST_VOLUME_REQUEST).build(); 222 223 expectWithMessage("Car volume request contents") 224 .that(request.describeContents()).isEqualTo(/* expected= */ 0); 225 } 226 } 227