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

Commit 6574fbe9 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "reduce the usage of LocalBluetoothAdapter in LocalBluetoothProfileManager"

parents 87c9423c 45a477e2
Loading
Loading
Loading
Loading
+15 −17
Original line number Original line Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.settingslib.bluetooth;


import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothA2dpSink;
import android.bluetooth.BluetoothA2dpSink;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothHeadsetClient;
import android.bluetooth.BluetoothHeadsetClient;
@@ -79,7 +80,6 @@ public class LocalBluetoothProfileManager {
    }
    }


    private final Context mContext;
    private final Context mContext;
    private final LocalBluetoothAdapter mLocalAdapter;
    private final CachedBluetoothDeviceManager mDeviceManager;
    private final CachedBluetoothDeviceManager mDeviceManager;
    private final BluetoothEventManager mEventManager;
    private final BluetoothEventManager mEventManager;


@@ -111,14 +111,13 @@ public class LocalBluetoothProfileManager {
            BluetoothEventManager eventManager) {
            BluetoothEventManager eventManager) {
        mContext = context;
        mContext = context;


        mLocalAdapter = adapter;
        mDeviceManager = deviceManager;
        mDeviceManager = deviceManager;
        mEventManager = eventManager;
        mEventManager = eventManager;
        mUsePbapPce = mContext.getResources().getBoolean(R.bool.enable_pbap_pce_profile);
        mUsePbapPce = mContext.getResources().getBoolean(R.bool.enable_pbap_pce_profile);
        // MAP Client is typically used in the same situations as PBAP Client
        // MAP Client is typically used in the same situations as PBAP Client
        mUseMapClient = mContext.getResources().getBoolean(R.bool.enable_pbap_pce_profile);
        mUseMapClient = mContext.getResources().getBoolean(R.bool.enable_pbap_pce_profile);
        // pass this reference to adapter and event manager (circular dependency)
        // pass this reference to adapter and event manager (circular dependency)
        mLocalAdapter.setProfileManager(this);
        adapter.setProfileManager(this);


        updateLocalProfiles();
        updateLocalProfiles();
        if (DEBUG) Log.d(TAG, "LocalBluetoothProfileManager construction complete");
        if (DEBUG) Log.d(TAG, "LocalBluetoothProfileManager construction complete");
@@ -128,26 +127,26 @@ public class LocalBluetoothProfileManager {
     * create profile instance according to bluetooth supported profile list
     * create profile instance according to bluetooth supported profile list
     */
     */
    void updateLocalProfiles() {
    void updateLocalProfiles() {
        List<Integer> supportedList = mLocalAdapter.getSupportedProfiles();
        List<Integer> supportedList = BluetoothAdapter.getDefaultAdapter().getSupportedProfiles();
        if (CollectionUtils.isEmpty(supportedList)) {
        if (CollectionUtils.isEmpty(supportedList)) {
            if(DEBUG) Log.d(TAG, "supportedList is null");
            if(DEBUG) Log.d(TAG, "supportedList is null");
            return;
            return;
        }
        }
        if (mA2dpProfile == null && supportedList.contains(BluetoothProfile.A2DP)) {
        if (mA2dpProfile == null && supportedList.contains(BluetoothProfile.A2DP)) {
            if(DEBUG) Log.d(TAG, "Adding local A2DP profile");
            if(DEBUG) Log.d(TAG, "Adding local A2DP profile");
            mA2dpProfile = new A2dpProfile(mContext, mLocalAdapter, mDeviceManager, this);
            mA2dpProfile = new A2dpProfile(mContext, mDeviceManager, this);
            addProfile(mA2dpProfile, A2dpProfile.NAME,
            addProfile(mA2dpProfile, A2dpProfile.NAME,
                    BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
                    BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
        }
        }
        if (mA2dpSinkProfile == null && supportedList.contains(BluetoothProfile.A2DP_SINK)) {
        if (mA2dpSinkProfile == null && supportedList.contains(BluetoothProfile.A2DP_SINK)) {
            if(DEBUG) Log.d(TAG, "Adding local A2DP SINK profile");
            if(DEBUG) Log.d(TAG, "Adding local A2DP SINK profile");
            mA2dpSinkProfile = new A2dpSinkProfile(mContext, mLocalAdapter, mDeviceManager, this);
            mA2dpSinkProfile = new A2dpSinkProfile(mContext, mDeviceManager, this);
            addProfile(mA2dpSinkProfile, A2dpSinkProfile.NAME,
            addProfile(mA2dpSinkProfile, A2dpSinkProfile.NAME,
                    BluetoothA2dpSink.ACTION_CONNECTION_STATE_CHANGED);
                    BluetoothA2dpSink.ACTION_CONNECTION_STATE_CHANGED);
        }
        }
        if (mHeadsetProfile == null && supportedList.contains(BluetoothProfile.HEADSET)) {
        if (mHeadsetProfile == null && supportedList.contains(BluetoothProfile.HEADSET)) {
            if (DEBUG) Log.d(TAG, "Adding local HEADSET profile");
            if (DEBUG) Log.d(TAG, "Adding local HEADSET profile");
            mHeadsetProfile = new HeadsetProfile(mContext, mLocalAdapter, mDeviceManager, this);
            mHeadsetProfile = new HeadsetProfile(mContext, mDeviceManager, this);
            addHeadsetProfile(mHeadsetProfile, HeadsetProfile.NAME,
            addHeadsetProfile(mHeadsetProfile, HeadsetProfile.NAME,
                    BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED,
                    BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED,
                    BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED,
                    BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED,
@@ -155,7 +154,7 @@ public class LocalBluetoothProfileManager {
        }
        }
        if (mHfpClientProfile == null && supportedList.contains(BluetoothProfile.HEADSET_CLIENT)) {
        if (mHfpClientProfile == null && supportedList.contains(BluetoothProfile.HEADSET_CLIENT)) {
            if(DEBUG) Log.d(TAG, "Adding local HfpClient profile");
            if(DEBUG) Log.d(TAG, "Adding local HfpClient profile");
            mHfpClientProfile = new HfpClientProfile(mContext, mLocalAdapter, mDeviceManager, this);
            mHfpClientProfile = new HfpClientProfile(mContext, mDeviceManager, this);
            addHeadsetProfile(mHfpClientProfile, HfpClientProfile.NAME,
            addHeadsetProfile(mHfpClientProfile, HfpClientProfile.NAME,
                    BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED,
                    BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED,
                    BluetoothHeadsetClient.ACTION_AUDIO_STATE_CHANGED,
                    BluetoothHeadsetClient.ACTION_AUDIO_STATE_CHANGED,
@@ -165,13 +164,13 @@ public class LocalBluetoothProfileManager {
            if (mMapClientProfile == null && supportedList.contains(BluetoothProfile.MAP_CLIENT)) {
            if (mMapClientProfile == null && supportedList.contains(BluetoothProfile.MAP_CLIENT)) {
                if(DEBUG) Log.d(TAG, "Adding local MAP CLIENT profile");
                if(DEBUG) Log.d(TAG, "Adding local MAP CLIENT profile");
                mMapClientProfile =
                mMapClientProfile =
                        new MapClientProfile(mContext, mLocalAdapter, mDeviceManager,this);
                        new MapClientProfile(mContext, mDeviceManager,this);
                addProfile(mMapClientProfile, MapClientProfile.NAME,
                addProfile(mMapClientProfile, MapClientProfile.NAME,
                        BluetoothMapClient.ACTION_CONNECTION_STATE_CHANGED);
                        BluetoothMapClient.ACTION_CONNECTION_STATE_CHANGED);
            }
            }
        } else if (mMapProfile == null && supportedList.contains(BluetoothProfile.MAP)) {
        } else if (mMapProfile == null && supportedList.contains(BluetoothProfile.MAP)) {
            if(DEBUG) Log.d(TAG, "Adding local MAP profile");
            if(DEBUG) Log.d(TAG, "Adding local MAP profile");
            mMapProfile = new MapProfile(mContext, mLocalAdapter, mDeviceManager, this);
            mMapProfile = new MapProfile(mContext, mDeviceManager, this);
            addProfile(mMapProfile, MapProfile.NAME, BluetoothMap.ACTION_CONNECTION_STATE_CHANGED);
            addProfile(mMapProfile, MapProfile.NAME, BluetoothMap.ACTION_CONNECTION_STATE_CHANGED);
        }
        }
        if (mOppProfile == null && supportedList.contains(BluetoothProfile.OPP)) {
        if (mOppProfile == null && supportedList.contains(BluetoothProfile.OPP)) {
@@ -182,26 +181,26 @@ public class LocalBluetoothProfileManager {
        }
        }
        if (mHearingAidProfile == null && supportedList.contains(BluetoothProfile.HEARING_AID)) {
        if (mHearingAidProfile == null && supportedList.contains(BluetoothProfile.HEARING_AID)) {
            if(DEBUG) Log.d(TAG, "Adding local Hearing Aid profile");
            if(DEBUG) Log.d(TAG, "Adding local Hearing Aid profile");
            mHearingAidProfile = new HearingAidProfile(mContext, mLocalAdapter, mDeviceManager,
            mHearingAidProfile = new HearingAidProfile(mContext, mDeviceManager,
                    this);
                    this);
            addProfile(mHearingAidProfile, HearingAidProfile.NAME,
            addProfile(mHearingAidProfile, HearingAidProfile.NAME,
                    BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED);
                    BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED);
        }
        }
        if (mHidProfile == null && supportedList.contains(BluetoothProfile.HID_HOST)) {
        if (mHidProfile == null && supportedList.contains(BluetoothProfile.HID_HOST)) {
            if(DEBUG) Log.d(TAG, "Adding local HID_HOST profile");
            if(DEBUG) Log.d(TAG, "Adding local HID_HOST profile");
            mHidProfile = new HidProfile(mContext, mLocalAdapter, mDeviceManager, this);
            mHidProfile = new HidProfile(mContext, mDeviceManager, this);
            addProfile(mHidProfile, HidProfile.NAME,
            addProfile(mHidProfile, HidProfile.NAME,
                    BluetoothHidHost.ACTION_CONNECTION_STATE_CHANGED);
                    BluetoothHidHost.ACTION_CONNECTION_STATE_CHANGED);
        }
        }
        if (mHidDeviceProfile == null && supportedList.contains(BluetoothProfile.HID_DEVICE)) {
        if (mHidDeviceProfile == null && supportedList.contains(BluetoothProfile.HID_DEVICE)) {
            if(DEBUG) Log.d(TAG, "Adding local HID_DEVICE profile");
            if(DEBUG) Log.d(TAG, "Adding local HID_DEVICE profile");
            mHidDeviceProfile = new HidDeviceProfile(mContext, mLocalAdapter, mDeviceManager, this);
            mHidDeviceProfile = new HidDeviceProfile(mContext, mDeviceManager, this);
            addProfile(mHidDeviceProfile, HidDeviceProfile.NAME,
            addProfile(mHidDeviceProfile, HidDeviceProfile.NAME,
                    BluetoothHidDevice.ACTION_CONNECTION_STATE_CHANGED);
                    BluetoothHidDevice.ACTION_CONNECTION_STATE_CHANGED);
        }
        }
        if (mPanProfile == null && supportedList.contains(BluetoothProfile.PAN)) {
        if (mPanProfile == null && supportedList.contains(BluetoothProfile.PAN)) {
            if(DEBUG) Log.d(TAG, "Adding local PAN profile");
            if(DEBUG) Log.d(TAG, "Adding local PAN profile");
            mPanProfile = new PanProfile(mContext, mLocalAdapter);
            mPanProfile = new PanProfile(mContext);
            addPanProfile(mPanProfile, PanProfile.NAME,
            addPanProfile(mPanProfile, PanProfile.NAME,
                    BluetoothPan.ACTION_CONNECTION_STATE_CHANGED);
                    BluetoothPan.ACTION_CONNECTION_STATE_CHANGED);
        }
        }
@@ -214,8 +213,7 @@ public class LocalBluetoothProfileManager {
        if (mUsePbapPce && mPbapClientProfile == null && supportedList.contains(
        if (mUsePbapPce && mPbapClientProfile == null && supportedList.contains(
                BluetoothProfile.PBAP_CLIENT)) {
                BluetoothProfile.PBAP_CLIENT)) {
            if(DEBUG) Log.d(TAG, "Adding local PBAP Client profile");
            if(DEBUG) Log.d(TAG, "Adding local PBAP Client profile");
            mPbapClientProfile = new PbapClientProfile(mContext, mLocalAdapter, mDeviceManager,
            mPbapClientProfile = new PbapClientProfile(mContext, mDeviceManager,this);
                    this);
            addProfile(mPbapClientProfile, PbapClientProfile.NAME,
            addProfile(mPbapClientProfile, PbapClientProfile.NAME,
                    BluetoothPbapClient.ACTION_CONNECTION_STATE_CHANGED);
                    BluetoothPbapClient.ACTION_CONNECTION_STATE_CHANGED);
        }
        }
@@ -271,7 +269,7 @@ public class LocalBluetoothProfileManager {
            CachedBluetoothDevice cachedDevice = mDeviceManager.findDevice(device);
            CachedBluetoothDevice cachedDevice = mDeviceManager.findDevice(device);
            if (cachedDevice == null) {
            if (cachedDevice == null) {
                Log.w(TAG, "StateChangedHandler found new device: " + device);
                Log.w(TAG, "StateChangedHandler found new device: " + device);
                cachedDevice = mDeviceManager.addDevice(mLocalAdapter, device);
                cachedDevice = mDeviceManager.addDevice(device);
            }
            }
            onReceiveInternal(intent, cachedDevice);
            onReceiveInternal(intent, cachedDevice);
        }
        }
+9 −13
Original line number Original line Diff line number Diff line
@@ -19,11 +19,11 @@ import static com.google.common.truth.Truth.assertThat;


import static org.mockito.Matchers.any;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.when;


import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothCodecConfig;
import android.bluetooth.BluetoothCodecConfig;
import android.bluetooth.BluetoothCodecStatus;
import android.bluetooth.BluetoothCodecStatus;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothDevice;
@@ -33,21 +33,23 @@ import android.content.res.Resources;


import com.android.settingslib.R;
import com.android.settingslib.R;
import com.android.settingslib.SettingsLibRobolectricTestRunner;
import com.android.settingslib.SettingsLibRobolectricTestRunner;
import com.android.settingslib.testutils.shadow.ShadowBluetoothAdapter;


import org.junit.Before;
import org.junit.Before;
import org.junit.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;


@RunWith(SettingsLibRobolectricTestRunner.class)
@RunWith(SettingsLibRobolectricTestRunner.class)
@Config(shadows = {ShadowBluetoothAdapter.class})
public class A2dpProfileTest {
public class A2dpProfileTest {


    @Mock
    @Mock
    Context mContext;
    Context mContext;
    @Mock
    @Mock
    LocalBluetoothAdapter mAdapter;
    @Mock
    CachedBluetoothDeviceManager mDeviceManager;
    CachedBluetoothDeviceManager mDeviceManager;
    @Mock
    @Mock
    LocalBluetoothProfileManager mProfileManager;
    LocalBluetoothProfileManager mProfileManager;
@@ -58,20 +60,14 @@ public class A2dpProfileTest {
    BluetoothProfile.ServiceListener mServiceListener;
    BluetoothProfile.ServiceListener mServiceListener;


    A2dpProfile mProfile;
    A2dpProfile mProfile;
    private ShadowBluetoothAdapter mShadowBluetoothAdapter;


    @Before
    @Before
    public void setUp() {
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        MockitoAnnotations.initMocks(this);

        mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
        // Capture the A2dpServiceListener our A2dpProfile will pass during its constructor, so that
        mProfile = new A2dpProfile(mContext, mDeviceManager, mProfileManager);
        // we can call its onServiceConnected method and get it to use our mock BluetoothA2dp
        mServiceListener = mShadowBluetoothAdapter.getServiceListener();
        // object.
        doAnswer((invocation) -> {
            mServiceListener = (BluetoothProfile.ServiceListener) invocation.getArguments()[1];
            return null;
        }).when(mAdapter).getProfileProxy(any(Context.class), any(), eq(BluetoothProfile.A2DP));

        mProfile = new A2dpProfile(mContext, mAdapter, mDeviceManager, mProfileManager);
        mServiceListener.onServiceConnected(BluetoothProfile.A2DP, mBluetoothA2dp);
        mServiceListener.onServiceConnected(BluetoothProfile.A2DP, mBluetoothA2dp);
    }
    }


+35 −71
Original line number Original line Diff line number Diff line
@@ -21,7 +21,6 @@ import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.when;


import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothProfile;
@@ -59,8 +58,6 @@ public class CachedBluetoothDeviceManagerTest {
    private final BluetoothClass DEVICE_CLASS_2 =
    private final BluetoothClass DEVICE_CLASS_2 =
        new BluetoothClass(BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE);
        new BluetoothClass(BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE);
    @Mock
    @Mock
    private LocalBluetoothAdapter mLocalAdapter;
    @Mock
    private LocalBluetoothProfileManager mLocalProfileManager;
    private LocalBluetoothProfileManager mLocalProfileManager;
    @Mock
    @Mock
    private LocalBluetoothManager mLocalBluetoothManager;
    private LocalBluetoothManager mLocalBluetoothManager;
@@ -110,18 +107,17 @@ public class CachedBluetoothDeviceManagerTest {


        when(mLocalBluetoothManager.getEventManager()).thenReturn(mBluetoothEventManager);
        when(mLocalBluetoothManager.getEventManager()).thenReturn(mBluetoothEventManager);
        when(mLocalBluetoothManager.getProfileManager()).thenReturn(mLocalProfileManager);
        when(mLocalBluetoothManager.getProfileManager()).thenReturn(mLocalProfileManager);
        when(mLocalAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON);
        when(mHfpProfile.isProfileReady()).thenReturn(true);
        when(mHfpProfile.isProfileReady()).thenReturn(true);
        when(mA2dpProfile.isProfileReady()).thenReturn(true);
        when(mA2dpProfile.isProfileReady()).thenReturn(true);
        when(mPanProfile.isProfileReady()).thenReturn(true);
        when(mPanProfile.isProfileReady()).thenReturn(true);
        when(mHearingAidProfile.isProfileReady()).thenReturn(true);
        when(mHearingAidProfile.isProfileReady()).thenReturn(true);
        mCachedDeviceManager = new CachedBluetoothDeviceManager(mContext, mLocalBluetoothManager);
        mCachedDeviceManager = new CachedBluetoothDeviceManager(mContext, mLocalBluetoothManager);
        mCachedDevice1 = spy(
        mCachedDevice1 = spy(
            new CachedBluetoothDevice(mContext, mLocalAdapter, mLocalProfileManager, mDevice1));
            new CachedBluetoothDevice(mContext, mLocalProfileManager, mDevice1));
        mCachedDevice2 = spy(
        mCachedDevice2 = spy(
            new CachedBluetoothDevice(mContext, mLocalAdapter, mLocalProfileManager, mDevice2));
            new CachedBluetoothDevice(mContext, mLocalProfileManager, mDevice2));
        mCachedDevice3 = spy(
        mCachedDevice3 = spy(
            new CachedBluetoothDevice(mContext, mLocalAdapter, mLocalProfileManager, mDevice3));
            new CachedBluetoothDevice(mContext, mLocalProfileManager, mDevice3));
    }
    }


    /**
    /**
@@ -129,11 +125,9 @@ public class CachedBluetoothDeviceManagerTest {
     */
     */
    @Test
    @Test
    public void testAddDevice_validCachedDevices_devicesAdded() {
    public void testAddDevice_validCachedDevices_devicesAdded() {
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1);
                mDevice1);
        assertThat(cachedDevice1).isNotNull();
        assertThat(cachedDevice1).isNotNull();
        CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2);
                mDevice2);
        assertThat(cachedDevice2).isNotNull();
        assertThat(cachedDevice2).isNotNull();


        Collection<CachedBluetoothDevice> devices = mCachedDeviceManager.getCachedDevicesCopy();
        Collection<CachedBluetoothDevice> devices = mCachedDeviceManager.getCachedDevicesCopy();
@@ -149,8 +143,7 @@ public class CachedBluetoothDeviceManagerTest {
     */
     */
    @Test
    @Test
    public void testGetName_validCachedDevice_nameFound() {
    public void testGetName_validCachedDevice_nameFound() {
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1);
                mDevice1);
        assertThat(cachedDevice1).isNotNull();
        assertThat(cachedDevice1).isNotNull();
        assertThat(mCachedDeviceManager.getName(mDevice1)).isEqualTo(DEVICE_ALIAS_1);
        assertThat(mCachedDeviceManager.getName(mDevice1)).isEqualTo(DEVICE_ALIAS_1);
    }
    }
@@ -160,8 +153,7 @@ public class CachedBluetoothDeviceManagerTest {
     */
     */
    @Test
    @Test
    public void testOnDeviceNameUpdated_validName_nameUpdated() {
    public void testOnDeviceNameUpdated_validName_nameUpdated() {
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1);
                mDevice1);
        assertThat(cachedDevice1).isNotNull();
        assertThat(cachedDevice1).isNotNull();
        assertThat(cachedDevice1.getName()).isEqualTo(DEVICE_ALIAS_1);
        assertThat(cachedDevice1.getName()).isEqualTo(DEVICE_ALIAS_1);


@@ -176,11 +168,9 @@ public class CachedBluetoothDeviceManagerTest {
     */
     */
    @Test
    @Test
    public void testClearNonBondedDevices_bondedAndNonBondedDevices_nonBondedDevicesCleared() {
    public void testClearNonBondedDevices_bondedAndNonBondedDevices_nonBondedDevicesCleared() {
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1);
                mDevice1);
        assertThat(cachedDevice1).isNotNull();
        assertThat(cachedDevice1).isNotNull();
        CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2);
                mDevice2);
        assertThat(cachedDevice2).isNotNull();
        assertThat(cachedDevice2).isNotNull();


        when(mDevice1.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
        when(mDevice1.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
@@ -231,11 +221,9 @@ public class CachedBluetoothDeviceManagerTest {
     */
     */
    @Test
    @Test
    public void testOnHiSyncIdChanged_sameHiSyncId_populateInDifferentLists() {
    public void testOnHiSyncIdChanged_sameHiSyncId_populateInDifferentLists() {
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1);
            mDevice1);
        assertThat(cachedDevice1).isNotNull();
        assertThat(cachedDevice1).isNotNull();
        CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2);
            mDevice2);
        assertThat(cachedDevice2).isNotNull();
        assertThat(cachedDevice2).isNotNull();


        // Since both devices do not have hiSyncId, they should be added in mCachedDevices.
        // Since both devices do not have hiSyncId, they should be added in mCachedDevices.
@@ -266,11 +254,9 @@ public class CachedBluetoothDeviceManagerTest {
     */
     */
    @Test
    @Test
    public void testOnHiSyncIdChanged_sameHiSyncIdAndOneConnected_chooseConnectedDevice() {
    public void testOnHiSyncIdChanged_sameHiSyncIdAndOneConnected_chooseConnectedDevice() {
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1);
            mDevice1);
        assertThat(cachedDevice1).isNotNull();
        assertThat(cachedDevice1).isNotNull();
        CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2);
            mDevice2);
        assertThat(cachedDevice2).isNotNull();
        assertThat(cachedDevice2).isNotNull();
        cachedDevice1.onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED);
        cachedDevice1.onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED);
        cachedDevice2.onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED);
        cachedDevice2.onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED);
@@ -303,11 +289,9 @@ public class CachedBluetoothDeviceManagerTest {
     */
     */
    @Test
    @Test
    public void testOnHiSyncIdChanged_differentHiSyncId_populateInSameList() {
    public void testOnHiSyncIdChanged_differentHiSyncId_populateInSameList() {
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1);
            mDevice1);
        assertThat(cachedDevice1).isNotNull();
        assertThat(cachedDevice1).isNotNull();
        CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2);
            mDevice2);
        assertThat(cachedDevice2).isNotNull();
        assertThat(cachedDevice2).isNotNull();


        // Since both devices do not have hiSyncId, they should be added in mCachedDevices.
        // Since both devices do not have hiSyncId, they should be added in mCachedDevices.
@@ -339,8 +323,7 @@ public class CachedBluetoothDeviceManagerTest {
     */
     */
    @Test
    @Test
    public void testOnProfileConnectionStateChanged_singleDeviceConnected_visible() {
    public void testOnProfileConnectionStateChanged_singleDeviceConnected_visible() {
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1);
            mDevice1);
        assertThat(cachedDevice1).isNotNull();
        assertThat(cachedDevice1).isNotNull();
        cachedDevice1.onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED);
        cachedDevice1.onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED);


@@ -377,11 +360,9 @@ public class CachedBluetoothDeviceManagerTest {
     */
     */
    @Test
    @Test
    public void testOnProfileConnectionStateChanged_twoDevicesConnected_oneDeviceVisible() {
    public void testOnProfileConnectionStateChanged_twoDevicesConnected_oneDeviceVisible() {
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1);
            mDevice1);
        assertThat(cachedDevice1).isNotNull();
        assertThat(cachedDevice1).isNotNull();
        CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2);
            mDevice2);
        assertThat(cachedDevice2).isNotNull();
        assertThat(cachedDevice2).isNotNull();
        cachedDevice1.onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED);
        cachedDevice1.onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED);
        cachedDevice2.onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED);
        cachedDevice2.onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED);
@@ -431,11 +412,9 @@ public class CachedBluetoothDeviceManagerTest {
     */
     */
    @Test
    @Test
    public void testOnProfileConnectionStateChanged_twoDevicesDisconnected_oneDeviceVisible() {
    public void testOnProfileConnectionStateChanged_twoDevicesDisconnected_oneDeviceVisible() {
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1);
            mDevice1);
        assertThat(cachedDevice1).isNotNull();
        assertThat(cachedDevice1).isNotNull();
        CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2);
            mDevice2);
        assertThat(cachedDevice2).isNotNull();
        assertThat(cachedDevice2).isNotNull();
        cachedDevice1.onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED);
        cachedDevice1.onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED);
        cachedDevice2.onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED);
        cachedDevice2.onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED);
@@ -486,11 +465,9 @@ public class CachedBluetoothDeviceManagerTest {
     */
     */
    @Test
    @Test
    public void testOnDeviceUnpaired_bothHearingAidsPaired_removesItsPairFromList() {
    public void testOnDeviceUnpaired_bothHearingAidsPaired_removesItsPairFromList() {
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1);
            mDevice1);
        assertThat(cachedDevice1).isNotNull();
        assertThat(cachedDevice1).isNotNull();
        CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2);
            mDevice2);
        assertThat(cachedDevice2).isNotNull();
        assertThat(cachedDevice2).isNotNull();


        cachedDevice1.setHiSyncId(HISYNCID1);
        cachedDevice1.setHiSyncId(HISYNCID1);
@@ -518,14 +495,11 @@ public class CachedBluetoothDeviceManagerTest {
     */
     */
    @Test
    @Test
    public void testOnDeviceUnpaired_bothHearingAidsNotPaired_doesNotRemoveAnyDeviceFromList() {
    public void testOnDeviceUnpaired_bothHearingAidsNotPaired_doesNotRemoveAnyDeviceFromList() {
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1);
            mDevice1);
        assertThat(cachedDevice1).isNotNull();
        assertThat(cachedDevice1).isNotNull();
        CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2);
            mDevice2);
        assertThat(cachedDevice2).isNotNull();
        assertThat(cachedDevice2).isNotNull();
        CachedBluetoothDevice cachedDevice3 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice3 = mCachedDeviceManager.addDevice(mDevice3);
            mDevice3);
        assertThat(cachedDevice2).isNotNull();
        assertThat(cachedDevice2).isNotNull();


        cachedDevice1.setHiSyncId(HISYNCID1);
        cachedDevice1.setHiSyncId(HISYNCID1);
@@ -570,8 +544,7 @@ public class CachedBluetoothDeviceManagerTest {
        doAnswer((invocation) -> HISYNCID1).when(mHearingAidProfile).getHiSyncId(mDevice1);
        doAnswer((invocation) -> HISYNCID1).when(mHearingAidProfile).getHiSyncId(mDevice1);
        doAnswer((invocation) -> HISYNCID1).when(mHearingAidProfile).getHiSyncId(mDevice2);
        doAnswer((invocation) -> HISYNCID1).when(mHearingAidProfile).getHiSyncId(mDevice2);


        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1);
            mDevice1);
        assertThat(cachedDevice1).isNotNull();
        assertThat(cachedDevice1).isNotNull();
        // The first hearing aid device should be populated in mCachedDevice and
        // The first hearing aid device should be populated in mCachedDevice and
        // mCachedDevicesMapForHearingAids.
        // mCachedDevicesMapForHearingAids.
@@ -581,8 +554,7 @@ public class CachedBluetoothDeviceManagerTest {
        assertThat(mCachedDeviceManager.mCachedDevicesMapForHearingAids.values())
        assertThat(mCachedDeviceManager.mCachedDevicesMapForHearingAids.values())
            .contains(cachedDevice1);
            .contains(cachedDevice1);


        CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2);
            mDevice2);
        assertThat(cachedDevice2).isNotNull();
        assertThat(cachedDevice2).isNotNull();
        // The second hearing aid device should be populated in mHearingAidDevicesNotAddedInCache.
        // The second hearing aid device should be populated in mHearingAidDevicesNotAddedInCache.
        assertThat(mCachedDeviceManager.getCachedDevicesCopy()).hasSize(1);
        assertThat(mCachedDeviceManager.getCachedDevicesCopy()).hasSize(1);
@@ -599,8 +571,7 @@ public class CachedBluetoothDeviceManagerTest {
            .getHearingAidProfile();
            .getHearingAidProfile();
        doAnswer((invocation) -> HISYNCID1).when(mHearingAidProfile).getHiSyncId(mDevice1);
        doAnswer((invocation) -> HISYNCID1).when(mHearingAidProfile).getHiSyncId(mDevice1);
        doAnswer((invocation) -> HISYNCID2).when(mHearingAidProfile).getHiSyncId(mDevice2);
        doAnswer((invocation) -> HISYNCID2).when(mHearingAidProfile).getHiSyncId(mDevice2);
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1);
            mDevice1);
        assertThat(cachedDevice1).isNotNull();
        assertThat(cachedDevice1).isNotNull();
        // The first hearing aid device should be populated in mCachedDevice and
        // The first hearing aid device should be populated in mCachedDevice and
        // mCachedDevicesMapForHearingAids.
        // mCachedDevicesMapForHearingAids.
@@ -610,8 +581,7 @@ public class CachedBluetoothDeviceManagerTest {
        assertThat(mCachedDeviceManager.mCachedDevicesMapForHearingAids.values())
        assertThat(mCachedDeviceManager.mCachedDevicesMapForHearingAids.values())
            .contains(cachedDevice1);
            .contains(cachedDevice1);


        CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2);
            mDevice2);
        assertThat(cachedDevice2).isNotNull();
        assertThat(cachedDevice2).isNotNull();
        // The second hearing aid device should also be populated in mCachedDevice
        // The second hearing aid device should also be populated in mCachedDevice
        // and mCachedDevicesMapForHearingAids as its not a pair of the first one.
        // and mCachedDevicesMapForHearingAids as its not a pair of the first one.
@@ -680,8 +650,7 @@ public class CachedBluetoothDeviceManagerTest {
     */
     */
    @Test
    @Test
    public void testOnBtClassChanged_validBtClass_classChanged() {
    public void testOnBtClassChanged_validBtClass_classChanged() {
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1);
                mDevice1);
        assertThat(cachedDevice1).isNotNull();
        assertThat(cachedDevice1).isNotNull();
        assertThat(cachedDevice1.getBtClass()).isEqualTo(DEVICE_CLASS_1);
        assertThat(cachedDevice1.getBtClass()).isEqualTo(DEVICE_CLASS_1);


@@ -696,8 +665,7 @@ public class CachedBluetoothDeviceManagerTest {
     */
     */
    @Test
    @Test
    public void testOnDeviceDisappeared_deviceBondedUnbonded_unbondedDeviceDisappeared() {
    public void testOnDeviceDisappeared_deviceBondedUnbonded_unbondedDeviceDisappeared() {
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1);
                mDevice1);
        assertThat(cachedDevice1).isNotNull();
        assertThat(cachedDevice1).isNotNull();


        when(mDevice1.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
        when(mDevice1.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
@@ -712,11 +680,9 @@ public class CachedBluetoothDeviceManagerTest {
     */
     */
    @Test
    @Test
    public void testOnActiveDeviceChanged_connectedDevices_activeDeviceChanged() {
    public void testOnActiveDeviceChanged_connectedDevices_activeDeviceChanged() {
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1);
                mDevice1);
        assertThat(cachedDevice1).isNotNull();
        assertThat(cachedDevice1).isNotNull();
        CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2);
                mDevice2);
        assertThat(cachedDevice2).isNotNull();
        assertThat(cachedDevice2).isNotNull();


        when(mDevice1.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
        when(mDevice1.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
@@ -777,11 +743,9 @@ public class CachedBluetoothDeviceManagerTest {
     */
     */
    @Test
    @Test
    public void testOnActiveDeviceChanged_withA2dpAndHearingAid() {
    public void testOnActiveDeviceChanged_withA2dpAndHearingAid() {
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1);
                mDevice1);
        assertThat(cachedDevice1).isNotNull();
        assertThat(cachedDevice1).isNotNull();
        CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mLocalAdapter,
        CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2);
                mDevice2);
        assertThat(cachedDevice2).isNotNull();
        assertThat(cachedDevice2).isNotNull();


        when(mDevice1.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
        when(mDevice1.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
+4 −9

File changed.

Preview size limit exceeded, changes collapsed.

+9 −10

File changed.

Preview size limit exceeded, changes collapsed.

Loading