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.bluetooth.qsdialog
18 
19 import android.bluetooth.BluetoothAdapter
20 import android.testing.TestableLooper
21 import androidx.test.ext.junit.runners.AndroidJUnit4
22 import androidx.test.filters.SmallTest
23 import com.android.settingslib.bluetooth.CachedBluetoothDevice
24 import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager
25 import com.android.settingslib.bluetooth.LocalBluetoothManager
26 import com.android.systemui.SysuiTestCase
27 import com.google.common.truth.Truth.assertThat
28 import org.junit.Before
29 import org.junit.Rule
30 import org.junit.Test
31 import org.junit.runner.RunWith
32 import org.mockito.Mock
33 import org.mockito.Mockito.`when`
34 import org.mockito.junit.MockitoJUnit
35 import org.mockito.junit.MockitoRule
36 
37 @SmallTest
38 @RunWith(AndroidJUnit4::class)
39 @TestableLooper.RunWithLooper(setAsMainLooper = true)
40 class BluetoothTileDialogRepositoryTest : SysuiTestCase() {
41 
42     @get:Rule val mockitoRule: MockitoRule = MockitoJUnit.rule()
43 
44     @Mock private lateinit var localBluetoothManager: LocalBluetoothManager
45 
46     @Mock private lateinit var bluetoothAdapter: BluetoothAdapter
47 
48     @Mock private lateinit var cachedDeviceManager: CachedBluetoothDeviceManager
49 
50     @Mock private lateinit var cachedDevicesCopy: Collection<CachedBluetoothDevice>
51 
52     private lateinit var repository: BluetoothTileDialogRepository
53 
54     @Before
setUpnull55     fun setUp() {
56         `when`(localBluetoothManager.cachedDeviceManager).thenReturn(cachedDeviceManager)
57         `when`(cachedDeviceManager.cachedDevicesCopy).thenReturn(cachedDevicesCopy)
58 
59         repository = BluetoothTileDialogRepository(localBluetoothManager, bluetoothAdapter)
60     }
61 
62     @Test
testCachedDevices_bluetoothOff_emptyListnull63     fun testCachedDevices_bluetoothOff_emptyList() {
64         `when`(bluetoothAdapter.isEnabled).thenReturn(false)
65 
66         val result = repository.cachedDevices
67 
68         assertThat(result).isEmpty()
69     }
70 
71     @Test
testCachedDevices_bluetoothOn_returnDevicenull72     fun testCachedDevices_bluetoothOn_returnDevice() {
73         `when`(bluetoothAdapter.isEnabled).thenReturn(true)
74 
75         val result = repository.cachedDevices
76 
77         assertThat(result).isEqualTo(cachedDevicesCopy)
78     }
79 }
80