Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1558b8ed authored by amehfooz's avatar amehfooz
Browse files

[SB] Clean up BluetoothRepository

This change moves BluetoothRepository into a data/repository
package. Also, use kosmos for BluetoothRepositoryImplTest

Bug: 414890231
Test: BluetoothConnectionStatusInteractorTest
Flag: com.android.systemui.status_bar_system_status_icons_in_compose
Change-Id: I60d1b349c98391d37c01702f0c09933ea950e041
parent 6e3a5d12
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -47,8 +47,8 @@ import com.android.systemui.SysuiTestCase;
import com.android.systemui.bluetooth.BluetoothLogger;
import com.android.systemui.dump.DumpManager;
import com.android.systemui.settings.UserTracker;
import com.android.systemui.statusbar.policy.bluetooth.BluetoothRepository;
import com.android.systemui.statusbar.policy.bluetooth.FakeBluetoothRepository;
import com.android.systemui.statusbar.policy.bluetooth.data.repository.BluetoothRepository;
import com.android.systemui.statusbar.policy.bluetooth.data.repository.FakeBluetoothRepository;
import com.android.systemui.util.concurrency.FakeExecutor;
import com.android.systemui.util.time.FakeSystemClock;

+143 −125
Original line number Diff line number Diff line
@@ -19,10 +19,15 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.settingslib.bluetooth.CachedBluetoothDevice
import com.android.settingslib.bluetooth.LocalBluetoothAdapter
import com.android.settingslib.bluetooth.LocalBluetoothManager
import com.android.systemui.SysuiTestCase
import com.android.systemui.bluetooth.localBluetoothManager
import com.android.systemui.kosmos.runTest
import com.android.systemui.kosmos.useUnconfinedTestDispatcher
import com.android.systemui.statusbar.policy.bluetooth.data.repository.BluetoothRepositoryImpl
import com.android.systemui.statusbar.policy.bluetooth.data.repository.ConnectionStatusModel
import com.android.systemui.statusbar.policy.bluetooth.data.repository.realBluetoothRepository
import com.android.systemui.testKosmos
import com.android.systemui.util.mockito.mock
import com.android.systemui.util.mockito.whenever
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestCoroutineScheduler
@@ -33,35 +38,34 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.MockitoAnnotations
import org.mockito.kotlin.whenever

@SmallTest
@RunWith(AndroidJUnit4::class)
class BluetoothRepositoryImplTest : SysuiTestCase() {

    private lateinit var underTest: BluetoothRepositoryImpl
    private val kosmos = testKosmos().useUnconfinedTestDispatcher()
    private val underTest: BluetoothRepositoryImpl =
        kosmos.realBluetoothRepository as BluetoothRepositoryImpl

    private lateinit var scheduler: TestCoroutineScheduler
    private lateinit var dispatcher: TestDispatcher
    private lateinit var testScope: TestScope

    @Mock private lateinit var localBluetoothManager: LocalBluetoothManager
    @Mock private lateinit var bluetoothAdapter: LocalBluetoothAdapter

    @Before
    fun setUp() {
        MockitoAnnotations.initMocks(this)
        whenever(localBluetoothManager.bluetoothAdapter).thenReturn(bluetoothAdapter)

        scheduler = TestCoroutineScheduler()
        dispatcher = StandardTestDispatcher(scheduler)
        testScope = TestScope(dispatcher)

        underTest =
            BluetoothRepositoryImpl(testScope.backgroundScope, dispatcher, localBluetoothManager)
        whenever(kosmos.localBluetoothManager?.bluetoothAdapter).thenReturn(bluetoothAdapter)
    }

    @Test
    fun fetchConnectionStatusInBackground_currentDevicesEmpty_maxStateIsManagerState() {
    fun fetchConnectionStatusInBackground_currentDevicesEmpty_maxStateIsManagerState() =
        kosmos.runTest {
            whenever(bluetoothAdapter.connectionState).thenReturn(BluetoothProfile.STATE_CONNECTING)

            val status = fetchConnectionStatus(currentDevices = emptyList())
@@ -70,23 +74,26 @@ class BluetoothRepositoryImplTest : SysuiTestCase() {
        }

    @Test
    fun fetchConnectionStatusInBackground_currentDevicesEmpty_nullManager_maxStateIsDisconnected() {
    fun fetchConnectionStatusInBackground_currentDevicesEmpty_nullManager_maxStateIsDisconnected() =
        kosmos.runTest {
            // This CONNECTING state should be unused because localBluetoothManager is null
            whenever(bluetoothAdapter.connectionState).thenReturn(BluetoothProfile.STATE_CONNECTING)
        underTest =
            val repository =
                BluetoothRepositoryImpl(
                    testScope.backgroundScope,
                    dispatcher,
                    localBluetoothManager = null,
                )

        val status = fetchConnectionStatus(currentDevices = emptyList())
            val status =
                fetchConnectionStatus(repository = repository, currentDevices = emptyList())

            assertThat(status.maxConnectionState).isEqualTo(BluetoothProfile.STATE_DISCONNECTED)
        }

    @Test
    fun fetchConnectionStatusInBackground_managerStateLargerThanDeviceStates_maxStateIsManager() {
    fun fetchConnectionStatusInBackground_managerStateLargerThanDeviceStates_maxStateIsManager() =
        kosmos.runTest {
            whenever(bluetoothAdapter.connectionState).thenReturn(BluetoothProfile.STATE_CONNECTING)
            val device1 =
                mock<CachedBluetoothDevice>().also {
@@ -103,8 +110,10 @@ class BluetoothRepositoryImplTest : SysuiTestCase() {
        }

    @Test
    fun fetchConnectionStatusInBackground_oneCurrentDevice_maxStateIsDeviceState() {
        whenever(bluetoothAdapter.connectionState).thenReturn(BluetoothProfile.STATE_DISCONNECTED)
    fun fetchConnectionStatusInBackground_oneCurrentDevice_maxStateIsDeviceState() =
        kosmos.runTest {
            whenever(bluetoothAdapter.connectionState)
                .thenReturn(BluetoothProfile.STATE_DISCONNECTED)
            val device =
                mock<CachedBluetoothDevice>().also {
                    whenever(it.maxConnectionState).thenReturn(BluetoothProfile.STATE_CONNECTING)
@@ -116,8 +125,10 @@ class BluetoothRepositoryImplTest : SysuiTestCase() {
        }

    @Test
    fun fetchConnectionStatusInBackground_multipleDevices_maxStateIsHighestState() {
        whenever(bluetoothAdapter.connectionState).thenReturn(BluetoothProfile.STATE_DISCONNECTED)
    fun fetchConnectionStatusInBackground_multipleDevices_maxStateIsHighestState() =
        kosmos.runTest {
            whenever(bluetoothAdapter.connectionState)
                .thenReturn(BluetoothProfile.STATE_DISCONNECTED)

            val device1 =
                mock<CachedBluetoothDevice>().also {
@@ -136,7 +147,8 @@ class BluetoothRepositoryImplTest : SysuiTestCase() {
        }

    @Test
    fun fetchConnectionStatusInBackground_devicesNotConnected_maxStateIsDisconnected() {
    fun fetchConnectionStatusInBackground_devicesNotConnected_maxStateIsDisconnected() =
        kosmos.runTest {
            whenever(bluetoothAdapter.connectionState).thenReturn(BluetoothProfile.STATE_CONNECTING)

            // WHEN the devices say their state is CONNECTED but [isConnected] is false
@@ -158,14 +170,16 @@ class BluetoothRepositoryImplTest : SysuiTestCase() {
        }

    @Test
    fun fetchConnectionStatusInBackground_currentDevicesEmpty_connectedDevicesEmpty() {
    fun fetchConnectionStatusInBackground_currentDevicesEmpty_connectedDevicesEmpty() =
        kosmos.runTest {
            val status = fetchConnectionStatus(currentDevices = emptyList())

            assertThat(status.connectedDevices).isEmpty()
        }

    @Test
    fun fetchConnectionStatusInBackground_oneCurrentDeviceDisconnected_connectedDevicesEmpty() {
    fun fetchConnectionStatusInBackground_oneCurrentDeviceDisconnected_connectedDevicesEmpty() =
        kosmos.runTest {
            val device =
                mock<CachedBluetoothDevice>().also { whenever(it.isConnected).thenReturn(false) }

@@ -175,7 +189,8 @@ class BluetoothRepositoryImplTest : SysuiTestCase() {
        }

    @Test
    fun fetchConnectionStatusInBackground_oneCurrentDeviceConnected_connectedDevicesHasDevice() {
    fun fetchConnectionStatusInBackground_oneCurrentDeviceConnected_connectedDevicesHasDevice() =
        kosmos.runTest {
            val device =
                mock<CachedBluetoothDevice>().also { whenever(it.isConnected).thenReturn(true) }

@@ -185,7 +200,8 @@ class BluetoothRepositoryImplTest : SysuiTestCase() {
        }

    @Test
    fun fetchConnectionStatusInBackground_multipleDevices_connectedDevicesHasOnlyConnected() {
    fun fetchConnectionStatusInBackground_multipleDevices_connectedDevicesHasOnlyConnected() =
        kosmos.runTest {
            val device1Connected =
                mock<CachedBluetoothDevice>().also { whenever(it.isConnected).thenReturn(true) }
            val device2Disconnected =
@@ -198,14 +214,16 @@ class BluetoothRepositoryImplTest : SysuiTestCase() {
                    currentDevices = listOf(device1Connected, device2Disconnected, device3Connected)
                )

        assertThat(status.connectedDevices).isEqualTo(listOf(device1Connected, device3Connected))
            assertThat(status.connectedDevices)
                .isEqualTo(listOf(device1Connected, device3Connected))
        }

    private fun fetchConnectionStatus(
        currentDevices: Collection<CachedBluetoothDevice>
        repository: BluetoothRepositoryImpl = underTest,
        currentDevices: Collection<CachedBluetoothDevice>,
    ): ConnectionStatusModel {
        var receivedStatus: ConnectionStatusModel? = null
        underTest.fetchConnectionStatusInBackground(currentDevices) { status ->
        repository.fetchConnectionStatusInBackground(currentDevices) { status ->
            receivedStatus = status
        }
        scheduler.runCurrent()
+2 −2
Original line number Diff line number Diff line
@@ -42,8 +42,8 @@ import com.android.systemui.dagger.qualifiers.Background;
import com.android.systemui.dagger.qualifiers.Main;
import com.android.systemui.dump.DumpManager;
import com.android.systemui.settings.UserTracker;
import com.android.systemui.statusbar.policy.bluetooth.BluetoothRepository;
import com.android.systemui.statusbar.policy.bluetooth.ConnectionStatusModel;
import com.android.systemui.statusbar.policy.bluetooth.data.repository.BluetoothRepository;
import com.android.systemui.statusbar.policy.bluetooth.data.repository.ConnectionStatusModel;

import java.io.PrintWriter;
import java.util.ArrayList;
+9 −7
Original line number Diff line number Diff line
/*
 * 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
 * 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.
 * 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.systemui.statusbar.policy.bluetooth
package com.android.systemui.statusbar.policy.bluetooth.data.repository

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothProfile
+2 −2
Original line number Diff line number Diff line
@@ -81,8 +81,8 @@ import com.android.systemui.statusbar.policy.WalletController;
import com.android.systemui.statusbar.policy.WalletControllerImpl;
import com.android.systemui.statusbar.policy.ZenModeController;
import com.android.systemui.statusbar.policy.ZenModeControllerImpl;
import com.android.systemui.statusbar.policy.bluetooth.BluetoothRepository;
import com.android.systemui.statusbar.policy.bluetooth.BluetoothRepositoryImpl;
import com.android.systemui.statusbar.policy.bluetooth.data.repository.BluetoothRepository;
import com.android.systemui.statusbar.policy.bluetooth.data.repository.BluetoothRepositoryImpl;
import com.android.systemui.statusbar.policy.data.repository.DeviceProvisioningRepositoryModule;

import dagger.Binds;
Loading