1 /*
2  * Copyright (C) 2024 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 com.android.systemui.volume.panel.component.anc.data.repository
18 
19 import android.bluetooth.BluetoothDevice
20 import android.net.Uri
21 import android.testing.TestableLooper
22 import androidx.test.ext.junit.runners.AndroidJUnit4
23 import androidx.test.filters.SmallTest
24 import com.android.systemui.SysuiTestCase
25 import com.android.systemui.coroutines.collectLastValue
26 import com.android.systemui.kosmos.testScope
27 import com.android.systemui.testKosmos
28 import com.android.systemui.volume.panel.component.anc.FakeSliceFactory
29 import com.android.systemui.volume.panel.component.anc.sliceViewManager
30 import com.google.common.truth.Truth.assertThat
31 import kotlinx.coroutines.ExperimentalCoroutinesApi
32 import kotlinx.coroutines.test.runCurrent
33 import kotlinx.coroutines.test.runTest
34 import org.junit.Before
35 import org.junit.Test
36 import org.junit.runner.RunWith
37 import org.mockito.kotlin.any
38 import org.mockito.kotlin.mock
39 import org.mockito.kotlin.whenever
40 
41 @OptIn(ExperimentalCoroutinesApi::class)
42 @SmallTest
43 @RunWith(AndroidJUnit4::class)
44 @TestableLooper.RunWithLooper(setAsMainLooper = true)
45 class AncSliceRepositoryTest : SysuiTestCase() {
46 
47     private val kosmos = testKosmos()
48 
49     private lateinit var underTest: AncSliceRepository
50 
51     @Before
setupnull52     fun setup() {
53         with(kosmos) {
54             val slice = FakeSliceFactory.createSlice(hasError = false, hasSliceItem = true)
55             whenever(sliceViewManager.bindSlice(any<Uri>())).thenReturn(slice)
56 
57             underTest = AncSliceRepositoryImpl(testScope.testScheduler, sliceViewManager)
58         }
59     }
60 
61     @Test
connectedDevice_noUri_noSlicenull62     fun connectedDevice_noUri_noSlice() {
63         with(kosmos) {
64             testScope.runTest {
65                 val slice by
66                     collectLastValue(
67                         underTest.ancSlice(
68                             device = createMediaDevice(""),
69                             width = 1,
70                             isCollapsed = false,
71                             hideLabel = false,
72                         )
73                     )
74                 runCurrent()
75 
76                 assertThat(slice).isNull()
77             }
78         }
79     }
80 
81     @Test
connectedDevice_hasUri_sliceReturnednull82     fun connectedDevice_hasUri_sliceReturned() {
83         with(kosmos) {
84             testScope.runTest {
85                 val slice by
86                     collectLastValue(
87                         underTest.ancSlice(
88                             device = createMediaDevice("content://test.slice"),
89                             width = 1,
90                             isCollapsed = false,
91                             hideLabel = false,
92                         )
93                     )
94                 runCurrent()
95 
96                 assertThat(slice).isNotNull()
97             }
98         }
99     }
100 
<lambda>null101     private fun createMediaDevice(sliceUri: String): BluetoothDevice = mock {
102         on { getMetadata(any()) }
103             .thenReturn(
104                 ("<HEARABLE_CONTROL_SLICE_WITH_WIDTH>" +
105                         sliceUri +
106                         "</HEARABLE_CONTROL_SLICE_WITH_WIDTH>")
107                     .toByteArray()
108             )
109     }
110 }
111