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

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

A2dpSinkService: stop calling NativeInterface in test

Bug: 295237486
Test: atest A2dpSinkServiceTest
Change-Id: I7b84e42d06390b98e28dd3a3a95fc5247f01cc80
parent ed233f52
Loading
Loading
Loading
Loading
+15 −1
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;

@@ -35,11 +36,16 @@ public class A2dpSinkNativeInterface {

    @GuardedBy("INSTANCE_LOCK")
    private static A2dpSinkNativeInterface 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 A2dpSinkNativeInterface() {
        mAdapterService = Objects.requireNonNull(AdapterService.getAdapterService(),
@@ -58,6 +64,14 @@ public class A2dpSinkNativeInterface {
        }
    }

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

    /**
     * Initializes the native interface and sets the max number of connected devices
     *
+36 −17
Original line number Diff line number Diff line
@@ -15,6 +15,8 @@
 */
package com.android.bluetooth.a2dpsink;

import static java.util.Objects.requireNonNull;

import android.annotation.RequiresPermission;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothAudioConfig;
@@ -22,6 +24,7 @@ import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.IBluetoothA2dpSink;
import android.content.AttributionSource;
import android.content.Context;
import android.media.AudioManager;
import android.sysprop.BluetoothProperties;
import android.util.Log;
@@ -30,6 +33,7 @@ import com.android.bluetooth.Utils;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.ProfileService;
import com.android.bluetooth.btservice.storage.DatabaseManager;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.modules.utils.SynchronousResultReceiver;

@@ -37,32 +41,47 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Provides Bluetooth A2DP Sink profile, as a service in the Bluetooth application.
 * @hide
 */
public class A2dpSinkService extends ProfileService {
    private static final String TAG = "A2dpSinkService";
    private static final String TAG = A2dpSinkService.class.getSimpleName();
    private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
    private int mMaxConnectedAudioDevices;

    private AdapterService mAdapterService;
    private DatabaseManager mDatabaseManager;
    private Map<BluetoothDevice, A2dpSinkStateMachine> mDeviceStateMap =
    private final Map<BluetoothDevice, A2dpSinkStateMachine> mDeviceStateMap =
            new ConcurrentHashMap<>(1);

    private final Object mStreamHandlerLock = new Object();
    private final A2dpSinkNativeInterface mNativeInterface;

    private final Object mActiveDeviceLock = new Object();

    @GuardedBy("mActiveDeviceLock")
    private BluetoothDevice mActiveDevice = null;

    private final Object mStreamHandlerLock = new Object();

    @GuardedBy("mStreamHandlerLock")
    private A2dpSinkStreamHandler mA2dpSinkStreamHandler;

    private static A2dpSinkService sService;

    A2dpSinkNativeInterface mNativeInterface;
    private int mMaxConnectedAudioDevices;

    private AdapterService mAdapterService;
    private DatabaseManager mDatabaseManager;

    A2dpSinkService() {
        mNativeInterface = requireNonNull(A2dpSinkNativeInterface.getInstance());
    }

    @VisibleForTesting
    A2dpSinkService(Context ctx, A2dpSinkNativeInterface nativeInterface) {
        attachBaseContext(ctx);
        mNativeInterface = requireNonNull(nativeInterface);
        onCreate();
    }

    public static boolean isEnabled() {
        return BluetoothProperties.isProfileA2dpSinkEnabled().orElse(false);
@@ -70,11 +89,14 @@ public class A2dpSinkService extends ProfileService {

    @Override
    protected boolean start() {
        mAdapterService = Objects.requireNonNull(AdapterService.getAdapterService(),
        mAdapterService =
                requireNonNull(
                        AdapterService.getAdapterService(),
                        "AdapterService cannot be null when A2dpSinkService starts");
        mDatabaseManager = Objects.requireNonNull(AdapterService.getAdapterService().getDatabase(),
        mDatabaseManager =
                requireNonNull(
                        AdapterService.getAdapterService().getDatabase(),
                        "DatabaseManager cannot be null when A2dpSinkService starts");
        mNativeInterface = A2dpSinkNativeInterface.getInstance();

        mMaxConnectedAudioDevices = mAdapterService.getMaxConnectedAudioDevices();
        mNativeInterface.init(mMaxConnectedAudioDevices);
@@ -127,9 +149,6 @@ public class A2dpSinkService extends ProfileService {
        sService = service;
    }


    public A2dpSinkService() {}

    /**
     * Set the device that should be allowed to actively stream
     */
+2 −3
Original line number Diff line number Diff line
@@ -32,13 +32,11 @@ 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.Assume;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -97,18 +95,19 @@ public class A2dpSinkServiceTest {
        when(mDatabaseManager.setProfileConnectionPolicy(any(), anyInt(),
                anyInt())).thenReturn(true);
        setMaxConnectedAudioDevices(1);
        A2dpSinkNativeInterface.setInstance(mNativeInterface);
        TestUtils.startService(mServiceRule, A2dpSinkService.class);
        mService = A2dpSinkService.getA2dpSinkService();
        assertThat(mService).isNotNull();
        verify(mAdapterService).notifyActivityAttributionInfo(any(), any());

        mService.mNativeInterface = mNativeInterface;
        doReturn(true).when(mNativeInterface).setActiveDevice(any());
    }

    @After
    public void tearDown() throws Exception {
        TestUtils.stopService(mServiceRule, A2dpSinkService.class);
        A2dpSinkNativeInterface.setInstance(null);
        mService = A2dpSinkService.getA2dpSinkService();
        assertThat(mService).isNull();
        TestUtils.clearAdapterService(mAdapterService);
+4 −0
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ import androidx.test.runner.AndroidJUnit4;
import com.android.bluetooth.R;
import com.android.bluetooth.TestUtils;
import com.android.bluetooth.a2dpsink.A2dpSinkService;
import com.android.bluetooth.a2dpsink.A2dpSinkNativeInterface;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.storage.DatabaseManager;

@@ -81,6 +82,7 @@ public class AvrcpControllerStateMachineTest {
    @Mock private Resources mMockResources;
    private ArgumentCaptor<Intent> mIntentArgument = ArgumentCaptor.forClass(Intent.class);
    @Mock private AvrcpControllerService mAvrcpControllerService;
    @Mock private A2dpSinkNativeInterface mA2dpSinkNativeInterface;
    @Mock private AvrcpCoverArtManager mCoverArtManager;

    private byte[] mTestAddress = new byte[]{01, 01, 01, 01, 01, 01};
@@ -100,6 +102,7 @@ public class AvrcpControllerStateMachineTest {
        doReturn(mDatabaseManager).when(mA2dpAdapterService).getDatabase();
        doReturn(true).when(mA2dpAdapterService).isStartedProfile(anyString());
        TestUtils.setAdapterService(mA2dpAdapterService);
        A2dpSinkNativeInterface.setInstance(mA2dpSinkNativeInterface);
        TestUtils.startService(mA2dpServiceRule, A2dpSinkService.class);
        A2dpSinkService.setA2dpSinkService(mA2dpSinkService);
        TestUtils.clearAdapterService(mA2dpAdapterService);
@@ -140,6 +143,7 @@ public class AvrcpControllerStateMachineTest {
    public void tearDown() throws Exception {
        destroyStateMachine(mAvrcpStateMachine);
        TestUtils.clearAdapterService(mAvrcpAdapterService);
        A2dpSinkNativeInterface.setInstance(null);
    }

    /**