From ab6c7a8d978fe1a748525a39103b8e6316ced0c6 Mon Sep 17 00:00:00 2001 From: Yiyi Shen Date: Tue, 21 Jan 2025 15:41:34 +0800 Subject: [PATCH] [Audiosharing] Use getBroadcastToUnicastFallbackGroup to get primary Flag: com.android.settingslib.flags.adopt_primary_group_management_api_v2 Test: atest Bug: 397568136 Change-Id: I50487a8e5948fec6d8e71e2a0cf499cc7f0cf59d --- ...oSharingCallAudioPreferenceController.java | 5 +- ...dioSharingDeviceVolumeGroupController.java | 3 +- .../AudioSharingDeviceVolumePreference.java | 9 +- ...ringCallAudioPreferenceControllerTest.java | 310 ++++++++++++++++-- ...haringDeviceVolumeGroupControllerTest.java | 69 +++- ...udioSharingDeviceVolumePreferenceTest.java | 75 +++++ 6 files changed, 416 insertions(+), 55 deletions(-) diff --git a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingCallAudioPreferenceController.java b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingCallAudioPreferenceController.java index 0c8b16bfab6..be861a66dd9 100644 --- a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingCallAudioPreferenceController.java +++ b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingCallAudioPreferenceController.java @@ -207,7 +207,7 @@ public class AudioSharingCallAudioPreferenceController extends AudioSharingBaseP (AudioSharingDeviceItem item) -> { int currentCallAudioGroupId = BluetoothUtils.getPrimaryGroupIdForBroadcast( - mContext.getContentResolver()); + mContext.getContentResolver(), mBtManager); int clickedGroupId = item.getGroupId(); if (clickedGroupId == currentCallAudioGroupId) { Log.d(TAG, "Skip set call audio device: unchanged"); @@ -414,7 +414,8 @@ public class AudioSharingCallAudioPreferenceController extends AudioSharingBaseP private Pair getActiveItemWithIndex() { List deviceItems = new ArrayList<>(mDeviceItemsInSharingSession); int fallbackActiveGroupId = - BluetoothUtils.getPrimaryGroupIdForBroadcast(mContext.getContentResolver()); + BluetoothUtils.getPrimaryGroupIdForBroadcast(mContext.getContentResolver(), + mBtManager); if (fallbackActiveGroupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID) { for (AudioSharingDeviceItem item : deviceItems) { if (item.getGroupId() == fallbackActiveGroupId) { diff --git a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDeviceVolumeGroupController.java b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDeviceVolumeGroupController.java index 7b670a80fba..1659d2da2da 100644 --- a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDeviceVolumeGroupController.java +++ b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDeviceVolumeGroupController.java @@ -418,7 +418,8 @@ public class AudioSharingDeviceVolumeGroupController extends AudioSharingBasePre int groupId = BluetoothUtils.getGroupId(cachedDevice); // The fallback device rank first among the audio sharing device list. return (groupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID - && groupId == BluetoothUtils.getPrimaryGroupIdForBroadcast(mContentResolver)) + && groupId == BluetoothUtils.getPrimaryGroupIdForBroadcast(mContentResolver, + mBtManager)) ? 0 : 1; } diff --git a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDeviceVolumePreference.java b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDeviceVolumePreference.java index 816ec6e2cd3..944ac25941b 100644 --- a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDeviceVolumePreference.java +++ b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDeviceVolumePreference.java @@ -46,6 +46,7 @@ public class AudioSharingDeviceVolumePreference extends SeekBarPreference { private final Context mContext; private final CachedBluetoothDevice mCachedDevice; + @Nullable private final LocalBluetoothManager mBtManager; @Nullable protected SeekBar mSeekBar; private Boolean mTrackingTouch = false; private MetricsFeatureProvider mMetricsFeatureProvider = @@ -57,6 +58,7 @@ public class AudioSharingDeviceVolumePreference extends SeekBarPreference { setLayoutResource(R.layout.preference_volume_slider); mContext = context; mCachedDevice = device; + mBtManager = Utils.getLocalBtManager(mContext); } @NonNull @@ -110,7 +112,7 @@ public class AudioSharingDeviceVolumePreference extends SeekBarPreference { if (groupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID && groupId == BluetoothUtils.getPrimaryGroupIdForBroadcast( - mContext.getContentResolver())) { + mContext.getContentResolver(), mBtManager)) { // Set media stream volume for primary buds, audio manager will // update all buds volume in the audio sharing. setAudioManagerStreamVolume(progress); @@ -126,9 +128,8 @@ public class AudioSharingDeviceVolumePreference extends SeekBarPreference { Log.d(TAG, "Skip set device volume, device is null"); return; } - LocalBluetoothManager btManager = Utils.getLocalBtManager(mContext); - VolumeControlProfile vc = - btManager == null ? null : btManager.getProfileManager().getVolumeControlProfile(); + VolumeControlProfile vc = mBtManager == null ? null + : mBtManager.getProfileManager().getVolumeControlProfile(); if (vc != null) { vc.setDeviceVolume(device, progress, /* isGroupOp= */ true); mMetricsFeatureProvider.action( diff --git a/tests/robotests/src/com/android/settings/connecteddevice/audiosharing/AudioSharingCallAudioPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/connecteddevice/audiosharing/AudioSharingCallAudioPreferenceControllerTest.java index 4f6fed7b732..7fece8bb2d3 100644 --- a/tests/robotests/src/com/android/settings/connecteddevice/audiosharing/AudioSharingCallAudioPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/connecteddevice/audiosharing/AudioSharingCallAudioPreferenceControllerTest.java @@ -43,6 +43,8 @@ import android.content.ContentResolver; import android.content.Context; import android.database.ContentObserver; import android.os.Looper; +import android.platform.test.annotations.DisableFlags; +import android.platform.test.annotations.EnableFlags; import android.platform.test.flag.junit.SetFlagsRule; import android.provider.Settings; import android.view.View; @@ -78,6 +80,7 @@ import com.android.settingslib.flags.Flags; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; import org.junit.After; import org.junit.Before; @@ -113,9 +116,6 @@ public class AudioSharingCallAudioPreferenceControllerTest { private static final int TEST_DEVICE_GROUP_ID1 = 1; private static final int TEST_DEVICE_GROUP_ID2 = 2; - private static final String TEST_SETTINGS_KEY = - "bluetooth_le_broadcast_fallback_active_group_id"; - @Rule public final MockitoRule mMockitoRule = MockitoJUnit.rule(); @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); @@ -125,6 +125,7 @@ public class AudioSharingCallAudioPreferenceControllerTest { @Mock private BluetoothEventManager mBtEventManager; @Mock private LocalBluetoothProfileManager mBtProfileManager; @Mock private CachedBluetoothDeviceManager mCacheManager; + @Mock private LeAudioProfile mLeaProfile; @Mock private LocalBluetoothLeBroadcast mBroadcast; @Mock private LocalBluetoothLeBroadcastAssistant mAssistant; @Mock private VolumeControlProfile mVolumeControl; @@ -169,6 +170,7 @@ public class AudioSharingCallAudioPreferenceControllerTest { when(btManager.getEventManager()).thenReturn(mBtEventManager); when(btManager.getProfileManager()).thenReturn(mBtProfileManager); when(btManager.getCachedDeviceManager()).thenReturn(mCacheManager); + when(mBtProfileManager.getLeAudioProfile()).thenReturn(mLeaProfile); when(mBtProfileManager.getLeAudioBroadcastProfile()).thenReturn(mBroadcast); when(mBtProfileManager.getLeAudioBroadcastAssistantProfile()).thenReturn(mAssistant); when(mBtProfileManager.getVolumeControlProfile()).thenReturn(mVolumeControl); @@ -210,8 +212,8 @@ public class AudioSharingCallAudioPreferenceControllerTest { } @Test + @DisableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) public void onStart_flagOff_doNothing() { - mSetFlagsRule.disableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); mController.onStart(mLifecycleOwner); verify(mBtEventManager, never()).registerCallback(mController); verify(mContentResolver, never()) @@ -225,8 +227,8 @@ public class AudioSharingCallAudioPreferenceControllerTest { } @Test + @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) public void onStart_flagOn_registerCallback() { - mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); mController.onStart(mLifecycleOwner); verify(mBtEventManager).registerCallback(mController); verify(mContentResolver) @@ -240,8 +242,8 @@ public class AudioSharingCallAudioPreferenceControllerTest { } @Test + @DisableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) public void onStop_flagOff_doNothing() { - mSetFlagsRule.disableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); mController.setCallbacksRegistered(true); mController.onStop(mLifecycleOwner); verify(mBtEventManager, never()).unregisterCallback(mController); @@ -251,8 +253,8 @@ public class AudioSharingCallAudioPreferenceControllerTest { } @Test + @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) public void onStop_flagOn_notRegistered_doNothing() { - mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); mController.setCallbacksRegistered(false); mController.onStop(mLifecycleOwner); verify(mBtEventManager, never()).unregisterCallback(mController); @@ -262,8 +264,8 @@ public class AudioSharingCallAudioPreferenceControllerTest { } @Test + @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) public void onStop_flagOn_registered_unregisterCallback() { - mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); mController.setCallbacksRegistered(true); mController.onStop(mLifecycleOwner); verify(mBtEventManager).unregisterCallback(mController); @@ -273,20 +275,20 @@ public class AudioSharingCallAudioPreferenceControllerTest { } @Test + @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) public void getAvailabilityStatus_flagOn() { - mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); } @Test + @DisableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) public void getAvailabilityStatus_flagOff() { - mSetFlagsRule.disableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE); } @Test + @DisableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) public void updateVisibility_flagOff_invisible() { - mSetFlagsRule.disableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); when(mBroadcast.isEnabled(any())).thenReturn(true); mController.displayPreference(mScreen); mController.updateVisibility(); @@ -295,8 +297,8 @@ public class AudioSharingCallAudioPreferenceControllerTest { } @Test + @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) public void updateVisibility_broadcastOffBluetoothOff_invisible() { - mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); when(mBroadcast.isEnabled(any())).thenReturn(false); mShadowBluetoothAdapter.setEnabled(false); mController.displayPreference(mScreen); @@ -306,8 +308,8 @@ public class AudioSharingCallAudioPreferenceControllerTest { } @Test + @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) public void updateVisibility_broadcastOnBluetoothOff_invisible() { - mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); when(mBroadcast.isEnabled(any())).thenReturn(true); mShadowBluetoothAdapter.setEnabled(false); mController.displayPreference(mScreen); @@ -317,8 +319,8 @@ public class AudioSharingCallAudioPreferenceControllerTest { } @Test + @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) public void updateVisibility_broadcastOffBluetoothOn_invisible() { - mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); when(mBroadcast.isEnabled(any())).thenReturn(false); mController.displayPreference(mScreen); mController.updateVisibility(); @@ -327,8 +329,8 @@ public class AudioSharingCallAudioPreferenceControllerTest { } @Test + @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) public void updateVisibility_broadcastOnBluetoothOn_visible() { - mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); when(mBroadcast.isEnabled(any())).thenReturn(true); mController.displayPreference(mScreen); mController.updateVisibility(); @@ -337,8 +339,9 @@ public class AudioSharingCallAudioPreferenceControllerTest { } @Test - public void onProfileConnectionStateChanged_noDeviceInSharing_updateSummary() { - Settings.Secure.putInt(mContentResolver, TEST_SETTINGS_KEY, TEST_DEVICE_GROUP_ID1); + @EnableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) + public void onProfileConnectionStateChanged_adoptApi_noDeviceInSharing_updateSummary() { + when(mLeaProfile.getBroadcastToUnicastFallbackGroup()).thenReturn(TEST_DEVICE_GROUP_ID1); when(mBroadcast.isEnabled(any())).thenReturn(true); when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of()); mController.displayPreference(mScreen); @@ -354,8 +357,9 @@ public class AudioSharingCallAudioPreferenceControllerTest { } @Test - public void onFallbackDeviceChanged_updateSummary() { - Settings.Secure.putInt(mContentResolver, TEST_SETTINGS_KEY, TEST_DEVICE_GROUP_ID1); + @EnableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) + public void onFallbackDeviceChanged_adoptApi_updateSummary() { + when(mLeaProfile.getBroadcastToUnicastFallbackGroup()).thenReturn(TEST_DEVICE_GROUP_ID1); when(mBroadcast.isEnabled(any())).thenReturn(true); when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice1)); when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of(mState)); @@ -372,8 +376,9 @@ public class AudioSharingCallAudioPreferenceControllerTest { } @Test - public void onActiveDeviceChanged_updateSummary() { - Settings.Secure.putInt(mContentResolver, TEST_SETTINGS_KEY, + @EnableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) + public void onActiveDeviceChanged_adoptApi_updateSummary() { + when(mLeaProfile.getBroadcastToUnicastFallbackGroup()).thenReturn( BluetoothCsipSetCoordinator.GROUP_ID_INVALID); when(mCachedDevice1.isActiveDevice(BluetoothProfile.LE_AUDIO)).thenReturn(true); when(mBroadcast.isEnabled(any())).thenReturn(true); @@ -392,8 +397,9 @@ public class AudioSharingCallAudioPreferenceControllerTest { } @Test - public void displayPreference_fallbackDeviceInSharing_showCorrectSummary() { - Settings.Secure.putInt(mContentResolver, TEST_SETTINGS_KEY, TEST_DEVICE_GROUP_ID1); + @EnableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) + public void displayPreference_adoptApi_fallbackDeviceInSharing_showCorrectSummary() { + when(mLeaProfile.getBroadcastToUnicastFallbackGroup()).thenReturn(TEST_DEVICE_GROUP_ID1); when(mCachedDevice3.isActiveDevice(BluetoothProfile.LE_AUDIO)).thenReturn(true); when(mBroadcast.isEnabled(any())).thenReturn(true); when(mAssistant.getAllConnectedDevices()) @@ -408,8 +414,9 @@ public class AudioSharingCallAudioPreferenceControllerTest { } @Test - public void displayPreference_activeDeviceInSharing_showCorrectSummary() { - Settings.Secure.putInt(mContentResolver, TEST_SETTINGS_KEY, TEST_DEVICE_GROUP_ID2); + @EnableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) + public void displayPreference_adoptApi_activeDeviceInSharing_showCorrectSummary() { + when(mLeaProfile.getBroadcastToUnicastFallbackGroup()).thenReturn(TEST_DEVICE_GROUP_ID2); when(mCachedDevice1.isActiveDevice(BluetoothProfile.LE_AUDIO)).thenReturn(true); when(mBroadcast.isEnabled(any())).thenReturn(true); when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice1, mDevice2)); @@ -422,8 +429,9 @@ public class AudioSharingCallAudioPreferenceControllerTest { } @Test - public void displayPreference_noFallbackDeviceOrActiveInSharing_showEmptySummary() { - Settings.Secure.putInt(mContentResolver, TEST_SETTINGS_KEY, TEST_DEVICE_GROUP_ID2); + @EnableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) + public void displayPreference_adoptApi_noFallbackDeviceOrActiveInSharing_showEmptySummary() { + when(mLeaProfile.getBroadcastToUnicastFallbackGroup()).thenReturn(TEST_DEVICE_GROUP_ID2); when(mBroadcast.isEnabled(any())).thenReturn(true); when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice1, mDevice2)); when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of(mState)); @@ -433,9 +441,10 @@ public class AudioSharingCallAudioPreferenceControllerTest { } @Test - public void displayPreference_noFallbackOrActiveDevice_showEmptySummary() { - Settings.Secure.putInt( - mContentResolver, TEST_SETTINGS_KEY, BluetoothCsipSetCoordinator.GROUP_ID_INVALID); + @EnableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) + public void displayPreference_adoptApi_noFallbackOrActiveDevice_showEmptySummary() { + when(mLeaProfile.getBroadcastToUnicastFallbackGroup()).thenReturn( + BluetoothCsipSetCoordinator.GROUP_ID_INVALID); when(mBroadcast.isEnabled(any())).thenReturn(true); when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of()); mController.displayPreference(mScreen); @@ -444,13 +453,235 @@ public class AudioSharingCallAudioPreferenceControllerTest { } @Test + @EnableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) + public void displayPreference_adoptApi_clickToShowCorrectDialog() { + AlertDialog latestAlertDialog = ShadowAlertDialogCompat.getLatestAlertDialog(); + if (latestAlertDialog != null) { + latestAlertDialog.dismiss(); + ShadowAlertDialogCompat.reset(); + } + when(mLeaProfile.getBroadcastToUnicastFallbackGroup()).thenReturn(TEST_DEVICE_GROUP_ID1); + mShadowBluetoothAdapter.setMostRecentlyConnectedDevices( + List.of(mDevice1, mDevice2, mDevice3)); + when(mBroadcast.isEnabled(any())).thenReturn(true); + when(mAssistant.getAllConnectedDevices()).thenReturn( + ImmutableList.of(mDevice1, mDevice2, mDevice3)); + when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of(mState)); + mController.init(mParentFragment); + mController.displayPreference(mScreen); + shadowOf(Looper.getMainLooper()).idle(); + mPreference.performClick(); + shadowOf(Looper.getMainLooper()).idle(); + AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog(); + assertThat(dialog.isShowing()).isTrue(); + assertThat(dialog.getListView().getCount()).isEqualTo(2); + ArrayList outViews = new ArrayList<>(); + dialog.getListView() + .findViewsWithText(outViews, TEST_DEVICE_NAME1, View.FIND_VIEWS_WITH_TEXT); + assertThat(outViews.size()).isEqualTo(1); + View view = Iterables.getOnlyElement(outViews); + assertThat(view instanceof CheckedTextView).isTrue(); + assertThat(((CheckedTextView) view).isChecked()).isTrue(); + verify(mFeatureFactory.metricsFeatureProvider) + .visible( + /* context= */ eq(null), + /* source= */ anyInt(), + eq(SettingsEnums.DIALOG_AUDIO_SHARING_CALL_AUDIO), + /* latency= */ anyInt()); + } + + @Test + @EnableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) + public void testBluetoothLeBroadcastAssistantCallbacks_adoptApi_updateSummary() { + when(mLeaProfile.getBroadcastToUnicastFallbackGroup()).thenReturn( + BluetoothCsipSetCoordinator.GROUP_ID_INVALID); + when(mBroadcast.isEnabled(any())).thenReturn(true); + when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of()); + mController.displayPreference(mScreen); + shadowOf(Looper.getMainLooper()).idle(); + assertThat(mPreference.getSummary().toString()).isEmpty(); + + // onSourceAdded will update summary + when(mLeaProfile.getBroadcastToUnicastFallbackGroup()).thenReturn(TEST_DEVICE_GROUP_ID1); + when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice1)); + when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of(mState)); + mController.mBroadcastAssistantCallback.onSourceAdded(mDevice1, /* sourceId= */ + 1, /* reason= */ 1); + shadowOf(Looper.getMainLooper()).idle(); + assertThat(mPreference.getSummary().toString()) + .isEqualTo( + mContext.getString( + R.string.audio_sharing_call_audio_description, TEST_DEVICE_NAME1)); + } + + @Test + @EnableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) + public void testBluetoothLeBroadcastAssistantCallbacks_adoptApi_doNothing() { + when(mLeaProfile.getBroadcastToUnicastFallbackGroup()).thenReturn( + BluetoothCsipSetCoordinator.GROUP_ID_INVALID); + when(mBroadcast.isEnabled(any())).thenReturn(true); + when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of()); + mController.displayPreference(mScreen); + shadowOf(Looper.getMainLooper()).idle(); + assertThat(mPreference.getSummary().toString()).isEmpty(); + + when(mLeaProfile.getBroadcastToUnicastFallbackGroup()).thenReturn(TEST_DEVICE_GROUP_ID1); + when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice1)); + when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of(mState)); + mController.mBroadcastAssistantCallback.onSearchStarted(/* reason= */ 1); + mController.mBroadcastAssistantCallback.onSearchStartFailed(/* reason= */ 1); + mController.mBroadcastAssistantCallback.onSearchStopped(/* reason= */ 1); + mController.mBroadcastAssistantCallback.onSearchStopFailed(/* reason= */ 1); + mController.mBroadcastAssistantCallback.onSourceAddFailed( + mDevice1, mSource, /* reason= */ 1); + mController.mBroadcastAssistantCallback.onSourceRemoved( + mDevice1, /* sourceId= */ 1, /* reason= */ 1); + mController.mBroadcastAssistantCallback.onSourceRemoveFailed( + mDevice1, /* sourceId= */ 1, /* reason= */ 1); + mController.mBroadcastAssistantCallback.onSourceModified( + mDevice1, /* sourceId= */ 1, /* reason= */ 1); + mController.mBroadcastAssistantCallback.onSourceModifyFailed( + mDevice1, /* sourceId= */ 1, /* reason= */ 1); + mController.mBroadcastAssistantCallback.onSourceFound(mSource); + mController.mBroadcastAssistantCallback.onSourceLost(/* broadcastId= */ 1); + shadowOf(Looper.getMainLooper()).idle(); + mController.mBroadcastAssistantCallback.onReceiveStateChanged(mDevice1, /* sourceId= */ 1, + mState); + + // Above callbacks won't update summary. + assertThat(mPreference.getSummary().toString()).isEmpty(); + } + + @Test + @DisableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) + public void onProfileConnectionStateChanged_noDeviceInSharing_updateSummary() { + Settings.Secure.putInt(mContentResolver, BluetoothUtils.getPrimaryGroupIdUriForBroadcast(), + TEST_DEVICE_GROUP_ID1); + when(mBroadcast.isEnabled(any())).thenReturn(true); + when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of()); + mController.displayPreference(mScreen); + shadowOf(Looper.getMainLooper()).idle(); + mPreference.setSummary("test"); + + mController.onProfileConnectionStateChanged( + mCachedDevice1, + BluetoothAdapter.STATE_DISCONNECTED, + BluetoothProfile.LE_AUDIO_BROADCAST_ASSISTANT); + shadowOf(Looper.getMainLooper()).idle(); + assertThat(mPreference.getSummary().toString()).isEmpty(); + } + + @Test + @DisableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) + public void onFallbackDeviceChanged_updateSummary() { + Settings.Secure.putInt(mContentResolver, BluetoothUtils.getPrimaryGroupIdUriForBroadcast(), + TEST_DEVICE_GROUP_ID1); + when(mBroadcast.isEnabled(any())).thenReturn(true); + when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice1)); + when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of(mState)); + mController.displayPreference(mScreen); + shadowOf(Looper.getMainLooper()).idle(); + mPreference.setSummary("test"); + + mContentObserver.onChange(true); + shadowOf(Looper.getMainLooper()).idle(); + assertThat(mPreference.getSummary().toString()) + .isEqualTo( + mContext.getString( + R.string.audio_sharing_call_audio_description, TEST_DEVICE_NAME1)); + } + + @Test + @DisableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) + public void onActiveDeviceChanged_updateSummary() { + Settings.Secure.putInt(mContentResolver, BluetoothUtils.getPrimaryGroupIdUriForBroadcast(), + BluetoothCsipSetCoordinator.GROUP_ID_INVALID); + when(mCachedDevice1.isActiveDevice(BluetoothProfile.LE_AUDIO)).thenReturn(true); + when(mBroadcast.isEnabled(any())).thenReturn(true); + when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice1)); + when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of(mState)); + mController.displayPreference(mScreen); + shadowOf(Looper.getMainLooper()).idle(); + mPreference.setSummary("test"); + + mController.onActiveDeviceChanged(mCachedDevice1, BluetoothProfile.LE_AUDIO); + shadowOf(Looper.getMainLooper()).idle(); + assertThat(mPreference.getSummary().toString()) + .isEqualTo( + mContext.getString( + R.string.audio_sharing_call_audio_description, TEST_DEVICE_NAME1)); + } + + @Test + @DisableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) + public void displayPreference_fallbackDeviceInSharing_showCorrectSummary() { + Settings.Secure.putInt(mContentResolver, BluetoothUtils.getPrimaryGroupIdUriForBroadcast(), + TEST_DEVICE_GROUP_ID1); + when(mCachedDevice3.isActiveDevice(BluetoothProfile.LE_AUDIO)).thenReturn(true); + when(mBroadcast.isEnabled(any())).thenReturn(true); + when(mAssistant.getAllConnectedDevices()) + .thenReturn(ImmutableList.of(mDevice1, mDevice2, mDevice3)); + when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of(mState)); + mController.displayPreference(mScreen); + shadowOf(Looper.getMainLooper()).idle(); + assertThat(mPreference.getSummary().toString()) + .isEqualTo( + mContext.getString( + R.string.audio_sharing_call_audio_description, TEST_DEVICE_NAME1)); + } + + @Test + @DisableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) + public void displayPreference_activeDeviceInSharing_showCorrectSummary() { + Settings.Secure.putInt(mContentResolver, BluetoothUtils.getPrimaryGroupIdUriForBroadcast(), + TEST_DEVICE_GROUP_ID2); + when(mCachedDevice1.isActiveDevice(BluetoothProfile.LE_AUDIO)).thenReturn(true); + when(mBroadcast.isEnabled(any())).thenReturn(true); + when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice1, mDevice2)); + when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of(mState)); + mController.displayPreference(mScreen); + shadowOf(Looper.getMainLooper()).idle(); + assertThat(mPreference.getSummary().toString()) + .isEqualTo(mContext.getString( + R.string.audio_sharing_call_audio_description, TEST_DEVICE_NAME1)); + } + + @Test + @DisableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) + public void displayPreference_noFallbackDeviceOrActiveInSharing_showEmptySummary() { + Settings.Secure.putInt(mContentResolver, BluetoothUtils.getPrimaryGroupIdUriForBroadcast(), + TEST_DEVICE_GROUP_ID2); + when(mBroadcast.isEnabled(any())).thenReturn(true); + when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice1, mDevice2)); + when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of(mState)); + mController.displayPreference(mScreen); + shadowOf(Looper.getMainLooper()).idle(); + assertThat(mPreference.getSummary().toString()).isEmpty(); + } + + @Test + @DisableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) + public void displayPreference_noFallbackOrActiveDevice_showEmptySummary() { + Settings.Secure.putInt( + mContentResolver, BluetoothUtils.getPrimaryGroupIdUriForBroadcast(), + BluetoothCsipSetCoordinator.GROUP_ID_INVALID); + when(mBroadcast.isEnabled(any())).thenReturn(true); + when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of()); + mController.displayPreference(mScreen); + shadowOf(Looper.getMainLooper()).idle(); + assertThat(mPreference.getSummary().toString()).isEmpty(); + } + + @Test + @DisableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) public void displayPreference_clickToShowCorrectDialog() { AlertDialog latestAlertDialog = ShadowAlertDialogCompat.getLatestAlertDialog(); if (latestAlertDialog != null) { latestAlertDialog.dismiss(); ShadowAlertDialogCompat.reset(); } - Settings.Secure.putInt(mContentResolver, TEST_SETTINGS_KEY, TEST_DEVICE_GROUP_ID1); + Settings.Secure.putInt(mContentResolver, BluetoothUtils.getPrimaryGroupIdUriForBroadcast(), + TEST_DEVICE_GROUP_ID1); mShadowBluetoothAdapter.setMostRecentlyConnectedDevices( List.of(mDevice1, mDevice2, mDevice3)); when(mBroadcast.isEnabled(any())).thenReturn(true); @@ -497,7 +728,8 @@ public class AudioSharingCallAudioPreferenceControllerTest { // Perform click to switch call audio device with API mSetFlagsRule.enableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API); - Settings.Secure.putInt(mContentResolver, TEST_SETTINGS_KEY, TEST_DEVICE_GROUP_ID2); + Settings.Secure.putInt(mContentResolver, BluetoothUtils.getPrimaryGroupIdUriForBroadcast(), + TEST_DEVICE_GROUP_ID2); index = listView.findIndexOfItemContainingText(TEST_DEVICE_NAME1); listView.performItemClick(index); shadowOf(Looper.getMainLooper()).idle(); @@ -548,9 +780,11 @@ public class AudioSharingCallAudioPreferenceControllerTest { } @Test + @DisableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) public void testBluetoothLeBroadcastAssistantCallbacks_updateSummary() { Settings.Secure.putInt( - mContentResolver, TEST_SETTINGS_KEY, BluetoothCsipSetCoordinator.GROUP_ID_INVALID); + mContentResolver, BluetoothUtils.getPrimaryGroupIdUriForBroadcast(), + BluetoothCsipSetCoordinator.GROUP_ID_INVALID); when(mBroadcast.isEnabled(any())).thenReturn(true); when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of()); mController.displayPreference(mScreen); @@ -558,7 +792,8 @@ public class AudioSharingCallAudioPreferenceControllerTest { assertThat(mPreference.getSummary().toString()).isEmpty(); // onSourceAdded will update summary - Settings.Secure.putInt(mContentResolver, TEST_SETTINGS_KEY, TEST_DEVICE_GROUP_ID1); + Settings.Secure.putInt(mContentResolver, BluetoothUtils.getPrimaryGroupIdUriForBroadcast(), + TEST_DEVICE_GROUP_ID1); when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice1)); when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of(mState)); mController.mBroadcastAssistantCallback.onSourceAdded(mDevice1, /* sourceId= */ @@ -571,16 +806,19 @@ public class AudioSharingCallAudioPreferenceControllerTest { } @Test + @DisableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) public void testBluetoothLeBroadcastAssistantCallbacks_doNothing() { Settings.Secure.putInt( - mContentResolver, TEST_SETTINGS_KEY, BluetoothCsipSetCoordinator.GROUP_ID_INVALID); + mContentResolver, BluetoothUtils.getPrimaryGroupIdUriForBroadcast(), + BluetoothCsipSetCoordinator.GROUP_ID_INVALID); when(mBroadcast.isEnabled(any())).thenReturn(true); when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of()); mController.displayPreference(mScreen); shadowOf(Looper.getMainLooper()).idle(); assertThat(mPreference.getSummary().toString()).isEmpty(); - Settings.Secure.putInt(mContentResolver, TEST_SETTINGS_KEY, TEST_DEVICE_GROUP_ID1); + Settings.Secure.putInt(mContentResolver, BluetoothUtils.getPrimaryGroupIdUriForBroadcast(), + TEST_DEVICE_GROUP_ID1); when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice1)); when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of(mState)); mController.mBroadcastAssistantCallback.onSearchStarted(/* reason= */ 1); diff --git a/tests/robotests/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDeviceVolumeGroupControllerTest.java b/tests/robotests/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDeviceVolumeGroupControllerTest.java index a739bb3b4fb..6ca03067a15 100644 --- a/tests/robotests/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDeviceVolumeGroupControllerTest.java +++ b/tests/robotests/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDeviceVolumeGroupControllerTest.java @@ -23,6 +23,7 @@ import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; @@ -42,6 +43,8 @@ import android.content.Context; import android.database.ContentObserver; import android.media.AudioManager; import android.os.Looper; +import android.platform.test.annotations.DisableFlags; +import android.platform.test.annotations.EnableFlags; import android.platform.test.flag.junit.SetFlagsRule; import android.provider.Settings; @@ -60,6 +63,7 @@ import com.android.settings.testutils.shadow.ShadowThreadUtils; import com.android.settingslib.bluetooth.BluetoothUtils; import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager; +import com.android.settingslib.bluetooth.LeAudioProfile; import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast; import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant; import com.android.settingslib.bluetooth.LocalBluetoothManager; @@ -196,8 +200,8 @@ public class AudioSharingDeviceVolumeGroupControllerTest { } @Test + @DisableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) public void onStart_flagOff_doNothing() { - mSetFlagsRule.disableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); mController.onStart(mLifecycleOwner); verify(mAssistant, never()) .registerServiceCallBack( @@ -214,8 +218,8 @@ public class AudioSharingDeviceVolumeGroupControllerTest { } @Test + @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) public void onStart_flagOn_registerCallbacks() { - mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); mController.onStart(mLifecycleOwner); verify(mAssistant) .registerServiceCallBack( @@ -229,8 +233,8 @@ public class AudioSharingDeviceVolumeGroupControllerTest { } @Test + @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) public void onAudioSharingProfilesConnected_flagOn_registerCallbacks() { - mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); mController.onAudioSharingProfilesConnected(); verify(mAssistant) .registerServiceCallBack( @@ -247,8 +251,8 @@ public class AudioSharingDeviceVolumeGroupControllerTest { } @Test + @DisableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) public void onStop_flagOff_doNothing() { - mSetFlagsRule.disableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); mController.onStop(mLifecycleOwner); verify(mAssistant, never()) .unregisterServiceCallBack(any(BluetoothLeBroadcastAssistant.Callback.class)); @@ -259,8 +263,8 @@ public class AudioSharingDeviceVolumeGroupControllerTest { } @Test + @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) public void onStop_flagOn_callbacksNotRegistered_doNothing() { - mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); mController.setCallbacksRegistered(false); mController.onStop(mLifecycleOwner); verify(mAssistant, never()) @@ -272,8 +276,8 @@ public class AudioSharingDeviceVolumeGroupControllerTest { } @Test + @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) public void onStop_flagOn_callbacksRegistered_unregisterCallbacks() { - mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); mController.setCallbacksRegistered(true); mController.onStop(mLifecycleOwner); verify(mAssistant) @@ -284,16 +288,16 @@ public class AudioSharingDeviceVolumeGroupControllerTest { } @Test + @DisableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) public void displayPreference_flagOff_doNothing() { - mSetFlagsRule.disableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); mController.displayPreference(mScreen); assertThat(mPreferenceGroup.isVisible()).isFalse(); verify(mDeviceUpdater, never()).forceUpdate(); } @Test + @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) public void displayPreference_flagOn_updateDeviceList() { - mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); mController.displayPreference(mScreen); assertThat(mPreferenceGroup.isVisible()).isFalse(); verify(mDeviceUpdater).forceUpdate(); @@ -316,6 +320,24 @@ public class AudioSharingDeviceVolumeGroupControllerTest { } @Test + @EnableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) + public void onDeviceAdded_adoptApi_rankFallbackDeviceOnTop() { + LeAudioProfile leAudioProfile = mock(LeAudioProfile.class); + when(leAudioProfile.getBroadcastToUnicastFallbackGroup()).thenReturn(TEST_DEVICE_GROUP_ID2); + when(mProfileManager.getLeAudioProfile()).thenReturn(leAudioProfile); + when(mPreference1.getProgress()).thenReturn(TEST_VOLUME_VALUE); + when(mPreference2.getProgress()).thenReturn(TEST_VOLUME_VALUE); + mController.setPreferenceGroup(mPreferenceGroup); + mController.onDeviceAdded(mPreference1); + mController.onDeviceAdded(mPreference2); + shadowOf(Looper.getMainLooper()).idle(); + + verify(mPreference1).setOrder(1); + verify(mPreference2).setOrder(0); + } + + @Test + @DisableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) public void onDeviceAdded_rankFallbackDeviceOnTop() { Settings.Secure.putInt( mContentResolver, BluetoothUtils.getPrimaryGroupIdUriForBroadcast(), @@ -374,8 +396,8 @@ public class AudioSharingDeviceVolumeGroupControllerTest { } @Test + @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) public void updateVisibility_emptyPreferenceGroup_doNothing() { - mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); mController.setCallbacksRegistered(true); mController.updateVisibility(); shadowOf(Looper.getMainLooper()).idle(); @@ -384,8 +406,8 @@ public class AudioSharingDeviceVolumeGroupControllerTest { } @Test + @DisableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) public void updateVisibility_flagOff_setVisibleToFalse() { - mSetFlagsRule.disableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); mController.setCallbacksRegistered(true); mPreferenceGroup.addPreference(mPreference1); when(mBroadcast.isEnabled(null)).thenReturn(true); @@ -399,8 +421,8 @@ public class AudioSharingDeviceVolumeGroupControllerTest { } @Test + @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) public void updateVisibility_notEmptyPreferenceGroup_noSharing_setVisibleToFalse() { - mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); mController.setCallbacksRegistered(true); mPreferenceGroup.addPreference(mPreference1); when(mBroadcast.isEnabled(null)).thenReturn(false); @@ -414,8 +436,8 @@ public class AudioSharingDeviceVolumeGroupControllerTest { } @Test + @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) public void updateVisibility_notEmptyPreferenceGroup_isSharing_setVisibleToTrue() { - mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); mController.setCallbacksRegistered(true); mPreferenceGroup.addPreference(mPreference1); when(mBroadcast.isEnabled(null)).thenReturn(true); @@ -429,6 +451,29 @@ public class AudioSharingDeviceVolumeGroupControllerTest { } @Test + @EnableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) + public void settingsObserverOnChange_adoptApi_updatePreferenceOrder() { + LeAudioProfile leAudioProfile = mock(LeAudioProfile.class); + when(leAudioProfile.getBroadcastToUnicastFallbackGroup()).thenReturn(TEST_DEVICE_GROUP_ID2); + when(mProfileManager.getLeAudioProfile()).thenReturn(leAudioProfile); + when(mPreference1.getProgress()).thenReturn(TEST_VOLUME_VALUE); + when(mPreference2.getProgress()).thenReturn(TEST_VOLUME_VALUE); + mController.setPreferenceGroup(mPreferenceGroup); + mController.onDeviceAdded(mPreference1); + mController.onDeviceAdded(mPreference2); + shadowOf(Looper.getMainLooper()).idle(); + + + when(leAudioProfile.getBroadcastToUnicastFallbackGroup()).thenReturn(TEST_DEVICE_GROUP_ID1); + mContentObserver.onChange(true); + shadowOf(Looper.getMainLooper()).idle(); + + verify(mPreference1).setOrder(0); + verify(mPreference2).setOrder(1); + } + + @Test + @DisableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) public void settingsObserverOnChange_updatePreferenceOrder() { Settings.Secure.putInt( mContentResolver, BluetoothUtils.getPrimaryGroupIdUriForBroadcast(), diff --git a/tests/robotests/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDeviceVolumePreferenceTest.java b/tests/robotests/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDeviceVolumePreferenceTest.java index 5ff143fa1dc..c84d7f8184d 100644 --- a/tests/robotests/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDeviceVolumePreferenceTest.java +++ b/tests/robotests/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDeviceVolumePreferenceTest.java @@ -31,6 +31,9 @@ import android.app.settings.SettingsEnums; import android.bluetooth.BluetoothDevice; import android.content.Context; import android.media.AudioManager; +import android.platform.test.annotations.DisableFlags; +import android.platform.test.annotations.EnableFlags; +import android.platform.test.flag.junit.SetFlagsRule; import android.provider.Settings; import android.widget.SeekBar; @@ -41,9 +44,11 @@ import com.android.settings.testutils.FakeFeatureFactory; import com.android.settings.testutils.shadow.ShadowBluetoothUtils; import com.android.settingslib.bluetooth.BluetoothUtils; import com.android.settingslib.bluetooth.CachedBluetoothDevice; +import com.android.settingslib.bluetooth.LeAudioProfile; import com.android.settingslib.bluetooth.LocalBluetoothManager; import com.android.settingslib.bluetooth.LocalBluetoothProfileManager; import com.android.settingslib.bluetooth.VolumeControlProfile; +import com.android.settingslib.flags.Flags; import org.junit.Before; import org.junit.Rule; @@ -64,9 +69,11 @@ public class AudioSharingDeviceVolumePreferenceTest { private static final int TEST_MIN_STREAM_VALUE = 0; @Rule public final MockitoRule mMockitoRule = MockitoJUnit.rule(); + @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); @Mock private LocalBluetoothManager mLocalBtManager; @Mock private LocalBluetoothProfileManager mLocalBtProfileManager; + @Mock private LeAudioProfile mLeAudioProfile; @Mock private VolumeControlProfile mVolumeControl; @Mock private CachedBluetoothDevice mCachedDevice; @Mock private BluetoothDevice mDevice; @@ -84,6 +91,7 @@ public class AudioSharingDeviceVolumePreferenceTest { mFeatureFactory = FakeFeatureFactory.setupForTest(); when(mLocalBtManager.getProfileManager()).thenReturn(mLocalBtProfileManager); when(mLocalBtProfileManager.getVolumeControlProfile()).thenReturn(mVolumeControl); + when(mLocalBtProfileManager.getLeAudioProfile()).thenReturn(mLeAudioProfile); when(mContext.getSystemService(AudioManager.class)).thenReturn(mAudioManager); when(mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)) .thenReturn(TEST_MAX_STREAM_VALUE); @@ -161,6 +169,70 @@ public class AudioSharingDeviceVolumePreferenceTest { } @Test + @EnableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) + public void onStopTrackingTouch_adoptApi_fallbackDevice_setDeviceVolume() { + when(mLeAudioProfile.getBroadcastToUnicastFallbackGroup()).thenReturn(TEST_DEVICE_GROUP_ID); + mPreference.onStopTrackingTouch(mSeekBar); + + verifyNoInteractions(mVolumeControl); + verify(mAudioManager) + .setStreamVolume(AudioManager.STREAM_MUSIC, TEST_MAX_STREAM_VALUE, /* flags= */ 0); + verify(mFeatureFactory.metricsFeatureProvider) + .action( + mContext, + SettingsEnums.ACTION_AUDIO_SHARING_CHANGE_MEDIA_DEVICE_VOLUME, + /* isPrimary= */ true); + } + + @Test + @EnableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) + public void onProgressChanged_adoptApi_fallbackDevice_fromUserNotInTouch_setDeviceVolume() { + when(mLeAudioProfile.getBroadcastToUnicastFallbackGroup()).thenReturn(TEST_DEVICE_GROUP_ID); + mPreference.onProgressChanged(mSeekBar, TEST_VOLUME_VALUE, /* fromUser= */ true); + + verifyNoInteractions(mVolumeControl); + verify(mAudioManager) + .setStreamVolume(AudioManager.STREAM_MUSIC, TEST_MAX_STREAM_VALUE, /* flags= */ 0); + verify(mFeatureFactory.metricsFeatureProvider) + .action( + mContext, + SettingsEnums.ACTION_AUDIO_SHARING_CHANGE_MEDIA_DEVICE_VOLUME, + /* isPrimary= */ true); + } + + @Test + @EnableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) + public void onProgressChanged_adoptApi_fallbackDevice_fromUserInTouch_doNothing() { + when(mLeAudioProfile.getBroadcastToUnicastFallbackGroup()).thenReturn(TEST_DEVICE_GROUP_ID); + mPreference.onStartTrackingTouch(mSeekBar); + mPreference.onProgressChanged(mSeekBar, TEST_VOLUME_VALUE, /* fromUser= */ true); + + verifyNoInteractions(mVolumeControl); + verifyNoInteractions(mAudioManager); + verify(mFeatureFactory.metricsFeatureProvider, never()) + .action( + any(Context.class), + eq(SettingsEnums.ACTION_AUDIO_SHARING_CHANGE_MEDIA_DEVICE_VOLUME), + anyBoolean()); + } + + @Test + @EnableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) + public void onProgressChanged_adoptApi_fallbackDevice_notFromUserNotInTouch_doNothing() { + when(mLeAudioProfile.getBroadcastToUnicastFallbackGroup()).thenReturn(TEST_DEVICE_GROUP_ID); + mPreference.onProgressChanged(mSeekBar, TEST_VOLUME_VALUE, /* fromUser= */ false); + + verifyNoInteractions(mVolumeControl); + verifyNoInteractions(mAudioManager); + verify(mFeatureFactory.metricsFeatureProvider, never()) + .action( + any(Context.class), + eq(SettingsEnums.ACTION_AUDIO_SHARING_CHANGE_MEDIA_DEVICE_VOLUME), + anyBoolean()); + } + + @Test + @DisableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) public void onStopTrackingTouch_fallbackDevice_setDeviceVolume() { Settings.Secure.putInt( mContext.getContentResolver(), @@ -179,6 +251,7 @@ public class AudioSharingDeviceVolumePreferenceTest { } @Test + @DisableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) public void onProgressChanged_fallbackDevice_fromUserNotInTouch_setDeviceVolume() { Settings.Secure.putInt( mContext.getContentResolver(), @@ -197,6 +270,7 @@ public class AudioSharingDeviceVolumePreferenceTest { } @Test + @DisableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) public void onProgressChanged_fallbackDevice_fromUserInTouch_doNothing() { Settings.Secure.putInt( mContext.getContentResolver(), @@ -215,6 +289,7 @@ public class AudioSharingDeviceVolumePreferenceTest { } @Test + @DisableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2) public void onProgressChanged_fallbackDevice_notFromUserNotInTouch_doNothing() { Settings.Secure.putInt( mContext.getContentResolver(),