• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
31  * Test cases for {@link BluetoothAudioConfig}.
32  */
33 @SmallTest
34 @RunWith(AndroidJUnit4.class)
35 public class BluetoothAudioConfigTest {
36 
37     private static final int TEST_SAMPLE_RATE = 44;
38     private static final int TEST_CHANNEL_COUNT = 1;
39 
40     @Test
createBluetoothAudioConfig()41     public void createBluetoothAudioConfig() {
42         BluetoothAudioConfig audioConfig = new BluetoothAudioConfig(
43                 TEST_SAMPLE_RATE,
44                 TEST_CHANNEL_COUNT,
45                 AudioFormat.ENCODING_PCM_16BIT
46         );
47 
48         assertThat(audioConfig.getSampleRate()).isEqualTo(TEST_SAMPLE_RATE);
49         assertThat(audioConfig.getChannelConfig()).isEqualTo(TEST_CHANNEL_COUNT);
50         assertThat(audioConfig.getAudioFormat()).isEqualTo(AudioFormat.ENCODING_PCM_16BIT);
51     }
52 
53     @Test
writeToParcel()54     public void writeToParcel() {
55         BluetoothAudioConfig originalConfig = new BluetoothAudioConfig(
56                 TEST_SAMPLE_RATE,
57                 TEST_CHANNEL_COUNT,
58                 AudioFormat.ENCODING_PCM_16BIT
59         );
60 
61         Parcel parcel = Parcel.obtain();
62         originalConfig.writeToParcel(parcel, 0);
63         parcel.setDataPosition(0);
64 
65         BluetoothAudioConfig configOut = BluetoothAudioConfig.CREATOR.createFromParcel(parcel);
66         parcel.recycle();
67 
68         assertThat(configOut.getSampleRate())
69                 .isEqualTo(originalConfig.getSampleRate());
70         assertThat(configOut.getChannelConfig())
71                 .isEqualTo(originalConfig.getChannelConfig());
72         assertThat(configOut.getAudioFormat())
73                 .isEqualTo(originalConfig.getAudioFormat());
74     }
75 
76     @Test
bluetoothAudioConfigHashCode()77     public void bluetoothAudioConfigHashCode() {
78         BluetoothAudioConfig audioConfig = new BluetoothAudioConfig(
79                 TEST_SAMPLE_RATE,
80                 TEST_CHANNEL_COUNT,
81                 AudioFormat.ENCODING_PCM_16BIT
82         );
83 
84         int hashCode = audioConfig.getSampleRate() | (audioConfig.getChannelConfig() << 24) | (
85                 audioConfig.getAudioFormat() << 28);
86         int describeContents = 0;
87 
88         assertThat(audioConfig.hashCode()).isEqualTo(hashCode);
89         assertThat(audioConfig.describeContents()).isEqualTo(describeContents);
90     }
91 
92     @Test
bluetoothAudioConfigToString()93     public void bluetoothAudioConfigToString() {
94         BluetoothAudioConfig audioConfig = new BluetoothAudioConfig(
95                 TEST_SAMPLE_RATE,
96                 TEST_CHANNEL_COUNT,
97                 AudioFormat.ENCODING_PCM_16BIT
98         );
99 
100         String audioConfigString = audioConfig.toString();
101         String expectedToString = "{mSampleRate:" + audioConfig.getSampleRate()
102                 + ",mChannelConfig:" + audioConfig.getChannelConfig()
103                 + ",mAudioFormat:" + audioConfig.getAudioFormat() + "}";
104 
105         assertThat(audioConfigString).isEqualTo(expectedToString);
106     }
107 }
108