Sound + Output Switcher on Sound Setting

1. Show "play media to" item when Previously Connected device is available
2. Click "Play media to" to launch output slice
3. Update test case

Bug: 126475101
Test: make -j50 RunSettingsRoboTests
Change-Id: Id8afd1a2407acb78c11e81d2420ae8c16130a321
This commit is contained in:
timhypeng
2019-03-04 16:32:47 +08:00
committed by tim peng
parent 94240c5a52
commit abdf739071
8 changed files with 350 additions and 522 deletions

View File

@@ -17,16 +17,15 @@
package com.android.settings.sound;
import static android.media.AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP;
import static android.media.AudioSystem.DEVICE_OUT_EARPIECE;
import static android.media.AudioSystem.DEVICE_OUT_HEARING_AID;
import static android.media.AudioSystem.DEVICE_OUT_REMOTE_SUBMIX;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -34,9 +33,10 @@ import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
@@ -49,11 +49,13 @@ import com.android.settingslib.bluetooth.BluetoothEventManager;
import com.android.settingslib.bluetooth.HearingAidProfile;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
import com.android.settingslib.media.MediaOutputSliceConstants;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
@@ -80,8 +82,6 @@ public class MediaOutputPreferenceControllerTest {
private static final String TEST_DEVICE_ADDRESS_2 = "00:B2:B2:B2:B2:B2";
private static final String TEST_DEVICE_ADDRESS_3 = "00:C3:C3:C3:C3:C3";
private static final String TEST_DEVICE_ADDRESS_4 = "00:D4:D4:D4:D4:D4";
private final static long HISYNCID1 = 10;
private final static long HISYNCID2 = 11;
@Mock
private LocalBluetoothManager mLocalManager;
@@ -98,7 +98,7 @@ public class MediaOutputPreferenceControllerTest {
private Context mContext;
private PreferenceScreen mScreen;
private ListPreference mPreference;
private Preference mPreference;
private AudioManager mAudioManager;
private ShadowAudioManager mShadowAudioManager;
private BluetoothManager mBluetoothManager;
@@ -108,8 +108,8 @@ public class MediaOutputPreferenceControllerTest {
private BluetoothDevice mLeftBluetoothHapDevice;
private BluetoothDevice mRightBluetoothHapDevice;
private LocalBluetoothManager mLocalBluetoothManager;
private AudioSwitchPreferenceController mController;
private List<BluetoothDevice> mProfileConnectedDevices;
private MediaOutputPreferenceController mController;
private List<BluetoothDevice> mProfileConnectableDevices;
private List<BluetoothDevice> mHearingAidActiveDevices;
@Before
@@ -149,8 +149,8 @@ public class MediaOutputPreferenceControllerTest {
mController = new MediaOutputPreferenceController(mContext, TEST_KEY);
mScreen = spy(new PreferenceScreen(mContext, null));
mPreference = new ListPreference(mContext);
mProfileConnectedDevices = new ArrayList<>();
mPreference = new Preference(mContext);
mProfileConnectableDevices = new ArrayList<>();
mHearingAidActiveDevices = new ArrayList<>(2);
when(mScreen.getPreferenceManager()).thenReturn(mock(PreferenceManager.class));
@@ -166,61 +166,140 @@ public class MediaOutputPreferenceControllerTest {
ShadowBluetoothUtils.reset();
}
/**
* In normal mode, bluetooth device with HisyncId.
* HearingAidProfile should set active device to this device.
* A2DP Bluetooth device(s) are not connected nor previously connected
* Preference should be invisible
*/
@Test
public void setActiveBluetoothDevice_btDeviceWithHisyncId_shouldSetBtDeviceActive() {
public void updateState_withoutConnectableBtDevice_preferenceInvisible() {
mShadowAudioManager.setOutputDevice(DEVICE_OUT_EARPIECE);
mAudioManager.setMode(AudioManager.MODE_NORMAL);
when(mHearingAidProfile.getHiSyncId(mLeftBluetoothHapDevice)).thenReturn(HISYNCID1);
mProfileConnectableDevices.clear();
when(mA2dpProfile.getConnectableDevices()).thenReturn(mProfileConnectableDevices);
mPreference.setVisible(true);
mController.setActiveBluetoothDevice(mLeftBluetoothHapDevice);
verify(mHearingAidProfile).setActiveDevice(mLeftBluetoothHapDevice);
verify(mA2dpProfile, never()).setActiveDevice(mLeftBluetoothHapDevice);
assertThat(mPreference.isVisible()).isTrue();
mController.updateState(mPreference);
assertThat(mPreference.isVisible()).isFalse();
}
/**
* In normal mode, bluetooth device without HisyncId.
* A2dpProfile should set active device to this device.
* A2DP Bluetooth device(s) are connectable, no matter active or inactive
* Preference should be visible
*/
@Test
public void setActiveBluetoothDevice_btDeviceWithoutHisyncId_shouldSetBtDeviceActive() {
public void updateState_withConnectableBtDevice_preferenceVisible() {
mShadowAudioManager.setOutputDevice(DEVICE_OUT_BLUETOOTH_A2DP);
mAudioManager.setMode(AudioManager.MODE_NORMAL);
mProfileConnectableDevices.clear();
mProfileConnectableDevices.add(mBluetoothDevice);
when(mA2dpProfile.getConnectableDevices()).thenReturn(mProfileConnectableDevices);
assertThat(mPreference.isVisible()).isFalse();
mController.setActiveBluetoothDevice(mBluetoothDevice);
// Without Active Bluetooth Device
mController.updateState(mPreference);
assertThat(mPreference.isVisible()).isTrue();
verify(mA2dpProfile).setActiveDevice(mBluetoothDevice);
verify(mHearingAidProfile, never()).setActiveDevice(mBluetoothDevice);
// With Active Bluetooth Device
when(mA2dpProfile.getActiveDevice()).thenReturn(mBluetoothDevice);
mController.updateState(mPreference);
assertThat(mPreference.isVisible()).isTrue();
}
/**
* In normal mode, set active device to "this device".
* A2dpProfile should set to null.
* HearingAidProfile should set to null.
* A2DP Bluetooth device(s) are connectable, but no device is set as activated
* Preference summary should be "This device"
*/
@Test
public void setActiveBluetoothDevice_setNull_shouldSetNullToBothProfiles() {
public void updateState_withConnectableBtDevice_withoutActiveBtDevice_setDefaultSummary() {
mShadowAudioManager.setOutputDevice(DEVICE_OUT_EARPIECE);
mAudioManager.setMode(AudioManager.MODE_NORMAL);
mProfileConnectableDevices.clear();
mProfileConnectableDevices.add(mBluetoothDevice);
mProfileConnectableDevices.add(mSecondBluetoothDevice);
when(mA2dpProfile.getConnectableDevices()).thenReturn(mProfileConnectableDevices);
when(mA2dpProfile.getActiveDevice()).thenReturn(null);
mController.setActiveBluetoothDevice(null);
verify(mA2dpProfile).setActiveDevice(null);
verify(mHearingAidProfile).setActiveDevice(null);
assertThat(mPreference.getSummary()).isNull();
mController.updateState(mPreference);
assertThat(mPreference.getSummary()).isEqualTo(
mContext.getText(R.string.media_output_default_summary));
}
/**
* During a call
* A2dpProfile should not set active device.
* A2DP Bluetooth device(s) are connected and active
* Preference summary should be device's name
*/
@Test
public void setActiveBluetoothDevice_duringACall_shouldNotSetActiveDeviceToA2dpProfile() {
mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
public void updateState_withActiveBtDevice_setActivatedDeviceName() {
mShadowAudioManager.setOutputDevice(DEVICE_OUT_BLUETOOTH_A2DP);
mAudioManager.setMode(AudioManager.MODE_NORMAL);
mProfileConnectableDevices.clear();
mProfileConnectableDevices.add(mBluetoothDevice);
mProfileConnectableDevices.add(mSecondBluetoothDevice);
when(mA2dpProfile.getConnectableDevices()).thenReturn(mProfileConnectableDevices);
when(mA2dpProfile.getActiveDevice()).thenReturn(mBluetoothDevice);
mController.setActiveBluetoothDevice(mBluetoothDevice);
assertThat(mPreference.getSummary()).isNull();
mController.updateState(mPreference);
assertThat(mPreference.getSummary()).isEqualTo(TEST_DEVICE_NAME_1);
}
verify(mA2dpProfile, times(0)).setActiveDevice(any(BluetoothDevice.class));
/**
* Hearing Aid device(s) are connectable, no matter active or inactive
* Preference should be visible
*/
@Test
public void updateState_withConnectableHADevice_preferenceVisible() {
mShadowAudioManager.setOutputDevice(DEVICE_OUT_HEARING_AID);
mAudioManager.setMode(AudioManager.MODE_NORMAL);
mHearingAidActiveDevices.clear();
mHearingAidActiveDevices.add(mLeftBluetoothHapDevice);
when(mHearingAidProfile.getConnectableDevices()).thenReturn(mHearingAidActiveDevices);
assertThat(mPreference.isVisible()).isFalse();
// Without Active Hearing Aid Device
mController.updateState(mPreference);
assertThat(mPreference.isVisible()).isTrue();
// With Active Hearing Aid Device
when(mHearingAidProfile.getActiveDevices()).thenReturn(mHearingAidActiveDevices);
mController.updateState(mPreference);
assertThat(mPreference.isVisible()).isTrue();
}
/**
* Hearing Aid device(s) are connected and active
* Preference summary should be device's name
*/
@Test
public void updateState_withActiveHADevice_setActivatedDeviceName() {
mShadowAudioManager.setOutputDevice(DEVICE_OUT_HEARING_AID);
mAudioManager.setMode(AudioManager.MODE_NORMAL);
mHearingAidActiveDevices.clear();
mHearingAidActiveDevices.add(mLeftBluetoothHapDevice);
when(mHearingAidProfile.getConnectableDevices()).thenReturn(mHearingAidActiveDevices);
when(mHearingAidProfile.getActiveDevices()).thenReturn(mHearingAidActiveDevices);
assertThat(mPreference.getSummary()).isNull();
mController.updateState(mPreference);
assertThat(mPreference.getSummary()).isEqualTo(TEST_HAP_DEVICE_NAME_1);
}
@Test
public void click_launch_outputSwitcherSlice() {
final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
mController.handlePreferenceTreeClick(mPreference);
verify(mContext, never()).startActivity(intentCaptor.capture());
mPreference.setKey(TEST_KEY);
mController.handlePreferenceTreeClick(mPreference);
verify(mContext).startActivity(intentCaptor.capture());
assertThat(intentCaptor.getValue().getAction())
.isEqualTo(MediaOutputSliceConstants.ACTION_MEDIA_OUTPUT);
}
/**
@@ -253,24 +332,6 @@ public class MediaOutputPreferenceControllerTest {
mContext.getText(R.string.media_out_summary_ongoing_call_state));
}
/**
* No available A2dp BT devices:
* Preference should be invisible
* Preference summary should be "This device"
*/
@Test
public void updateState_noAvailableA2dpBtDevices_shouldDisableAndSetDefaultSummary() {
mAudioManager.setMode(AudioManager.MODE_NORMAL);
List<BluetoothDevice> emptyDeviceList = new ArrayList<>();
when(mA2dpProfile.getConnectedDevices()).thenReturn(emptyDeviceList);
mController.updateState(mPreference);
assertThat(mPreference.isVisible()).isFalse();
String defaultString = mContext.getString(R.string.media_output_default_summary);
assertThat(mPreference.getSummary()).isEqualTo(defaultString);
}
/**
* Media stream is captured by something else (cast device):
* Preference should be invisible
@@ -287,235 +348,6 @@ public class MediaOutputPreferenceControllerTest {
assertThat(mPreference.getSummary()).isEqualTo(defaultString);
}
/**
* One A2DP Bluetooth device is available and active.
* Preference should be visible
* Preference summary should be the activated device name
*/
@Test
public void updateState_oneA2dpBtDeviceAreAvailable_shouldSetActivatedDeviceName() {
mAudioManager.setMode(AudioManager.MODE_NORMAL);
mShadowAudioManager.setOutputDevice(DEVICE_OUT_BLUETOOTH_A2DP);
mProfileConnectedDevices.clear();
mProfileConnectedDevices.add(mBluetoothDevice);
when(mA2dpProfile.getConnectedDevices()).thenReturn(mProfileConnectedDevices);
when(mA2dpProfile.getActiveDevice()).thenReturn(mBluetoothDevice);
mController.updateState(mPreference);
assertThat(mPreference.isVisible()).isTrue();
assertThat(mPreference.getSummary()).isEqualTo(TEST_DEVICE_NAME_1);
}
/**
* More than one A2DP Bluetooth devices are available, and second device is active.
* Preference should be visible
* Preference summary should be the activated device name
*/
@Test
public void updateState_moreThanOneA2DpBtDevicesAreAvailable_shouldSetActivatedDeviceName() {
mAudioManager.setMode(AudioManager.MODE_NORMAL);
mShadowAudioManager.setOutputDevice(DEVICE_OUT_BLUETOOTH_A2DP);
mProfileConnectedDevices.clear();
mProfileConnectedDevices.add(mBluetoothDevice);
mProfileConnectedDevices.add(mSecondBluetoothDevice);
when(mA2dpProfile.getConnectedDevices()).thenReturn(mProfileConnectedDevices);
when(mA2dpProfile.getActiveDevice()).thenReturn(mSecondBluetoothDevice);
mController.updateState(mPreference);
assertThat(mPreference.isVisible()).isTrue();
assertThat(mPreference.getSummary()).isEqualTo(TEST_DEVICE_NAME_2);
}
/**
* A2DP Bluetooth device(s) are available, but wired headset is plugged in and activated
* Preference should be visible
* Preference summary should be "This device"
*/
@Test
public void updateState_a2dpDevicesAvailableWiredHeadsetIsActivated_shouldSetDefaultSummary() {
mAudioManager.setMode(AudioManager.MODE_NORMAL);
mProfileConnectedDevices.clear();
mProfileConnectedDevices.add(mBluetoothDevice);
when(mLocalBluetoothProfileManager.getHearingAidProfile()).thenReturn(null);
when(mA2dpProfile.getConnectedDevices()).thenReturn(mProfileConnectedDevices);
when(mA2dpProfile.getActiveDevice()).thenReturn(null);
mController.updateState(mPreference);
assertThat(mPreference.isVisible()).isTrue();
assertThat(mPreference.getSummary()).isEqualTo(
mContext.getString(R.string.media_output_default_summary));
}
/**
* A2DP Bluetooth device(s) are available, but current device speaker is activated
* Preference should be visible
* Preference summary should be "This device"
*/
@Test
public void updateState_a2dpDevicesAvailableCurrentDeviceActivated_shouldSetDefaultSummary() {
mAudioManager.setMode(AudioManager.MODE_NORMAL);
mProfileConnectedDevices.clear();
mProfileConnectedDevices.add(mBluetoothDevice);
when(mA2dpProfile.getConnectedDevices()).thenReturn(mProfileConnectedDevices);
when(mA2dpProfile.getActiveDevice()).thenReturn(null);
mController.updateState(mPreference);
assertThat(mPreference.isVisible()).isTrue();
assertThat(mPreference.getSummary()).isEqualTo(
mContext.getString(R.string.media_output_default_summary));
}
/**
* One hearing aid profile Bluetooth device is available and active.
* Preference should be visible
* Preference summary should be the activated device name
*/
@Test
public void updateState_oneHapBtDeviceAreAvailable_shouldSetActivatedDeviceName() {
mAudioManager.setMode(AudioManager.MODE_NORMAL);
mShadowAudioManager.setOutputDevice(DEVICE_OUT_HEARING_AID);
mProfileConnectedDevices.clear();
mProfileConnectedDevices.add(mLeftBluetoothHapDevice);
mHearingAidActiveDevices.clear();
mHearingAidActiveDevices.add(mLeftBluetoothHapDevice);
when(mHearingAidProfile.getConnectedDevices()).thenReturn(mProfileConnectedDevices);
when(mHearingAidProfile.getActiveDevices()).thenReturn(mHearingAidActiveDevices);
when(mHearingAidProfile.getHiSyncId(mLeftBluetoothHapDevice)).thenReturn(HISYNCID1);
mController.updateState(mPreference);
assertThat(mPreference.isVisible()).isTrue();
assertThat(mPreference.getSummary()).isEqualTo(mLeftBluetoothHapDevice.getName());
}
/**
* More than one hearing aid profile Bluetooth devices are available, and second
* device is active.
* Preference should be visible
* Preference summary should be the activated device name
*/
@Test
public void updateState_moreThanOneHapBtDevicesAreAvailable_shouldSetActivatedDeviceName() {
mAudioManager.setMode(AudioManager.MODE_NORMAL);
mShadowAudioManager.setOutputDevice(DEVICE_OUT_HEARING_AID);
mProfileConnectedDevices.clear();
mProfileConnectedDevices.add(mLeftBluetoothHapDevice);
mProfileConnectedDevices.add(mRightBluetoothHapDevice);
mHearingAidActiveDevices.clear();
mHearingAidActiveDevices.add(mRightBluetoothHapDevice);
when(mHearingAidProfile.getConnectedDevices()).thenReturn(mProfileConnectedDevices);
when(mHearingAidProfile.getActiveDevices()).thenReturn(mHearingAidActiveDevices);
when(mHearingAidProfile.getHiSyncId(mLeftBluetoothHapDevice)).thenReturn(HISYNCID1);
when(mHearingAidProfile.getHiSyncId(mRightBluetoothHapDevice)).thenReturn(HISYNCID2);
mController.updateState(mPreference);
assertThat(mPreference.isVisible()).isTrue();
assertThat(mPreference.getSummary()).isEqualTo(mRightBluetoothHapDevice.getName());
}
/**
* Both hearing aid profile and A2dp Bluetooth devices are available, and two hearing aid
* profile devices with same HisyncId are active. Both of HAP device are active,
* "left" side HAP device is added first.
* Preference should be visible
* Preference summary should be the activated device name
* ConnectedDevice should not contain second HAP device with same HisyncId
*/
@Test
public void updateState_hapBtDeviceWithSameId_shouldSetActivatedDeviceName() {
mAudioManager.setMode(AudioManager.MODE_NORMAL);
mShadowAudioManager.setOutputDevice(DEVICE_OUT_HEARING_AID);
mProfileConnectedDevices.clear();
mProfileConnectedDevices.add(mBluetoothDevice);
//with same HisyncId, first one will remain in UI.
mProfileConnectedDevices.add(mLeftBluetoothHapDevice);
mProfileConnectedDevices.add(mRightBluetoothHapDevice);
mHearingAidActiveDevices.clear();
mHearingAidActiveDevices.add(mLeftBluetoothHapDevice);
mHearingAidActiveDevices.add(mRightBluetoothHapDevice);
when(mHearingAidProfile.getConnectedDevices()).thenReturn(mProfileConnectedDevices);
when(mHearingAidProfile.getActiveDevices()).thenReturn(mHearingAidActiveDevices);
when(mHearingAidProfile.getHiSyncId(mLeftBluetoothHapDevice)).thenReturn(HISYNCID1);
when(mHearingAidProfile.getHiSyncId(mRightBluetoothHapDevice)).thenReturn(HISYNCID1);
mController.updateState(mPreference);
assertThat(mPreference.isVisible()).isTrue();
assertThat(mPreference.getSummary()).isEqualTo(mLeftBluetoothHapDevice.getName());
assertThat(mController.mConnectedDevices.contains(mLeftBluetoothHapDevice)).isTrue();
assertThat(mController.mConnectedDevices.contains(mRightBluetoothHapDevice)).isFalse();
}
/**
* Both hearing aid profile and A2dp Bluetooth devices are available, and two hearing aid
* profile devices with same HisyncId. Both of HAP device are active,
* "right" side HAP device is added first.
* Preference should be visible
* Preference summary should be the activated device name
* ConnectedDevice should not contain second HAP device with same HisyncId
*/
@Test
public void updateState_hapBtDeviceWithSameIdButDifferentOrder_shouldSetActivatedDeviceName() {
mAudioManager.setMode(AudioManager.MODE_NORMAL);
mShadowAudioManager.setOutputDevice(DEVICE_OUT_HEARING_AID);
mProfileConnectedDevices.clear();
mProfileConnectedDevices.add(mBluetoothDevice);
//with same HisyncId, first one will remain in UI.
mProfileConnectedDevices.add(mRightBluetoothHapDevice);
mProfileConnectedDevices.add(mLeftBluetoothHapDevice);
mHearingAidActiveDevices.clear();
mHearingAidActiveDevices.add(mLeftBluetoothHapDevice);
mHearingAidActiveDevices.add(mRightBluetoothHapDevice);
when(mHearingAidProfile.getConnectedDevices()).thenReturn(mProfileConnectedDevices);
when(mHearingAidProfile.getActiveDevices()).thenReturn(mHearingAidActiveDevices);
when(mHearingAidProfile.getHiSyncId(mLeftBluetoothHapDevice)).thenReturn(HISYNCID1);
when(mHearingAidProfile.getHiSyncId(mRightBluetoothHapDevice)).thenReturn(HISYNCID1);
mController.updateState(mPreference);
assertThat(mPreference.isVisible()).isTrue();
assertThat(mPreference.getSummary()).isEqualTo(mRightBluetoothHapDevice.getName());
assertThat(mController.mConnectedDevices.contains(mRightBluetoothHapDevice)).isTrue();
assertThat(mController.mConnectedDevices.contains(mLeftBluetoothHapDevice)).isFalse();
}
/**
* Both hearing aid profile and A2dp Bluetooth devices are available, and two hearing aid
* profile devices with different HisyncId. One of HAP device is active.
* Preference should be visible
* Preference summary should be the activated device name
* ConnectedDevice should contain both HAP device with different HisyncId
*/
@Test
public void updateState_hapBtDeviceWithDifferentId_shouldSetActivatedDeviceName() {
mAudioManager.setMode(AudioManager.MODE_NORMAL);
mShadowAudioManager.setOutputDevice(DEVICE_OUT_HEARING_AID);
mProfileConnectedDevices.clear();
mProfileConnectedDevices.add(mBluetoothDevice);
mProfileConnectedDevices.add(mLeftBluetoothHapDevice);
mProfileConnectedDevices.add(mRightBluetoothHapDevice);
mHearingAidActiveDevices.clear();
mHearingAidActiveDevices.add(null);
mHearingAidActiveDevices.add(mRightBluetoothHapDevice);
when(mHearingAidProfile.getConnectedDevices()).thenReturn(mProfileConnectedDevices);
when(mHearingAidProfile.getActiveDevices()).thenReturn(mHearingAidActiveDevices);
when(mHearingAidProfile.getHiSyncId(mLeftBluetoothHapDevice)).thenReturn(HISYNCID1);
when(mHearingAidProfile.getHiSyncId(mRightBluetoothHapDevice)).thenReturn(HISYNCID2);
mController.updateState(mPreference);
assertThat(mPreference.isVisible()).isTrue();
assertThat(mPreference.getSummary()).isEqualTo(mRightBluetoothHapDevice.getName());
assertThat(mController.mConnectedDevices).containsExactly(mBluetoothDevice,
mLeftBluetoothHapDevice, mRightBluetoothHapDevice);
}
@Test
public void findActiveDevice_onlyA2dpDeviceActive_returnA2dpDevice() {
when(mLocalBluetoothProfileManager.getHearingAidProfile()).thenReturn(null);