1 /* 2 * Copyright (C) 2025 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 package com.android.settings.bluetooth; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import static org.mockito.Mockito.when; 21 22 import android.bluetooth.BluetoothAdapter; 23 import android.bluetooth.BluetoothLeBroadcastReceiveState; 24 import android.bluetooth.BluetoothProfile; 25 import android.bluetooth.BluetoothStatusCodes; 26 import android.platform.test.annotations.EnableFlags; 27 import android.platform.test.flag.junit.SetFlagsRule; 28 29 import androidx.preference.PreferenceCategory; 30 31 import com.android.settings.R; 32 import com.android.settings.connecteddevice.audiosharing.audiostreams.AudioStreamsHelper; 33 import com.android.settings.connecteddevice.audiosharing.audiostreams.testshadows.ShadowAudioStreamsHelper; 34 import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; 35 import com.android.settingslib.bluetooth.LocalBluetoothManager; 36 import com.android.settingslib.flags.Flags; 37 38 import org.junit.Rule; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.mockito.Answers; 42 import org.mockito.Mock; 43 import org.mockito.junit.MockitoJUnit; 44 import org.mockito.junit.MockitoRule; 45 import org.robolectric.RobolectricTestRunner; 46 import org.robolectric.annotation.Config; 47 import org.robolectric.shadow.api.Shadow; 48 49 import java.util.ArrayList; 50 import java.util.List; 51 52 @RunWith(RobolectricTestRunner.class) 53 @Config(shadows = {ShadowBluetoothAdapter.class, ShadowAudioStreamsHelper.class}) 54 public class BluetoothDetailsAudioSharingControllerTest extends BluetoothDetailsControllerTestBase { 55 @Rule public final MockitoRule mocks = MockitoJUnit.rule(); 56 @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); 57 58 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 59 private LocalBluetoothManager mLocalManager; 60 @Mock private AudioStreamsHelper mAudioStreamsHelper; 61 @Mock private BluetoothLeBroadcastReceiveState mBroadcastReceiveState; 62 63 private ShadowBluetoothAdapter mShadowBluetoothAdapter; 64 private BluetoothDetailsAudioSharingController mController; 65 private PreferenceCategory mContainer; 66 67 @Override setUp()68 public void setUp() { 69 super.setUp(); 70 mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter()); 71 ShadowAudioStreamsHelper.setUseMock(mAudioStreamsHelper); 72 mController = 73 new BluetoothDetailsAudioSharingController( 74 mContext, mFragment, mLocalManager, mCachedDevice, mLifecycle); 75 mContainer = new PreferenceCategory(mContext); 76 mContainer.setKey(mController.getPreferenceKey()); 77 mScreen.addPreference(mContainer); 78 setupDevice(mDeviceConfig); 79 } 80 81 @Test 82 @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) notConnected_noAudioSharingPreferences()83 public void notConnected_noAudioSharingPreferences() { 84 when(mCachedDevice.isConnectedLeAudioDevice()).thenReturn(false); 85 86 showScreen(mController); 87 88 assertThat(mContainer.isVisible()).isFalse(); 89 } 90 91 @Test 92 @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) connected_showOnePreference()93 public void connected_showOnePreference() { 94 when(mCachedDevice.isConnectedLeAudioDevice()).thenReturn(true); 95 when(mCachedDevice.isActiveDevice(BluetoothProfile.LE_AUDIO)).thenReturn(false); 96 when(mLocalManager 97 .getProfileManager() 98 .getLeAudioBroadcastAssistantProfile() 99 .getAllSources(mDevice)) 100 .thenReturn(List.of()); 101 when(mLocalManager 102 .getProfileManager() 103 .getLeAudioBroadcastProfile() 104 .isEnabled(mDevice)) 105 .thenReturn(true); 106 mShadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported( 107 BluetoothStatusCodes.FEATURE_SUPPORTED); 108 mShadowBluetoothAdapter.setIsLeAudioBroadcastAssistantSupported( 109 BluetoothStatusCodes.FEATURE_SUPPORTED); 110 111 showScreen(mController); 112 113 assertThat(mContainer.isVisible()).isTrue(); 114 assertThat(mContainer.getPreferenceCount()).isEqualTo(1); 115 assertThat(mContainer.getPreference(0).getTitle()) 116 .isEqualTo(mContext.getString(R.string.audio_sharing_title)); 117 } 118 119 @Test 120 @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) connected_active_showTwoPreference()121 public void connected_active_showTwoPreference() { 122 when(mCachedDevice.isConnectedLeAudioDevice()).thenReturn(true); 123 when(mCachedDevice.isActiveDevice(BluetoothProfile.LE_AUDIO)).thenReturn(true); 124 when(mLocalManager 125 .getProfileManager() 126 .getLeAudioBroadcastAssistantProfile() 127 .getAllSources(mDevice)) 128 .thenReturn(List.of()); 129 when(mLocalManager 130 .getProfileManager() 131 .getLeAudioBroadcastProfile() 132 .isEnabled(mDevice)) 133 .thenReturn(false); 134 mShadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported( 135 BluetoothStatusCodes.FEATURE_SUPPORTED); 136 mShadowBluetoothAdapter.setIsLeAudioBroadcastAssistantSupported( 137 BluetoothStatusCodes.FEATURE_SUPPORTED); 138 139 showScreen(mController); 140 141 assertThat(mContainer.isVisible()).isTrue(); 142 assertThat(mContainer.getPreferenceCount()).isEqualTo(2); 143 } 144 145 @Test 146 @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) connected_hasBroadcastSource_showTwoPreference()147 public void connected_hasBroadcastSource_showTwoPreference() { 148 when(mCachedDevice.isConnectedLeAudioDevice()).thenReturn(true); 149 when(mCachedDevice.isActiveDevice(BluetoothProfile.LE_AUDIO)).thenReturn(false); 150 List<Long> bisSyncState = new ArrayList<>(); 151 bisSyncState.add(1L); 152 when(mBroadcastReceiveState.getBisSyncState()).thenReturn(bisSyncState); 153 when(mLocalManager 154 .getProfileManager() 155 .getLeAudioBroadcastAssistantProfile() 156 .getAllSources(mDevice)) 157 .thenReturn(List.of(mBroadcastReceiveState)); 158 when(mLocalManager 159 .getProfileManager() 160 .getLeAudioBroadcastProfile() 161 .isEnabled(mDevice)) 162 .thenReturn(false); 163 mShadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported( 164 BluetoothStatusCodes.FEATURE_SUPPORTED); 165 mShadowBluetoothAdapter.setIsLeAudioBroadcastAssistantSupported( 166 BluetoothStatusCodes.FEATURE_SUPPORTED); 167 168 showScreen(mController); 169 170 assertThat(mContainer.isVisible()).isTrue(); 171 assertThat(mContainer.getPreferenceCount()).isEqualTo(2); 172 } 173 } 174