• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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 org.mockito.ArgumentMatchers.any;
19 import static org.mockito.Mockito.doNothing;
20 import static org.mockito.Mockito.doReturn;
21 import static org.mockito.Mockito.spy;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24 
25 import android.bluetooth.BluetoothAdapter;
26 import android.bluetooth.BluetoothDevice;
27 import android.bluetooth.BluetoothLeBroadcastReceiveState;
28 import android.bluetooth.BluetoothProfile;
29 import android.bluetooth.BluetoothStatusCodes;
30 import android.content.Context;
31 import android.graphics.drawable.Drawable;
32 import android.media.AudioManager;
33 import android.platform.test.flag.junit.SetFlagsRule;
34 import android.util.Pair;
35 
36 import com.android.settings.connecteddevice.DevicePreferenceCallback;
37 import com.android.settings.dashboard.DashboardFragment;
38 import com.android.settings.testutils.shadow.ShadowAudioManager;
39 import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
40 import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
41 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
42 import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
43 import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
44 import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
45 import com.android.settingslib.bluetooth.LocalBluetoothManager;
46 import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
47 import com.android.settingslib.flags.Flags;
48 
49 import com.google.common.collect.ImmutableList;
50 import com.google.common.collect.ImmutableSet;
51 
52 import org.junit.Before;
53 import org.junit.Rule;
54 import org.junit.Test;
55 import org.junit.runner.RunWith;
56 import org.mockito.Mock;
57 import org.mockito.MockitoAnnotations;
58 import org.robolectric.RobolectricTestRunner;
59 import org.robolectric.RuntimeEnvironment;
60 import org.robolectric.annotation.Config;
61 import org.robolectric.shadow.api.Shadow;
62 
63 import java.util.ArrayList;
64 import java.util.Collection;
65 import java.util.List;
66 
67 @RunWith(RobolectricTestRunner.class)
68 @Config(
69         shadows = {
70             ShadowAudioManager.class,
71             ShadowBluetoothAdapter.class,
72             ShadowBluetoothUtils.class
73         })
74 public class AvailableMediaBluetoothDeviceUpdaterTest {
75     private static final String MAC_ADDRESS = "04:52:C7:0B:D8:3C";
76 
77     @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
78 
79     @Mock private DashboardFragment mDashboardFragment;
80     @Mock private DevicePreferenceCallback mDevicePreferenceCallback;
81     @Mock private CachedBluetoothDevice mCachedBluetoothDevice;
82     @Mock private BluetoothDevice mBluetoothDevice;
83     @Mock private Drawable mDrawable;
84     @Mock private LocalBluetoothManager mLocalBtManager;
85     @Mock private CachedBluetoothDeviceManager mCachedDeviceManager;
86     @Mock private LocalBluetoothProfileManager mProfileManager;
87     @Mock private LocalBluetoothLeBroadcastAssistant mAssistant;
88     @Mock private LocalBluetoothLeBroadcast mBroadcast;
89     @Mock private BluetoothLeBroadcastReceiveState mBroadcastReceiveState;
90 
91     private Context mContext;
92     private AvailableMediaBluetoothDeviceUpdater mBluetoothDeviceUpdater;
93     private Collection<CachedBluetoothDevice> mCachedDevices;
94     private AudioManager mAudioManager;
95     private BluetoothDevicePreference mPreference;
96     private ShadowBluetoothAdapter mShadowBluetoothAdapter;
97 
98     @Before
setUp()99     public void setUp() {
100         MockitoAnnotations.initMocks(this);
101 
102         mContext = RuntimeEnvironment.application;
103         mAudioManager = mContext.getSystemService(AudioManager.class);
104         ShadowBluetoothUtils.sLocalBluetoothManager = mLocalBtManager;
105         mLocalBtManager = Utils.getLocalBtManager(mContext);
106         when(mProfileManager.getLeAudioBroadcastAssistantProfile()).thenReturn(mAssistant);
107         when(mProfileManager.getLeAudioBroadcastProfile()).thenReturn(mBroadcast);
108         when(mLocalBtManager.getProfileManager()).thenReturn(mProfileManager);
109         when(mLocalBtManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager);
110         mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
111         mShadowBluetoothAdapter.setEnabled(true);
112         mShadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported(
113                 BluetoothStatusCodes.FEATURE_SUPPORTED);
114         mShadowBluetoothAdapter.setIsLeAudioBroadcastAssistantSupported(
115                 BluetoothStatusCodes.FEATURE_SUPPORTED);
116         mCachedDevices = new ArrayList<>();
117         mCachedDevices.add(mCachedBluetoothDevice);
118         when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(mCachedDevices);
119         Pair<Drawable, String> pairs = new Pair<>(mDrawable, "fake_device");
120 
121         doReturn(mContext).when(mDashboardFragment).getContext();
122         when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
123         when(mCachedBluetoothDevice.getAddress()).thenReturn(MAC_ADDRESS);
124         when(mCachedBluetoothDevice.getDrawableWithDescription()).thenReturn(pairs);
125         when(mCachedBluetoothDevice.getMemberDevice()).thenReturn(ImmutableSet.of());
126 
127         mPreference =
128                 new BluetoothDevicePreference(
129                         mContext,
130                         mCachedBluetoothDevice,
131                         false,
132                         BluetoothDevicePreference.SortType.TYPE_DEFAULT);
133     }
134 
135     @Test
onAudioModeChanged_hfpDeviceConnected_inCall_addPreference()136     public void onAudioModeChanged_hfpDeviceConnected_inCall_addPreference() {
137         setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL);
138         when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
139                 .thenReturn(true);
140         when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true);
141 
142         mBluetoothDeviceUpdater.onAudioModeChanged();
143 
144         verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
145     }
146 
147     @Test
onAudioModeChanged_hfpDeviceConnected_notInCall_removePreference()148     public void onAudioModeChanged_hfpDeviceConnected_notInCall_removePreference() {
149         setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
150         when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
151                 .thenReturn(true);
152         when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true);
153 
154         mBluetoothDeviceUpdater.onAudioModeChanged();
155 
156         verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
157     }
158 
159     @Test
onAudioModeChanged_a2dpDeviceConnected_inCall_removePreference()160     public void onAudioModeChanged_a2dpDeviceConnected_inCall_removePreference() {
161         setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL);
162         when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
163                 .thenReturn(true);
164         when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true);
165 
166         mBluetoothDeviceUpdater.onAudioModeChanged();
167 
168         verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
169     }
170 
171     @Test
onAudioModeChanged_a2dpDeviceConnected_notInCall_addPreference()172     public void onAudioModeChanged_a2dpDeviceConnected_notInCall_addPreference() {
173         setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
174         when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
175                 .thenReturn(true);
176         when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true);
177 
178         mBluetoothDeviceUpdater.onAudioModeChanged();
179 
180         verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
181     }
182 
183     @Test
onProfileConnectionStateChanged_a2dpDeviceConnected_notInCall_addPreference()184     public void onProfileConnectionStateChanged_a2dpDeviceConnected_notInCall_addPreference() {
185         setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
186         when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
187                 .thenReturn(true);
188         when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true);
189 
190         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(
191                 mCachedBluetoothDevice, BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP);
192 
193         verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
194     }
195 
196     @Test
onProfileConnectionStateChanged_a2dpDeviceConnected_inCall_removePreference()197     public void onProfileConnectionStateChanged_a2dpDeviceConnected_inCall_removePreference() {
198         setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL);
199         when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
200                 .thenReturn(true);
201         when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true);
202 
203         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(
204                 mCachedBluetoothDevice, BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP);
205 
206         verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
207     }
208 
209     @Test
onProfileConnectionStateChanged_hfpDeviceConnected_notInCall_removePreference()210     public void onProfileConnectionStateChanged_hfpDeviceConnected_notInCall_removePreference() {
211         setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
212         when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
213                 .thenReturn(true);
214         when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true);
215 
216         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(
217                 mCachedBluetoothDevice, BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP);
218 
219         verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
220     }
221 
222     @Test
onProfileConnectionStateChanged_hfpDeviceConnected_inCall_addPreference()223     public void onProfileConnectionStateChanged_hfpDeviceConnected_inCall_addPreference() {
224         setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL);
225         when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
226                 .thenReturn(true);
227         when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true);
228 
229         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(
230                 mCachedBluetoothDevice, BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP);
231 
232         verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
233     }
234 
235     @Test
onProfileConnectionStateChanged_ashaHearingAidConnected_notInCall_addPreference()236     public void onProfileConnectionStateChanged_ashaHearingAidConnected_notInCall_addPreference() {
237         setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
238         when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
239                 .thenReturn(true);
240         when(mCachedBluetoothDevice.isConnectedAshaHearingAidDevice()).thenReturn(true);
241 
242         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(
243                 mCachedBluetoothDevice,
244                 BluetoothProfile.STATE_CONNECTED,
245                 BluetoothProfile.HEARING_AID);
246 
247         verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
248     }
249 
250     @Test
onProfileConnectionStateChanged_ashaHearingAidConnected_inCall_addPreference()251     public void onProfileConnectionStateChanged_ashaHearingAidConnected_inCall_addPreference() {
252         setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL);
253         when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
254                 .thenReturn(true);
255         when(mCachedBluetoothDevice.isConnectedAshaHearingAidDevice()).thenReturn(true);
256 
257         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(
258                 mCachedBluetoothDevice,
259                 BluetoothProfile.STATE_CONNECTED,
260                 BluetoothProfile.HEARING_AID);
261 
262         verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
263     }
264 
265     @Test
onProfileConnectionStateChanged_leaConnected_notInCallSharingFlagOff_addPref()266     public void onProfileConnectionStateChanged_leaConnected_notInCallSharingFlagOff_addPref() {
267         mSetFlagsRule.disableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
268         setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
269         when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
270                 .thenReturn(true);
271         when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);
272         when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of(mBroadcastReceiveState));
273         List<Long> bisSyncState = new ArrayList<>();
274         bisSyncState.add(1L);
275         when(mBroadcastReceiveState.getBisSyncState()).thenReturn(bisSyncState);
276 
277         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(
278                 mCachedBluetoothDevice,
279                 BluetoothProfile.STATE_CONNECTED,
280                 BluetoothProfile.LE_AUDIO);
281 
282         verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
283     }
284 
285     @Test
286     public void
onProfileConnectionStateChanged_hasLeaMemberConnected_notInCallFlagOff_addPref()287             onProfileConnectionStateChanged_hasLeaMemberConnected_notInCallFlagOff_addPref() {
288         mSetFlagsRule.disableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
289         setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
290         when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
291                 .thenReturn(true);
292         when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(false);
293         when(mCachedBluetoothDevice.hasConnectedLeAudioMemberDevice()).thenReturn(true);
294         when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of(mBroadcastReceiveState));
295         List<Long> bisSyncState = new ArrayList<>();
296         bisSyncState.add(1L);
297         when(mBroadcastReceiveState.getBisSyncState()).thenReturn(bisSyncState);
298 
299         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(
300                 mCachedBluetoothDevice,
301                 BluetoothProfile.STATE_CONNECTED,
302                 BluetoothProfile.LE_AUDIO);
303 
304         verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
305     }
306 
307     @Test
onProfileConnectionStateChanged_leaConnected_notInCallNotInSharing_addPref()308     public void onProfileConnectionStateChanged_leaConnected_notInCallNotInSharing_addPref() {
309         mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
310         setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
311         when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
312                 .thenReturn(true);
313         when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);
314         when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of());
315 
316         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(
317                 mCachedBluetoothDevice,
318                 BluetoothProfile.STATE_CONNECTED,
319                 BluetoothProfile.LE_AUDIO);
320 
321         verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
322     }
323 
324     @Test
325     public void
onProfileConnectionStateChanged_hasLeaMemberConnected_notInCallNotInSharing_addPref()326             onProfileConnectionStateChanged_hasLeaMemberConnected_notInCallNotInSharing_addPref() {
327         mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
328         setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
329         when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
330                 .thenReturn(true);
331         when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(false);
332         when(mCachedBluetoothDevice.hasConnectedLeAudioMemberDevice()).thenReturn(true);
333         when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of());
334 
335         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(
336                 mCachedBluetoothDevice,
337                 BluetoothProfile.STATE_CONNECTED,
338                 BluetoothProfile.LE_AUDIO);
339 
340         verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
341     }
342 
343     @Test
onProfileConnectionStateChanged_leaConnected_inCallSharingFlagOff_addPref()344     public void onProfileConnectionStateChanged_leaConnected_inCallSharingFlagOff_addPref() {
345         mSetFlagsRule.disableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
346         setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL);
347         when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
348                 .thenReturn(true);
349         when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);
350         when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of());
351 
352         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(
353                 mCachedBluetoothDevice,
354                 BluetoothProfile.STATE_CONNECTED,
355                 BluetoothProfile.LE_AUDIO);
356 
357         verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
358     }
359 
360     @Test
onProfileConnectionStateChanged_leaConnected_inCallNotInSharing_addPref()361     public void onProfileConnectionStateChanged_leaConnected_inCallNotInSharing_addPref() {
362         mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
363         setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL);
364         when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
365                 .thenReturn(true);
366         when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);
367         when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of());
368 
369         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(
370                 mCachedBluetoothDevice,
371                 BluetoothProfile.STATE_CONNECTED,
372                 BluetoothProfile.LE_AUDIO);
373 
374         verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
375     }
376 
377     @Test
onProfileConnectionStateChanged_leaConnected_notInCallInSharing_removePref()378     public void onProfileConnectionStateChanged_leaConnected_notInCallInSharing_removePref() {
379         mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
380         mSetFlagsRule.disableFlags(Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX);
381         setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
382         when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
383                 .thenReturn(true);
384         when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);
385         when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true);
386         when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of(mBroadcastReceiveState));
387         List<Long> bisSyncState = new ArrayList<>();
388         bisSyncState.add(1L);
389         when(mBroadcastReceiveState.getBisSyncState()).thenReturn(bisSyncState);
390 
391         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(
392                 mCachedBluetoothDevice,
393                 BluetoothProfile.STATE_CONNECTED,
394                 BluetoothProfile.LE_AUDIO);
395 
396         verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
397     }
398 
399     @Test
400     public void
onProfileConnectionStateChanged_leaConnected_noInCallInSharing_hysteresis_removePref()401             onProfileConnectionStateChanged_leaConnected_noInCallInSharing_hysteresis_removePref() {
402         mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
403         mSetFlagsRule.enableFlags(Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX);
404         setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
405         when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
406                 .thenReturn(true);
407         when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);
408         when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true);
409         when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of(mBroadcastReceiveState));
410         when(mBroadcast.getLatestBroadcastId()).thenReturn(1);
411         when(mBroadcastReceiveState.getBroadcastId()).thenReturn(1);
412 
413         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(
414                 mCachedBluetoothDevice,
415                 BluetoothProfile.STATE_CONNECTED,
416                 BluetoothProfile.LE_AUDIO);
417 
418         verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
419     }
420 
421     @Test
onProfileConnectionStateChanged_leaConnected_inCallSharing_removePref()422     public void onProfileConnectionStateChanged_leaConnected_inCallSharing_removePref() {
423         mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
424         mSetFlagsRule.disableFlags(Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX);
425         setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
426         when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
427                 .thenReturn(true);
428         when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);
429         when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true);
430         when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of(mBroadcastReceiveState));
431         List<Long> bisSyncState = new ArrayList<>();
432         bisSyncState.add(1L);
433         when(mBroadcastReceiveState.getBisSyncState()).thenReturn(bisSyncState);
434 
435         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(
436                 mCachedBluetoothDevice,
437                 BluetoothProfile.STATE_CONNECTED,
438                 BluetoothProfile.LE_AUDIO);
439 
440         verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
441     }
442 
443     @Test
onProfileConnectionStateChanged_leaConnected_inCallSharing_hysteresis_removePref()444     public void onProfileConnectionStateChanged_leaConnected_inCallSharing_hysteresis_removePref() {
445         mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
446         mSetFlagsRule.enableFlags(Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX);
447         setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
448         when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
449                 .thenReturn(true);
450         when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);
451         when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true);
452         when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of(mBroadcastReceiveState));
453         when(mBroadcast.getLatestBroadcastId()).thenReturn(1);
454         when(mBroadcastReceiveState.getBroadcastId()).thenReturn(1);
455 
456         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(
457                 mCachedBluetoothDevice,
458                 BluetoothProfile.STATE_CONNECTED,
459                 BluetoothProfile.LE_AUDIO);
460 
461         verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
462     }
463 
464     @Test
465     public void
onProfileConnectionStateChanged_deviceIsNotInList_notInCall_invokesRemovePreference()466             onProfileConnectionStateChanged_deviceIsNotInList_notInCall_invokesRemovePreference() {
467         setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
468         when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
469                 .thenReturn(true);
470         when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);
471         mCachedDevices.clear();
472 
473         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(
474                 mCachedBluetoothDevice,
475                 BluetoothProfile.STATE_CONNECTED,
476                 BluetoothProfile.LE_AUDIO);
477 
478         verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
479     }
480 
481     @Test
onProfileConnectionStateChanged_deviceIsNotInList_inCall_invokesRemovePreference()482     public void onProfileConnectionStateChanged_deviceIsNotInList_inCall_invokesRemovePreference() {
483         setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL);
484         when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class)))
485                 .thenReturn(true);
486         when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);
487         mCachedDevices.clear();
488 
489         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(
490                 mCachedBluetoothDevice,
491                 BluetoothProfile.STATE_CONNECTED,
492                 BluetoothProfile.LE_AUDIO);
493 
494         verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
495     }
496 
497     @Test
onProfileConnectionStateChanged_deviceDisconnected_removePreference()498     public void onProfileConnectionStateChanged_deviceDisconnected_removePreference() {
499         setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
500         mBluetoothDeviceUpdater.onProfileConnectionStateChanged(
501                 mCachedBluetoothDevice, BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.A2DP);
502 
503         verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
504     }
505 
506     @Test
onClick_Preference_setActive()507     public void onClick_Preference_setActive() {
508         setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL);
509         mBluetoothDeviceUpdater.onPreferenceClick(mPreference);
510 
511         verify(mDevicePreferenceCallback).onDeviceClick(mPreference);
512     }
513 
setUpDeviceUpdaterWithAudioMode(int audioMode)514     private void setUpDeviceUpdaterWithAudioMode(int audioMode) {
515         mAudioManager.setMode(audioMode);
516         mBluetoothDeviceUpdater =
517                 spy(new AvailableMediaBluetoothDeviceUpdater(
518                         mContext, mDevicePreferenceCallback, /* metricsCategory= */ 0));
519         mBluetoothDeviceUpdater.setPrefContext(mContext);
520         doNothing().when(mBluetoothDeviceUpdater).addPreference(any());
521         doNothing().when(mBluetoothDeviceUpdater).removePreference(
522                 any(CachedBluetoothDevice.class));
523     }
524 }
525