From 8561d193f14214c39b2fd564ed0149019301bf08 Mon Sep 17 00:00:00 2001 From: ryanywlin Date: Wed, 28 Mar 2018 17:19:39 +0800 Subject: [PATCH] Add isAudioOn() in HeadsetProfile * Add this API for checking audio status from BlueTooth Headset Service Bug: 74130772 Test: ROBOTEST_FILTER="HeadsetProfileTest" make -j40 RunSettingsLibRoboTests Change-Id: I80b3dbc8b0f9926859ff08f6849cafca89e940a7 --- .../settingslib/bluetooth/HeadsetProfile.java | 5 ++ .../bluetooth/HeadsetProfileTest.java | 60 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/HeadsetProfileTest.java 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(); + } +}