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

Commit 2185e04b authored by David Duarte's avatar David Duarte Committed by William Escande
Browse files

HapClientTest: Pass NativeInterface in constructor

Test: atest HapClientTest
Test: atest ProfileServiceTest
Test: atest HapClientNativeInterfaceTest
Bug: 324268843
Flag: Exempt, test only change
Change-Id: Ia409c3e9ea24dae013569d39e0407b502cd3fdf1
parent bef85a13
Loading
Loading
Loading
Loading
+1 −27
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ import android.bluetooth.BluetoothHapPresetInfo;
import android.util.Log;

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

import java.util.ArrayList;
@@ -35,38 +34,13 @@ public class HapClientNativeInterface {

    private final BluetoothAdapter mAdapter;

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

    private static final Object INSTANCE_LOCK = new Object();

    private HapClientNativeInterface() {
    public HapClientNativeInterface() {
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mAdapter == null) {
            Log.wtf(TAG, "No Bluetooth Adapter Available");
        }
    }

    /**
     * Get singleton instance.
     */
    public static HapClientNativeInterface getInstance() {
        synchronized (INSTANCE_LOCK) {
            if (sInstance == null) {
                sInstance = new HapClientNativeInterface();
            }
            return sInstance;
        }
    }

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

    /**
     * Initiates HapClientService connection to a remote device.
     *
+7 −6
Original line number Diff line number Diff line
@@ -119,8 +119,14 @@ public class HapClientService extends ProfileService {
    }

    public HapClientService(AdapterService adapterService) {
        this(adapterService, new HapClientNativeInterface());
    }

    @VisibleForTesting
    HapClientService(AdapterService adapterService, HapClientNativeInterface nativeInterface) {
        super(adapterService);
        mAdapterService = Objects.requireNonNull(adapterService);
        mHapClientNativeInterface = Objects.requireNonNull(nativeInterface);
    }

    @Override
@@ -141,15 +147,11 @@ public class HapClientService extends ProfileService {
            throw new IllegalStateException("start() called twice");
        }

        // Get HapClientNativeInterface, DatabaseManager, AudioManager.
        // None of them can be null.
        // Get DatabaseManager
        mDatabaseManager =
                Objects.requireNonNull(
                        mAdapterService.getDatabase(),
                        "DatabaseManager cannot be null when HapClientService starts");
        mHapClientNativeInterface = Objects.requireNonNull(
                HapClientNativeInterface.getInstance(),
                "HapClientNativeInterface cannot be null when HapClientService starts");

        // Start handler thread for state machines
        mHandler = new Handler(Looper.getMainLooper());
@@ -204,7 +206,6 @@ public class HapClientService extends ProfileService {

        // Cleanup GATT interface
        mHapClientNativeInterface.cleanup();
        mHapClientNativeInterface = null;

        // Cleanup the internals
        mDeviceCurrentPresetMap.clear();
+8 −18
Original line number Diff line number Diff line
@@ -35,7 +35,6 @@ import com.android.bluetooth.a2dp.A2dpNativeInterface;
import com.android.bluetooth.avrcp.AvrcpNativeInterface;
import com.android.bluetooth.btservice.storage.DatabaseManager;
import com.android.bluetooth.csip.CsipSetCoordinatorNativeInterface;
import com.android.bluetooth.hap.HapClientNativeInterface;
import com.android.bluetooth.hearingaid.HearingAidNativeInterface;
import com.android.bluetooth.hfp.HeadsetNativeInterface;
import com.android.bluetooth.hid.HidDeviceNativeInterface;
@@ -57,6 +56,7 @@ import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.FutureTask;
@@ -85,7 +85,6 @@ public class ProfileServiceTest {
    @Mock private HidHostNativeInterface mHidHostNativeInterface;
    @Mock private PanNativeInterface mPanNativeInterface;
    @Mock private CsipSetCoordinatorNativeInterface mCsipSetCoordinatorInterface;
    @Mock private HapClientNativeInterface mHapClientInterface;
    @Mock private LeAudioNativeInterface mLeAudioInterface;
    @Mock private VolumeControlNativeInterface mVolumeControlInterface;

@@ -103,11 +102,6 @@ public class ProfileServiceTest {
    private void setAllProfilesState(int state, int invocationNumber) {
        int profileCount = mProfiles.length;
        for (int profile : mProfiles) {
            if (profile == BluetoothProfile.GATT) {
                // GattService is no longer a service to be start independently
                profileCount--;
                continue;
            }
            setProfileState(profile, state);
        }
        if (invocationNumber == 0) {
@@ -145,7 +139,13 @@ public class ProfileServiceTest {
        doReturn(false).when(mAdapterService).isA2dpOffloadEnabled();
        doReturn(false).when(mAdapterService).pbapPseDynamicVersionUpgradeIsEnabled();

        mProfiles = Config.getSupportedProfiles();
        mProfiles =
                Arrays.stream(Config.getSupportedProfiles())
                        .filter(
                                profile ->
                                        profile != BluetoothProfile.HAP_CLIENT
                                                && profile != BluetoothProfile.GATT)
                        .toArray();
        TestUtils.setAdapterService(mAdapterService);

        Assert.assertNotNull(AdapterService.getAdapterService());
@@ -158,7 +158,6 @@ public class ProfileServiceTest {
        HidHostNativeInterface.setInstance(mHidHostNativeInterface);
        PanNativeInterface.setInstance(mPanNativeInterface);
        CsipSetCoordinatorNativeInterface.setInstance(mCsipSetCoordinatorInterface);
        HapClientNativeInterface.setInstance(mHapClientInterface);
        LeAudioNativeInterface.setInstance(mLeAudioInterface);
        VolumeControlNativeInterface.setInstance(mVolumeControlInterface);
    }
@@ -177,7 +176,6 @@ public class ProfileServiceTest {
        HidHostNativeInterface.setInstance(null);
        PanNativeInterface.setInstance(null);
        CsipSetCoordinatorNativeInterface.setInstance(null);
        HapClientNativeInterface.setInstance(null);
        LeAudioNativeInterface.setInstance(null);
        VolumeControlNativeInterface.setInstance(null);
    }
@@ -242,10 +240,6 @@ public class ProfileServiceTest {
    public void testRepeatedEnableDisableSingly() {
        int profileNumber = 0;
        for (int profile : mProfiles) {
            if (profile == BluetoothProfile.GATT) {
                // GattService is no longer a service to be start independently
                continue;
            }
            for (int i = 0; i < NUM_REPEATS; i++) {
                setProfileState(profile, BluetoothAdapter.STATE_ON);
                ArgumentCaptor<ProfileService> start =
@@ -272,10 +266,6 @@ public class ProfileServiceTest {
    public void testProfileServiceRegisterUnregister() {
        int profileNumber = 0;
        for (int profile : mProfiles) {
            if (profile == BluetoothProfile.GATT) {
                // GattService is no longer a service to be start independently
                continue;
            }
            for (int i = 0; i < NUM_REPEATS; i++) {
                setProfileState(profile, BluetoothAdapter.STATE_ON);
                ArgumentCaptor<ProfileService> start =
+1 −1
Original line number Diff line number Diff line
@@ -47,7 +47,7 @@ public class HapClientNativeInterfaceTest {
    public void setUp() throws Exception {
        when(mService.isAvailable()).thenReturn(true);
        HapClientService.setHapClient(mService);
        mNativeInterface = HapClientNativeInterface.getInstance();
        mNativeInterface = new HapClientNativeInterface();
    }

    @After
+1 −3
Original line number Diff line number Diff line
@@ -126,7 +126,6 @@ public class HapClientTest {
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        mAttributionSource = mAdapter.getAttributionSource();

        HapClientNativeInterface.setInstance(mNativeInterface);
        startService();
        mService.mFactory = mServiceFactory;
        doReturn(mCsipService).when(mServiceFactory).getCsipSetCoordinatorService();
@@ -200,13 +199,12 @@ public class HapClientTest {
        mService.mCallbacks.unregister(mCallback);

        stopService();
        HapClientNativeInterface.setInstance(null);

        mAdapter = null;
    }

    private void startService() throws TimeoutException {
        mService = new HapClientService(mAdapterService);
        mService = new HapClientService(mAdapterService, mNativeInterface);
        mService.start();
        mService.setAvailable(true);
    }