diff options
7 files changed, 166 insertions, 160 deletions
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/LocalBluetoothProfileManager.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/LocalBluetoothProfileManager.java index 3ebbf7e57f72..8bb8210631ed 100644 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/LocalBluetoothProfileManager.java +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/LocalBluetoothProfileManager.java @@ -18,6 +18,7 @@ package com.android.settingslib.bluetooth; import android.bluetooth.BluetoothA2dp; import android.bluetooth.BluetoothA2dpSink; +import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothHeadset; import android.bluetooth.BluetoothHeadsetClient; @@ -79,7 +80,6 @@ public class LocalBluetoothProfileManager { } private final Context mContext; - private final LocalBluetoothAdapter mLocalAdapter; private final CachedBluetoothDeviceManager mDeviceManager; private final BluetoothEventManager mEventManager; @@ -111,14 +111,13 @@ public class LocalBluetoothProfileManager { BluetoothEventManager eventManager) { mContext = context; - mLocalAdapter = adapter; mDeviceManager = deviceManager; mEventManager = eventManager; mUsePbapPce = mContext.getResources().getBoolean(R.bool.enable_pbap_pce_profile); // MAP Client is typically used in the same situations as PBAP Client mUseMapClient = mContext.getResources().getBoolean(R.bool.enable_pbap_pce_profile); // pass this reference to adapter and event manager (circular dependency) - mLocalAdapter.setProfileManager(this); + adapter.setProfileManager(this); updateLocalProfiles(); if (DEBUG) Log.d(TAG, "LocalBluetoothProfileManager construction complete"); @@ -128,26 +127,26 @@ public class LocalBluetoothProfileManager { * create profile instance according to bluetooth supported profile list */ void updateLocalProfiles() { - List<Integer> supportedList = mLocalAdapter.getSupportedProfiles(); + List<Integer> supportedList = BluetoothAdapter.getDefaultAdapter().getSupportedProfiles(); if (CollectionUtils.isEmpty(supportedList)) { if(DEBUG) Log.d(TAG, "supportedList is null"); return; } if (mA2dpProfile == null && supportedList.contains(BluetoothProfile.A2DP)) { 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, BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED); } if (mA2dpSinkProfile == null && supportedList.contains(BluetoothProfile.A2DP_SINK)) { 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, BluetoothA2dpSink.ACTION_CONNECTION_STATE_CHANGED); } if (mHeadsetProfile == null && supportedList.contains(BluetoothProfile.HEADSET)) { 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, BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED, BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED, @@ -155,7 +154,7 @@ public class LocalBluetoothProfileManager { } if (mHfpClientProfile == null && supportedList.contains(BluetoothProfile.HEADSET_CLIENT)) { 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, BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED, BluetoothHeadsetClient.ACTION_AUDIO_STATE_CHANGED, @@ -165,13 +164,13 @@ public class LocalBluetoothProfileManager { if (mMapClientProfile == null && supportedList.contains(BluetoothProfile.MAP_CLIENT)) { if(DEBUG) Log.d(TAG, "Adding local MAP CLIENT profile"); mMapClientProfile = - new MapClientProfile(mContext, mLocalAdapter, mDeviceManager,this); + new MapClientProfile(mContext, mDeviceManager,this); addProfile(mMapClientProfile, MapClientProfile.NAME, BluetoothMapClient.ACTION_CONNECTION_STATE_CHANGED); } } else if (mMapProfile == null && supportedList.contains(BluetoothProfile.MAP)) { 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); } if (mOppProfile == null && supportedList.contains(BluetoothProfile.OPP)) { @@ -182,26 +181,26 @@ public class LocalBluetoothProfileManager { } if (mHearingAidProfile == null && supportedList.contains(BluetoothProfile.HEARING_AID)) { if(DEBUG) Log.d(TAG, "Adding local Hearing Aid profile"); - mHearingAidProfile = new HearingAidProfile(mContext, mLocalAdapter, mDeviceManager, + mHearingAidProfile = new HearingAidProfile(mContext, mDeviceManager, this); addProfile(mHearingAidProfile, HearingAidProfile.NAME, BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED); } if (mHidProfile == null && supportedList.contains(BluetoothProfile.HID_HOST)) { 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, BluetoothHidHost.ACTION_CONNECTION_STATE_CHANGED); } if (mHidDeviceProfile == null && supportedList.contains(BluetoothProfile.HID_DEVICE)) { 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, BluetoothHidDevice.ACTION_CONNECTION_STATE_CHANGED); } if (mPanProfile == null && supportedList.contains(BluetoothProfile.PAN)) { if(DEBUG) Log.d(TAG, "Adding local PAN profile"); - mPanProfile = new PanProfile(mContext, mLocalAdapter); + mPanProfile = new PanProfile(mContext); addPanProfile(mPanProfile, PanProfile.NAME, BluetoothPan.ACTION_CONNECTION_STATE_CHANGED); } @@ -214,8 +213,7 @@ public class LocalBluetoothProfileManager { if (mUsePbapPce && mPbapClientProfile == null && supportedList.contains( BluetoothProfile.PBAP_CLIENT)) { if(DEBUG) Log.d(TAG, "Adding local PBAP Client profile"); - mPbapClientProfile = new PbapClientProfile(mContext, mLocalAdapter, mDeviceManager, - this); + mPbapClientProfile = new PbapClientProfile(mContext, mDeviceManager,this); addProfile(mPbapClientProfile, PbapClientProfile.NAME, BluetoothPbapClient.ACTION_CONNECTION_STATE_CHANGED); } @@ -271,7 +269,7 @@ public class LocalBluetoothProfileManager { CachedBluetoothDevice cachedDevice = mDeviceManager.findDevice(device); if (cachedDevice == null) { Log.w(TAG, "StateChangedHandler found new device: " + device); - cachedDevice = mDeviceManager.addDevice(mLocalAdapter, device); + cachedDevice = mDeviceManager.addDevice(device); } onReceiveInternal(intent, cachedDevice); } diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/A2dpProfileTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/A2dpProfileTest.java index b2ab45ce9dc5..29831a89027a 100644 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/A2dpProfileTest.java +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/A2dpProfileTest.java @@ -19,11 +19,11 @@ import static com.google.common.truth.Truth.assertThat; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import android.bluetooth.BluetoothA2dp; +import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothCodecConfig; import android.bluetooth.BluetoothCodecStatus; import android.bluetooth.BluetoothDevice; @@ -33,21 +33,23 @@ import android.content.res.Resources; import com.android.settingslib.R; import com.android.settingslib.SettingsLibRobolectricTestRunner; +import com.android.settingslib.testutils.shadow.ShadowBluetoothAdapter; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.robolectric.annotation.Config; +import org.robolectric.shadow.api.Shadow; @RunWith(SettingsLibRobolectricTestRunner.class) +@Config(shadows = {ShadowBluetoothAdapter.class}) public class A2dpProfileTest { @Mock Context mContext; @Mock - LocalBluetoothAdapter mAdapter; - @Mock CachedBluetoothDeviceManager mDeviceManager; @Mock LocalBluetoothProfileManager mProfileManager; @@ -58,20 +60,14 @@ public class A2dpProfileTest { BluetoothProfile.ServiceListener mServiceListener; A2dpProfile mProfile; + private ShadowBluetoothAdapter mShadowBluetoothAdapter; @Before public void setUp() { MockitoAnnotations.initMocks(this); - - // Capture the A2dpServiceListener our A2dpProfile will pass during its constructor, so that - // we can call its onServiceConnected method and get it to use our mock BluetoothA2dp - // 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); + mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter()); + mProfile = new A2dpProfile(mContext, mDeviceManager, mProfileManager); + mServiceListener = mShadowBluetoothAdapter.getServiceListener(); mServiceListener.onServiceConnected(BluetoothProfile.A2DP, mBluetoothA2dp); } diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManagerTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManagerTest.java index b33e9c3dd482..2d34f237a555 100644 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManagerTest.java +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManagerTest.java @@ -21,7 +21,6 @@ import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; -import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothProfile; @@ -59,8 +58,6 @@ public class CachedBluetoothDeviceManagerTest { private final BluetoothClass DEVICE_CLASS_2 = new BluetoothClass(BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE); @Mock - private LocalBluetoothAdapter mLocalAdapter; - @Mock private LocalBluetoothProfileManager mLocalProfileManager; @Mock private LocalBluetoothManager mLocalBluetoothManager; @@ -110,18 +107,17 @@ public class CachedBluetoothDeviceManagerTest { when(mLocalBluetoothManager.getEventManager()).thenReturn(mBluetoothEventManager); when(mLocalBluetoothManager.getProfileManager()).thenReturn(mLocalProfileManager); - when(mLocalAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON); when(mHfpProfile.isProfileReady()).thenReturn(true); when(mA2dpProfile.isProfileReady()).thenReturn(true); when(mPanProfile.isProfileReady()).thenReturn(true); when(mHearingAidProfile.isProfileReady()).thenReturn(true); mCachedDeviceManager = new CachedBluetoothDeviceManager(mContext, mLocalBluetoothManager); mCachedDevice1 = spy( - new CachedBluetoothDevice(mContext, mLocalAdapter, mLocalProfileManager, mDevice1)); + new CachedBluetoothDevice(mContext, mLocalProfileManager, mDevice1)); mCachedDevice2 = spy( - new CachedBluetoothDevice(mContext, mLocalAdapter, mLocalProfileManager, mDevice2)); + new CachedBluetoothDevice(mContext, mLocalProfileManager, mDevice2)); mCachedDevice3 = spy( - new CachedBluetoothDevice(mContext, mLocalAdapter, mLocalProfileManager, mDevice3)); + new CachedBluetoothDevice(mContext, mLocalProfileManager, mDevice3)); } /** @@ -129,11 +125,9 @@ public class CachedBluetoothDeviceManagerTest { */ @Test public void testAddDevice_validCachedDevices_devicesAdded() { - CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice1); + CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1); assertThat(cachedDevice1).isNotNull(); - CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice2); + CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2); assertThat(cachedDevice2).isNotNull(); Collection<CachedBluetoothDevice> devices = mCachedDeviceManager.getCachedDevicesCopy(); @@ -149,8 +143,7 @@ public class CachedBluetoothDeviceManagerTest { */ @Test public void testGetName_validCachedDevice_nameFound() { - CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice1); + CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1); assertThat(cachedDevice1).isNotNull(); assertThat(mCachedDeviceManager.getName(mDevice1)).isEqualTo(DEVICE_ALIAS_1); } @@ -160,8 +153,7 @@ public class CachedBluetoothDeviceManagerTest { */ @Test public void testOnDeviceNameUpdated_validName_nameUpdated() { - CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice1); + CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1); assertThat(cachedDevice1).isNotNull(); assertThat(cachedDevice1.getName()).isEqualTo(DEVICE_ALIAS_1); @@ -176,11 +168,9 @@ public class CachedBluetoothDeviceManagerTest { */ @Test public void testClearNonBondedDevices_bondedAndNonBondedDevices_nonBondedDevicesCleared() { - CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice1); + CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1); assertThat(cachedDevice1).isNotNull(); - CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice2); + CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2); assertThat(cachedDevice2).isNotNull(); when(mDevice1.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); @@ -231,11 +221,9 @@ public class CachedBluetoothDeviceManagerTest { */ @Test public void testOnHiSyncIdChanged_sameHiSyncId_populateInDifferentLists() { - CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice1); + CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1); assertThat(cachedDevice1).isNotNull(); - CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice2); + CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2); assertThat(cachedDevice2).isNotNull(); // Since both devices do not have hiSyncId, they should be added in mCachedDevices. @@ -266,11 +254,9 @@ public class CachedBluetoothDeviceManagerTest { */ @Test public void testOnHiSyncIdChanged_sameHiSyncIdAndOneConnected_chooseConnectedDevice() { - CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice1); + CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1); assertThat(cachedDevice1).isNotNull(); - CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice2); + CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2); assertThat(cachedDevice2).isNotNull(); cachedDevice1.onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED); cachedDevice2.onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED); @@ -303,11 +289,9 @@ public class CachedBluetoothDeviceManagerTest { */ @Test public void testOnHiSyncIdChanged_differentHiSyncId_populateInSameList() { - CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice1); + CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1); assertThat(cachedDevice1).isNotNull(); - CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice2); + CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2); assertThat(cachedDevice2).isNotNull(); // Since both devices do not have hiSyncId, they should be added in mCachedDevices. @@ -339,8 +323,7 @@ public class CachedBluetoothDeviceManagerTest { */ @Test public void testOnProfileConnectionStateChanged_singleDeviceConnected_visible() { - CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice1); + CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1); assertThat(cachedDevice1).isNotNull(); cachedDevice1.onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED); @@ -377,11 +360,9 @@ public class CachedBluetoothDeviceManagerTest { */ @Test public void testOnProfileConnectionStateChanged_twoDevicesConnected_oneDeviceVisible() { - CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice1); + CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1); assertThat(cachedDevice1).isNotNull(); - CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice2); + CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2); assertThat(cachedDevice2).isNotNull(); cachedDevice1.onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED); cachedDevice2.onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED); @@ -431,11 +412,9 @@ public class CachedBluetoothDeviceManagerTest { */ @Test public void testOnProfileConnectionStateChanged_twoDevicesDisconnected_oneDeviceVisible() { - CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice1); + CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1); assertThat(cachedDevice1).isNotNull(); - CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice2); + CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2); assertThat(cachedDevice2).isNotNull(); cachedDevice1.onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED); cachedDevice2.onProfileStateChanged(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED); @@ -486,11 +465,9 @@ public class CachedBluetoothDeviceManagerTest { */ @Test public void testOnDeviceUnpaired_bothHearingAidsPaired_removesItsPairFromList() { - CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice1); + CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1); assertThat(cachedDevice1).isNotNull(); - CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice2); + CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2); assertThat(cachedDevice2).isNotNull(); cachedDevice1.setHiSyncId(HISYNCID1); @@ -518,14 +495,11 @@ public class CachedBluetoothDeviceManagerTest { */ @Test public void testOnDeviceUnpaired_bothHearingAidsNotPaired_doesNotRemoveAnyDeviceFromList() { - CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice1); + CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1); assertThat(cachedDevice1).isNotNull(); - CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice2); + CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2); assertThat(cachedDevice2).isNotNull(); - CachedBluetoothDevice cachedDevice3 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice3); + CachedBluetoothDevice cachedDevice3 = mCachedDeviceManager.addDevice(mDevice3); assertThat(cachedDevice2).isNotNull(); cachedDevice1.setHiSyncId(HISYNCID1); @@ -570,8 +544,7 @@ public class CachedBluetoothDeviceManagerTest { doAnswer((invocation) -> HISYNCID1).when(mHearingAidProfile).getHiSyncId(mDevice1); doAnswer((invocation) -> HISYNCID1).when(mHearingAidProfile).getHiSyncId(mDevice2); - CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice1); + CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1); assertThat(cachedDevice1).isNotNull(); // The first hearing aid device should be populated in mCachedDevice and // mCachedDevicesMapForHearingAids. @@ -581,8 +554,7 @@ public class CachedBluetoothDeviceManagerTest { assertThat(mCachedDeviceManager.mCachedDevicesMapForHearingAids.values()) .contains(cachedDevice1); - CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice2); + CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2); assertThat(cachedDevice2).isNotNull(); // The second hearing aid device should be populated in mHearingAidDevicesNotAddedInCache. assertThat(mCachedDeviceManager.getCachedDevicesCopy()).hasSize(1); @@ -599,8 +571,7 @@ public class CachedBluetoothDeviceManagerTest { .getHearingAidProfile(); doAnswer((invocation) -> HISYNCID1).when(mHearingAidProfile).getHiSyncId(mDevice1); doAnswer((invocation) -> HISYNCID2).when(mHearingAidProfile).getHiSyncId(mDevice2); - CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice1); + CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1); assertThat(cachedDevice1).isNotNull(); // The first hearing aid device should be populated in mCachedDevice and // mCachedDevicesMapForHearingAids. @@ -610,8 +581,7 @@ public class CachedBluetoothDeviceManagerTest { assertThat(mCachedDeviceManager.mCachedDevicesMapForHearingAids.values()) .contains(cachedDevice1); - CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice2); + CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2); assertThat(cachedDevice2).isNotNull(); // The second hearing aid device should also be populated in mCachedDevice // and mCachedDevicesMapForHearingAids as its not a pair of the first one. @@ -680,8 +650,7 @@ public class CachedBluetoothDeviceManagerTest { */ @Test public void testOnBtClassChanged_validBtClass_classChanged() { - CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice1); + CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1); assertThat(cachedDevice1).isNotNull(); assertThat(cachedDevice1.getBtClass()).isEqualTo(DEVICE_CLASS_1); @@ -696,8 +665,7 @@ public class CachedBluetoothDeviceManagerTest { */ @Test public void testOnDeviceDisappeared_deviceBondedUnbonded_unbondedDeviceDisappeared() { - CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice1); + CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1); assertThat(cachedDevice1).isNotNull(); when(mDevice1.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); @@ -712,11 +680,9 @@ public class CachedBluetoothDeviceManagerTest { */ @Test public void testOnActiveDeviceChanged_connectedDevices_activeDeviceChanged() { - CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice1); + CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1); assertThat(cachedDevice1).isNotNull(); - CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice2); + CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2); assertThat(cachedDevice2).isNotNull(); when(mDevice1.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); @@ -777,11 +743,9 @@ public class CachedBluetoothDeviceManagerTest { */ @Test public void testOnActiveDeviceChanged_withA2dpAndHearingAid() { - CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice1); + CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1); assertThat(cachedDevice1).isNotNull(); - CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mLocalAdapter, - mDevice2); + CachedBluetoothDevice cachedDevice2 = mCachedDeviceManager.addDevice(mDevice2); assertThat(cachedDevice2).isNotNull(); when(mDevice1.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java index c39fb85428fa..034574faf6db 100644 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java @@ -26,7 +26,6 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.robolectric.Shadows.shadowOf; -import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothProfile; import android.content.Context; @@ -49,8 +48,6 @@ public class CachedBluetoothDeviceTest { private final static String DEVICE_ADDRESS = "AA:BB:CC:DD:EE:FF"; private final static String DEVICE_ALIAS_NEW = "TestAliasNew"; @Mock - private LocalBluetoothAdapter mAdapter; - @Mock private LocalBluetoothProfileManager mProfileManager; @Mock private HeadsetProfile mHfpProfile; @@ -73,13 +70,11 @@ public class CachedBluetoothDeviceTest { mContext = RuntimeEnvironment.application; mShadowAudioManager = shadowOf(mContext.getSystemService(AudioManager.class)); when(mDevice.getAddress()).thenReturn(DEVICE_ADDRESS); - when(mAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON); when(mHfpProfile.isProfileReady()).thenReturn(true); when(mA2dpProfile.isProfileReady()).thenReturn(true); when(mPanProfile.isProfileReady()).thenReturn(true); when(mHearingAidProfile.isProfileReady()).thenReturn(true); - mCachedDevice = spy( - new CachedBluetoothDevice(mContext, mAdapter, mProfileManager, mDevice)); + mCachedDevice = spy(new CachedBluetoothDevice(mContext, mProfileManager, mDevice)); doAnswer((invocation) -> mBatteryLevel).when(mCachedDevice).getBatteryLevel(); } @@ -477,7 +472,7 @@ public class CachedBluetoothDeviceTest { when(mDevice.getAliasName()).thenReturn(DEVICE_ALIAS); when(mDevice.getName()).thenReturn(DEVICE_NAME); CachedBluetoothDevice cachedBluetoothDevice = - new CachedBluetoothDevice(mContext, mAdapter, mProfileManager, mDevice); + new CachedBluetoothDevice(mContext, mProfileManager, mDevice); // Verify alias is returned on getName assertThat(cachedBluetoothDevice.getName()).isEqualTo(DEVICE_ALIAS); // Verify device is visible @@ -487,7 +482,7 @@ public class CachedBluetoothDeviceTest { @Test public void testDeviceName_testNameNotAvailable() { CachedBluetoothDevice cachedBluetoothDevice = - new CachedBluetoothDevice(mContext, mAdapter, mProfileManager, mDevice); + new CachedBluetoothDevice(mContext, mProfileManager, mDevice); // Verify device address is returned on getName assertThat(cachedBluetoothDevice.getName()).isEqualTo(DEVICE_ADDRESS); // Verify device is not visible @@ -504,7 +499,7 @@ public class CachedBluetoothDeviceTest { }).when(mDevice).setAlias(anyString()); when(mDevice.getName()).thenReturn(DEVICE_NAME); CachedBluetoothDevice cachedBluetoothDevice = - new CachedBluetoothDevice(mContext, mAdapter, mProfileManager, mDevice); + new CachedBluetoothDevice(mContext, mProfileManager, mDevice); // Verify alias is returned on getName assertThat(cachedBluetoothDevice.getName()).isEqualTo(DEVICE_ALIAS); // Verify null name does not get set diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/HeadsetProfileTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/HeadsetProfileTest.java index bc8be4d5c3dd..c0a1f0cda3ee 100644 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/HeadsetProfileTest.java +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/HeadsetProfileTest.java @@ -2,18 +2,17 @@ package com.android.settingslib.bluetooth; import static com.google.common.truth.Truth.assertThat; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; +import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothHeadset; import android.bluetooth.BluetoothProfile; import android.content.Context; import com.android.settingslib.SettingsLibRobolectricTestRunner; +import com.android.settingslib.testutils.shadow.ShadowBluetoothAdapter; import org.junit.Before; import org.junit.Test; @@ -21,13 +20,14 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; +import org.robolectric.shadow.api.Shadow; @RunWith(SettingsLibRobolectricTestRunner.class) +@Config(shadows = {ShadowBluetoothAdapter.class}) public class HeadsetProfileTest { @Mock - private LocalBluetoothAdapter mAdapter; - @Mock private CachedBluetoothDeviceManager mDeviceManager; @Mock private LocalBluetoothProfileManager mProfileManager; @@ -39,19 +39,18 @@ public class HeadsetProfileTest { private BluetoothDevice mBluetoothDevice; private BluetoothProfile.ServiceListener mServiceListener; private HeadsetProfile mProfile; + private ShadowBluetoothAdapter mShadowBluetoothAdapter; @Before public void setUp() { MockitoAnnotations.initMocks(this); Context context = spy(RuntimeEnvironment.application); + mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter()); - doAnswer((invocation) -> { - mServiceListener = (BluetoothProfile.ServiceListener) invocation.getArguments()[1]; - return null; - }).when(mAdapter).getProfileProxy(any(Context.class), any(), eq(BluetoothProfile.HEADSET)); when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); - mProfile = new HeadsetProfile(context, mAdapter, mDeviceManager, mProfileManager); + mProfile = new HeadsetProfile(context, mDeviceManager, mProfileManager); + mServiceListener = mShadowBluetoothAdapter.getServiceListener(); mServiceListener.onServiceConnected(BluetoothProfile.HEADSET, mService); } diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/LocalBluetoothProfileManagerTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/LocalBluetoothProfileManagerTest.java index af66f7a62146..f223176795b8 100644 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/LocalBluetoothProfileManagerTest.java +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/LocalBluetoothProfileManagerTest.java @@ -38,6 +38,7 @@ import android.content.Intent; import android.os.ParcelUuid; import com.android.settingslib.SettingsLibRobolectricTestRunner; +import com.android.settingslib.testutils.shadow.ShadowBluetoothAdapter; import org.junit.Before; import org.junit.Test; @@ -45,34 +46,38 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; +import org.robolectric.shadow.api.Shadow; import java.util.ArrayList; import java.util.List; @RunWith(SettingsLibRobolectricTestRunner.class) +@Config(shadows = {ShadowBluetoothAdapter.class}) public class LocalBluetoothProfileManagerTest { @Mock private CachedBluetoothDeviceManager mDeviceManager; @Mock private BluetoothEventManager mEventManager; @Mock - private LocalBluetoothAdapter mAdapter; - @Mock private BluetoothDevice mDevice; @Mock private CachedBluetoothDevice mCachedBluetoothDevice; private Context mContext; - private LocalBluetoothProfileManager mProfileManager; private Intent mIntent; + private LocalBluetoothAdapter mLocalBluetoothAdapter; + private LocalBluetoothProfileManager mProfileManager; + private ShadowBluetoothAdapter mShadowBluetoothAdapter; @Before public void setUp() { MockitoAnnotations.initMocks(this); mContext = spy(RuntimeEnvironment.application); - mEventManager = spy(new BluetoothEventManager(mAdapter, - mDeviceManager, mContext)); - when(mAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON); + mLocalBluetoothAdapter = LocalBluetoothAdapter.getInstance(); + mEventManager = spy(new BluetoothEventManager(mLocalBluetoothAdapter, mDeviceManager, + mContext)); + mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter()); when(mDeviceManager.findDevice(mDevice)).thenReturn(mCachedBluetoothDevice); } @@ -81,12 +86,10 @@ public class LocalBluetoothProfileManagerTest { */ @Test public void constructor_initiateHidAndHidDeviceProfile() { - when(mAdapter.getSupportedProfiles()).thenReturn( - generateList(new int[] {BluetoothProfile.HID_HOST})); - when(mAdapter.getSupportedProfiles()).thenReturn( - generateList(new int[] {BluetoothProfile.HID_HOST, BluetoothProfile.HID_DEVICE})); - mProfileManager = - new LocalBluetoothProfileManager(mContext, mAdapter, mDeviceManager, mEventManager); + mShadowBluetoothAdapter.setSupportedProfiles(generateList( + new int[] {BluetoothProfile.HID_HOST, BluetoothProfile.HID_DEVICE})); + mProfileManager = new LocalBluetoothProfileManager(mContext, mLocalBluetoothAdapter, + mDeviceManager, mEventManager); assertThat(mProfileManager.getHidProfile()).isNotNull(); assertThat(mProfileManager.getHidDeviceProfile()).isNotNull(); @@ -97,12 +100,12 @@ public class LocalBluetoothProfileManagerTest { */ @Test public void updateLocalProfiles_addA2dpToLocalProfiles() { - mProfileManager = - new LocalBluetoothProfileManager(mContext, mAdapter, mDeviceManager, mEventManager); + mProfileManager = new LocalBluetoothProfileManager(mContext, mLocalBluetoothAdapter, + mDeviceManager, mEventManager); assertThat(mProfileManager.getA2dpProfile()).isNull(); assertThat(mProfileManager.getHeadsetProfile()).isNull(); - when(mAdapter.getSupportedProfiles()).thenReturn(generateList( + mShadowBluetoothAdapter.setSupportedProfiles(generateList( new int[] {BluetoothProfile.A2DP})); mProfileManager.updateLocalProfiles(); @@ -115,10 +118,10 @@ public class LocalBluetoothProfileManagerTest { */ @Test public void updateProfiles_addHidProfileForRemoteDevice() { - when(mAdapter.getSupportedProfiles()).thenReturn(generateList( + mShadowBluetoothAdapter.setSupportedProfiles(generateList( new int[] {BluetoothProfile.HID_HOST})); - mProfileManager = - new LocalBluetoothProfileManager(mContext, mAdapter, mDeviceManager, mEventManager); + mProfileManager = new LocalBluetoothProfileManager(mContext, mLocalBluetoothAdapter, + mDeviceManager, mEventManager); ParcelUuid[] uuids = new ParcelUuid[]{BluetoothUuid.Hid}; ParcelUuid[] localUuids = new ParcelUuid[]{}; List<LocalBluetoothProfile> profiles = new ArrayList<>(); @@ -138,10 +141,10 @@ public class LocalBluetoothProfileManagerTest { */ @Test public void stateChangedHandler_receiveA2dpConnectionStateChanged_shouldDispatchCallback() { - when(mAdapter.getSupportedProfiles()).thenReturn(generateList( + mShadowBluetoothAdapter.setSupportedProfiles(generateList( new int[] {BluetoothProfile.A2DP})); - mProfileManager = new LocalBluetoothProfileManager(mContext, mAdapter, mDeviceManager, - mEventManager); + mProfileManager = new LocalBluetoothProfileManager(mContext, mLocalBluetoothAdapter, + mDeviceManager, mEventManager); // Refer to BluetoothControllerImpl, it will call setReceiverHandler after // LocalBluetoothProfileManager created. mEventManager.setReceiverHandler(null); @@ -162,10 +165,10 @@ public class LocalBluetoothProfileManagerTest { */ @Test public void stateChangedHandler_receiveHeadsetConnectionStateChanged_shouldDispatchCallback() { - when(mAdapter.getSupportedProfiles()).thenReturn(generateList( + mShadowBluetoothAdapter.setSupportedProfiles(generateList( new int[] {BluetoothProfile.HEADSET})); - mProfileManager = new LocalBluetoothProfileManager(mContext, mAdapter, mDeviceManager, - mEventManager); + mProfileManager = new LocalBluetoothProfileManager(mContext, mLocalBluetoothAdapter, + mDeviceManager, mEventManager); // Refer to BluetoothControllerImpl, it will call setReceiverHandler after // LocalBluetoothProfileManager created. mEventManager.setReceiverHandler(null); @@ -186,12 +189,10 @@ public class LocalBluetoothProfileManagerTest { */ @Test public void stateChangedHandler_receiveHAPConnectionStateChanged_shouldDispatchCallback() { - ArrayList<Integer> supportProfiles = new ArrayList<>(); - supportProfiles.add(BluetoothProfile.HEARING_AID); - when(mAdapter.getSupportedProfiles()).thenReturn(supportProfiles); - when(mAdapter.getUuids()).thenReturn(new ParcelUuid[]{BluetoothUuid.HearingAid}); - mProfileManager = new LocalBluetoothProfileManager(mContext, mAdapter, mDeviceManager, - mEventManager); + mShadowBluetoothAdapter.setSupportedProfiles(generateList( + new int[] {BluetoothProfile.HEARING_AID})); + mProfileManager = new LocalBluetoothProfileManager(mContext, mLocalBluetoothAdapter, + mDeviceManager, mEventManager); // Refer to BluetoothControllerImpl, it will call setReceiverHandler after // LocalBluetoothProfileManager created. mEventManager.setReceiverHandler(null); @@ -212,10 +213,10 @@ public class LocalBluetoothProfileManagerTest { */ @Test public void stateChangedHandler_receivePanConnectionStateChanged_shouldNotDispatchCallback() { - when(mAdapter.getSupportedProfiles()).thenReturn( - generateList(new int[] {BluetoothProfile.PAN})); - mProfileManager = new LocalBluetoothProfileManager(mContext, mAdapter, mDeviceManager, - mEventManager); + mShadowBluetoothAdapter.setSupportedProfiles(generateList( + new int[] {BluetoothProfile.PAN})); + mProfileManager = new LocalBluetoothProfileManager(mContext, mLocalBluetoothAdapter, + mDeviceManager, mEventManager); // Refer to BluetoothControllerImpl, it will call setReceiverHandler after // LocalBluetoothProfileManager created. mEventManager.setReceiverHandler(null); @@ -237,9 +238,9 @@ public class LocalBluetoothProfileManagerTest { @Test public void stateChangedHandler_receivePanConnectionStateChangedWithoutProfile_shouldNotRefresh () { - when(mAdapter.getSupportedProfiles()).thenReturn(null); - mProfileManager = new LocalBluetoothProfileManager(mContext, mAdapter, mDeviceManager, - mEventManager); + mShadowBluetoothAdapter.setSupportedProfiles(null); + mProfileManager = new LocalBluetoothProfileManager(mContext, mLocalBluetoothAdapter, + mDeviceManager, mEventManager); // Refer to BluetoothControllerImpl, it will call setReceiverHandler after // LocalBluetoothProfileManager created. mEventManager.setReceiverHandler(null); @@ -259,10 +260,10 @@ public class LocalBluetoothProfileManagerTest { */ @Test public void stateChangedHandler_receivePanConnectionStateChangedWithProfile_shouldRefresh() { - when(mAdapter.getSupportedProfiles()).thenReturn(generateList( + mShadowBluetoothAdapter.setSupportedProfiles(generateList( new int[] {BluetoothProfile.PAN})); - mProfileManager = new LocalBluetoothProfileManager(mContext, mAdapter, mDeviceManager, - mEventManager); + mProfileManager = new LocalBluetoothProfileManager(mContext, mLocalBluetoothAdapter, + mDeviceManager, mEventManager); // Refer to BluetoothControllerImpl, it will call setReceiverHandler after // LocalBluetoothProfileManager created. mEventManager.setReceiverHandler(null); diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/testutils/shadow/ShadowBluetoothAdapter.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/testutils/shadow/ShadowBluetoothAdapter.java new file mode 100644 index 000000000000..9b8c230dded4 --- /dev/null +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/testutils/shadow/ShadowBluetoothAdapter.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settingslib.testutils.shadow; + +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothProfile; +import android.content.Context; + +import org.robolectric.annotation.Implementation; +import org.robolectric.annotation.Implements; + +import java.util.List; + +@Implements(value = BluetoothAdapter.class, inheritImplementationMethods = true) +public class ShadowBluetoothAdapter extends org.robolectric.shadows.ShadowBluetoothAdapter { + + private List<Integer> mSupportedProfiles; + private BluetoothProfile.ServiceListener mServiceListener; + + @Implementation + public boolean getProfileProxy(Context context, BluetoothProfile.ServiceListener listener, + int profile) { + mServiceListener = listener; + return true; + } + + public BluetoothProfile.ServiceListener getServiceListener() { + return mServiceListener; + } + + @Implementation + public List<Integer> getSupportedProfiles() { + return mSupportedProfiles; + } + + public void setSupportedProfiles(List<Integer> supportedProfiles) { + mSupportedProfiles = supportedProfiles; + } +} |