summaryrefslogtreecommitdiff
path: root/framework/tests/src/android/bluetooth/BluetoothCodecConfigTest.java
blob: 53623b809c88796e6d428cf5a0dc1fb7389ca57b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.bluetooth;

import android.test.suitebuilder.annotation.SmallTest;

import junit.framework.TestCase;

/**
 * Unit test cases for {@link BluetoothCodecConfig}.
 * <p>
 * To run this test, use:
 * runtest --path core/tests/bluetoothtests/src/android/bluetooth/BluetoothCodecConfigTest.java
 */
public class BluetoothCodecConfigTest extends TestCase {
  private static final int[] kCodecTypeArray = new int[] {
      BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC,
      BluetoothCodecConfig.SOURCE_CODEC_TYPE_AAC,
      BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX,
      BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX_HD,
      BluetoothCodecConfig.SOURCE_CODEC_TYPE_LDAC,
      BluetoothCodecConfig.SOURCE_CODEC_TYPE_LC3,
      BluetoothCodecConfig.SOURCE_CODEC_TYPE_INVALID,
  };
  private static final int[] kCodecPriorityArray = new int[] {
      BluetoothCodecConfig.CODEC_PRIORITY_DISABLED,
      BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT,
      BluetoothCodecConfig.CODEC_PRIORITY_HIGHEST,
  };
  private static final int[] kSampleRateArray = new int[] {
      BluetoothCodecConfig.SAMPLE_RATE_NONE,
      BluetoothCodecConfig.SAMPLE_RATE_44100,
      BluetoothCodecConfig.SAMPLE_RATE_48000,
      BluetoothCodecConfig.SAMPLE_RATE_88200,
      BluetoothCodecConfig.SAMPLE_RATE_96000,
      BluetoothCodecConfig.SAMPLE_RATE_176400,
      BluetoothCodecConfig.SAMPLE_RATE_192000,
  };
  private static final int[] kBitsPerSampleArray = new int[] {
      BluetoothCodecConfig.BITS_PER_SAMPLE_NONE,
      BluetoothCodecConfig.BITS_PER_SAMPLE_16,
      BluetoothCodecConfig.BITS_PER_SAMPLE_24,
      BluetoothCodecConfig.BITS_PER_SAMPLE_32,
  };
  private static final int[] kChannelModeArray = new int[] {
      BluetoothCodecConfig.CHANNEL_MODE_NONE,
      BluetoothCodecConfig.CHANNEL_MODE_MONO,
      BluetoothCodecConfig.CHANNEL_MODE_STEREO,
  };
  private static final long[] kCodecSpecific1Array = new long[] {
      1000,
      1001,
      1002,
      1003,
  };
  private static final long[] kCodecSpecific2Array = new long[] {
      2000,
      2001,
      2002,
      2003,
  };
  private static final long[] kCodecSpecific3Array = new long[] {
      3000,
      3001,
      3002,
      3003,
  };
  private static final long[] kCodecSpecific4Array = new long[] {
      4000,
      4001,
      4002,
      4003,
  };

  private static final int kTotalConfigs = kCodecTypeArray.length * kCodecPriorityArray.length
      * kSampleRateArray.length * kBitsPerSampleArray.length * kChannelModeArray.length
      * kCodecSpecific1Array.length * kCodecSpecific2Array.length * kCodecSpecific3Array.length
      * kCodecSpecific4Array.length;

  private int selectCodecType(int configId) {
    int left = kCodecTypeArray.length;
    int right = kTotalConfigs / left;
    int index = configId / right;
    index = index % kCodecTypeArray.length;
    return kCodecTypeArray[index];
  }

    private int selectCodecPriority(int configId) {
        int left = kCodecTypeArray.length * kCodecPriorityArray.length;
        int right = kTotalConfigs / left;
        int index = configId / right;
        index = index % kCodecPriorityArray.length;
        return kCodecPriorityArray[index];
    }

    private int selectSampleRate(int configId) {
        int left = kCodecTypeArray.length * kCodecPriorityArray.length * kSampleRateArray.length;
        int right = kTotalConfigs / left;
        int index = configId / right;
        index = index % kSampleRateArray.length;
        return kSampleRateArray[index];
    }

    private int selectBitsPerSample(int configId) {
        int left = kCodecTypeArray.length * kCodecPriorityArray.length * kSampleRateArray.length *
            kBitsPerSampleArray.length;
        int right = kTotalConfigs / left;
        int index = configId / right;
        index = index % kBitsPerSampleArray.length;
        return kBitsPerSampleArray[index];
    }

    private int selectChannelMode(int configId) {
        int left = kCodecTypeArray.length * kCodecPriorityArray.length * kSampleRateArray.length *
            kBitsPerSampleArray.length * kChannelModeArray.length;
        int right = kTotalConfigs / left;
        int index = configId / right;
        index = index % kChannelModeArray.length;
        return kChannelModeArray[index];
    }

    private long selectCodecSpecific1(int configId) {
        int left = kCodecTypeArray.length * kCodecPriorityArray.length * kSampleRateArray.length *
            kBitsPerSampleArray.length * kChannelModeArray.length * kCodecSpecific1Array.length;
        int right = kTotalConfigs / left;
        int index = configId / right;
        index = index % kCodecSpecific1Array.length;
        return kCodecSpecific1Array[index];
    }

    private long selectCodecSpecific2(int configId) {
        int left = kCodecTypeArray.length * kCodecPriorityArray.length * kSampleRateArray.length *
            kBitsPerSampleArray.length * kChannelModeArray.length * kCodecSpecific1Array.length *
            kCodecSpecific2Array.length;
        int right = kTotalConfigs / left;
        int index = configId / right;
        index = index % kCodecSpecific2Array.length;
        return kCodecSpecific2Array[index];
    }

    private long selectCodecSpecific3(int configId) {
        int left = kCodecTypeArray.length * kCodecPriorityArray.length * kSampleRateArray.length *
            kBitsPerSampleArray.length * kChannelModeArray.length * kCodecSpecific1Array.length *
            kCodecSpecific2Array.length * kCodecSpecific3Array.length;
        int right = kTotalConfigs / left;
        int index = configId / right;
        index = index % kCodecSpecific3Array.length;
        return kCodecSpecific3Array[index];
    }

    private long selectCodecSpecific4(int configId) {
        int left = kCodecTypeArray.length * kCodecPriorityArray.length * kSampleRateArray.length *
            kBitsPerSampleArray.length * kChannelModeArray.length * kCodecSpecific1Array.length *
            kCodecSpecific2Array.length * kCodecSpecific3Array.length *
            kCodecSpecific4Array.length;
        int right = kTotalConfigs / left;
        int index = configId / right;
        index = index % kCodecSpecific4Array.length;
        return kCodecSpecific4Array[index];
    }

    @SmallTest
    public void testBluetoothCodecConfig_valid_get_methods() {

        for (int config_id = 0; config_id < kTotalConfigs; config_id++) {
            int codec_type = selectCodecType(config_id);
            int codec_priority = selectCodecPriority(config_id);
            int sample_rate = selectSampleRate(config_id);
            int bits_per_sample = selectBitsPerSample(config_id);
            int channel_mode = selectChannelMode(config_id);
            long codec_specific1 = selectCodecSpecific1(config_id);
            long codec_specific2 = selectCodecSpecific2(config_id);
            long codec_specific3 = selectCodecSpecific3(config_id);
            long codec_specific4 = selectCodecSpecific4(config_id);

            BluetoothCodecConfig bcc = buildBluetoothCodecConfig(codec_type, codec_priority,
                                                                sample_rate, bits_per_sample,
                                                                channel_mode, codec_specific1,
                                                                codec_specific2, codec_specific3,
                                                                codec_specific4);

            if (codec_type == BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC) {
                assertTrue(bcc.isMandatoryCodec());
            } else {
                assertFalse(bcc.isMandatoryCodec());
            }

            if (codec_type == BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC) {
                assertEquals("SBC", BluetoothCodecConfig.getCodecName(codec_type));
            }
            if (codec_type == BluetoothCodecConfig.SOURCE_CODEC_TYPE_AAC) {
                assertEquals("AAC", BluetoothCodecConfig.getCodecName(codec_type));
            }
            if (codec_type == BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX) {
                assertEquals("aptX", BluetoothCodecConfig.getCodecName(codec_type));
            }
            if (codec_type == BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX_HD) {
                assertEquals("aptX HD", BluetoothCodecConfig.getCodecName(codec_type));
            }
            if (codec_type == BluetoothCodecConfig.SOURCE_CODEC_TYPE_LDAC) {
                assertEquals("LDAC", BluetoothCodecConfig.getCodecName(codec_type));
            }
            if (codec_type == BluetoothCodecConfig.SOURCE_CODEC_TYPE_LC3) {
                assertEquals("LC3", BluetoothCodecConfig.getCodecName(codec_type));
            }
            if (codec_type == BluetoothCodecConfig.SOURCE_CODEC_TYPE_INVALID) {
                assertEquals("INVALID CODEC", BluetoothCodecConfig.getCodecName(codec_type));
            }

            assertEquals(codec_type, bcc.getCodecType());
            assertEquals(codec_priority, bcc.getCodecPriority());
            assertEquals(sample_rate, bcc.getSampleRate());
            assertEquals(bits_per_sample, bcc.getBitsPerSample());
            assertEquals(channel_mode, bcc.getChannelMode());
            assertEquals(codec_specific1, bcc.getCodecSpecific1());
            assertEquals(codec_specific2, bcc.getCodecSpecific2());
            assertEquals(codec_specific3, bcc.getCodecSpecific3());
            assertEquals(codec_specific4, bcc.getCodecSpecific4());
        }
    }

    @SmallTest
    public void testBluetoothCodecConfig_equals() {
        BluetoothCodecConfig bcc1 =
                buildBluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC,
                                     BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT,
                                     BluetoothCodecConfig.SAMPLE_RATE_44100,
                                     BluetoothCodecConfig.BITS_PER_SAMPLE_16,
                                     BluetoothCodecConfig.CHANNEL_MODE_STEREO,
                                     1000, 2000, 3000, 4000);

        BluetoothCodecConfig bcc2_same =
                buildBluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC,
                                     BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT,
                                     BluetoothCodecConfig.SAMPLE_RATE_44100,
                                     BluetoothCodecConfig.BITS_PER_SAMPLE_16,
                                     BluetoothCodecConfig.CHANNEL_MODE_STEREO,
                                     1000, 2000, 3000, 4000);
        assertTrue(bcc1.equals(bcc2_same));

        BluetoothCodecConfig bcc3_codec_type =
                buildBluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_AAC,
                                     BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT,
                                     BluetoothCodecConfig.SAMPLE_RATE_44100,
                                     BluetoothCodecConfig.BITS_PER_SAMPLE_16,
                                     BluetoothCodecConfig.CHANNEL_MODE_STEREO,
                                     1000, 2000, 3000, 4000);
        assertFalse(bcc1.equals(bcc3_codec_type));

        BluetoothCodecConfig bcc4_codec_priority =
                buildBluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC,
                                     BluetoothCodecConfig.CODEC_PRIORITY_HIGHEST,
                                     BluetoothCodecConfig.SAMPLE_RATE_44100,
                                     BluetoothCodecConfig.BITS_PER_SAMPLE_16,
                                     BluetoothCodecConfig.CHANNEL_MODE_STEREO,
                                     1000, 2000, 3000, 4000);
        assertFalse(bcc1.equals(bcc4_codec_priority));

        BluetoothCodecConfig bcc5_sample_rate =
                buildBluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC,
                                     BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT,
                                     BluetoothCodecConfig.SAMPLE_RATE_48000,
                                     BluetoothCodecConfig.BITS_PER_SAMPLE_16,
                                     BluetoothCodecConfig.CHANNEL_MODE_STEREO,
                                     1000, 2000, 3000, 4000);
        assertFalse(bcc1.equals(bcc5_sample_rate));

        BluetoothCodecConfig bcc6_bits_per_sample =
                buildBluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC,
                                     BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT,
                                     BluetoothCodecConfig.SAMPLE_RATE_44100,
                                     BluetoothCodecConfig.BITS_PER_SAMPLE_24,
                                     BluetoothCodecConfig.CHANNEL_MODE_STEREO,
                                     1000, 2000, 3000, 4000);
        assertFalse(bcc1.equals(bcc6_bits_per_sample));

        BluetoothCodecConfig bcc7_channel_mode =
                buildBluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC,
                                     BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT,
                                     BluetoothCodecConfig.SAMPLE_RATE_44100,
                                     BluetoothCodecConfig.BITS_PER_SAMPLE_16,
                                     BluetoothCodecConfig.CHANNEL_MODE_MONO,
                                     1000, 2000, 3000, 4000);
        assertFalse(bcc1.equals(bcc7_channel_mode));

        BluetoothCodecConfig bcc8_codec_specific1 =
                buildBluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC,
                                     BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT,
                                     BluetoothCodecConfig.SAMPLE_RATE_44100,
                                     BluetoothCodecConfig.BITS_PER_SAMPLE_16,
                                     BluetoothCodecConfig.CHANNEL_MODE_STEREO,
                                     1001, 2000, 3000, 4000);
        assertFalse(bcc1.equals(bcc8_codec_specific1));

        BluetoothCodecConfig bcc9_codec_specific2 =
                buildBluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC,
                                     BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT,
                                     BluetoothCodecConfig.SAMPLE_RATE_44100,
                                     BluetoothCodecConfig.BITS_PER_SAMPLE_16,
                                     BluetoothCodecConfig.CHANNEL_MODE_STEREO,
                                     1000, 2002, 3000, 4000);
        assertFalse(bcc1.equals(bcc9_codec_specific2));

        BluetoothCodecConfig bcc10_codec_specific3 =
                buildBluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC,
                                     BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT,
                                     BluetoothCodecConfig.SAMPLE_RATE_44100,
                                     BluetoothCodecConfig.BITS_PER_SAMPLE_16,
                                     BluetoothCodecConfig.CHANNEL_MODE_STEREO,
                                     1000, 2000, 3003, 4000);
        assertFalse(bcc1.equals(bcc10_codec_specific3));

        BluetoothCodecConfig bcc11_codec_specific4 =
                buildBluetoothCodecConfig(BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC,
                                     BluetoothCodecConfig.CODEC_PRIORITY_DEFAULT,
                                     BluetoothCodecConfig.SAMPLE_RATE_44100,
                                     BluetoothCodecConfig.BITS_PER_SAMPLE_16,
                                     BluetoothCodecConfig.CHANNEL_MODE_STEREO,
                                     1000, 2000, 3000, 4004);
        assertFalse(bcc1.equals(bcc11_codec_specific4));
    }

    private BluetoothCodecConfig buildBluetoothCodecConfig(int sourceCodecType,
            int codecPriority, int sampleRate, int bitsPerSample, int channelMode,
            long codecSpecific1, long codecSpecific2, long codecSpecific3, long codecSpecific4) {
        return new BluetoothCodecConfig.Builder()
                    .setCodecType(sourceCodecType)
                    .setCodecPriority(codecPriority)
                    .setSampleRate(sampleRate)
                    .setBitsPerSample(bitsPerSample)
                    .setChannelMode(channelMode)
                    .setCodecSpecific1(codecSpecific1)
                    .setCodecSpecific2(codecSpecific2)
                    .setCodecSpecific3(codecSpecific3)
                    .setCodecSpecific4(codecSpecific4)
                    .build();

    }
}