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

Commit b7bef92f authored by William Escande's avatar William Escande
Browse files

HeadsetService: stop calling NativeInterface in test

Bug: 295237486
Test: atest HeadsetServiceTest
Change-Id: If642b272ffa044f2f440289124481acd5a656101
parent 679f5dd9
Loading
Loading
Loading
Loading
+21 −5
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.util.Log;

import com.android.bluetooth.Utils;
import com.android.bluetooth.btservice.AdapterService;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;

import java.util.Objects;
@@ -37,11 +38,18 @@ public class HeadsetNativeInterface {
    private final BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();

    static {
        if (Utils.isInstrumentationTestMode()) {
            Log.w(TAG, "App is instrumented. Skip loading the native");
        } else {
            classInitNative();
        }
    }

    @GuardedBy("INSTANCE_LOCK")
    private static HeadsetNativeInterface sInstance;

    private static HeadsetNativeInterface sInterface;
    private static final Object INSTANCE_LOCK = new Object();

    private AdapterService mAdapterService;

    private HeadsetNativeInterface() {
@@ -56,11 +64,19 @@ public class HeadsetNativeInterface {
     */
    public static HeadsetNativeInterface getInstance() {
        synchronized (INSTANCE_LOCK) {
            if (sInterface == null) {
                sInterface = new HeadsetNativeInterface();
            if (sInstance == null) {
                sInstance = new HeadsetNativeInterface();
            }
            return sInstance;
        }
    }
        return sInterface;

    /** Set singleton instance. */
    @VisibleForTesting
    public static void setInstance(HeadsetNativeInterface instance) {
        synchronized (INSTANCE_LOCK) {
            sInstance = instance;
        }
    }

    private void sendMessageToService(HeadsetStackEvent event) {
+5 −2
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ import com.android.bluetooth.a2dp.A2dpNativeInterface;
import com.android.bluetooth.btservice.storage.DatabaseManager;
import com.android.bluetooth.gatt.GattService;
import com.android.bluetooth.hearingaid.HearingAidNativeInterface;
import com.android.bluetooth.hfp.HeadsetNativeInterface;
import com.android.bluetooth.pan.PanNativeInterface;

import org.junit.After;
@@ -86,6 +87,7 @@ public class ProfileServiceTest {
    }

    @Mock private A2dpNativeInterface mA2dpNativeInterface;
    @Mock private HeadsetNativeInterface mHeadsetNativeInterface;
    @Mock private HearingAidNativeInterface mHearingAidNativeInterface;
    @Mock private PanNativeInterface mPanNativeInterface;

@@ -168,6 +170,7 @@ public class ProfileServiceTest {
                new String[0], false, "");
        mMockAdapterService.enableNative();
        A2dpNativeInterface.setInstance(mA2dpNativeInterface);
        HeadsetNativeInterface.setInstance(mHeadsetNativeInterface);
        HearingAidNativeInterface.setInstance(mHearingAidNativeInterface);
        PanNativeInterface.setInstance(mPanNativeInterface);
    }
@@ -182,13 +185,13 @@ public class ProfileServiceTest {
        mMockAdapterService = null;
        mProfiles = null;
        A2dpNativeInterface.setInstance(null);
        HeadsetNativeInterface.setInstance(null);
        HearingAidNativeInterface.setInstance(null);
        PanNativeInterface.setInstance(null);
    }

    /**
     * Test: Start the Bluetooth services that are configured.
     * Verify that the same services start.
     * Test: Start the Bluetooth services that are configured. Verify that the same services start.
     */
    @Test
    public void testEnableDisable() throws TimeoutException {
+2 −5
Original line number Diff line number Diff line
@@ -41,7 +41,6 @@ import android.os.ParcelUuid;
import android.os.PowerManager;
import android.os.RemoteException;
import android.telecom.PhoneAccount;
import android.util.Log;

import androidx.test.InstrumentationRegistry;
import androidx.test.espresso.intent.Intents;
@@ -98,7 +97,6 @@ public class HeadsetServiceAndStateMachineTest {
    private Context mTargetContext;
    private HeadsetService mHeadsetService;
    private BluetoothAdapter mAdapter;
    private HeadsetNativeInterface mNativeInterface;
    private ArgumentCaptor<HeadsetStateMachine> mStateMachineArgument =
            ArgumentCaptor.forClass(HeadsetStateMachine.class);
    private HashSet<BluetoothDevice> mBondedDevices = new HashSet<>();
@@ -109,6 +107,8 @@ public class HeadsetServiceAndStateMachineTest {
    private int mOriginalVrTimeoutMs = 5000;
    private PowerManager.WakeLock mVoiceRecognitionWakeLock;

    @Mock private HeadsetNativeInterface mNativeInterface;

    private class HeadsetIntentReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
@@ -196,9 +196,6 @@ public class HeadsetServiceAndStateMachineTest {
        doReturn(mVoiceRecognitionWakeLock).when(mSystemInterface).getVoiceRecognitionWakeLock();
        doReturn(true).when(mSystemInterface).isCallIdle();
        // Mock methods in HeadsetNativeInterface
        mNativeInterface = spy(HeadsetNativeInterface.getInstance());
        doNothing().when(mNativeInterface).init(anyInt(), anyBoolean());
        doNothing().when(mNativeInterface).cleanup();
        doReturn(true).when(mNativeInterface).connectHfp(any(BluetoothDevice.class));
        doReturn(true).when(mNativeInterface).disconnectHfp(any(BluetoothDevice.class));
        doReturn(true).when(mNativeInterface).connectAudio(any(BluetoothDevice.class));
+3 −3
Original line number Diff line number Diff line
@@ -26,7 +26,6 @@ import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@@ -87,7 +86,6 @@ public class HeadsetServiceTest {
    private Context mTargetContext;
    private HeadsetService mHeadsetService;
    private BluetoothAdapter mAdapter;
    private HeadsetNativeInterface mNativeInterface;
    private BluetoothDevice mCurrentDevice;
    private final HashMap<BluetoothDevice, HeadsetStateMachine> mStateMachines = new HashMap<>();

@@ -98,6 +96,7 @@ public class HeadsetServiceTest {
    @Mock private ActiveDeviceManager mActiveDeviceManager;
    @Mock private DatabaseManager mDatabaseManager;
    @Mock private HeadsetSystemInterface mSystemInterface;
    @Mock private HeadsetNativeInterface mNativeInterface;
    @Mock private AudioManager mAudioManager;
    @Mock private HeadsetPhoneState mPhoneState;

@@ -138,7 +137,6 @@ public class HeadsetServiceTest {
        when(mSystemInterface.getAudioManager()).thenReturn(mAudioManager);
        when(mSystemInterface.isCallIdle()).thenReturn(true, false, true, false);
        // Mock methods in HeadsetNativeInterface
        mNativeInterface = spy(HeadsetNativeInterface.getInstance());
        doNothing().when(mNativeInterface).init(anyInt(), anyBoolean());
        doNothing().when(mNativeInterface).cleanup();
        doReturn(true).when(mNativeInterface).connectHfp(any(BluetoothDevice.class));
@@ -158,6 +156,7 @@ public class HeadsetServiceTest {
        }).when(mObjectsFactory).makeStateMachine(any(), any(), any(), any(), any(), any());
        doReturn(mSystemInterface).when(mObjectsFactory).makeSystemInterface(any());
        doReturn(mNativeInterface).when(mObjectsFactory).getNativeInterface();
        HeadsetNativeInterface.setInstance(mNativeInterface);
        TestUtils.startService(mServiceRule, HeadsetService.class);
        mHeadsetService = HeadsetService.getHeadsetService();
        Assert.assertNotNull(mHeadsetService);
@@ -170,6 +169,7 @@ public class HeadsetServiceTest {
    @After
    public void tearDown() throws Exception {
        TestUtils.stopService(mServiceRule, HeadsetService.class);
        HeadsetNativeInterface.setInstance(null);
        mHeadsetService = HeadsetService.getHeadsetService();
        Assert.assertNull(mHeadsetService);
        mStateMachines.clear();
+1 −4
Original line number Diff line number Diff line
@@ -94,7 +94,7 @@ public class HeadsetStateMachineTest {
    @Mock private HeadsetPhoneState mPhoneState;
    @Mock private Intent mIntent;
    private MockContentResolver mMockContentResolver;
    private HeadsetNativeInterface mNativeInterface;
    @Mock private HeadsetNativeInterface mNativeInterface;

    @Before
    public void setUp() throws Exception {
@@ -114,9 +114,6 @@ public class HeadsetStateMachineTest {
        doReturn(true).when(mDatabaseManager).setAudioPolicyMetadata(anyObject(), anyObject());
        // Get an active device manager
        doReturn(mActiveDeviceManager).when(mAdapterService).getActiveDeviceManager();
        // Spy on native interface
        mNativeInterface = spy(HeadsetNativeInterface.getInstance());
        doNothing().when(mNativeInterface).init(anyInt(), anyBoolean());
        doReturn(true).when(mNativeInterface).connectHfp(mTestDevice);
        doReturn(true).when(mNativeInterface).disconnectHfp(mTestDevice);
        doReturn(true).when(mNativeInterface).connectAudio(mTestDevice);