summaryrefslogtreecommitdiff
path: root/packages/SettingsLib/tests/unit/src/com/android/settingslib/bluetooth/BluetoothLeBroadcastMetadataExtTest.kt
blob: 27d7078774d5a99489490a2d6729498c784bab49 (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
/*
 * Copyright (C) 2023 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 com.android.settingslib.bluetooth

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothLeAudioCodecConfigMetadata
import android.bluetooth.BluetoothLeAudioContentMetadata
import android.bluetooth.BluetoothLeBroadcastChannel
import android.bluetooth.BluetoothLeBroadcastMetadata
import android.bluetooth.BluetoothLeBroadcastSubgroup
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settingslib.bluetooth.BluetoothLeBroadcastMetadataExt.toQrCodeString
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class BluetoothLeBroadcastMetadataExtTest {

    @Test
    fun toQrCodeString() {
        val subgroup = BluetoothLeBroadcastSubgroup.Builder().apply {
            setCodecId(0x6)
            val audioCodecConfigMetadata = BluetoothLeAudioCodecConfigMetadata.Builder().build()
            setCodecSpecificConfig(audioCodecConfigMetadata)
            setContentMetadata(BluetoothLeAudioContentMetadata.Builder()
                    .setProgramInfo("Test").setLanguage("eng").build())
            addChannel(BluetoothLeBroadcastChannel.Builder().apply {
                setSelected(true)
                setChannelIndex(2)
                setCodecMetadata(audioCodecConfigMetadata)
            }.build())
            addChannel(BluetoothLeBroadcastChannel.Builder().apply {
                setSelected(true)
                setChannelIndex(1)
                setCodecMetadata(audioCodecConfigMetadata)
            }.build())
        }.build()

        val metadata = BluetoothLeBroadcastMetadata.Builder().apply {
            setSourceDevice(Device, BluetoothDevice.ADDRESS_TYPE_RANDOM)
            setSourceAdvertisingSid(1)
            setBroadcastId(123456)
            setBroadcastName("Test")
            setPublicBroadcastMetadata(BluetoothLeAudioContentMetadata.Builder()
                    .setProgramInfo("pTest").build())
            setPaSyncInterval(160)
            setEncrypted(true)
            setBroadcastCode("TestCode".toByteArray(Charsets.UTF_8))
            addSubgroup(subgroup)
        }.build()

        val qrCodeString = metadata.toQrCodeString()

        assertThat(qrCodeString).isEqualTo(QR_CODE_STRING)
    }

    @Test
    fun toQrCodeString_NoChannelSelected() {
        val subgroup = BluetoothLeBroadcastSubgroup.Builder().apply {
            setCodecId(0x6)
            val audioCodecConfigMetadata = BluetoothLeAudioCodecConfigMetadata.Builder().build()
            setCodecSpecificConfig(audioCodecConfigMetadata)
            setContentMetadata(BluetoothLeAudioContentMetadata.Builder()
                .setProgramInfo("Test").setLanguage("eng").build())
            addChannel(BluetoothLeBroadcastChannel.Builder().apply {
                setSelected(false)
                setChannelIndex(2)
                setCodecMetadata(audioCodecConfigMetadata)
            }.build())
            addChannel(BluetoothLeBroadcastChannel.Builder().apply {
                setSelected(false)
                setChannelIndex(1)
                setCodecMetadata(audioCodecConfigMetadata)
            }.build())
        }.build()

        val metadata = BluetoothLeBroadcastMetadata.Builder().apply {
            setSourceDevice(Device, BluetoothDevice.ADDRESS_TYPE_RANDOM)
            setSourceAdvertisingSid(1)
            setBroadcastId(123456)
            setBroadcastName("Test")
            setPublicBroadcastMetadata(BluetoothLeAudioContentMetadata.Builder()
                .setProgramInfo("pTest").build())
            setPaSyncInterval(160)
            setEncrypted(true)
            setBroadcastCode("TestCode".toByteArray(Charsets.UTF_8))
            addSubgroup(subgroup)
        }.build()

        val qrCodeString = metadata.toQrCodeString()

        val parsedMetadata =
            BluetoothLeBroadcastMetadataExt.convertToBroadcastMetadata(qrCodeString)!!

        assertThat(parsedMetadata).isNotNull()
        assertThat(parsedMetadata.subgroups).isNotNull()
        assertThat(parsedMetadata.subgroups.size).isEqualTo(1)
        assertThat(parsedMetadata.subgroups[0].channels).isNotNull()
        assertThat(parsedMetadata.subgroups[0].channels.size).isEqualTo(2)
        assertThat(parsedMetadata.subgroups[0].hasChannelPreference()).isFalse()
        // Input order does not matter due to parsing through bisMask
        assertThat(parsedMetadata.subgroups[0].channels[0].channelIndex).isEqualTo(1)
        assertThat(parsedMetadata.subgroups[0].channels[0].isSelected).isFalse()
        assertThat(parsedMetadata.subgroups[0].channels[1].channelIndex).isEqualTo(2)
        assertThat(parsedMetadata.subgroups[0].channels[1].isSelected).isFalse()
    }

    @Test
    fun toQrCodeString_OneChannelSelected() {
        val subgroup = BluetoothLeBroadcastSubgroup.Builder().apply {
            setCodecId(0x6)
            val audioCodecConfigMetadata = BluetoothLeAudioCodecConfigMetadata.Builder().build()
            setCodecSpecificConfig(audioCodecConfigMetadata)
            setContentMetadata(BluetoothLeAudioContentMetadata.Builder()
                .setProgramInfo("Test").setLanguage("eng").build())
            addChannel(BluetoothLeBroadcastChannel.Builder().apply {
                setSelected(false)
                setChannelIndex(1)
                setCodecMetadata(audioCodecConfigMetadata)
            }.build())
            addChannel(BluetoothLeBroadcastChannel.Builder().apply {
                setSelected(true)
                setChannelIndex(2)
                setCodecMetadata(audioCodecConfigMetadata)
            }.build())
        }.build()

        val metadata = BluetoothLeBroadcastMetadata.Builder().apply {
            setSourceDevice(Device, BluetoothDevice.ADDRESS_TYPE_RANDOM)
            setSourceAdvertisingSid(1)
            setBroadcastId(123456)
            setBroadcastName("Test")
            setPublicBroadcastMetadata(BluetoothLeAudioContentMetadata.Builder()
                .setProgramInfo("pTest").build())
            setPaSyncInterval(160)
            setEncrypted(true)
            setBroadcastCode("TestCode".toByteArray(Charsets.UTF_8))
            addSubgroup(subgroup)
        }.build()

        val qrCodeString = metadata.toQrCodeString()

        val parsedMetadata =
            BluetoothLeBroadcastMetadataExt.convertToBroadcastMetadata(qrCodeString)!!

        assertThat(parsedMetadata).isNotNull()
        assertThat(parsedMetadata.subgroups).isNotNull()
        assertThat(parsedMetadata.subgroups.size).isEqualTo(1)
        assertThat(parsedMetadata.subgroups[0].channels).isNotNull()
        // Only selected channel can be recovered
        assertThat(parsedMetadata.subgroups[0].channels.size).isEqualTo(2)
        assertThat(parsedMetadata.subgroups[0].hasChannelPreference()).isTrue()
        assertThat(parsedMetadata.subgroups[0].channels[0].channelIndex).isEqualTo(1)
        assertThat(parsedMetadata.subgroups[0].channels[0].isSelected).isFalse()
        assertThat(parsedMetadata.subgroups[0].channels[1].channelIndex).isEqualTo(2)
        assertThat(parsedMetadata.subgroups[0].channels[1].isSelected).isTrue()
    }

    @Test
    fun decodeAndEncodeAgain_sameString() {
        val metadata = BluetoothLeBroadcastMetadataExt.convertToBroadcastMetadata(QR_CODE_STRING)!!

        val qrCodeString = metadata.toQrCodeString()

        assertThat(qrCodeString).isEqualTo(QR_CODE_STRING)
    }

    private companion object {
        const val TEST_DEVICE_ADDRESS = "00:A1:A1:A1:A1:A1"

        val Device: BluetoothDevice =
            BluetoothAdapter.getDefaultAdapter().getRemoteLeDevice(TEST_DEVICE_ADDRESS,
                BluetoothDevice.ADDRESS_TYPE_RANDOM)

        const val QR_CODE_STRING =
            "BT:R:65536;T:1;D:00-A1-A1-A1-A1-A1;AS:1;B:123456;BN:VGVzdA==;" +
            "PM:BgNwVGVzdA==;SI:160;C:VGVzdENvZGU=;SG:BS:3,BM:3,AC:BQNUZXN0BARlbmc=;" +
            "VN:U;;"
    }
}