From 0ed31dcff196cb31db22bf67a617f1925f1e64fd Mon Sep 17 00:00:00 2001 From: Michał Narajowski Date: Mon, 6 Mar 2023 13:34:43 +0000 Subject: CachedBluetoothDevice: Fix merge conflict Bug: 268587046 Test: atest CachedBluetoothDeviceTest Merged-In: I9c50a7b759b7fbcd80f1f5706909ce0a6ddc5959 Change-Id: I46df1c8b43781dac076bc9a4a9f2ea32d20f6337 --- .../com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java | 2 ++ 1 file changed, 2 insertions(+) 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 315ab0aac878..3f19792f342c 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 @@ -74,6 +74,8 @@ public class CachedBluetoothDeviceTest { @Mock private HearingAidProfile mHearingAidProfile; @Mock + private LeAudioProfile mLeAudioProfile; + @Mock private BluetoothDevice mDevice; @Mock private BluetoothDevice mSubDevice; -- cgit v1.2.3-59-g8ed1b From fb4369ae83de1b1fd873a8444db27bc225a55a0d Mon Sep 17 00:00:00 2001 From: Michał Narajowski Date: Mon, 6 Mar 2023 11:28:55 +0000 Subject: CachedBluetoothDevice: Add tests for connect fail Bug: 268587046 Test: atest CachedBluetoothDeviceTest Change-Id: I3af271d0a67fa38fa1225f77fb76a6b5f855760b --- .../bluetooth/CachedBluetoothDeviceTest.java | 61 ++++++++++++++++++++++ 1 file changed, 61 insertions(+) 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 3f19792f342c..df3861a699b0 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 @@ -94,15 +94,76 @@ public class CachedBluetoothDeviceTest { mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter()); when(mDevice.getAddress()).thenReturn(DEVICE_ADDRESS); when(mHfpProfile.isProfileReady()).thenReturn(true); + when(mHfpProfile.getProfileId()).thenReturn(BluetoothProfile.HEADSET); when(mA2dpProfile.isProfileReady()).thenReturn(true); + when(mA2dpProfile.getProfileId()).thenReturn(BluetoothProfile.A2DP); when(mPanProfile.isProfileReady()).thenReturn(true); + when(mPanProfile.getProfileId()).thenReturn(BluetoothProfile.PAN); when(mHearingAidProfile.isProfileReady()).thenReturn(true); + when(mHearingAidProfile.getProfileId()).thenReturn(BluetoothProfile.HEARING_AID); + when(mLeAudioProfile.isProfileReady()).thenReturn(true); + when(mLeAudioProfile.getProfileId()).thenReturn(BluetoothProfile.LE_AUDIO); mCachedDevice = spy(new CachedBluetoothDevice(mContext, mProfileManager, mDevice)); mSubCachedDevice = spy(new CachedBluetoothDevice(mContext, mProfileManager, mSubDevice)); doAnswer((invocation) -> mBatteryLevel).when(mCachedDevice).getBatteryLevel(); doAnswer((invocation) -> mBatteryLevel).when(mSubCachedDevice).getBatteryLevel(); } + private void testTransitionFromConnectingToDisconnected( + LocalBluetoothProfile connectingProfile, LocalBluetoothProfile connectedProfile, + int connectionPolicy, String expectedSummary) { + // Arrange: + // At least one profile has to be connected + updateProfileStatus(connectedProfile, BluetoothProfile.STATE_CONNECTED); + // Set profile under test to CONNECTING + updateProfileStatus(connectingProfile, BluetoothProfile.STATE_CONNECTING); + // Set connection policy + when(connectingProfile.getConnectionPolicy(mDevice)).thenReturn(connectionPolicy); + + // Act & Assert: + // Get the expected connection summary. + updateProfileStatus(connectingProfile, BluetoothProfile.STATE_DISCONNECTED); + assertThat(mCachedDevice.getConnectionSummary()).isEqualTo(expectedSummary); + } + + @Test + public void onProfileStateChanged_testConnectingToDisconnected_policyAllowed_problem() { + String connectTimeoutString = mContext.getString(R.string.profile_connect_timeout_subtext); + + testTransitionFromConnectingToDisconnected(mA2dpProfile, mLeAudioProfile, + BluetoothProfile.CONNECTION_POLICY_ALLOWED, connectTimeoutString); + testTransitionFromConnectingToDisconnected(mHearingAidProfile, mLeAudioProfile, + BluetoothProfile.CONNECTION_POLICY_ALLOWED, connectTimeoutString); + testTransitionFromConnectingToDisconnected(mHfpProfile, mLeAudioProfile, + BluetoothProfile.CONNECTION_POLICY_ALLOWED, connectTimeoutString); + testTransitionFromConnectingToDisconnected(mLeAudioProfile, mA2dpProfile, + BluetoothProfile.CONNECTION_POLICY_ALLOWED, connectTimeoutString); + } + + @Test + public void onProfileStateChanged_testConnectingToDisconnected_policyForbidden_noProblem() { + testTransitionFromConnectingToDisconnected(mA2dpProfile, mLeAudioProfile, + BluetoothProfile.CONNECTION_POLICY_FORBIDDEN, null); + testTransitionFromConnectingToDisconnected(mHearingAidProfile, mLeAudioProfile, + BluetoothProfile.CONNECTION_POLICY_FORBIDDEN, null); + testTransitionFromConnectingToDisconnected(mHfpProfile, mLeAudioProfile, + BluetoothProfile.CONNECTION_POLICY_FORBIDDEN, null); + testTransitionFromConnectingToDisconnected(mLeAudioProfile, mA2dpProfile, + BluetoothProfile.CONNECTION_POLICY_FORBIDDEN, null); + } + + @Test + public void onProfileStateChanged_testConnectingToDisconnected_policyUnknown_noProblem() { + testTransitionFromConnectingToDisconnected(mA2dpProfile, mLeAudioProfile, + BluetoothProfile.CONNECTION_POLICY_UNKNOWN, null); + testTransitionFromConnectingToDisconnected(mHearingAidProfile, mLeAudioProfile, + BluetoothProfile.CONNECTION_POLICY_UNKNOWN, null); + testTransitionFromConnectingToDisconnected(mHfpProfile, mLeAudioProfile, + BluetoothProfile.CONNECTION_POLICY_UNKNOWN, null); + testTransitionFromConnectingToDisconnected(mLeAudioProfile, mA2dpProfile, + BluetoothProfile.CONNECTION_POLICY_UNKNOWN, null); + } + @Test public void getConnectionSummary_testProfilesInactive_returnPairing() { // Arrange: -- cgit v1.2.3-59-g8ed1b