Home | History | Annotate | Download | only in bluetooth
      1 /*
      2  * Copyright 2018 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 android.test.suitebuilder.annotation.SmallTest;
     20 import android.util.Log;
     21 
     22 import junit.framework.TestCase;
     23 
     24 /**
     25  * Unit test cases for {@link BluetoothCodecConfig}.
     26  * <p>
     27  * To run this test, use:
     28  * runtest --path core/tests/bluetoothtests/src/android/bluetooth/BluetoothCodecConfigTest.java
     29  */
     30 public class BluetoothCodecConfigTest extends TestCase {
     31     private static final int[] kCodecTypeArray = new int[] {
     32         BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC,
     33         BluetoothCodecConfig.SOURCE_CODEC_TYPE_AAC,
     34         BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX,
     35         BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX_HD,
     36         BluetoothCodecConfig.SOURCE_CODEC_TYPE_LDAC,
     37         BluetoothCodecConfig.SOURCE_CODEC_TYPE_MAX,
     38         BluetoothCodecConfig.SOURCE_CODEC_TYPE_INVALID,
     39     };
     40     private static final int[] kCodecPriorityArray = new int[] {
     41         BluetoothCodecConfig.CODEC_PRIORITY_DISABLED,
     42         BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT,
     43         BluetoothCodecConfig.CODEC_PRIORITY_HIGHEST,
     44     };
     45     private static final int[] kSampleRateArray = new int[] {
     46         BluetoothCodecConfig.SAMPLE_RATE_NONE,
     47         BluetoothCodecConfig.SAMPLE_RATE_44100,
     48         BluetoothCodecConfig.SAMPLE_RATE_48000,
     49         BluetoothCodecConfig.SAMPLE_RATE_88200,
     50         BluetoothCodecConfig.SAMPLE_RATE_96000,
     51         BluetoothCodecConfig.SAMPLE_RATE_176400,
     52         BluetoothCodecConfig.SAMPLE_RATE_192000,
     53     };
     54     private static final int[] kBitsPerSampleArray = new int[] {
     55         BluetoothCodecConfig.BITS_PER_SAMPLE_NONE,
     56         BluetoothCodecConfig.BITS_PER_SAMPLE_16,
     57         BluetoothCodecConfig.BITS_PER_SAMPLE_24,
     58         BluetoothCodecConfig.BITS_PER_SAMPLE_32,
     59     };
     60     private static final int[] kChannelModeArray = new int[] {
     61         BluetoothCodecConfig.CHANNEL_MODE_NONE,
     62         BluetoothCodecConfig.CHANNEL_MODE_MONO,
     63         BluetoothCodecConfig.CHANNEL_MODE_STEREO,
     64     };
     65     private static final long[] kCodecSpecific1Array = new long[] { 1000, 1001, 1002, 1003, };
     66     private static final long[] kCodecSpecific2Array = new long[] { 2000, 2001, 2002, 2003, };
     67     private static final long[] kCodecSpecific3Array = new long[] { 3000, 3001, 3002, 3003, };
     68     private static final long[] kCodecSpecific4Array = new long[] { 4000, 4001, 4002, 4003, };
     69 
     70     private static final int kTotalConfigs = kCodecTypeArray.length * kCodecPriorityArray.length *
     71         kSampleRateArray.length * kBitsPerSampleArray.length * kChannelModeArray.length *
     72         kCodecSpecific1Array.length * kCodecSpecific2Array.length * kCodecSpecific3Array.length *
     73         kCodecSpecific4Array.length;
     74 
     75     private int selectCodecType(int configId) {
     76         int left = kCodecTypeArray.length;
     77         int right = kTotalConfigs / left;
     78         int index = configId / right;
     79         index = index % kCodecTypeArray.length;
     80         return kCodecTypeArray[index];
     81     }
     82 
     83     private int selectCodecPriority(int configId) {
     84         int left = kCodecTypeArray.length * kCodecPriorityArray.length;
     85         int right = kTotalConfigs / left;
     86         int index = configId / right;
     87         index = index % kCodecPriorityArray.length;
     88         return kCodecPriorityArray[index];
     89     }
     90 
     91     private int selectSampleRate(int configId) {
     92         int left = kCodecTypeArray.length * kCodecPriorityArray.length * kSampleRateArray.length;
     93         int right = kTotalConfigs / left;
     94         int index = configId / right;
     95         index = index % kSampleRateArray.length;
     96         return kSampleRateArray[index];
     97     }
     98 
     99     private int selectBitsPerSample(int configId) {
    100         int left = kCodecTypeArray.length * kCodecPriorityArray.length * kSampleRateArray.length *
    101             kBitsPerSampleArray.length;
    102         int right = kTotalConfigs / left;
    103         int index = configId / right;
    104         index = index % kBitsPerSampleArray.length;
    105         return kBitsPerSampleArray[index];
    106     }
    107 
    108     private int selectChannelMode(int configId) {
    109         int left = kCodecTypeArray.length * kCodecPriorityArray.length * kSampleRateArray.length *
    110             kBitsPerSampleArray.length * kChannelModeArray.length;
    111         int right = kTotalConfigs / left;
    112         int index = configId / right;
    113         index = index % kChannelModeArray.length;
    114         return kChannelModeArray[index];
    115     }
    116 
    117     private long selectCodecSpecific1(int configId) {
    118         int left = kCodecTypeArray.length * kCodecPriorityArray.length * kSampleRateArray.length *
    119             kBitsPerSampleArray.length * kChannelModeArray.length * kCodecSpecific1Array.length;
    120         int right = kTotalConfigs / left;
    121         int index = configId / right;
    122         index = index % kCodecSpecific1Array.length;
    123         return kCodecSpecific1Array[index];
    124     }
    125 
    126     private long selectCodecSpecific2(int configId) {
    127         int left = kCodecTypeArray.length * kCodecPriorityArray.length * kSampleRateArray.length *
    128             kBitsPerSampleArray.length * kChannelModeArray.length * kCodecSpecific1Array.length *
    129             kCodecSpecific2Array.length;
    130         int right = kTotalConfigs / left;
    131         int index = configId / right;
    132         index = index % kCodecSpecific2Array.length;
    133         return kCodecSpecific2Array[index];
    134     }
    135 
    136     private long selectCodecSpecific3(int configId) {
    137         int left = kCodecTypeArray.length * kCodecPriorityArray.length * kSampleRateArray.length *
    138             kBitsPerSampleArray.length * kChannelModeArray.length * kCodecSpecific1Array.length *
    139             kCodecSpecific2Array.length * kCodecSpecific3Array.length;
    140         int right = kTotalConfigs / left;
    141         int index = configId / right;
    142         index = index % kCodecSpecific3Array.length;
    143         return kCodecSpecific3Array[index];
    144     }
    145 
    146     private long selectCodecSpecific4(int configId) {
    147         int left = kCodecTypeArray.length * kCodecPriorityArray.length * kSampleRateArray.length *
    148             kBitsPerSampleArray.length * kChannelModeArray.length * kCodecSpecific1Array.length *
    149             kCodecSpecific2Array.length * kCodecSpecific3Array.length *
    150             kCodecSpecific4Array.length;
    151         int right = kTotalConfigs / left;
    152         int index = configId / right;
    153         index = index % kCodecSpecific4Array.length;
    154         return kCodecSpecific4Array[index];
    155     }
    156 
    157     @SmallTest
    158     public void testBluetoothCodecConfig_valid_get_methods() {
    159 
    160         for (int config_id = 0; config_id < kTotalConfigs; config_id++) {
    161             int codec_type = selectCodecType(config_id);
    162             int codec_priority = selectCodecPriority(config_id);
    163             int sample_rate = selectSampleRate(config_id);
    164             int bits_per_sample = selectBitsPerSample(config_id);
    165             int channel_mode = selectChannelMode(config_id);
    166             long codec_specific1 = selectCodecSpecific1(config_id);
    167             long codec_specific2 = selectCodecSpecific2(config_id);
    168             long codec_specific3 = selectCodecSpecific3(config_id);
    169             long codec_specific4 = selectCodecSpecific4(config_id);
    170 
    171             BluetoothCodecConfig bcc = new BluetoothCodecConfig(codec_type, codec_priority,
    172                                                                 sample_rate, bits_per_sample,
    173                                                                 channel_mode, codec_specific1,
    174                                                                 codec_specific2, codec_specific3,
    175                                                                 codec_specific4);
    176             if (sample_rate == BluetoothCodecConfig.SAMPLE_RATE_NONE) {
    177                 assertFalse(bcc.isValid());
    178             } else if (bits_per_sample == BluetoothCodecConfig.BITS_PER_SAMPLE_NONE) {
    179                 assertFalse(bcc.isValid());
    180             } else if (channel_mode == BluetoothCodecConfig.CHANNEL_MODE_NONE) {
    181                 assertFalse(bcc.isValid());
    182             } else {
    183                 assertTrue(bcc.isValid());
    184             }
    185 
    186             if (codec_type == BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC) {
    187                 assertTrue(bcc.isMandatoryCodec());
    188             } else {
    189                 assertFalse(bcc.isMandatoryCodec());
    190             }
    191 
    192             if (codec_type == BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC) {
    193                 assertEquals("SBC", bcc.getCodecName());
    194             }
    195             if (codec_type == BluetoothCodecConfig.SOURCE_CODEC_TYPE_AAC) {
    196                 assertEquals("AAC", bcc.getCodecName());
    197             }
    198             if (codec_type == BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX) {
    199                 assertEquals("aptX", bcc.getCodecName());
    200             }
    201             if (codec_type == BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX_HD) {
    202                 assertEquals("aptX HD", bcc.getCodecName());
    203             }
    204             if (codec_type == BluetoothCodecConfig.SOURCE_CODEC_TYPE_LDAC) {
    205                 assertEquals("LDAC", bcc.getCodecName());
    206             }
    207             if (codec_type == BluetoothCodecConfig.SOURCE_CODEC_TYPE_MAX) {
    208                 assertEquals("UNKNOWN CODEC(" + BluetoothCodecConfig.SOURCE_CODEC_TYPE_MAX + ")",
    209                              bcc.getCodecName());
    210             }
    211             if (codec_type == BluetoothCodecConfig.SOURCE_CODEC_TYPE_INVALID) {
    212                 assertEquals("INVALID CODEC", bcc.getCodecName());
    213             }
    214 
    215             assertEquals(codec_type, bcc.getCodecType());
    216             assertEquals(codec_priority, bcc.getCodecPriority());
    217             assertEquals(sample_rate, bcc.getSampleRate());
    218             assertEquals(bits_per_sample, bcc.getBitsPerSample());
    219             assertEquals(channel_mode, bcc.getChannelMode());
    220             assertEquals(codec_specific1, bcc.getCodecSpecific1());
    221             assertEquals(codec_specific2, bcc.getCodecSpecific2());
    222             assertEquals(codec_specific3, bcc.getCodecSpecific3());
    223             assertEquals(codec_specific4, bcc.getCodecSpecific4());
    224         }
    225     }
    226 
    227     @SmallTest
    228     public void testBluetoothCodecConfig_equals() {
    229         BluetoothCodecConfig bcc1 =
    230             new BluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC,
    231                                      BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT,
    232                                      BluetoothCodecConfig.SAMPLE_RATE_44100,
    233                                      BluetoothCodecConfig.BITS_PER_SAMPLE_16,
    234                                      BluetoothCodecConfig.CHANNEL_MODE_STEREO,
    235                                      1000, 2000, 3000, 4000);
    236 
    237         BluetoothCodecConfig bcc2_same =
    238             new BluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC,
    239                                      BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT,
    240                                      BluetoothCodecConfig.SAMPLE_RATE_44100,
    241                                      BluetoothCodecConfig.BITS_PER_SAMPLE_16,
    242                                      BluetoothCodecConfig.CHANNEL_MODE_STEREO,
    243                                      1000, 2000, 3000, 4000);
    244         assertTrue(bcc1.equals(bcc2_same));
    245 
    246         BluetoothCodecConfig bcc3_codec_type =
    247             new BluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_AAC,
    248                                      BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT,
    249                                      BluetoothCodecConfig.SAMPLE_RATE_44100,
    250                                      BluetoothCodecConfig.BITS_PER_SAMPLE_16,
    251                                      BluetoothCodecConfig.CHANNEL_MODE_STEREO,
    252                                      1000, 2000, 3000, 4000);
    253         assertFalse(bcc1.equals(bcc3_codec_type));
    254 
    255         BluetoothCodecConfig bcc4_codec_priority =
    256             new BluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC,
    257                                      BluetoothCodecConfig.CODEC_PRIORITY_HIGHEST,
    258                                      BluetoothCodecConfig.SAMPLE_RATE_44100,
    259                                      BluetoothCodecConfig.BITS_PER_SAMPLE_16,
    260                                      BluetoothCodecConfig.CHANNEL_MODE_STEREO,
    261                                      1000, 2000, 3000, 4000);
    262         assertFalse(bcc1.equals(bcc4_codec_priority));
    263 
    264         BluetoothCodecConfig bcc5_sample_rate =
    265             new BluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC,
    266                                      BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT,
    267                                      BluetoothCodecConfig.SAMPLE_RATE_48000,
    268                                      BluetoothCodecConfig.BITS_PER_SAMPLE_16,
    269                                      BluetoothCodecConfig.CHANNEL_MODE_STEREO,
    270                                      1000, 2000, 3000, 4000);
    271         assertFalse(bcc1.equals(bcc5_sample_rate));
    272 
    273         BluetoothCodecConfig bcc6_bits_per_sample =
    274             new BluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC,
    275                                      BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT,
    276                                      BluetoothCodecConfig.SAMPLE_RATE_44100,
    277                                      BluetoothCodecConfig.BITS_PER_SAMPLE_24,
    278                                      BluetoothCodecConfig.CHANNEL_MODE_STEREO,
    279                                      1000, 2000, 3000, 4000);
    280         assertFalse(bcc1.equals(bcc6_bits_per_sample));
    281 
    282         BluetoothCodecConfig bcc7_channel_mode =
    283             new BluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC,
    284                                      BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT,
    285                                      BluetoothCodecConfig.SAMPLE_RATE_44100,
    286                                      BluetoothCodecConfig.BITS_PER_SAMPLE_16,
    287                                      BluetoothCodecConfig.CHANNEL_MODE_MONO,
    288                                      1000, 2000, 3000, 4000);
    289         assertFalse(bcc1.equals(bcc7_channel_mode));
    290 
    291         BluetoothCodecConfig bcc8_codec_specific1 =
    292             new BluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC,
    293                                      BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT,
    294                                      BluetoothCodecConfig.SAMPLE_RATE_44100,
    295                                      BluetoothCodecConfig.BITS_PER_SAMPLE_16,
    296                                      BluetoothCodecConfig.CHANNEL_MODE_STEREO,
    297                                      1001, 2000, 3000, 4000);
    298         assertFalse(bcc1.equals(bcc8_codec_specific1));
    299 
    300         BluetoothCodecConfig bcc9_codec_specific2 =
    301             new BluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC,
    302                                      BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT,
    303                                      BluetoothCodecConfig.SAMPLE_RATE_44100,
    304                                      BluetoothCodecConfig.BITS_PER_SAMPLE_16,
    305                                      BluetoothCodecConfig.CHANNEL_MODE_STEREO,
    306                                      1000, 2002, 3000, 4000);
    307         assertFalse(bcc1.equals(bcc9_codec_specific2));
    308 
    309         BluetoothCodecConfig bcc10_codec_specific3 =
    310             new BluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC,
    311                                      BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT,
    312                                      BluetoothCodecConfig.SAMPLE_RATE_44100,
    313                                      BluetoothCodecConfig.BITS_PER_SAMPLE_16,
    314                                      BluetoothCodecConfig.CHANNEL_MODE_STEREO,
    315                                      1000, 2000, 3003, 4000);
    316         assertFalse(bcc1.equals(bcc10_codec_specific3));
    317 
    318         BluetoothCodecConfig bcc11_codec_specific4 =
    319             new BluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC,
    320                                      BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT,
    321                                      BluetoothCodecConfig.SAMPLE_RATE_44100,
    322                                      BluetoothCodecConfig.BITS_PER_SAMPLE_16,
    323                                      BluetoothCodecConfig.CHANNEL_MODE_STEREO,
    324                                      1000, 2000, 3000, 4004);
    325         assertFalse(bcc1.equals(bcc11_codec_specific4));
    326     }
    327 }
    328