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

Commit 2112a3d3 authored by William Escande's avatar William Escande
Browse files

HeadsetClient: Properly mock native

Bug: 295237486
Test: atest HeadsetClientServiceTest
Test: atest HeadsetClientStateMachineTest
Change-Id: I10dd2a3b941a34e95f1dc36aaf15b4070187a2ba
parent 852bb087
Loading
Loading
Loading
Loading
+24 −6
Original line number Diff line number Diff line
@@ -23,7 +23,9 @@ package com.android.bluetooth.hfpclient;
import android.bluetooth.BluetoothDevice;
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;
@@ -36,18 +38,26 @@ import java.util.Objects;
public class NativeInterface {
    private static final String TAG = "NativeInterface";
    private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);

    private AdapterService mAdapterService;

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

    private static final Object INSTANCE_LOCK = new Object();

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

    private NativeInterface() {
        mAdapterService = Objects.requireNonNull(AdapterService.getAdapterService(),
                "AdapterService cannot be null when NativeInterface init");
    }
    private static NativeInterface sInterface;
    private static final Object INSTANCE_LOCK = new Object();

    /**
     * This class is a singleton because native library should only be loaded once
@@ -56,11 +66,19 @@ public class NativeInterface {
     */
    public static NativeInterface getInstance() {
        synchronized (INSTANCE_LOCK) {
            if (sInterface == null) {
                sInterface = new NativeInterface();
            if (sInstance == null) {
                sInstance = new NativeInterface();
            }
            return sInstance;
        }
    }

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

    // Native wrappers to help unit testing
+4 −5
Original line number Diff line number Diff line
@@ -26,9 +26,7 @@ import static org.mockito.Mockito.verify;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothSinkAudioPolicy;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
@@ -38,14 +36,12 @@ import androidx.test.filters.MediumTest;
import androidx.test.rule.ServiceTestRule;
import androidx.test.runner.AndroidJUnit4;

import com.android.bluetooth.R;
import com.android.bluetooth.TestUtils;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.storage.DatabaseManager;

import org.junit.After;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
@@ -67,16 +63,18 @@ public class HeadsetClientServiceTest {

    @Mock private AdapterService mAdapterService;
    @Mock private HeadsetClientStateMachine mStateMachine;

    @Mock private NativeInterface mNativeInterface;
    @Mock private DatabaseManager mDatabaseManager;

    @Before
    public void setUp() throws Exception {
        mTargetContext = InstrumentationRegistry.getTargetContext();
        MockitoAnnotations.initMocks(this);

        TestUtils.setAdapterService(mAdapterService);
        doReturn(mDatabaseManager).when(mAdapterService).getDatabase();
        doReturn(true, false).when(mAdapterService).isStartedProfile(anyString());
        NativeInterface.setInstance(mNativeInterface);
        TestUtils.startService(mServiceRule, HeadsetClientService.class);
        // At this point the service should have started so check NOT null
        mService = HeadsetClientService.getHeadsetClientService();
@@ -89,6 +87,7 @@ public class HeadsetClientServiceTest {
    @After
    public void tearDown() throws Exception {
        TestUtils.stopService(mServiceRule, HeadsetClientService.class);
        NativeInterface.setInstance(null);
        mService = HeadsetClientService.getHeadsetClientService();
        Assert.assertNull(mService);
        TestUtils.clearAdapterService(mAdapterService);
+1 −2
Original line number Diff line number Diff line
@@ -97,7 +97,7 @@ public class HeadsetClientStateMachineTest {
    @Mock
    private AudioManager mAudioManager;

    private NativeInterface mNativeInterface;
    @Mock private NativeInterface mNativeInterface;

    private static final int STANDARD_WAIT_MILLIS = 1000;
    private static final int QUERY_CURRENT_CALLS_WAIT_MILLIS = 2000;
@@ -122,7 +122,6 @@ public class HeadsetClientStateMachineTest {
                .thenReturn(2000);

        TestUtils.setAdapterService(mAdapterService);
        mNativeInterface = spy(NativeInterface.getInstance());
        doReturn(true).when(mNativeInterface).sendAndroidAt(anyObject(), anyString());

        // This line must be called to make sure relevant objects are initialized properly
+1 −3
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ package com.android.bluetooth.hfpclient;
import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothAssignedNumbers;
@@ -45,7 +44,7 @@ public class VendorCommandResponseProcessorTest {

    private BluetoothAdapter mAdapter;
    private BluetoothDevice mTestDevice;
    private NativeInterface mNativeInterface;
    @Mock private NativeInterface mNativeInterface;
    private VendorCommandResponseProcessor mProcessor;

    @Mock
@@ -57,7 +56,6 @@ public class VendorCommandResponseProcessorTest {
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        TestUtils.setAdapterService(mAdapterService);
        mNativeInterface = spy(NativeInterface.getInstance());

        mAdapter = BluetoothAdapter.getDefaultAdapter();
        mTestDevice = mAdapter.getRemoteDevice("00:01:02:03:04:05");