Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8561d193 authored by ryanywlin's avatar ryanywlin
Browse files

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
parent 11866a42
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -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;
+60 −0
Original line number Diff line number Diff line
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();
    }
}