Clean up getConnectionSummary()

Bug: 119391779
Test: make -j56 RunSettingsLibRoboTests
Change-Id: I44245a51d27e5cc7e71a2be096f1b1cf400da04e
This commit is contained in:
wengsu
2018-11-15 16:51:45 +08:00
parent 2823d3abdd
commit 6d263c206e
2 changed files with 162 additions and 38 deletions

View File

@@ -887,39 +887,24 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
int stringRes = R.string.bluetooth_pairing;
//when profile is connected, information would be available
if (profileConnected) {
// Set default string with battery level in device connected situation.
if (batteryLevelPercentageString != null) {
stringRes = R.string.bluetooth_battery_level;
}
// Set active string in following device connected situation.
// 1. Hearing Aid device active.
// 2. Headset device active with in-calling state.
// 3. A2DP device active without in-calling state.
if (a2dpConnected || hfpConnected || hearingAidConnected) {
//contain battery information
if (batteryLevelPercentageString != null) {
//device is in phone call
if (com.android.settingslib.Utils.isAudioModeOngoingCall(mContext)) {
if (mIsActiveDeviceHearingAid || mIsActiveDeviceHeadset) {
stringRes = R.string.bluetooth_active_battery_level;
} else {
stringRes = R.string.bluetooth_battery_level;
}
} else {//device is not in phone call(ex. idle or playing media)
//need to check if A2DP and HearingAid are exclusive
if (mIsActiveDeviceHearingAid || mIsActiveDeviceA2dp) {
stringRes = R.string.bluetooth_active_battery_level;
} else {
stringRes = R.string.bluetooth_battery_level;
}
}
} else {
//no battery information
if (com.android.settingslib.Utils.isAudioModeOngoingCall(mContext)) {
if (mIsActiveDeviceHearingAid || mIsActiveDeviceHeadset) {
stringRes = R.string.bluetooth_active_no_battery_level;
}
} else {
if (mIsActiveDeviceHearingAid || mIsActiveDeviceA2dp) {
stringRes = R.string.bluetooth_active_no_battery_level;
}
}
}
} else {//unknown profile with battery information
if (batteryLevelPercentageString != null) {
stringRes = R.string.bluetooth_battery_level;
final boolean isOnCall =
com.android.settingslib.Utils.isAudioModeOngoingCall(mContext);
if ((mIsActiveDeviceHearingAid)
|| (mIsActiveDeviceHeadset && isOnCall)
|| (mIsActiveDeviceA2dp && !isOnCall)) {
stringRes = (batteryLevelPercentageString != null)
? R.string.bluetooth_active_battery_level
: R.string.bluetooth_active_no_battery_level;
}
}
}

View File

@@ -85,6 +85,17 @@ public class CachedBluetoothDeviceTest {
doAnswer((invocation) -> mBatteryLevel).when(mCachedDevice).getBatteryLevel();
}
@Test
public void getConnectionSummary_testProfilesInactive_returnPairing() {
// Arrange:
// Bond State: Bonding
when(mDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDING);
// Act & Assert:
// Get "Pairing…" result without Battery Level.
assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Pairing…");
}
@Test
public void getConnectionSummary_testSingleProfileConnectDisconnect() {
// Test without battery level
@@ -181,6 +192,49 @@ public class CachedBluetoothDeviceTest {
assertThat(mCachedDevice.getConnectionSummary()).isNull();
}
@Test
public void getConnectionSummary_testA2dpBatteryInactive_returnBattery() {
// Arrange:
// 1. Profile: {A2DP, CONNECTED, Inactive}
// 2. Battery Level: 10
updateProfileStatus(mA2dpProfile, BluetoothProfile.STATE_CONNECTED);
mBatteryLevel = 10;
// Act & Assert:
// Get "10% battery" result without Battery Level.
assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("10% battery");
}
@Test
public void getConnectionSummary_testA2dpInCall_returnNull() {
// Arrange:
// 1. Profile: {A2DP, Connected, Active}
// 2. Audio Manager: In Call
updateProfileStatus(mA2dpProfile, BluetoothProfile.STATE_CONNECTED);
mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP);
mShadowAudioManager.setMode(AudioManager.MODE_IN_CALL);
// Act & Assert:
// Get null result without Battery Level.
assertThat(mCachedDevice.getConnectionSummary()).isNull();
}
@Test
public void getConnectionSummary_testA2dpBatteryInCall_returnBattery() {
// Arrange:
// 1. Profile: {A2DP, Connected, Active}
// 3. Battery Level: 10
// 2. Audio Manager: In Call
updateProfileStatus(mA2dpProfile, BluetoothProfile.STATE_CONNECTED);
mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.A2DP);
mBatteryLevel = 10;
mShadowAudioManager.setMode(AudioManager.MODE_IN_CALL);
// Act & Assert:
// Get "10% battery" result with Battery Level 10.
assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("10% battery");
}
@Test
public void getConnectionSummary_testSingleProfileActiveDeviceHfp() {
// Test without battery level
@@ -215,6 +269,47 @@ public class CachedBluetoothDeviceTest {
assertThat(mCachedDevice.getConnectionSummary()).isNull();
}
@Test
public void getConnectionSummary_testHeadsetBatteryInactive_returnBattery() {
// Arrange:
// 1. Profile: {HEADSET, CONNECTED, Inactive}
// 2. Battery Level: 10
updateProfileStatus(mHfpProfile, BluetoothProfile.STATE_CONNECTED);
mBatteryLevel = 10;
// Act & Assert:
// Get "10% battery" result without Battery Level.
assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("10% battery");
}
@Test
public void getConnectionSummary_testHeadsetWithoutInCall_returnNull() {
// Arrange:
// 1. Profile: {HEADSET, Connected, Active}
// 2. Audio Manager: Normal (Without In Call)
updateProfileStatus(mHfpProfile, BluetoothProfile.STATE_CONNECTED);
mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEADSET);
// Act & Assert:
// Get null result without Battery Level.
assertThat(mCachedDevice.getConnectionSummary()).isNull();
}
@Test
public void getConnectionSummary_testHeadsetBatteryWithoutInCall_returnBattery() {
// Arrange:
// 1. Profile: {HEADSET, Connected, Active}
// 2. Battery Level: 10
// 3. Audio Manager: Normal (Without In Call)
updateProfileStatus(mHfpProfile, BluetoothProfile.STATE_CONNECTED);
mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEADSET);
mBatteryLevel = 10;
// Act & Assert:
// Get "10% battery" result with Battery Level 10.
assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("10% battery");
}
@Test
public void getConnectionSummary_testSingleProfileActiveDeviceHearingAid() {
// Test without battery level
@@ -234,11 +329,38 @@ public class CachedBluetoothDeviceTest {
}
@Test
public void getConnectionSummary_testHearingAidInCall_active() {
public void getConnectionSummary_testHearingAidBatteryInactive_returnBattery() {
// Arrange:
// 1. Profile: {HEARING_AID, CONNECTED, Inactive}
// 2. Battery Level: 10
updateProfileStatus(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED);
mBatteryLevel = 10;
// Act & Assert:
// Get "10% battery" result without Battery Level.
assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("10% battery");
}
@Test
public void getConnectionSummary_testHearingAidBatteryWithoutInCall_returnActiveBattery() {
// Arrange:
// 1. Profile: {HEARING_AID, Connected, Active}
// 2. Battery Level: 10
// 3. Audio Manager: Normal (Without In Call)
updateProfileStatus(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED);
mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEARING_AID);
mBatteryLevel = 10;
// Act & Assert:
// Get "Active, 10% battery" result with Battery Level 10.
assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Active, 10% battery");
}
@Test
public void getConnectionSummary_testHearingAidInCall_returnActive() {
// Arrange:
// 1. Profile: {HEARING_AID, Connected, Active}
// 2. Audio Manager: In Call
// 3. Battery Level: Unknown
updateProfileStatus(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED);
mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEARING_AID);
mShadowAudioManager.setMode(AudioManager.MODE_IN_CALL);
@@ -249,15 +371,15 @@ public class CachedBluetoothDeviceTest {
}
@Test
public void getConnectionSummary_testHearingAidInCall_activeBattery10() {
public void getConnectionSummary_testHearingAidBatteryInCall_returnActiveBattery() {
// Arrange:
// 1. Profile: {HEARING_AID, Connected, Active}
// 2. Audio Manager: In Call
// 3. Battery Level: 10
// 2. Battery Level: 10
// 3. Audio Manager: In Call
updateProfileStatus(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED);
mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEARING_AID);
mShadowAudioManager.setMode(AudioManager.MODE_IN_CALL);
mBatteryLevel = 10;
mShadowAudioManager.setMode(AudioManager.MODE_IN_CALL);
// Act & Assert:
// Get "Active, 10% battery" result with Battery Level 10.
@@ -311,6 +433,23 @@ public class CachedBluetoothDeviceTest {
assertThat(mCachedDevice.getConnectionSummary()).isNull();
}
@Test
public void getConnectionSummary_testMultipleProfilesInactive_returnPairing() {
// Arrange:
// 1. Profile 1: {A2DP, CONNECTED, Inactive}
// 2. Profile 2: {HEADSET, CONNECTED, Inactive}
// 3. Profile 3: {HEARING_AID, CONNECTED, Inactive}
// 4. Bond State: Bonding
updateProfileStatus(mA2dpProfile, BluetoothProfile.STATE_CONNECTED);
updateProfileStatus(mHfpProfile, BluetoothProfile.STATE_CONNECTED);
updateProfileStatus(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED);
when(mDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDING);
// Act & Assert:
// Get "Pairing…" result without Battery Level.
assertThat(mCachedDevice.getConnectionSummary()).isEqualTo("Pairing…");
}
@Test
public void getCarConnectionSummary_singleProfileConnectDisconnect() {
// Test without battery level