[Audiosharing] Impl audio sharing feature provider in Settings

Bug: 324023639
Test: manual
Change-Id: I4005ea8fbc0983a833f5a7dee2815f09eab48d3c
This commit is contained in:
Yiyi Shen
2024-02-19 18:18:23 +08:00
parent 38e6e7f513
commit a2577fd60e
11 changed files with 290 additions and 143 deletions

View File

@@ -16,7 +16,6 @@
package com.android.settings.bluetooth;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
@@ -25,36 +24,26 @@ import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothLeBroadcastReceiveState;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothStatusCodes;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.platform.test.annotations.RequiresFlagsDisabled;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
import android.util.Pair;
import com.android.settings.connecteddevice.DevicePreferenceCallback;
import com.android.settings.connecteddevice.audiosharing.AudioSharingFeatureProvider;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.flags.Flags;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.shadow.ShadowAudioManager;
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@@ -75,9 +64,6 @@ import java.util.Collection;
ShadowBluetoothUtils.class
})
public class AvailableMediaBluetoothDeviceUpdaterTest {
@Rule
public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
private static final String MAC_ADDRESS = "04:52:C7:0B:D8:3C";
@Mock private DashboardFragment mDashboardFragment;
@@ -86,11 +72,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
@Mock private BluetoothDevice mBluetoothDevice;
@Mock private Drawable mDrawable;
@Mock private LocalBluetoothManager mLocalBtManager;
@Mock private LocalBluetoothProfileManager mLocalBtProfileManager;
@Mock private CachedBluetoothDeviceManager mCachedDeviceManager;
@Mock private LocalBluetoothLeBroadcast mBroadcast;
@Mock private LocalBluetoothLeBroadcastAssistant mAssistant;
@Mock private BluetoothLeBroadcastReceiveState mBroadcastReceiveState;
private Context mContext;
private AvailableMediaBluetoothDeviceUpdater mBluetoothDeviceUpdater;
@@ -98,12 +80,14 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
private AudioManager mAudioManager;
private BluetoothDevicePreference mPreference;
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
private AudioSharingFeatureProvider mFeatureProvider;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mFeatureProvider = FakeFeatureFactory.setupForTest().getAudioSharingFeatureProvider();
mAudioManager = mContext.getSystemService(AudioManager.class);
ShadowBluetoothUtils.sLocalBluetoothManager = mLocalBtManager;
mLocalBtManager = Utils.getLocalBtManager(mContext);
@@ -267,13 +251,15 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
}
@Test
@RequiresFlagsDisabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onProfileConnectionStateChanged_leAudioDeviceConnected_notInCall_addsPreference() {
setUpBroadcast(/* isSupported= */ false, /* isBroadcasting= */ false);
public void
onProfileConnectionStateChanged_leaDeviceConnected_notInCallNoSharing_addsPreference() {
mAudioManager.setMode(AudioManager.MODE_NORMAL);
when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
.thenReturn(true);
when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);
when(mFeatureProvider.isAudioSharingFilterMatched(
any(CachedBluetoothDevice.class), any(LocalBluetoothManager.class)))
.thenReturn(false);
mBluetoothDeviceUpdater.onProfileConnectionStateChanged(
mCachedBluetoothDevice,
@@ -284,13 +270,15 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
}
@Test
@RequiresFlagsDisabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onProfileConnectionStateChanged_leAudioDeviceConnected_inCall_addsPreference() {
setUpBroadcast(/* isSupported= */ false, /* isBroadcasting= */ false);
public void
onProfileConnectionStateChanged_leaDeviceConnected_inCallNoSharing_addsPreference() {
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
.thenReturn(true);
when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);
when(mFeatureProvider.isAudioSharingFilterMatched(
any(CachedBluetoothDevice.class), any(LocalBluetoothManager.class)))
.thenReturn(false);
mBluetoothDeviceUpdater.onProfileConnectionStateChanged(
mCachedBluetoothDevice,
@@ -301,50 +289,16 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void
onProfileConnectionStateChanged_leaDeviceConnected_notInCall_notInBroadcast_addsPref() {
setUpBroadcast(/* isSupported= */ true, /* isBroadcasting= */ false);
onProfileConnectionStateChanged_leaDeviceConnected_notInCallInSharing_removesPref() {
mAudioManager.setMode(AudioManager.MODE_NORMAL);
when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
.thenReturn(true);
when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);
mBluetoothDeviceUpdater.onProfileConnectionStateChanged(
mCachedBluetoothDevice,
BluetoothProfile.STATE_CONNECTED,
BluetoothProfile.LE_AUDIO);
verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void
onProfileConnectionStateChanged_leaDeviceConnected_inCall_notInBroadcast_addsPref() {
setUpBroadcast(/* isSupported= */ true, /* isBroadcasting= */ false);
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true);
when(mFeatureProvider.isAudioSharingFilterMatched(
any(CachedBluetoothDevice.class), any(LocalBluetoothManager.class)))
.thenReturn(true);
when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);
mBluetoothDeviceUpdater.onProfileConnectionStateChanged(
mCachedBluetoothDevice,
BluetoothProfile.STATE_CONNECTED,
BluetoothProfile.LE_AUDIO);
verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void
onProfileConnectionStateChanged_leaDeviceConnected_notInCall_inBroadcast_removesPref() {
setUpBroadcast(/* isSupported= */ true, /* isBroadcasting= */ true);
mAudioManager.setMode(AudioManager.MODE_NORMAL);
when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
.thenReturn(true);
when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);
mBluetoothDeviceUpdater.onProfileConnectionStateChanged(
mCachedBluetoothDevice,
@@ -355,14 +309,15 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void
onProfileConnectionStateChanged_leaDeviceConnected_inCall_inBroadcast_removesPref() {
setUpBroadcast(/* isSupported= */ true, /* isBroadcasting= */ true);
mAudioManager.setMode(AudioManager.MODE_IN_CALL);
public void onProfileConnectionStateChanged_leaDeviceConnected_inCallInSharing_removesPref() {
mAudioManager.setMode(AudioManager.MODE_NORMAL);
when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
.thenReturn(true);
when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);
when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true);
when(mFeatureProvider.isAudioSharingFilterMatched(
any(CachedBluetoothDevice.class), any(LocalBluetoothManager.class)))
.thenReturn(true);
mBluetoothDeviceUpdater.onProfileConnectionStateChanged(
mCachedBluetoothDevice,
@@ -414,56 +369,9 @@ public class AvailableMediaBluetoothDeviceUpdaterTest {
}
@Test
@RequiresFlagsDisabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onClick_Preference_setActive() {
setUpBroadcast(/* isSupported= */ false, /* isBroadcasting= */ false);
mBluetoothDeviceUpdater.onPreferenceClick(mPreference);
verify(mCachedBluetoothDevice).setActive();
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onClick_Preference_isNotBroadcasting_setActive() {
setUpBroadcast(/* isSupported= */ true, /* isBroadcasting= */ false);
mBluetoothDeviceUpdater.onPreferenceClick(mPreference);
verify(mCachedBluetoothDevice).setActive();
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
public void onClick_Preference_isBroadcasting_stopBroadcastingAndSetActive() {
setUpBroadcast(/* isSupported= */ true, /* isBroadcasting= */ true);
doNothing().when(mBroadcast).stopBroadcast(anyInt());
mBluetoothDeviceUpdater.onPreferenceClick(mPreference);
verify(mBroadcast).stopBroadcast(anyInt());
verify(mCachedBluetoothDevice).setActive();
}
private void setUpBroadcast(boolean isSupported, boolean isBroadcasting) {
if (isSupported) {
mShadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported(
BluetoothStatusCodes.FEATURE_SUPPORTED);
mShadowBluetoothAdapter.setIsLeAudioBroadcastAssistantSupported(
BluetoothStatusCodes.FEATURE_SUPPORTED);
when(mLocalBtManager.getProfileManager()).thenReturn(mLocalBtProfileManager);
when(mLocalBtProfileManager.getLeAudioBroadcastProfile()).thenReturn(mBroadcast);
when(mBroadcast.isEnabled(null)).thenReturn(isBroadcasting);
when(mLocalBtProfileManager.getLeAudioBroadcastAssistantProfile())
.thenReturn(mAssistant);
if (isBroadcasting) {
when(mAssistant.getAllSources(any()))
.thenReturn(ImmutableList.of(mBroadcastReceiveState));
} else {
when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of());
}
} else {
mShadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported(
BluetoothStatusCodes.FEATURE_NOT_SUPPORTED);
mShadowBluetoothAdapter.setIsLeAudioBroadcastAssistantSupported(
BluetoothStatusCodes.FEATURE_NOT_SUPPORTED);
}
}
}

View File

@@ -0,0 +1,67 @@
/*
* Copyright (C) 2024 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
*
* 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.
*/
package com.android.settings.connecteddevice.audiosharing;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class AudioSharingFeatureProviderImplTest {
@Rule public final MockitoRule mMockitoRule = MockitoJUnit.rule();
@Mock private CachedBluetoothDevice mCachedDevice;
@Mock private LocalBluetoothManager mLocalBtManager;
@Mock private DashboardFragment mFragment;
private Context mContext;
private AudioSharingFeatureProviderImpl mFeatureProvider;
@Before
public void setUp() {
mContext = ApplicationProvider.getApplicationContext();
mFeatureProvider = new AudioSharingFeatureProviderImpl();
}
@Test
public void createAudioSharingDevicePreferenceController_returnsNull() {
assertThat(
mFeatureProvider.createAudioSharingDevicePreferenceController(
mContext, mFragment, /* lifecycle= */ null))
.isNull();
}
@Test
public void isAudioSharingFilterMatched_returnsFalse() {
assertThat(mFeatureProvider.isAudioSharingFilterMatched(mCachedDevice, mLocalBtManager))
.isFalse();
}
}

View File

@@ -27,6 +27,7 @@ import com.android.settings.biometrics.face.FaceFeatureProvider;
import com.android.settings.biometrics.fingerprint.FingerprintFeatureProvider;
import com.android.settings.biometrics2.factory.BiometricsRepositoryProvider;
import com.android.settings.bluetooth.BluetoothFeatureProvider;
import com.android.settings.connecteddevice.audiosharing.AudioSharingFeatureProvider;
import com.android.settings.connecteddevice.fastpair.FastPairFeatureProvider;
import com.android.settings.connecteddevice.stylus.StylusFeatureProvider;
import com.android.settings.dashboard.DashboardFeatureProvider;
@@ -103,6 +104,7 @@ public class FakeFeatureFactory extends FeatureFactory {
public FastPairFeatureProvider mFastPairFeatureProvider;
public PrivateSpaceLoginFeatureProvider mPrivateSpaceLoginFeatureProvider;
public DisplayFeatureProvider mDisplayFeatureProvider;
public AudioSharingFeatureProvider mAudioSharingFeatureProvider;
/**
* Call this in {@code @Before} method of the test class to use fake factory.
@@ -152,6 +154,7 @@ public class FakeFeatureFactory extends FeatureFactory {
mFastPairFeatureProvider = mock(FastPairFeatureProvider.class);
mPrivateSpaceLoginFeatureProvider = mock(PrivateSpaceLoginFeatureProvider.class);
mDisplayFeatureProvider = mock(DisplayFeatureProvider.class);
mAudioSharingFeatureProvider = mock(AudioSharingFeatureProvider.class);
}
@Override
@@ -339,5 +342,10 @@ public class FakeFeatureFactory extends FeatureFactory {
public DisplayFeatureProvider getDisplayFeatureProvider() {
return mDisplayFeatureProvider;
}
@Override
public AudioSharingFeatureProvider getAudioSharingFeatureProvider() {
return mAudioSharingFeatureProvider;
}
}

View File

@@ -25,6 +25,7 @@ import com.android.settings.biometrics.face.FaceFeatureProvider
import com.android.settings.biometrics.fingerprint.FingerprintFeatureProvider
import com.android.settings.biometrics2.factory.BiometricsRepositoryProvider
import com.android.settings.bluetooth.BluetoothFeatureProvider
import com.android.settings.connecteddevice.audiosharing.AudioSharingFeatureProvider
import com.android.settings.connecteddevice.fastpair.FastPairFeatureProvider
import com.android.settings.connecteddevice.stylus.StylusFeatureProvider
import com.android.settings.dashboard.DashboardFeatureProvider
@@ -149,4 +150,6 @@ class FakeFeatureFactory : FeatureFactory() {
get() = TODO("Not yet implemented")
override val displayFeatureProvider: DisplayFeatureProvider
get() = TODO("Not yet implemented")
override val audioSharingFeatureProvider: AudioSharingFeatureProvider
get() = TODO("Not yet implemented")
}

View File

@@ -27,6 +27,7 @@ import com.android.settings.biometrics.face.FaceFeatureProvider;
import com.android.settings.biometrics.fingerprint.FingerprintFeatureProvider;
import com.android.settings.biometrics2.factory.BiometricsRepositoryProvider;
import com.android.settings.bluetooth.BluetoothFeatureProvider;
import com.android.settings.connecteddevice.audiosharing.AudioSharingFeatureProvider;
import com.android.settings.connecteddevice.fastpair.FastPairFeatureProvider;
import com.android.settings.connecteddevice.stylus.StylusFeatureProvider;
import com.android.settings.dashboard.DashboardFeatureProvider;
@@ -102,6 +103,7 @@ public class FakeFeatureFactory extends FeatureFactory {
public FastPairFeatureProvider mFastPairFeatureProvider;
public PrivateSpaceLoginFeatureProvider mPrivateSpaceLoginFeatureProvider;
public DisplayFeatureProvider mDisplayFeatureProvider;
public AudioSharingFeatureProvider mAudioSharingFeatureProvider;
/** Call this in {@code @Before} method of the test class to use fake factory. */
public static FakeFeatureFactory setupForTest() {
@@ -153,6 +155,7 @@ public class FakeFeatureFactory extends FeatureFactory {
mFastPairFeatureProvider = mock(FastPairFeatureProvider.class);
mPrivateSpaceLoginFeatureProvider = mock(PrivateSpaceLoginFeatureProvider.class);
mDisplayFeatureProvider = mock(DisplayFeatureProvider.class);
mAudioSharingFeatureProvider = mock(AudioSharingFeatureProvider.class);
}
@Override
@@ -340,4 +343,9 @@ public class FakeFeatureFactory extends FeatureFactory {
public DisplayFeatureProvider getDisplayFeatureProvider() {
return mDisplayFeatureProvider;
}
@Override
public AudioSharingFeatureProvider getAudioSharingFeatureProvider() {
return mAudioSharingFeatureProvider;
}
}