Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6d263c20 authored by wengsu's avatar wengsu
Browse files

Clean up getConnectionSummary()

Bug: 119391779
Test: make -j56 RunSettingsLibRoboTests
Change-Id: I44245a51d27e5cc7e71a2be096f1b1cf400da04e
parent 2823d3ab
Loading
Loading
Loading
Loading
+17 −32
Original line number Original line Diff line number Diff line
@@ -887,39 +887,24 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
        int stringRes = R.string.bluetooth_pairing;
        int stringRes = R.string.bluetooth_pairing;
        //when profile is connected, information would be available
        //when profile is connected, information would be available
        if (profileConnected) {
        if (profileConnected) {
            if (a2dpConnected || hfpConnected || hearingAidConnected) {
            // Set default string with battery level in device connected situation.
                //contain battery information
            if (batteryLevelPercentageString != null) {
            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;
                stringRes = R.string.bluetooth_battery_level;
            }
            }
                    }

                } else {
            // Set active string in following device connected situation.
                    //no battery information
            //    1. Hearing Aid device active.
                    if (com.android.settingslib.Utils.isAudioModeOngoingCall(mContext)) {
            //    2. Headset device active with in-calling state.
                        if (mIsActiveDeviceHearingAid || mIsActiveDeviceHeadset) {
            //    3. A2DP device active without in-calling state.
                            stringRes = R.string.bluetooth_active_no_battery_level;
            if (a2dpConnected || hfpConnected || hearingAidConnected) {
                        }
                final boolean isOnCall =
                    } else {
                        com.android.settingslib.Utils.isAudioModeOngoingCall(mContext);
                        if (mIsActiveDeviceHearingAid || mIsActiveDeviceA2dp) {
                if ((mIsActiveDeviceHearingAid)
                            stringRes = R.string.bluetooth_active_no_battery_level;
                        || (mIsActiveDeviceHeadset && isOnCall)
                        }
                        || (mIsActiveDeviceA2dp && !isOnCall)) {
                    }
                    stringRes = (batteryLevelPercentageString != null)
                }
                            ? R.string.bluetooth_active_battery_level
            } else {//unknown profile with battery information
                            : R.string.bluetooth_active_no_battery_level;
                if (batteryLevelPercentageString != null) {
                    stringRes = R.string.bluetooth_battery_level;
                }
                }
            }
            }
        }
        }
+145 −6
Original line number Original line Diff line number Diff line
@@ -85,6 +85,17 @@ public class CachedBluetoothDeviceTest {
        doAnswer((invocation) -> mBatteryLevel).when(mCachedDevice).getBatteryLevel();
        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
    @Test
    public void getConnectionSummary_testSingleProfileConnectDisconnect() {
    public void getConnectionSummary_testSingleProfileConnectDisconnect() {
        // Test without battery level
        // Test without battery level
@@ -181,6 +192,49 @@ public class CachedBluetoothDeviceTest {
        assertThat(mCachedDevice.getConnectionSummary()).isNull();
        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
    @Test
    public void getConnectionSummary_testSingleProfileActiveDeviceHfp() {
    public void getConnectionSummary_testSingleProfileActiveDeviceHfp() {
        // Test without battery level
        // Test without battery level
@@ -215,6 +269,47 @@ public class CachedBluetoothDeviceTest {
        assertThat(mCachedDevice.getConnectionSummary()).isNull();
        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
    @Test
    public void getConnectionSummary_testSingleProfileActiveDeviceHearingAid() {
    public void getConnectionSummary_testSingleProfileActiveDeviceHearingAid() {
        // Test without battery level
        // Test without battery level
@@ -234,11 +329,38 @@ public class CachedBluetoothDeviceTest {
    }
    }


    @Test
    @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:
        // Arrange:
        //   1. Profile:       {HEARING_AID, Connected, Active}
        //   1. Profile:       {HEARING_AID, Connected, Active}
        //   2. Audio Manager: In Call
        //   2. Audio Manager: In Call
        //   3. Battery Level: Unknown
        updateProfileStatus(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED);
        updateProfileStatus(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED);
        mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEARING_AID);
        mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEARING_AID);
        mShadowAudioManager.setMode(AudioManager.MODE_IN_CALL);
        mShadowAudioManager.setMode(AudioManager.MODE_IN_CALL);
@@ -249,15 +371,15 @@ public class CachedBluetoothDeviceTest {
    }
    }


    @Test
    @Test
    public void getConnectionSummary_testHearingAidInCall_activeBattery10() {
    public void getConnectionSummary_testHearingAidBatteryInCall_returnActiveBattery() {
        // Arrange:
        // Arrange:
        //   1. Profile:       {HEARING_AID, Connected, Active}
        //   1. Profile:       {HEARING_AID, Connected, Active}
        //   2. Audio Manager: In Call
        //   2. Battery Level: 10
        //   3. Battery Level: 10
        //   3. Audio Manager: In Call
        updateProfileStatus(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED);
        updateProfileStatus(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED);
        mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEARING_AID);
        mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEARING_AID);
        mShadowAudioManager.setMode(AudioManager.MODE_IN_CALL);
        mBatteryLevel = 10;
        mBatteryLevel = 10;
        mShadowAudioManager.setMode(AudioManager.MODE_IN_CALL);


        // Act & Assert:
        // Act & Assert:
        //   Get "Active, 10% battery" result with Battery Level 10.
        //   Get "Active, 10% battery" result with Battery Level 10.
@@ -311,6 +433,23 @@ public class CachedBluetoothDeviceTest {
        assertThat(mCachedDevice.getConnectionSummary()).isNull();
        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
    @Test
    public void getCarConnectionSummary_singleProfileConnectDisconnect() {
    public void getCarConnectionSummary_singleProfileConnectDisconnect() {
        // Test without battery level
        // Test without battery level