Move enable/disable LE Audio to Utils

Bug: 381353150
Test: atest UtilsTest
Flag: EXEMPT minor refactor
Change-Id: Id017f2b5a0f51d19878bf91b219334baeda97d6d
This commit is contained in:
Haijie Hong
2025-03-18 11:25:33 +08:00
parent 16fa7c1e33
commit a333827d6d
4 changed files with 313 additions and 106 deletions

View File

@@ -18,22 +18,29 @@ package com.android.settings.bluetooth;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothCsipSetCoordinator;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothLeBroadcastReceiveState;
import android.content.Context;
import android.os.SystemProperties;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
import com.android.settingslib.bluetooth.A2dpProfile;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.HeadsetProfile;
import com.android.settingslib.bluetooth.HearingAidProfile;
import com.android.settingslib.bluetooth.LeAudioProfile;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
@@ -41,8 +48,8 @@ import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -52,10 +59,8 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowBluetoothUtils.class})
public class UtilsTest {
private static final int METADATA_FAST_PAIR_CUSTOMIZED_FIELDS = 25;
private static final String TEMP_BOND_METADATA =
@@ -73,6 +78,14 @@ public class UtilsTest {
@Mock
private LocalBluetoothLeBroadcastAssistant mAssistant;
@Mock
private A2dpProfile mA2dpProfile;
@Mock
private HeadsetProfile mHeadsetProfile;
@Mock
private LeAudioProfile mLeAudioProfile;
@Mock
private HearingAidProfile mHearingAidProfile;
@Mock
private CachedBluetoothDeviceManager mDeviceManager;
private MetricsFeatureProvider mMetricsFeatureProvider;
@@ -80,17 +93,14 @@ public class UtilsTest {
@Before
public void setUp() {
mMetricsFeatureProvider = FakeFeatureFactory.setupForTest().getMetricsFeatureProvider();
ShadowBluetoothUtils.sLocalBluetoothManager = mLocalBtManager;
mLocalBtManager = Utils.getLocalBtManager(mContext);
when(mLocalBtManager.getProfileManager()).thenReturn(mProfileManager);
when(mLocalBtManager.getCachedDeviceManager()).thenReturn(mDeviceManager);
when(mProfileManager.getLeAudioBroadcastProfile()).thenReturn(mBroadcast);
when(mProfileManager.getLeAudioBroadcastAssistantProfile()).thenReturn(mAssistant);
}
@After
public void tearDown() {
ShadowBluetoothUtils.reset();
when(mProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile);
when(mProfileManager.getHeadsetProfile()).thenReturn(mHeadsetProfile);
when(mProfileManager.getLeAudioProfile()).thenReturn(mLeAudioProfile);
when(mProfileManager.getHearingAidProfile()).thenReturn(mHearingAidProfile);
}
@Test
@@ -170,4 +180,148 @@ public class UtilsTest {
when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of(state));
assertThat(Utils.shouldBlockPairingInAudioSharing(mLocalBtManager)).isTrue();
}
@Test
public void enableLeAudioProfile_multipleDeviceInGroup() {
CachedBluetoothDevice cachedDevice1 = mock(CachedBluetoothDevice.class);
CachedBluetoothDevice cachedDevice2 = mock(CachedBluetoothDevice.class);
CachedBluetoothDevice cachedDevice3 = mock(CachedBluetoothDevice.class);
BluetoothDevice device1 = mock(BluetoothDevice.class);
BluetoothDevice device2 = mock(BluetoothDevice.class);
BluetoothDevice device3 = mock(BluetoothDevice.class);
when(cachedDevice1.getDevice()).thenReturn(device1);
when(cachedDevice2.getDevice()).thenReturn(device2);
when(cachedDevice3.getDevice()).thenReturn(device3);
when(cachedDevice1.getMemberDevice()).thenReturn(ImmutableSet.of(cachedDevice2));
when(mDeviceManager.getCachedDevicesCopy())
.thenReturn(ImmutableList.of(cachedDevice1, cachedDevice3));
when(cachedDevice1.getGroupId()).thenReturn(1);
when(cachedDevice2.getGroupId()).thenReturn(1);
when(cachedDevice3.getGroupId()).thenReturn(2);
when(cachedDevice1.getProfiles())
.thenReturn(ImmutableList.of(mA2dpProfile, mHeadsetProfile, mLeAudioProfile));
when(cachedDevice2.getProfiles()).thenReturn(ImmutableList.of(mLeAudioProfile));
when(cachedDevice3.getProfiles())
.thenReturn(ImmutableList.of(mA2dpProfile, mHeadsetProfile, mLeAudioProfile));
Utils.setLeAudioEnabled(mLocalBtManager, cachedDevice2, true);
verify(mLeAudioProfile).setEnabled(device1, true);
verify(mLeAudioProfile).setEnabled(device2, true);
verify(mHearingAidProfile).setEnabled(device1, false);
verify(mAssistant).setEnabled(device1, true);
verify(mLeAudioProfile, never()).setEnabled(eq(device3), anyBoolean());
verify(mA2dpProfile, never()).setEnabled(eq(device3), anyBoolean());
verify(mHeadsetProfile, never()).setEnabled(eq(device3), anyBoolean());
}
@Test
public void enableLeAudioProfile_dualModeEnabled_a2dpAndHfpNotChanged() {
SystemProperties.set("persist.bluetooth.enable_dual_mode_audio", "true");
CachedBluetoothDevice cachedDevice1 = mock(CachedBluetoothDevice.class);
BluetoothDevice device1 = mock(BluetoothDevice.class);
when(cachedDevice1.getDevice()).thenReturn(device1);
when(cachedDevice1.getGroupId()).thenReturn(BluetoothCsipSetCoordinator.GROUP_ID_INVALID);
when(cachedDevice1.getProfiles())
.thenReturn(ImmutableList.of(mA2dpProfile, mHeadsetProfile, mLeAudioProfile));
when(mA2dpProfile.isEnabled(device1)).thenReturn(true);
when(mHeadsetProfile.isEnabled(device1)).thenReturn(true);
Utils.setLeAudioEnabled(mLocalBtManager, cachedDevice1, true);
verify(mLeAudioProfile).setEnabled(device1, true);
verify(mA2dpProfile, never()).setEnabled(device1, false);
verify(mHeadsetProfile, never()).setEnabled(device1, false);
}
@Test
public void enableLeAudioProfile_dualModeDisabled_disableA2dpAndHfp() {
SystemProperties.set("persist.bluetooth.enable_dual_mode_audio", "false");
CachedBluetoothDevice cachedDevice1 = mock(CachedBluetoothDevice.class);
BluetoothDevice device1 = mock(BluetoothDevice.class);
when(cachedDevice1.getDevice()).thenReturn(device1);
when(cachedDevice1.getGroupId()).thenReturn(BluetoothCsipSetCoordinator.GROUP_ID_INVALID);
when(cachedDevice1.getProfiles())
.thenReturn(ImmutableList.of(mA2dpProfile, mHeadsetProfile, mLeAudioProfile));
when(mA2dpProfile.isEnabled(device1)).thenReturn(true);
when(mHeadsetProfile.isEnabled(device1)).thenReturn(true);
Utils.setLeAudioEnabled(mLocalBtManager, cachedDevice1, true);
verify(mLeAudioProfile).setEnabled(device1, true);
verify(mA2dpProfile).setEnabled(device1, false);
verify(mHeadsetProfile).setEnabled(device1, false);
}
@Test
public void disableLeAudioProfile_multipleDeviceInGroup() {
CachedBluetoothDevice cachedDevice1 = mock(CachedBluetoothDevice.class);
CachedBluetoothDevice cachedDevice2 = mock(CachedBluetoothDevice.class);
CachedBluetoothDevice cachedDevice3 = mock(CachedBluetoothDevice.class);
BluetoothDevice device1 = mock(BluetoothDevice.class);
BluetoothDevice device2 = mock(BluetoothDevice.class);
BluetoothDevice device3 = mock(BluetoothDevice.class);
when(cachedDevice1.getDevice()).thenReturn(device1);
when(cachedDevice2.getDevice()).thenReturn(device2);
when(cachedDevice3.getDevice()).thenReturn(device3);
when(cachedDevice1.getMemberDevice()).thenReturn(ImmutableSet.of(cachedDevice2));
when(mDeviceManager.getCachedDevicesCopy())
.thenReturn(ImmutableList.of(cachedDevice1, cachedDevice3));
when(cachedDevice1.getGroupId()).thenReturn(1);
when(cachedDevice2.getGroupId()).thenReturn(1);
when(cachedDevice3.getGroupId()).thenReturn(2);
when(cachedDevice1.getProfiles())
.thenReturn(ImmutableList.of(mA2dpProfile, mHeadsetProfile, mLeAudioProfile));
when(cachedDevice2.getProfiles()).thenReturn(ImmutableList.of(mLeAudioProfile));
when(cachedDevice3.getProfiles())
.thenReturn(ImmutableList.of(mA2dpProfile, mHeadsetProfile, mLeAudioProfile));
Utils.setLeAudioEnabled(mLocalBtManager, cachedDevice2, false);
verify(mLeAudioProfile).setEnabled(device1, false);
verify(mLeAudioProfile).setEnabled(device2, false);
verify(mHearingAidProfile).setEnabled(device1, true);
verify(mAssistant).setEnabled(device1, false);
verify(mLeAudioProfile, never()).setEnabled(eq(device3), anyBoolean());
verify(mA2dpProfile, never()).setEnabled(eq(device3), anyBoolean());
verify(mHeadsetProfile, never()).setEnabled(eq(device3), anyBoolean());
}
@Test
public void disableLeAudioProfile_dualModeEnabled_a2dpAndHfpNotChanged() {
SystemProperties.set("persist.bluetooth.enable_dual_mode_audio", "true");
CachedBluetoothDevice cachedDevice1 = mock(CachedBluetoothDevice.class);
BluetoothDevice device1 = mock(BluetoothDevice.class);
when(cachedDevice1.getDevice()).thenReturn(device1);
when(cachedDevice1.getGroupId()).thenReturn(BluetoothCsipSetCoordinator.GROUP_ID_INVALID);
when(cachedDevice1.getProfiles())
.thenReturn(ImmutableList.of(mA2dpProfile, mHeadsetProfile, mLeAudioProfile));
when(mA2dpProfile.isEnabled(device1)).thenReturn(false);
when(mHeadsetProfile.isEnabled(device1)).thenReturn(false);
Utils.setLeAudioEnabled(mLocalBtManager, cachedDevice1, false);
verify(mLeAudioProfile).setEnabled(device1, false);
verify(mA2dpProfile, never()).setEnabled(device1, true);
verify(mHeadsetProfile, never()).setEnabled(device1, true);
}
@Test
public void disableLeAudioProfile_dualModeDisabled_enableA2dpAndHfp() {
SystemProperties.set("persist.bluetooth.enable_dual_mode_audio", "false");
CachedBluetoothDevice cachedDevice1 = mock(CachedBluetoothDevice.class);
BluetoothDevice device1 = mock(BluetoothDevice.class);
when(cachedDevice1.getDevice()).thenReturn(device1);
when(cachedDevice1.getGroupId()).thenReturn(BluetoothCsipSetCoordinator.GROUP_ID_INVALID);
when(cachedDevice1.getProfiles())
.thenReturn(ImmutableList.of(mA2dpProfile, mHeadsetProfile, mLeAudioProfile));
when(mA2dpProfile.isEnabled(device1)).thenReturn(false);
when(mHeadsetProfile.isEnabled(device1)).thenReturn(false);
Utils.setLeAudioEnabled(mLocalBtManager, cachedDevice1, false);
verify(mLeAudioProfile).setEnabled(device1, false);
verify(mA2dpProfile).setEnabled(device1, true);
verify(mHeadsetProfile).setEnabled(device1, true);
}
}

View File

@@ -18,24 +18,48 @@ package com.android.settings.testutils.shadow;
import android.content.Context;
import androidx.annotation.NonNull;
import com.android.settings.bluetooth.Utils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.Resetter;
import java.util.HashMap;
import java.util.Map;
/** Robolectric shadow for the bluetooth utils. */
@Implements(Utils.class)
public class ShadowBluetoothUtils {
public static LocalBluetoothManager sLocalBluetoothManager;
private static final Map<CachedBluetoothDevice, Boolean> sLeAudioState = new HashMap<>();
@Implementation
protected static LocalBluetoothManager getLocalBtManager(Context context) {
return sLocalBluetoothManager;
}
/** Sets le audio state for the device. */
@Implementation
public static void setLeAudioEnabled(
@NonNull LocalBluetoothManager manager,
@NonNull CachedBluetoothDevice cachedDevice,
boolean enable) {
sLeAudioState.put(cachedDevice, enable);
}
/** Checks whether le audio is enabled for the device. */
public static boolean isLeAudioEnabled(@NonNull CachedBluetoothDevice cachedDevice) {
if (sLeAudioState.containsKey(cachedDevice)) {
return sLeAudioState.get(cachedDevice);
}
return false;
}
/** Resets the local bluetooth manager to null. */
@Resetter
public static void reset() {