OSDN Git Service

Merge "Make user explicitly set security type for tether network" into pi-dev
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / sound / AudioOutputSwitchPreferenceControllerTest.java
1 /*
2  * Copyright (C) 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
17 package com.android.settings.sound;
18
19
20 import static android.media.AudioManager.DEVICE_OUT_BLUETOOTH_SCO;
21 import static android.media.AudioManager.STREAM_RING;
22 import static android.media.AudioManager.STREAM_VOICE_CALL;
23 import static android.media.AudioSystem.DEVICE_OUT_ALL_SCO;
24 import static android.media.AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP;
25 import static android.media.AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
26 import static android.media.AudioSystem.DEVICE_OUT_HEARING_AID;
27 import static android.media.AudioSystem.STREAM_MUSIC;
28
29 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
30 import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
31
32 import static com.google.common.truth.Truth.assertThat;
33
34 import static org.mockito.ArgumentMatchers.any;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.spy;
37 import static org.mockito.Mockito.verify;
38 import static org.mockito.Mockito.when;
39
40 import static org.robolectric.Shadows.shadowOf;
41
42 import android.bluetooth.BluetoothAdapter;
43 import android.bluetooth.BluetoothDevice;
44 import android.bluetooth.BluetoothManager;
45 import android.content.BroadcastReceiver;
46 import android.content.Context;
47 import android.content.IntentFilter;
48 import android.support.v7.preference.ListPreference;
49 import android.support.v7.preference.PreferenceManager;
50 import android.support.v7.preference.PreferenceScreen;
51 import android.util.FeatureFlagUtils;
52
53 import com.android.settings.R;
54 import com.android.settings.core.FeatureFlags;
55 import com.android.settings.testutils.SettingsRobolectricTestRunner;
56 import com.android.settings.testutils.shadow.ShadowAudioManager;
57 import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
58 import com.android.settings.testutils.shadow.ShadowMediaRouter;
59 import com.android.settingslib.bluetooth.A2dpProfile;
60 import com.android.settingslib.bluetooth.BluetoothCallback;
61 import com.android.settingslib.bluetooth.BluetoothEventManager;
62 import com.android.settingslib.bluetooth.HeadsetProfile;
63 import com.android.settingslib.bluetooth.HearingAidProfile;
64 import com.android.settingslib.bluetooth.LocalBluetoothManager;
65 import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
66
67 import org.junit.After;
68 import org.junit.Before;
69 import org.junit.Test;
70 import org.junit.runner.RunWith;
71 import org.mockito.Mock;
72 import org.mockito.MockitoAnnotations;
73 import org.robolectric.RuntimeEnvironment;
74 import org.robolectric.annotation.Config;
75 import org.robolectric.shadows.ShadowBluetoothDevice;
76
77 import java.util.ArrayList;
78 import java.util.List;
79
80 @RunWith(SettingsRobolectricTestRunner.class)
81 @Config(shadows = {
82         ShadowAudioManager.class,
83         ShadowMediaRouter.class,
84         ShadowBluetoothUtils.class,
85         ShadowBluetoothDevice.class}
86 )
87 public class AudioOutputSwitchPreferenceControllerTest {
88     private static final String TEST_KEY = "Test_Key";
89     private static final String TEST_DEVICE_NAME_1 = "Test_A2DP_BT_Device_NAME_1";
90     private static final String TEST_DEVICE_NAME_2 = "Test_A2DP_BT_Device_NAME_2";
91     private static final String TEST_DEVICE_ADDRESS_1 = "00:A1:A1:A1:A1:A1";
92     private static final String TEST_DEVICE_ADDRESS_2 = "00:B2:B2:B2:B2:B2";
93     private static final String TEST_DEVICE_ADDRESS_3 = "00:C3:C3:C3:C3:C3";
94     private final static long HISYNCID1 = 10;
95     private final static long HISYNCID2 = 11;
96
97     @Mock
98     private LocalBluetoothManager mLocalManager;
99     @Mock
100     private BluetoothEventManager mBluetoothEventManager;
101     @Mock
102     private LocalBluetoothProfileManager mLocalBluetoothProfileManager;
103     @Mock
104     private A2dpProfile mA2dpProfile;
105     @Mock
106     private HeadsetProfile mHeadsetProfile;
107     @Mock
108     private HearingAidProfile mHearingAidProfile;
109
110     private Context mContext;
111     private PreferenceScreen mScreen;
112     private ListPreference mPreference;
113     private ShadowAudioManager mShadowAudioManager;
114     private ShadowMediaRouter mShadowMediaRouter;
115     private BluetoothManager mBluetoothManager;
116     private BluetoothAdapter mBluetoothAdapter;
117     private BluetoothDevice mBluetoothDevice;
118     private BluetoothDevice mLeftBluetoothHapDevice;
119     private BluetoothDevice mRightBluetoothHapDevice;
120     private LocalBluetoothManager mLocalBluetoothManager;
121     private AudioSwitchPreferenceController mController;
122     private List<BluetoothDevice> mProfileConnectedDevices;
123     private List<BluetoothDevice> mHearingAidActiveDevices;
124     private List<BluetoothDevice> mEmptyDevices;
125
126     @Before
127     public void setUp() {
128         MockitoAnnotations.initMocks(this);
129         mContext = spy(RuntimeEnvironment.application);
130
131         mShadowAudioManager = ShadowAudioManager.getShadow();
132         mShadowMediaRouter = ShadowMediaRouter.getShadow();
133
134         ShadowBluetoothUtils.sLocalBluetoothManager = mLocalManager;
135         mLocalBluetoothManager = ShadowBluetoothUtils.getLocalBtManager(mContext);
136
137         when(mLocalBluetoothManager.getEventManager()).thenReturn(mBluetoothEventManager);
138         when(mLocalBluetoothManager.getProfileManager()).thenReturn(mLocalBluetoothProfileManager);
139         when(mLocalBluetoothProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile);
140         when(mLocalBluetoothProfileManager.getHearingAidProfile()).thenReturn(mHearingAidProfile);
141         when(mLocalBluetoothProfileManager.getHeadsetProfile()).thenReturn(mHeadsetProfile);
142
143         mBluetoothManager = new BluetoothManager(mContext);
144         mBluetoothAdapter = mBluetoothManager.getAdapter();
145
146         mBluetoothDevice = spy(mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS_1));
147         when(mBluetoothDevice.getName()).thenReturn(TEST_DEVICE_NAME_1);
148         when(mBluetoothDevice.isConnected()).thenReturn(true);
149
150         mLeftBluetoothHapDevice = spy(mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS_2));
151         when(mLeftBluetoothHapDevice.isConnected()).thenReturn(true);
152         mRightBluetoothHapDevice = spy(mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS_3));
153         when(mRightBluetoothHapDevice.isConnected()).thenReturn(true);
154
155         mController = new AudioSwitchPreferenceControllerTestable(mContext, TEST_KEY);
156         mScreen = spy(new PreferenceScreen(mContext, null));
157         mPreference = new ListPreference(mContext);
158         mProfileConnectedDevices = new ArrayList<>();
159         mHearingAidActiveDevices = new ArrayList<>(2);
160         mEmptyDevices = new ArrayList<>(2);
161
162         when(mScreen.getPreferenceManager()).thenReturn(mock(PreferenceManager.class));
163         when(mScreen.getContext()).thenReturn(mContext);
164         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
165         mScreen.addPreference(mPreference);
166         mController.displayPreference(mScreen);
167     }
168
169     @After
170     public void tearDown() {
171         mShadowAudioManager.reset();
172         mShadowMediaRouter.reset();
173         ShadowBluetoothUtils.reset();
174     }
175
176     @Test
177     public void getAvailabilityStatus_byDefault_isAvailable() {
178         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
179     }
180
181     @Test
182     public void getAvailabilityStatus_whenNotVisible_isDisable() {
183         FeatureFlagUtils.setEnabled(mContext, FeatureFlags.AUDIO_SWITCHER_SETTINGS, false);
184         assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
185     }
186
187     @Test
188     public void onStart_shouldRegisterCallbackAndRegisterReceiver() {
189         mController.onStart();
190
191         verify(mLocalBluetoothManager.getEventManager()).registerCallback(
192                 any(BluetoothCallback.class));
193         verify(mContext).registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class));
194     }
195
196     @Test
197     public void onStop_shouldUnregisterCallbackAndUnregisterReceiver() {
198         mController.onStart();
199         mController.onStop();
200
201         verify(mLocalBluetoothManager.getEventManager()).unregisterCallback(
202                 any(BluetoothCallback.class));
203         verify(mContext).unregisterReceiver(any(BroadcastReceiver.class));
204     }
205
206     @Test
207     public void onPreferenceChange_toThisDevice_shouldSetDefaultSummary() {
208         mController.mConnectedDevices.clear();
209         mController.mConnectedDevices.add(mBluetoothDevice);
210
211         mController.onPreferenceChange(mPreference,
212                 mContext.getText(R.string.media_output_default_summary));
213
214         assertThat(mPreference.getSummary()).isEqualTo(
215                 mContext.getText(R.string.media_output_default_summary));
216     }
217
218     /**
219      * One Bluetooth devices are available, and select the device.
220      * Preference summary should be device name.
221      */
222     @Test
223     public void onPreferenceChange_toBtDevice_shouldSetBtDeviceName() {
224         mController.mConnectedDevices.clear();
225         mController.mConnectedDevices.add(mBluetoothDevice);
226
227         mController.onPreferenceChange(mPreference, TEST_DEVICE_ADDRESS_1);
228
229         assertThat(mPreference.getSummary()).isEqualTo(TEST_DEVICE_NAME_1);
230     }
231
232     /**
233      * More than one Bluetooth devices are available, and select second device.
234      * Preference summary should be second device name.
235      */
236     @Test
237     public void onPreferenceChange_toBtDevices_shouldSetSecondBtDeviceName() {
238         ShadowBluetoothDevice shadowBluetoothDevice;
239         BluetoothDevice secondBluetoothDevice;
240         secondBluetoothDevice = mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS_2);
241         shadowBluetoothDevice = shadowOf(secondBluetoothDevice);
242         shadowBluetoothDevice.setName(TEST_DEVICE_NAME_2);
243         mController.mConnectedDevices.clear();
244         mController.mConnectedDevices.add(mBluetoothDevice);
245         mController.mConnectedDevices.add(secondBluetoothDevice);
246
247         mController.onPreferenceChange(mPreference, TEST_DEVICE_ADDRESS_2);
248
249         assertThat(mPreference.getSummary()).isEqualTo(TEST_DEVICE_NAME_2);
250     }
251
252     /**
253      * mConnectedDevices is empty.
254      * onPreferenceChange should return false.
255      */
256     @Test
257     public void onPreferenceChange_connectedDeviceIsNull_shouldReturnFalse() {
258         mController.mConnectedDevices.clear();
259
260         assertThat(mController.onPreferenceChange(mPreference, TEST_DEVICE_ADDRESS_1)).isFalse();
261     }
262
263     /**
264      * Audio stream output to bluetooth sco headset which is the subset of all sco device.
265      * isStreamFromOutputDevice should return true.
266      */
267     @Test
268     public void isStreamFromOutputDevice_outputDeviceIsBtScoHeadset_shouldReturnTrue() {
269         mShadowAudioManager.setOutputDevice(DEVICE_OUT_BLUETOOTH_SCO_HEADSET);
270
271         assertThat(mController.isStreamFromOutputDevice(STREAM_MUSIC, DEVICE_OUT_ALL_SCO)).isTrue();
272     }
273
274     /**
275      * Audio stream is not STREAM_MUSIC or STREAM_VOICE_CALL.
276      * findActiveDevice should return null.
277      */
278     @Test
279     public void findActiveDevice_streamIsRing_shouldReturnNull() {
280         assertThat(mController.findActiveDevice(STREAM_RING)).isNull();
281     }
282
283     /**
284      * Audio stream is STREAM_MUSIC and output device is A2dp bluetooth device.
285      * findActiveDevice should return A2dp active device.
286      */
287     @Test
288     public void findActiveDevice_streamMusicToA2dpDevice_shouldReturnActiveA2dpDevice() {
289         mShadowAudioManager.setOutputDevice(DEVICE_OUT_BLUETOOTH_A2DP);
290         mHearingAidActiveDevices.clear();
291         mHearingAidActiveDevices.add(mLeftBluetoothHapDevice);
292         when(mHeadsetProfile.getActiveDevice()).thenReturn(mLeftBluetoothHapDevice);
293         when(mA2dpProfile.getActiveDevice()).thenReturn(mBluetoothDevice);
294         when(mHearingAidProfile.getActiveDevices()).thenReturn(mHearingAidActiveDevices);
295
296         assertThat(mController.findActiveDevice(STREAM_MUSIC)).isEqualTo(mBluetoothDevice);
297     }
298
299     /**
300      * Audio stream is STREAM_VOICE_CALL and output device is Hands free profile bluetooth device.
301      * findActiveDevice should return Hands free profile active device.
302      */
303     @Test
304     public void findActiveDevice_streamVoiceCallToHfpDevice_shouldReturnActiveHfpDevice() {
305         mShadowAudioManager.setOutputDevice(DEVICE_OUT_BLUETOOTH_SCO);
306         mHearingAidActiveDevices.clear();
307         mHearingAidActiveDevices.add(mLeftBluetoothHapDevice);
308         when(mHeadsetProfile.getActiveDevice()).thenReturn(mBluetoothDevice);
309         when(mA2dpProfile.getActiveDevice()).thenReturn(mLeftBluetoothHapDevice);
310         when(mHearingAidProfile.getActiveDevices()).thenReturn(mHearingAidActiveDevices);
311
312         assertThat(mController.findActiveDevice(STREAM_VOICE_CALL)).isEqualTo(mBluetoothDevice);
313     }
314
315     /**
316      * Audio stream is STREAM_MUSIC or STREAM_VOICE_CALL and output device is hearing aid profile
317      * bluetooth device. And left side of HAP device is active.
318      * findActiveDevice should return hearing aid device active device.
319      */
320     @Test
321     public void findActiveDevice_streamToHapDeviceLeftActiveDevice_shouldReturnActiveHapDevice() {
322         mShadowAudioManager.setOutputDevice(DEVICE_OUT_HEARING_AID);
323         mController.mConnectedDevices.clear();
324         mController.mConnectedDevices.add(mBluetoothDevice);
325         mController.mConnectedDevices.add(mLeftBluetoothHapDevice);
326         mHearingAidActiveDevices.clear();
327         mHearingAidActiveDevices.add(mLeftBluetoothHapDevice);
328         mHearingAidActiveDevices.add(null);
329         when(mHeadsetProfile.getActiveDevice()).thenReturn(mBluetoothDevice);
330         when(mA2dpProfile.getActiveDevice()).thenReturn(mBluetoothDevice);
331         when(mHearingAidProfile.getActiveDevices()).thenReturn(mHearingAidActiveDevices);
332
333         assertThat(mController.findActiveDevice(STREAM_MUSIC)).isEqualTo(mLeftBluetoothHapDevice);
334         assertThat(mController.findActiveDevice(STREAM_VOICE_CALL)).isEqualTo(
335                 mLeftBluetoothHapDevice);
336     }
337
338     /**
339      * Audio stream is STREAM_MUSIC or STREAM_VOICE_CALL and output device is hearing aid profile
340      * bluetooth device. And right side of HAP device is active.
341      * findActiveDevice should return hearing aid device active device.
342      */
343     @Test
344     public void findActiveDevice_streamToHapDeviceRightActiveDevice_shouldReturnActiveHapDevice() {
345         mShadowAudioManager.setOutputDevice(DEVICE_OUT_HEARING_AID);
346         mController.mConnectedDevices.clear();
347         mController.mConnectedDevices.add(mBluetoothDevice);
348         mController.mConnectedDevices.add(mRightBluetoothHapDevice);
349         mHearingAidActiveDevices.clear();
350         mHearingAidActiveDevices.add(null);
351         mHearingAidActiveDevices.add(mRightBluetoothHapDevice);
352         mHearingAidActiveDevices.add(mRightBluetoothHapDevice);
353         when(mHeadsetProfile.getActiveDevice()).thenReturn(mBluetoothDevice);
354         when(mA2dpProfile.getActiveDevice()).thenReturn(mBluetoothDevice);
355         when(mHearingAidProfile.getActiveDevices()).thenReturn(mHearingAidActiveDevices);
356
357         assertThat(mController.findActiveDevice(STREAM_MUSIC)).isEqualTo(mRightBluetoothHapDevice);
358         assertThat(mController.findActiveDevice(STREAM_VOICE_CALL)).isEqualTo(
359                 mRightBluetoothHapDevice);
360     }
361
362     /**
363      * Audio stream is STREAM_MUSIC or STREAM_VOICE_CALL and output device is hearing aid
364      * profile bluetooth device. And both are active device.
365      * findActiveDevice should return only return the active device in mConnectedDevices.
366      */
367     @Test
368     public void findActiveDevice_streamToHapDeviceTwoActiveDevice_shouldReturnActiveHapDevice() {
369         mShadowAudioManager.setOutputDevice(DEVICE_OUT_HEARING_AID);
370         mController.mConnectedDevices.clear();
371         mController.mConnectedDevices.add(mBluetoothDevice);
372         mController.mConnectedDevices.add(mRightBluetoothHapDevice);
373         mHearingAidActiveDevices.clear();
374         mHearingAidActiveDevices.add(mLeftBluetoothHapDevice);
375         mHearingAidActiveDevices.add(mRightBluetoothHapDevice);
376         when(mHeadsetProfile.getActiveDevice()).thenReturn(mBluetoothDevice);
377         when(mA2dpProfile.getActiveDevice()).thenReturn(mBluetoothDevice);
378         when(mHearingAidProfile.getActiveDevices()).thenReturn(mHearingAidActiveDevices);
379
380         assertThat(mController.findActiveDevice(STREAM_MUSIC)).isEqualTo(mRightBluetoothHapDevice);
381         assertThat(mController.findActiveDevice(STREAM_VOICE_CALL)).isEqualTo(
382                 mRightBluetoothHapDevice);
383     }
384
385     /**
386      * Audio stream is STREAM_MUSIC or STREAM_VOICE_CALL and output device is hearing aid
387      * profile bluetooth device. And none of them are active.
388      * findActiveDevice should return null.
389      */
390     @Test
391     public void findActiveDevice_streamToOtherDevice_shouldReturnActiveHapDevice() {
392         mShadowAudioManager.setOutputDevice(DEVICE_OUT_HEARING_AID);
393         mController.mConnectedDevices.clear();
394         mController.mConnectedDevices.add(mBluetoothDevice);
395         mController.mConnectedDevices.add(mLeftBluetoothHapDevice);
396         mHearingAidActiveDevices.clear();
397         when(mHeadsetProfile.getActiveDevice()).thenReturn(mBluetoothDevice);
398         when(mA2dpProfile.getActiveDevice()).thenReturn(mBluetoothDevice);
399         when(mHearingAidProfile.getActiveDevices()).thenReturn(mHearingAidActiveDevices);
400
401         assertThat(mController.findActiveDevice(STREAM_MUSIC)).isNull();
402         assertThat(mController.findActiveDevice(STREAM_VOICE_CALL)).isNull();
403     }
404
405     /**
406      * Two hearing aid devices with different HisyncId
407      * getConnectedHearingAidDevices should add both device to list.
408      */
409     @Test
410     public void getConnectedHearingAidDevices_deviceHisyncIdIsDifferent_shouldAddBothToList() {
411         mEmptyDevices.clear();
412         mProfileConnectedDevices.clear();
413         mProfileConnectedDevices.add(mLeftBluetoothHapDevice);
414         mProfileConnectedDevices.add(mRightBluetoothHapDevice);
415         when(mHearingAidProfile.getConnectedDevices()).thenReturn(mProfileConnectedDevices);
416         when(mHearingAidProfile.getHiSyncId(mLeftBluetoothHapDevice)).thenReturn(HISYNCID1);
417         when(mHearingAidProfile.getHiSyncId(mRightBluetoothHapDevice)).thenReturn(HISYNCID2);
418
419         mEmptyDevices.addAll(mController.getConnectedHearingAidDevices());
420
421         assertThat(mEmptyDevices).containsExactly(mLeftBluetoothHapDevice,
422                 mRightBluetoothHapDevice);
423     }
424
425     /**
426      * Two hearing aid devices with same HisyncId
427      * getConnectedHearingAidDevices should only add first device to list.
428      */
429     @Test
430     public void getConnectedHearingAidDevices_deviceHisyncIdIsSame_shouldAddOneToList() {
431         mEmptyDevices.clear();
432         mProfileConnectedDevices.clear();
433         mProfileConnectedDevices.add(mLeftBluetoothHapDevice);
434         mProfileConnectedDevices.add(mRightBluetoothHapDevice);
435         when(mHearingAidProfile.getConnectedDevices()).thenReturn(mProfileConnectedDevices);
436         when(mHearingAidProfile.getHiSyncId(mLeftBluetoothHapDevice)).thenReturn(HISYNCID1);
437         when(mHearingAidProfile.getHiSyncId(mRightBluetoothHapDevice)).thenReturn(HISYNCID1);
438
439         mEmptyDevices.addAll(mController.getConnectedHearingAidDevices());
440
441         assertThat(mEmptyDevices).containsExactly(mLeftBluetoothHapDevice);
442     }
443
444     /**
445      * One A2dp device is connected.
446      * getConnectedA2dpDevices should add this device to list.
447      */
448     @Test
449     public void getConnectedA2dpDevices_oneConnectedA2dpDevice_shouldAddDeviceToList() {
450         mEmptyDevices.clear();
451         mProfileConnectedDevices.clear();
452         mProfileConnectedDevices.add(mBluetoothDevice);
453         when(mA2dpProfile.getConnectedDevices()).thenReturn(mProfileConnectedDevices);
454
455         mEmptyDevices.addAll(mController.getConnectedA2dpDevices());
456
457         assertThat(mEmptyDevices).containsExactly(mBluetoothDevice);
458     }
459
460     /**
461      * More than one A2dp devices are connected.
462      * getConnectedA2dpDevices should add all devices to list.
463      */
464     @Test
465     public void getConnectedA2dpDevices_moreThanOneConnectedA2dpDevice_shouldAddDeviceToList() {
466         mEmptyDevices.clear();
467         mProfileConnectedDevices.clear();
468         mProfileConnectedDevices.add(mBluetoothDevice);
469         mProfileConnectedDevices.add(mLeftBluetoothHapDevice);
470         when(mA2dpProfile.getConnectedDevices()).thenReturn(mProfileConnectedDevices);
471
472         mEmptyDevices.addAll(mController.getConnectedA2dpDevices());
473
474         assertThat(mEmptyDevices).containsExactly(mBluetoothDevice, mLeftBluetoothHapDevice);
475     }
476
477     /**
478      * One hands free profile device is connected.
479      * getConnectedA2dpDevices should add this device to list.
480      */
481     @Test
482     public void getConnectedHfpDevices_oneConnectedHfpDevice_shouldAddDeviceToList() {
483         mEmptyDevices.clear();
484         mProfileConnectedDevices.clear();
485         mProfileConnectedDevices.add(mBluetoothDevice);
486         when(mHeadsetProfile.getConnectedDevices()).thenReturn(mProfileConnectedDevices);
487
488         mEmptyDevices.addAll(mController.getConnectedHfpDevices());
489
490         assertThat(mEmptyDevices).containsExactly(mBluetoothDevice);
491     }
492
493     /**
494      * More than one hands free profile devices are connected.
495      * getConnectedA2dpDevices should add all devices to list.
496      */
497     @Test
498     public void getConnectedHfpDevices_moreThanOneConnectedHfpDevice_shouldAddDeviceToList() {
499         mEmptyDevices.clear();
500         mProfileConnectedDevices.clear();
501         mProfileConnectedDevices.add(mBluetoothDevice);
502         mProfileConnectedDevices.add(mLeftBluetoothHapDevice);
503         when(mHeadsetProfile.getConnectedDevices()).thenReturn(mProfileConnectedDevices);
504
505         mEmptyDevices.addAll(mController.getConnectedHfpDevices());
506
507         assertThat(mEmptyDevices).containsExactly(mBluetoothDevice, mLeftBluetoothHapDevice);
508     }
509
510     private class AudioSwitchPreferenceControllerTestable extends
511             AudioSwitchPreferenceController {
512         AudioSwitchPreferenceControllerTestable(Context context, String key) {
513             super(context, key);
514         }
515
516         @Override
517         public void setActiveBluetoothDevice(BluetoothDevice device) {
518         }
519
520         @Override
521         public String getPreferenceKey() {
522             return TEST_KEY;
523         }
524     }
525 }