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 androidx.test.ext.junit.runners.AndroidJUnit4 21 import androidx.test.filters.SmallTest 22 import com.android.settingslib.bluetooth.BluetoothEventManager 23 import com.android.settingslib.bluetooth.LocalBluetoothManager 24 import com.android.systemui.SysuiTestCase 25 import com.android.systemui.coroutines.collectLastValue 26 import com.android.systemui.util.mockito.whenever 27 import com.google.common.truth.Truth.assertThat 28 import kotlinx.coroutines.test.StandardTestDispatcher 29 import kotlinx.coroutines.test.TestScope 30 import kotlinx.coroutines.test.runCurrent 31 import kotlinx.coroutines.test.runTest 32 import org.junit.Before 33 import org.junit.Rule 34 import org.junit.Test 35 import org.junit.runner.RunWith 36 import org.mockito.Mock 37 import org.mockito.junit.MockitoJUnit 38 import org.mockito.junit.MockitoRule 39 40 @SmallTest 41 @RunWith(AndroidJUnit4::class) 42 class BluetoothAutoOnRepositoryTest : SysuiTestCase() { 43 @get:Rule val mockitoRule: MockitoRule = MockitoJUnit.rule() 44 private val testDispatcher = StandardTestDispatcher() 45 private val testScope = TestScope(testDispatcher) 46 @Mock private lateinit var bluetoothAdapter: BluetoothAdapter 47 @Mock private lateinit var localBluetoothManager: LocalBluetoothManager 48 @Mock private lateinit var eventManager: BluetoothEventManager 49 50 private lateinit var bluetoothAutoOnRepository: BluetoothAutoOnRepository 51 52 @Before setUpnull53 fun setUp() { 54 whenever(localBluetoothManager.eventManager).thenReturn(eventManager) 55 bluetoothAutoOnRepository = 56 BluetoothAutoOnRepository( 57 localBluetoothManager, 58 bluetoothAdapter, 59 testScope.backgroundScope, 60 testDispatcher, 61 ) 62 } 63 64 @Test testIsAutoOn_returnFalsenull65 fun testIsAutoOn_returnFalse() { 66 testScope.runTest { 67 whenever(bluetoothAdapter.isAutoOnEnabled).thenReturn(false) 68 val actualValue by collectLastValue(bluetoothAutoOnRepository.isAutoOn) 69 70 runCurrent() 71 72 assertThat(actualValue).isEqualTo(false) 73 } 74 } 75 76 @Test testIsAutoOn_returnTruenull77 fun testIsAutoOn_returnTrue() { 78 testScope.runTest { 79 whenever(bluetoothAdapter.isAutoOnEnabled).thenReturn(true) 80 val actualValue by collectLastValue(bluetoothAutoOnRepository.isAutoOn) 81 82 runCurrent() 83 84 assertThat(actualValue).isEqualTo(true) 85 } 86 } 87 88 @Test testIsAutoOnSupported_returnTruenull89 fun testIsAutoOnSupported_returnTrue() { 90 testScope.runTest { 91 whenever(bluetoothAdapter.isAutoOnSupported).thenReturn(true) 92 val actualValue = bluetoothAutoOnRepository.isAutoOnSupported() 93 94 runCurrent() 95 96 assertThat(actualValue).isEqualTo(true) 97 } 98 } 99 } 100