OSDN Git Service

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