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.testing.TestableLooper 20 import androidx.test.ext.junit.runners.AndroidJUnit4 21 import androidx.test.filters.SmallTest 22 import com.android.settingslib.bluetooth.LocalBluetoothAdapter 23 import com.android.settingslib.bluetooth.LocalBluetoothManager 24 import com.android.systemui.SysuiTestCase 25 import com.google.common.truth.Truth.assertThat 26 import kotlinx.coroutines.test.StandardTestDispatcher 27 import kotlinx.coroutines.test.TestScope 28 import kotlinx.coroutines.test.runTest 29 import org.junit.Before 30 import org.junit.Rule 31 import org.junit.Test 32 import org.junit.runner.RunWith 33 import org.mockito.Mock 34 import org.mockito.Mockito.never 35 import org.mockito.Mockito.verify 36 import org.mockito.Mockito.`when` 37 import org.mockito.junit.MockitoJUnit 38 import org.mockito.junit.MockitoRule 39 40 @SmallTest 41 @RunWith(AndroidJUnit4::class) 42 @TestableLooper.RunWithLooper(setAsMainLooper = true) 43 class BluetoothStateInteractorTest : SysuiTestCase() { 44 @get:Rule val mockitoRule: MockitoRule = MockitoJUnit.rule() 45 private val testDispatcher = StandardTestDispatcher() 46 private val testScope = TestScope(testDispatcher) 47 48 private lateinit var bluetoothStateInteractor: BluetoothStateInteractor 49 50 @Mock private lateinit var bluetoothAdapter: LocalBluetoothAdapter 51 @Mock private lateinit var localBluetoothManager: LocalBluetoothManager 52 @Mock private lateinit var logger: BluetoothTileDialogLogger 53 54 @Before setUpnull55 fun setUp() { 56 bluetoothStateInteractor = 57 BluetoothStateInteractor( 58 localBluetoothManager, 59 logger, 60 testScope.backgroundScope, 61 testDispatcher 62 ) 63 `when`(localBluetoothManager.bluetoothAdapter).thenReturn(bluetoothAdapter) 64 } 65 66 @Test testGet_isBluetoothEnablednull67 fun testGet_isBluetoothEnabled() { 68 testScope.runTest { 69 `when`(bluetoothAdapter.isEnabled).thenReturn(true) 70 71 assertThat(bluetoothStateInteractor.isBluetoothEnabled()).isTrue() 72 } 73 } 74 75 @Test testGet_isBluetoothDisablednull76 fun testGet_isBluetoothDisabled() { 77 testScope.runTest { 78 `when`(bluetoothAdapter.isEnabled).thenReturn(false) 79 80 assertThat(bluetoothStateInteractor.isBluetoothEnabled()).isFalse() 81 } 82 } 83 84 @Test testSet_bluetoothEnablednull85 fun testSet_bluetoothEnabled() { 86 testScope.runTest { 87 `when`(bluetoothAdapter.isEnabled).thenReturn(false) 88 89 bluetoothStateInteractor.setBluetoothEnabled(true) 90 verify(bluetoothAdapter).enable() 91 verify(logger) 92 .logBluetoothState(BluetoothStateStage.BLUETOOTH_STATE_VALUE_SET, true.toString()) 93 } 94 } 95 96 @Test testSet_bluetoothNoChangenull97 fun testSet_bluetoothNoChange() { 98 testScope.runTest { 99 `when`(bluetoothAdapter.isEnabled).thenReturn(false) 100 101 bluetoothStateInteractor.setBluetoothEnabled(false) 102 verify(bluetoothAdapter, never()).enable() 103 } 104 } 105 } 106