diff --git a/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdater.java b/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdater.java index 14f55b81264..bd160e17527 100644 --- a/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdater.java +++ b/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdater.java @@ -39,6 +39,7 @@ public class AvailableMediaBluetoothDeviceUpdater extends BluetoothDeviceUpdater private final AudioManager mAudioManager; private final LocalBluetoothManager mLocalBtManager; + private int mAudioMode; public AvailableMediaBluetoothDeviceUpdater( Context context, @@ -47,21 +48,23 @@ public class AvailableMediaBluetoothDeviceUpdater extends BluetoothDeviceUpdater super(context, devicePreferenceCallback, metricsCategory); mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); mLocalBtManager = Utils.getLocalBtManager(context); + mAudioMode = mAudioManager.getMode(); } @Override public void onAudioModeChanged() { + // TODO: move to background thread + mAudioMode = mAudioManager.getMode(); forceUpdate(); } @Override public boolean isFilterMatched(CachedBluetoothDevice cachedDevice) { - final int audioMode = mAudioManager.getMode(); final int currentAudioProfile; - if (audioMode == AudioManager.MODE_RINGTONE - || audioMode == AudioManager.MODE_IN_CALL - || audioMode == AudioManager.MODE_IN_COMMUNICATION) { + if (mAudioMode == AudioManager.MODE_RINGTONE + || mAudioMode == AudioManager.MODE_IN_CALL + || mAudioMode == AudioManager.MODE_IN_COMMUNICATION) { // in phone call currentAudioProfile = BluetoothProfile.HEADSET; } else { diff --git a/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdater.java b/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdater.java index 2107569d86b..7cc874caba9 100644 --- a/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdater.java +++ b/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdater.java @@ -39,26 +39,29 @@ public class ConnectedBluetoothDeviceUpdater extends BluetoothDeviceUpdater { private static final String PREF_KEY_PREFIX = "connected_bt_"; private final AudioManager mAudioManager; + private int mAudioMode; public ConnectedBluetoothDeviceUpdater(Context context, DevicePreferenceCallback devicePreferenceCallback, int metricsCategory) { super(context, devicePreferenceCallback, metricsCategory); mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + mAudioMode = mAudioManager.getMode(); } @Override public void onAudioModeChanged() { + // TODO: move to background thread + mAudioMode = mAudioManager.getMode(); forceUpdate(); } @Override public boolean isFilterMatched(CachedBluetoothDevice cachedDevice) { - final int audioMode = mAudioManager.getMode(); final int currentAudioProfile; - if (audioMode == AudioManager.MODE_RINGTONE - || audioMode == AudioManager.MODE_IN_CALL - || audioMode == AudioManager.MODE_IN_COMMUNICATION) { + if (mAudioMode == AudioManager.MODE_RINGTONE + || mAudioMode == AudioManager.MODE_IN_CALL + || mAudioMode == AudioManager.MODE_IN_COMMUNICATION) { // in phone call currentAudioProfile = BluetoothProfile.HEADSET; } else { diff --git a/tests/robotests/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdaterTest.java b/tests/robotests/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdaterTest.java index 9609af4a5e3..2251c3bff5a 100644 --- a/tests/robotests/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdaterTest.java +++ b/tests/robotests/src/com/android/settings/bluetooth/AvailableMediaBluetoothDeviceUpdaterTest.java @@ -124,24 +124,17 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { when(mCachedBluetoothDevice.getDrawableWithDescription()).thenReturn(pairs); when(mCachedBluetoothDevice.getMemberDevice()).thenReturn(ImmutableSet.of()); - mBluetoothDeviceUpdater = - spy( - new AvailableMediaBluetoothDeviceUpdater( - mContext, mDevicePreferenceCallback, /* metricsCategory= */ 0)); - mBluetoothDeviceUpdater.setPrefContext(mContext); mPreference = new BluetoothDevicePreference( mContext, mCachedBluetoothDevice, false, BluetoothDevicePreference.SortType.TYPE_DEFAULT); - doNothing().when(mBluetoothDeviceUpdater).addPreference(any()); - doNothing().when(mBluetoothDeviceUpdater).removePreference(any()); } @Test public void onAudioModeChanged_hfpDeviceConnected_inCall_addPreference() { - mAudioManager.setMode(AudioManager.MODE_IN_CALL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL); when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class))) .thenReturn(true); when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); @@ -153,7 +146,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { @Test public void onAudioModeChanged_hfpDeviceConnected_notInCall_removePreference() { - mAudioManager.setMode(AudioManager.MODE_NORMAL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class))) .thenReturn(true); when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); @@ -165,7 +158,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { @Test public void onAudioModeChanged_a2dpDeviceConnected_inCall_removePreference() { - mAudioManager.setMode(AudioManager.MODE_IN_CALL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL); when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class))) .thenReturn(true); when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true); @@ -177,7 +170,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { @Test public void onAudioModeChanged_a2dpDeviceConnected_notInCall_addPreference() { - mAudioManager.setMode(AudioManager.MODE_NORMAL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class))) .thenReturn(true); when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true); @@ -189,7 +182,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { @Test public void onProfileConnectionStateChanged_a2dpDeviceConnected_notInCall_addPreference() { - mAudioManager.setMode(AudioManager.MODE_NORMAL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class))) .thenReturn(true); when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true); @@ -202,7 +195,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { @Test public void onProfileConnectionStateChanged_a2dpDeviceConnected_inCall_removePreference() { - mAudioManager.setMode(AudioManager.MODE_IN_CALL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL); when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class))) .thenReturn(true); when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true); @@ -215,7 +208,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { @Test public void onProfileConnectionStateChanged_hfpDeviceConnected_notInCall_removePreference() { - mAudioManager.setMode(AudioManager.MODE_NORMAL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class))) .thenReturn(true); when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); @@ -228,7 +221,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { @Test public void onProfileConnectionStateChanged_hfpDeviceConnected_inCall_addPreference() { - mAudioManager.setMode(AudioManager.MODE_IN_CALL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL); when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class))) .thenReturn(true); when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); @@ -241,7 +234,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { @Test public void onProfileConnectionStateChanged_ashaHearingAidConnected_notInCall_addPreference() { - mAudioManager.setMode(AudioManager.MODE_NORMAL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class))) .thenReturn(true); when(mCachedBluetoothDevice.isConnectedAshaHearingAidDevice()).thenReturn(true); @@ -256,7 +249,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { @Test public void onProfileConnectionStateChanged_ashaHearingAidConnected_inCall_addPreference() { - mAudioManager.setMode(AudioManager.MODE_IN_CALL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL); when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class))) .thenReturn(true); when(mCachedBluetoothDevice.isConnectedAshaHearingAidDevice()).thenReturn(true); @@ -272,7 +265,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { @Test public void onProfileConnectionStateChanged_leaConnected_notInCallSharingFlagOff_addPref() { mSetFlagsRule.disableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); - mAudioManager.setMode(AudioManager.MODE_NORMAL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class))) .thenReturn(true); when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true); @@ -292,7 +285,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { @Test public void onProfileConnectionStateChanged_leaConnected_notInCallNotInSharing_addPref() { mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); - mAudioManager.setMode(AudioManager.MODE_NORMAL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class))) .thenReturn(true); when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true); @@ -309,7 +302,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { @Test public void onProfileConnectionStateChanged_leaConnected_inCallSharingFlagOff_addPref() { mSetFlagsRule.disableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); - mAudioManager.setMode(AudioManager.MODE_IN_CALL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL); when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class))) .thenReturn(true); when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true); @@ -326,7 +319,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { @Test public void onProfileConnectionStateChanged_leaConnected_inCallNotInSharing_addPref() { mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); - mAudioManager.setMode(AudioManager.MODE_IN_CALL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL); when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class))) .thenReturn(true); when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true); @@ -344,7 +337,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { public void onProfileConnectionStateChanged_leaConnected_notInCallInSharing_removePref() { mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); mSetFlagsRule.disableFlags(Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX); - mAudioManager.setMode(AudioManager.MODE_NORMAL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class))) .thenReturn(true); when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true); @@ -367,7 +360,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { onProfileConnectionStateChanged_leaConnected_noInCallInSharing_hysteresis_removePref() { mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); mSetFlagsRule.enableFlags(Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX); - mAudioManager.setMode(AudioManager.MODE_NORMAL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class))) .thenReturn(true); when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true); @@ -388,7 +381,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { public void onProfileConnectionStateChanged_leaConnected_inCallSharing_removePref() { mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); mSetFlagsRule.disableFlags(Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX); - mAudioManager.setMode(AudioManager.MODE_NORMAL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class))) .thenReturn(true); when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true); @@ -410,7 +403,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { public void onProfileConnectionStateChanged_leaConnected_inCallSharing_hysteresis_removePref() { mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING); mSetFlagsRule.enableFlags(Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX); - mAudioManager.setMode(AudioManager.MODE_NORMAL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class))) .thenReturn(true); when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true); @@ -430,7 +423,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { @Test public void onProfileConnectionStateChanged_deviceIsNotInList_notInCall_invokesRemovePreference() { - mAudioManager.setMode(AudioManager.MODE_NORMAL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class))) .thenReturn(true); when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true); @@ -446,7 +439,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { @Test public void onProfileConnectionStateChanged_deviceIsNotInList_inCall_invokesRemovePreference() { - mAudioManager.setMode(AudioManager.MODE_IN_CALL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL); when(mBluetoothDeviceUpdater.isDeviceConnected(any(CachedBluetoothDevice.class))) .thenReturn(true); when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true); @@ -462,6 +455,7 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { @Test public void onProfileConnectionStateChanged_deviceDisconnected_removePreference() { + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); mBluetoothDeviceUpdater.onProfileConnectionStateChanged( mCachedBluetoothDevice, BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.A2DP); @@ -470,8 +464,19 @@ public class AvailableMediaBluetoothDeviceUpdaterTest { @Test public void onClick_Preference_setActive() { + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); mBluetoothDeviceUpdater.onPreferenceClick(mPreference); verify(mDevicePreferenceCallback).onDeviceClick(mPreference); } + + private void setUpDeviceUpdaterWithAudioMode(int audioMode) { + mAudioManager.setMode(audioMode); + mBluetoothDeviceUpdater = + spy(new AvailableMediaBluetoothDeviceUpdater( + mContext, mDevicePreferenceCallback, /* metricsCategory= */ 0)); + mBluetoothDeviceUpdater.setPrefContext(mContext); + doNothing().when(mBluetoothDeviceUpdater).addPreference(any()); + doNothing().when(mBluetoothDeviceUpdater).removePreference(any()); + } } diff --git a/tests/robotests/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdaterTest.java b/tests/robotests/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdaterTest.java index b2449dab39e..f68a8d4cf6a 100644 --- a/tests/robotests/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdaterTest.java +++ b/tests/robotests/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdaterTest.java @@ -112,16 +112,11 @@ public class ConnectedBluetoothDeviceUpdaterTest { when(mCachedBluetoothDevice.getAddress()).thenReturn(MAC_ADDRESS); when(mCachedBluetoothDevice.getDrawableWithDescription()).thenReturn(pairs); mShadowCachedBluetoothDeviceManager.setCachedDevicesCopy(mCachedDevices); - mBluetoothDeviceUpdater = spy(new ConnectedBluetoothDeviceUpdater(mContext, - mDevicePreferenceCallback, /* metricsCategory= */ 0)); - mBluetoothDeviceUpdater.setPrefContext(mContext); - doNothing().when(mBluetoothDeviceUpdater).addPreference(any()); - doNothing().when(mBluetoothDeviceUpdater).removePreference(any()); } @Test public void onAudioModeChanged_hfpDeviceConnected_notInCall_addPreference() { - mAudioManager.setMode(AudioManager.MODE_NORMAL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); when(mBluetoothDeviceUpdater. isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); @@ -133,7 +128,7 @@ public class ConnectedBluetoothDeviceUpdaterTest { @Test public void onAudioModeChanged_hfpDeviceConnected_inCall_removePreference() { - mAudioManager.setMode(AudioManager.MODE_IN_CALL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL); when(mBluetoothDeviceUpdater. isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); @@ -145,7 +140,7 @@ public class ConnectedBluetoothDeviceUpdaterTest { @Test public void onAudioModeChanged_a2dpDeviceConnected_notInCall_removePreference() { - mAudioManager.setMode(AudioManager.MODE_NORMAL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); when(mBluetoothDeviceUpdater. isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true); @@ -157,7 +152,7 @@ public class ConnectedBluetoothDeviceUpdaterTest { @Test public void onAudioModeChanged_a2dpDeviceConnected_inCall_addPreference() { - mAudioManager.setMode(AudioManager.MODE_IN_CALL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL); when(mBluetoothDeviceUpdater. isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true); @@ -169,7 +164,7 @@ public class ConnectedBluetoothDeviceUpdaterTest { @Test public void onProfileConnectionStateChanged_a2dpDeviceConnected_inCall_addPreference() { - mAudioManager.setMode(AudioManager.MODE_IN_CALL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL); when(mBluetoothDeviceUpdater. isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true); @@ -182,7 +177,7 @@ public class ConnectedBluetoothDeviceUpdaterTest { @Test public void onProfileConnectionStateChanged_deviceIsNotInList_inCall_invokesRemovePreference() { - mAudioManager.setMode(AudioManager.MODE_IN_CALL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL); when(mBluetoothDeviceUpdater. isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true); @@ -196,7 +191,7 @@ public class ConnectedBluetoothDeviceUpdaterTest { @Test public void onProfileConnectionStateChanged_a2dpDeviceConnected_notInCall_removePreference() { - mAudioManager.setMode(AudioManager.MODE_NORMAL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); when(mBluetoothDeviceUpdater. isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true); @@ -209,7 +204,7 @@ public class ConnectedBluetoothDeviceUpdaterTest { @Test public void onProfileConnectionStateChanged_hfpDeviceConnected_inCall_removePreference() { - mAudioManager.setMode(AudioManager.MODE_IN_CALL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL); when(mBluetoothDeviceUpdater. isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); @@ -222,7 +217,7 @@ public class ConnectedBluetoothDeviceUpdaterTest { @Test public void onProfileConnectionStateChanged_hfpDeviceConnected_notInCall_addPreference() { - mAudioManager.setMode(AudioManager.MODE_NORMAL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); when(mBluetoothDeviceUpdater. isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); @@ -236,7 +231,7 @@ public class ConnectedBluetoothDeviceUpdaterTest { @Test public void onProfileConnectionStateChanged_ashaHearingAidConnected_inCall_removePreference() { - mAudioManager.setMode(AudioManager.MODE_IN_CALL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL); when(mBluetoothDeviceUpdater. isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedAshaHearingAidDevice()).thenReturn(true); @@ -250,7 +245,7 @@ public class ConnectedBluetoothDeviceUpdaterTest { @Test public void onProfileConnectionStateChanged_ashaHearingAidConnected_notInCall_removePreference() { - mAudioManager.setMode(AudioManager.MODE_NORMAL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); when(mBluetoothDeviceUpdater. isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedAshaHearingAidDevice()).thenReturn(true); @@ -263,7 +258,7 @@ public class ConnectedBluetoothDeviceUpdaterTest { @Test public void onProfileConnectionStateChanged_leAudioDeviceConnected_inCall_removesPreference() { - mAudioManager.setMode(AudioManager.MODE_IN_CALL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL); when(mBluetoothDeviceUpdater .isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true); @@ -277,7 +272,7 @@ public class ConnectedBluetoothDeviceUpdaterTest { @Test public void onProfileConnectionStateChanged_leAudioDeviceConnected_notInCall_removesPreference() { - mAudioManager.setMode(AudioManager.MODE_NORMAL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); when(mBluetoothDeviceUpdater .isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true); @@ -290,7 +285,7 @@ public class ConnectedBluetoothDeviceUpdaterTest { @Test public void onProfileConnectionStateChanged_deviceIsNotInList_inCall_invokesRemovesPreference() { - mAudioManager.setMode(AudioManager.MODE_IN_CALL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_IN_CALL); when(mBluetoothDeviceUpdater .isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true); @@ -305,7 +300,7 @@ public class ConnectedBluetoothDeviceUpdaterTest { @Test public void onProfileConnectionStateChanged_deviceIsNotInList_notInCall_invokesRemovesPreference () { - mAudioManager.setMode(AudioManager.MODE_NORMAL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); when(mBluetoothDeviceUpdater .isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true); @@ -319,6 +314,7 @@ public class ConnectedBluetoothDeviceUpdaterTest { @Test public void onProfileConnectionStateChanged_deviceDisconnected_removePreference() { + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.A2DP); @@ -327,6 +323,7 @@ public class ConnectedBluetoothDeviceUpdaterTest { @Test public void addPreference_addPreference_shouldHideSecondTarget() { + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); BluetoothDevicePreference btPreference = new BluetoothDevicePreference(mContext, mCachedBluetoothDevice, true, BluetoothDevicePreference.SortType.TYPE_DEFAULT); @@ -340,7 +337,7 @@ public class ConnectedBluetoothDeviceUpdaterTest { @Test @RequiresFlagsEnabled(Flags.FLAG_ENABLE_HIDE_EXCLUSIVELY_MANAGED_BLUETOOTH_DEVICE) public void update_notExclusiveManagedDevice_addDevice() { - mAudioManager.setMode(AudioManager.MODE_NORMAL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); when(mBluetoothDeviceUpdater .isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); @@ -356,7 +353,7 @@ public class ConnectedBluetoothDeviceUpdaterTest { @RequiresFlagsEnabled(Flags.FLAG_ENABLE_HIDE_EXCLUSIVELY_MANAGED_BLUETOOTH_DEVICE) public void update_exclusivelyManagedDevice_packageNotInstalled_addDevice() throws Exception { - mAudioManager.setMode(AudioManager.MODE_NORMAL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); when(mBluetoothDeviceUpdater .isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); @@ -376,7 +373,7 @@ public class ConnectedBluetoothDeviceUpdaterTest { throws Exception { ApplicationInfo appInfo = new ApplicationInfo(); appInfo.enabled = false; - mAudioManager.setMode(AudioManager.MODE_NORMAL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); when(mBluetoothDeviceUpdater .isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); @@ -393,7 +390,7 @@ public class ConnectedBluetoothDeviceUpdaterTest { @RequiresFlagsEnabled(Flags.FLAG_ENABLE_HIDE_EXCLUSIVELY_MANAGED_BLUETOOTH_DEVICE) public void update_exclusivelyManagedDevice_packageInstalledAndEnabled_removePreference() throws Exception { - mAudioManager.setMode(AudioManager.MODE_NORMAL); + setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); when(mBluetoothDeviceUpdater .isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); @@ -407,4 +404,13 @@ public class ConnectedBluetoothDeviceUpdaterTest { verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); verify(mBluetoothDeviceUpdater, never()).addPreference(mCachedBluetoothDevice); } + + private void setUpDeviceUpdaterWithAudioMode(int audioMode) { + mAudioManager.setMode(audioMode); + mBluetoothDeviceUpdater = spy(new ConnectedBluetoothDeviceUpdater(mContext, + mDevicePreferenceCallback, /* metricsCategory= */ 0)); + mBluetoothDeviceUpdater.setPrefContext(mContext); + doNothing().when(mBluetoothDeviceUpdater).addPreference(any()); + doNothing().when(mBluetoothDeviceUpdater).removePreference(any()); + } }