1 /*
2  * Copyright 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.bluetooth;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.media.AudioFormat;
22 import android.os.Parcel;
23 
24 import androidx.test.filters.SmallTest;
25 import androidx.test.runner.AndroidJUnit4;
26 
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 
30 /** Test cases for {@link BluetoothAudioConfig}. */
31 @SmallTest
32 @RunWith(AndroidJUnit4.class)
33 public class BluetoothAudioConfigTest {
34 
35     private static final int TEST_SAMPLE_RATE = 44;
36     private static final int TEST_CHANNEL_COUNT = 1;
37 
38     @Test
createBluetoothAudioConfig()39     public void createBluetoothAudioConfig() {
40         BluetoothAudioConfig audioConfig =
41                 new BluetoothAudioConfig(
42                         TEST_SAMPLE_RATE, TEST_CHANNEL_COUNT, AudioFormat.ENCODING_PCM_16BIT);
43 
44         assertThat(audioConfig.getSampleRate()).isEqualTo(TEST_SAMPLE_RATE);
45         assertThat(audioConfig.getChannelConfig()).isEqualTo(TEST_CHANNEL_COUNT);
46         assertThat(audioConfig.getAudioFormat()).isEqualTo(AudioFormat.ENCODING_PCM_16BIT);
47     }
48 
49     @Test
writeToParcel()50     public void writeToParcel() {
51         BluetoothAudioConfig originalConfig =
52                 new BluetoothAudioConfig(
53                         TEST_SAMPLE_RATE, TEST_CHANNEL_COUNT, AudioFormat.ENCODING_PCM_16BIT);
54 
55         Parcel parcel = Parcel.obtain();
56         originalConfig.writeToParcel(parcel, 0);
57         parcel.setDataPosition(0);
58 
59         BluetoothAudioConfig configOut = BluetoothAudioConfig.CREATOR.createFromParcel(parcel);
60         parcel.recycle();
61 
62         assertThat(configOut.getSampleRate()).isEqualTo(originalConfig.getSampleRate());
63         assertThat(configOut.getChannelConfig()).isEqualTo(originalConfig.getChannelConfig());
64         assertThat(configOut.getAudioFormat()).isEqualTo(originalConfig.getAudioFormat());
65     }
66 
67     @Test
bluetoothAudioConfigHashCode()68     public void bluetoothAudioConfigHashCode() {
69         BluetoothAudioConfig audioConfig =
70                 new BluetoothAudioConfig(
71                         TEST_SAMPLE_RATE, TEST_CHANNEL_COUNT, AudioFormat.ENCODING_PCM_16BIT);
72 
73         int hashCode =
74                 audioConfig.getSampleRate()
75                         | (audioConfig.getChannelConfig() << 24)
76                         | (audioConfig.getAudioFormat() << 28);
77         int describeContents = 0;
78 
79         assertThat(audioConfig.hashCode()).isEqualTo(hashCode);
80         assertThat(audioConfig.describeContents()).isEqualTo(describeContents);
81     }
82 
83     @Test
bluetoothAudioConfigToString()84     public void bluetoothAudioConfigToString() {
85         BluetoothAudioConfig audioConfig =
86                 new BluetoothAudioConfig(
87                         TEST_SAMPLE_RATE, TEST_CHANNEL_COUNT, AudioFormat.ENCODING_PCM_16BIT);
88 
89         String audioConfigString = audioConfig.toString();
90         String expectedToString =
91                 "{mSampleRate:"
92                         + audioConfig.getSampleRate()
93                         + ",mChannelConfig:"
94                         + audioConfig.getChannelConfig()
95                         + ",mAudioFormat:"
96                         + audioConfig.getAudioFormat()
97                         + "}";
98 
99         assertThat(audioConfigString).isEqualTo(expectedToString);
100     }
101 }
102