2 * Copyright (C) 2018 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.sound;
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;
29 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
30 import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
32 import static com.google.common.truth.Truth.assertThat;
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;
40 import static org.robolectric.Shadows.shadowOf;
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;
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;
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;
77 import java.util.ArrayList;
78 import java.util.List;
80 @RunWith(SettingsRobolectricTestRunner.class)
82 ShadowAudioManager.class,
83 ShadowMediaRouter.class,
84 ShadowBluetoothUtils.class,
85 ShadowBluetoothDevice.class}
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;
98 private LocalBluetoothManager mLocalManager;
100 private BluetoothEventManager mBluetoothEventManager;
102 private LocalBluetoothProfileManager mLocalBluetoothProfileManager;
104 private A2dpProfile mA2dpProfile;
106 private HeadsetProfile mHeadsetProfile;
108 private HearingAidProfile mHearingAidProfile;
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;
127 public void setUp() {
128 MockitoAnnotations.initMocks(this);
129 mContext = spy(RuntimeEnvironment.application);
131 mShadowAudioManager = ShadowAudioManager.getShadow();
132 mShadowMediaRouter = ShadowMediaRouter.getShadow();
134 ShadowBluetoothUtils.sLocalBluetoothManager = mLocalManager;
135 mLocalBluetoothManager = ShadowBluetoothUtils.getLocalBtManager(mContext);
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);
143 mBluetoothManager = new BluetoothManager(mContext);
144 mBluetoothAdapter = mBluetoothManager.getAdapter();
146 mBluetoothDevice = spy(mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS_1));
147 when(mBluetoothDevice.getName()).thenReturn(TEST_DEVICE_NAME_1);
148 when(mBluetoothDevice.isConnected()).thenReturn(true);
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);
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);
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);
170 public void tearDown() {
171 mShadowAudioManager.reset();
172 mShadowMediaRouter.reset();
173 ShadowBluetoothUtils.reset();
177 public void getAvailabilityStatus_byDefault_isAvailable() {
178 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
182 public void getAvailabilityStatus_whenNotVisible_isDisable() {
183 FeatureFlagUtils.setEnabled(mContext, FeatureFlags.AUDIO_SWITCHER_SETTINGS, false);
184 assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
188 public void onStart_shouldRegisterCallbackAndRegisterReceiver() {
189 mController.onStart();
191 verify(mLocalBluetoothManager.getEventManager()).registerCallback(
192 any(BluetoothCallback.class));
193 verify(mContext).registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class));
197 public void onStop_shouldUnregisterCallbackAndUnregisterReceiver() {
198 mController.onStart();
199 mController.onStop();
201 verify(mLocalBluetoothManager.getEventManager()).unregisterCallback(
202 any(BluetoothCallback.class));
203 verify(mContext).unregisterReceiver(any(BroadcastReceiver.class));
207 public void onPreferenceChange_toThisDevice_shouldSetDefaultSummary() {
208 mController.mConnectedDevices.clear();
209 mController.mConnectedDevices.add(mBluetoothDevice);
211 mController.onPreferenceChange(mPreference,
212 mContext.getText(R.string.media_output_default_summary));
214 assertThat(mPreference.getSummary()).isEqualTo(
215 mContext.getText(R.string.media_output_default_summary));
219 * One Bluetooth devices are available, and select the device.
220 * Preference summary should be device name.
223 public void onPreferenceChange_toBtDevice_shouldSetBtDeviceName() {
224 mController.mConnectedDevices.clear();
225 mController.mConnectedDevices.add(mBluetoothDevice);
227 mController.onPreferenceChange(mPreference, TEST_DEVICE_ADDRESS_1);
229 assertThat(mPreference.getSummary()).isEqualTo(mBluetoothDevice.getName());
233 * More than one Bluetooth devices are available, and select second device.
234 * Preference summary should be second device name.
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);
247 mController.onPreferenceChange(mPreference, TEST_DEVICE_ADDRESS_2);
249 assertThat(mPreference.getSummary()).isEqualTo(secondBluetoothDevice.getName());
253 * mConnectedDevices is empty.
254 * onPreferenceChange should return false.
257 public void onPreferenceChange_connectedDeviceIsNull_shouldReturnFalse() {
258 mController.mConnectedDevices.clear();
260 assertThat(mController.onPreferenceChange(mPreference, TEST_DEVICE_ADDRESS_1)).isFalse();
264 * Audio stream output to bluetooth sco headset which is the subset of all sco device.
265 * isStreamFromOutputDevice should return true.
268 public void isStreamFromOutputDevice_outputDeviceIsBtScoHeadset_shouldReturnTrue() {
269 mShadowAudioManager.setOutputDevice(DEVICE_OUT_BLUETOOTH_SCO_HEADSET);
271 assertThat(mController.isStreamFromOutputDevice(STREAM_MUSIC, DEVICE_OUT_ALL_SCO)).isTrue();
275 * Audio stream is not STREAM_MUSIC or STREAM_VOICE_CALL.
276 * findActiveDevice should return null.
279 public void findActiveDevice_streamIsRing_shouldReturnNull() {
280 assertThat(mController.findActiveDevice(STREAM_RING)).isNull();
284 * Audio stream is STREAM_MUSIC and output device is A2dp bluetooth device.
285 * findActiveDevice should return A2dp active device.
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);
296 assertThat(mController.findActiveDevice(STREAM_MUSIC)).isEqualTo(mBluetoothDevice);
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.
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);
312 assertThat(mController.findActiveDevice(STREAM_VOICE_CALL)).isEqualTo(mBluetoothDevice);
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.
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);
333 assertThat(mController.findActiveDevice(STREAM_MUSIC)).isEqualTo(mLeftBluetoothHapDevice);
334 assertThat(mController.findActiveDevice(STREAM_VOICE_CALL)).isEqualTo(
335 mLeftBluetoothHapDevice);
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.
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);
357 assertThat(mController.findActiveDevice(STREAM_MUSIC)).isEqualTo(mRightBluetoothHapDevice);
358 assertThat(mController.findActiveDevice(STREAM_VOICE_CALL)).isEqualTo(
359 mRightBluetoothHapDevice);
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.
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);
380 assertThat(mController.findActiveDevice(STREAM_MUSIC)).isEqualTo(mRightBluetoothHapDevice);
381 assertThat(mController.findActiveDevice(STREAM_VOICE_CALL)).isEqualTo(
382 mRightBluetoothHapDevice);
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.
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);
401 assertThat(mController.findActiveDevice(STREAM_MUSIC)).isNull();
402 assertThat(mController.findActiveDevice(STREAM_VOICE_CALL)).isNull();
406 * Two hearing aid devices with different HisyncId
407 * getConnectedHearingAidDevices should add both device to list.
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);
419 mEmptyDevices.addAll(mController.getConnectedHearingAidDevices());
421 assertThat(mEmptyDevices).containsExactly(mLeftBluetoothHapDevice,
422 mRightBluetoothHapDevice);
426 * Two hearing aid devices with same HisyncId
427 * getConnectedHearingAidDevices should only add first device to list.
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);
439 mEmptyDevices.addAll(mController.getConnectedHearingAidDevices());
441 assertThat(mEmptyDevices).containsExactly(mLeftBluetoothHapDevice);
445 * One A2dp device is connected.
446 * getConnectedA2dpDevices should add this device to list.
449 public void getConnectedA2dpDevices_oneConnectedA2dpDevice_shouldAddDeviceToList() {
450 mEmptyDevices.clear();
451 mProfileConnectedDevices.clear();
452 mProfileConnectedDevices.add(mBluetoothDevice);
453 when(mA2dpProfile.getConnectedDevices()).thenReturn(mProfileConnectedDevices);
455 mEmptyDevices.addAll(mController.getConnectedA2dpDevices());
457 assertThat(mEmptyDevices).containsExactly(mBluetoothDevice);
461 * More than one A2dp devices are connected.
462 * getConnectedA2dpDevices should add all devices to list.
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);
472 mEmptyDevices.addAll(mController.getConnectedA2dpDevices());
474 assertThat(mEmptyDevices).containsExactly(mBluetoothDevice, mLeftBluetoothHapDevice);
478 * One hands free profile device is connected.
479 * getConnectedA2dpDevices should add this device to list.
482 public void getConnectedHfpDevices_oneConnectedHfpDevice_shouldAddDeviceToList() {
483 mEmptyDevices.clear();
484 mProfileConnectedDevices.clear();
485 mProfileConnectedDevices.add(mBluetoothDevice);
486 when(mHeadsetProfile.getConnectedDevices()).thenReturn(mProfileConnectedDevices);
488 mEmptyDevices.addAll(mController.getConnectedHfpDevices());
490 assertThat(mEmptyDevices).containsExactly(mBluetoothDevice);
494 * More than one hands free profile devices are connected.
495 * getConnectedA2dpDevices should add all devices to list.
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);
505 mEmptyDevices.addAll(mController.getConnectedHfpDevices());
507 assertThat(mEmptyDevices).containsExactly(mBluetoothDevice, mLeftBluetoothHapDevice);
510 private class AudioSwitchPreferenceControllerTestable extends
511 AudioSwitchPreferenceController {
512 AudioSwitchPreferenceControllerTestable(Context context, String key) {
517 public void setActiveBluetoothDevice(BluetoothDevice device) {
521 public String getPreferenceKey() {