diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/HeadsetProfile.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/HeadsetProfile.java index ee1219126fe35..f9f80eb0d3c44 100644 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/HeadsetProfile.java +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/HeadsetProfile.java @@ -163,6 +163,11 @@ public class HeadsetProfile implements LocalBluetoothProfile { return mService.getActiveDevice(); } + public boolean isAudioOn() { + if (mService == null) return false; + return mService.isAudioOn(); + } + public boolean isPreferred(BluetoothDevice device) { if (mService == null) return false; return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF; 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 new file mode 100644 index 0000000000000..117f4472825cf --- /dev/null +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/HeadsetProfileTest.java @@ -0,0 +1,60 @@ +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.BluetoothHeadset; +import android.bluetooth.BluetoothProfile; +import android.content.Context; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; + +@RunWith(RobolectricTestRunner.class) +public class HeadsetProfileTest { + + @Mock + private LocalBluetoothAdapter mAdapter; + @Mock + private CachedBluetoothDeviceManager mDeviceManager; + @Mock + private LocalBluetoothProfileManager mProfileManager; + @Mock + private BluetoothHeadset mService; + + private BluetoothProfile.ServiceListener mServiceListener; + private HeadsetProfile mProfile; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + Context context = spy(RuntimeEnvironment.application); + + doAnswer((invocation) -> { + mServiceListener = (BluetoothProfile.ServiceListener) invocation.getArguments()[1]; + return null; + }).when(mAdapter).getProfileProxy(any(Context.class), any(), eq(BluetoothProfile.HEADSET)); + + mProfile = new HeadsetProfile(context, mAdapter, mDeviceManager, mProfileManager); + mServiceListener.onServiceConnected(BluetoothProfile.HEADSET, mService); + } + + @Test + public void bluetoothProfile_shouldReturnTheAudioStatusFromBlueToothHeadsetService() { + when(mService.isAudioOn()).thenReturn(true); + assertThat(mProfile.isAudioOn()).isTrue(); + + when(mService.isAudioOn()).thenReturn(false); + assertThat(mProfile.isAudioOn()).isFalse(); + } +}