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

Commit 31ca9627 authored by William Escande's avatar William Escande
Browse files

A2dpService: Test: use static flagging

static flagging is the recommended way to use trunk stable flagging
Change binder builder to take dependency in argument at construct and
not during its runtime

Bug: 321252288
Bug: 300174072
Test: atest A2dpServiceTest
Test: atest A2dpServiceBinderTest
Flag: Exempt, Test only
Change-Id: I9750f09c263cc269eb097a6fa3e3244b66e27401
parent 98df216a
Loading
Loading
Loading
Loading
+11 −11
Original line number Diff line number Diff line
@@ -59,14 +59,14 @@ import android.util.Log;
import com.android.bluetooth.BluetoothMetricsProto;
import com.android.bluetooth.BluetoothStatsLog;
import com.android.bluetooth.Utils;
import com.android.bluetooth.btservice.ActiveDeviceManager;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.AudioRoutingManager;
import com.android.bluetooth.btservice.MetricsLogger;
import com.android.bluetooth.btservice.ProfileService;
import com.android.bluetooth.btservice.ServiceFactory;
import com.android.bluetooth.btservice.storage.DatabaseManager;
import com.android.bluetooth.flags.FeatureFlags;
import com.android.bluetooth.flags.FeatureFlagsImpl;
import com.android.bluetooth.flags.Flags;
import com.android.bluetooth.hfp.HeadsetService;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
@@ -90,7 +90,6 @@ public class A2dpService extends ProfileService {

    private static A2dpService sA2dpService;

    private final FeatureFlags mFeatureFlags;
    private AdapterService mAdapterService;
    private DatabaseManager mDatabaseManager;
    private HandlerThread mStateMachinesThread;
@@ -130,14 +129,12 @@ public class A2dpService extends ProfileService {
    public A2dpService(Context ctx) {
        super(ctx);
        mNativeInterface = requireNonNull(A2dpNativeInterface.getInstance());
        mFeatureFlags = new FeatureFlagsImpl();
    }

    @VisibleForTesting
    A2dpService(Context ctx, A2dpNativeInterface nativeInterface, FeatureFlags featureFlags) {
    A2dpService(Context ctx, A2dpNativeInterface nativeInterface) {
        super(ctx);
        mNativeInterface = requireNonNull(nativeInterface);
        mFeatureFlags = featureFlags;
    }

    public static boolean isEnabled() {
@@ -146,7 +143,8 @@ public class A2dpService extends ProfileService {

    @Override
    protected IProfileServiceBinder initBinder() {
        return new BluetoothA2dpBinder(this);
        return new BluetoothA2dpBinder(
                this, AdapterService.getAdapterService().getActiveDeviceManager());
    }

    @Override
@@ -1323,7 +1321,7 @@ public class A2dpService extends ProfileService {
        if (toState == BluetoothProfile.STATE_CONNECTED) {
            MetricsLogger.logProfileConnectionEvent(BluetoothMetricsProto.ProfileId.A2DP);
        }
        if (!mFeatureFlags.audioRoutingCentralization()) {
        if (!Flags.audioRoutingCentralization()) {
            // Set the active device if only one connected device is supported and it was connected
            if (toState == BluetoothProfile.STATE_CONNECTED && (mMaxConnectedAudioDevices == 1)) {
                setActiveDevice(device);
@@ -1374,6 +1372,7 @@ public class A2dpService extends ProfileService {
    static class BluetoothA2dpBinder extends IBluetoothA2dp.Stub
            implements IProfileServiceBinder {
        private A2dpService mService;
        private ActiveDeviceManager mActiveDeviceManager;

        @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
        private A2dpService getService(AttributionSource source) {
@@ -1388,8 +1387,9 @@ public class A2dpService extends ProfileService {
            return mService;
        }

        BluetoothA2dpBinder(A2dpService svc) {
        BluetoothA2dpBinder(A2dpService svc, ActiveDeviceManager activeDeviceManager) {
            mService = svc;
            mActiveDeviceManager = activeDeviceManager;
        }

        @Override
@@ -1478,8 +1478,8 @@ public class A2dpService extends ProfileService {
            try {
                A2dpService service = getService(source);
                if (service != null) {
                    if (service.mFeatureFlags.audioRoutingCentralization()) {
                        ((AudioRoutingManager) service.mAdapterService.getActiveDeviceManager())
                    if (Flags.audioRoutingCentralization()) {
                        ((AudioRoutingManager) mActiveDeviceManager)
                                .activateDeviceProfile(device, BluetoothProfile.A2DP, receiver);
                    } else {
                        boolean result;
+68 −105
Original line number Diff line number Diff line
@@ -18,8 +18,9 @@ package com.android.bluetooth.a2dp;

import static android.bluetooth.BluetoothCodecConfig.SOURCE_CODEC_TYPE_INVALID;

import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

import android.bluetooth.BluetoothA2dp;
@@ -30,20 +31,18 @@ import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BufferConstraints;
import android.content.AttributionSource;
import android.content.Context;

import androidx.test.InstrumentationRegistry;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.platform.test.flag.junit.SetFlagsRule;

import com.android.bluetooth.TestUtils;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.AudioRoutingManager;
import com.android.bluetooth.btservice.storage.DatabaseManager;
import com.android.bluetooth.flags.FakeFeatureFlagsImpl;
import com.android.bluetooth.flags.Flags;
import com.android.bluetooth.x.com.android.modules.utils.SynchronousResultReceiver;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@@ -51,250 +50,215 @@ import org.mockito.MockitoAnnotations;
import java.util.List;

public class A2dpServiceBinderTest {
    private A2dpService mA2dpService;
    private FakeFeatureFlagsImpl mFakeFlagsImpl;
    @Mock private AdapterService mAdapterService;
    private static final AttributionSource sSource = new AttributionSource.Builder(0).build();
    private static final BluetoothAdapter sAdapter = BluetoothAdapter.getDefaultAdapter();
    private static final BluetoothDevice sDevice = TestUtils.getTestDevice(sAdapter, 0);

    @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();

    @Mock private A2dpService mA2dpService;
    @Mock private A2dpNativeInterface mNativeInterface;
    @Mock private DatabaseManager mDatabaseManager;
    @Mock private AudioRoutingManager mAudioRoutingManager;
    @Mock private PackageManager mPackageManager;

    private A2dpService.BluetoothA2dpBinder mBinder;
    private BluetoothAdapter mAdapter;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        TestUtils.setAdapterService(mAdapterService);
        doReturn(false).when(mAdapterService).isQuietModeEnabled();
        doReturn(mDatabaseManager).when(mAdapterService).getDatabase();
        doReturn(mAudioRoutingManager).when(mAdapterService).getActiveDeviceManager();

        Context context = InstrumentationRegistry.getTargetContext();
        mFakeFlagsImpl = new FakeFeatureFlagsImpl();
        mA2dpService = spy(new A2dpService(context, mNativeInterface, mFakeFlagsImpl));
        mA2dpService.doStart();

        mAdapter = BluetoothAdapter.getDefaultAdapter();
        mBinder = new A2dpService.BluetoothA2dpBinder(mA2dpService);
        doReturn(mPackageManager).when(mA2dpService).getPackageManager();
        ApplicationInfo appInfo = new ApplicationInfo();
        appInfo.targetSdkVersion = android.os.Build.VERSION_CODES.CUR_DEVELOPMENT;
        doReturn(appInfo).when(mPackageManager).getApplicationInfo(any(), anyInt());

        mBinder = new A2dpService.BluetoothA2dpBinder(mA2dpService, mAudioRoutingManager);
    }

    @After
    public void cleaUp() {
    public void cleanUp() {
        mBinder.cleanup();
        mA2dpService.doStop();
        TestUtils.clearAdapterService(mAdapterService);
    }

    @Test
    public void connect() {
        BluetoothDevice device = TestUtils.getTestDevice(mAdapter, 0);
        AttributionSource source = new AttributionSource.Builder(0).build();
        final SynchronousResultReceiver<Boolean> recv = SynchronousResultReceiver.get();

        mBinder.connect(device, source, recv);
        verify(mA2dpService).connect(device);
        mBinder.connect(sDevice, sSource, recv);
        verify(mA2dpService).connect(sDevice);
    }

    @Test
    public void disconnect() {
        BluetoothDevice device = TestUtils.getTestDevice(mAdapter, 0);
        AttributionSource source = new AttributionSource.Builder(0).build();
        final SynchronousResultReceiver<Boolean> recv = SynchronousResultReceiver.get();

        mBinder.disconnect(device, source, recv);
        verify(mA2dpService).disconnect(device);
        mBinder.disconnect(sDevice, sSource, recv);
        verify(mA2dpService).disconnect(sDevice);
    }

    @Test
    public void getConnectedDevices() {
        AttributionSource source = new AttributionSource.Builder(0).build();
        final SynchronousResultReceiver<List<BluetoothDevice>> recv =
                SynchronousResultReceiver.get();

        mBinder.getConnectedDevices(source, recv);
        mBinder.getConnectedDevices(sSource, recv);
        verify(mA2dpService).getConnectedDevices();
    }

    @Test
    public void getDevicesMatchingConnectionStates() {
        int[] states = new int[] {BluetoothProfile.STATE_CONNECTED };
        AttributionSource source = new AttributionSource.Builder(0).build();
        final SynchronousResultReceiver<List<BluetoothDevice>> recv =
                SynchronousResultReceiver.get();

        mBinder.getDevicesMatchingConnectionStates(states, source, recv);
        mBinder.getDevicesMatchingConnectionStates(states, sSource, recv);
        verify(mA2dpService).getDevicesMatchingConnectionStates(states);
    }

    @Test
    public void getConnectionState() {
        BluetoothDevice device = TestUtils.getTestDevice(mAdapter, 0);
        AttributionSource source = new AttributionSource.Builder(0).build();
        final SynchronousResultReceiver<List<BluetoothDevice>> recv =
                SynchronousResultReceiver.get();

        mBinder.getConnectionState(device, source, recv);
        verify(mA2dpService).getConnectionState(device);
        mBinder.getConnectionState(sDevice, sSource, recv);
        verify(mA2dpService).getConnectionState(sDevice);
    }

    @Test
    public void setActiveDevice() {
        BluetoothDevice device = TestUtils.getTestDevice(mAdapter, 0);
        AttributionSource source = new AttributionSource.Builder(0).build();
        mSetFlagsRule.disableFlags(Flags.FLAG_AUDIO_ROUTING_CENTRALIZATION);

        mFakeFlagsImpl.setFlag(Flags.FLAG_AUDIO_ROUTING_CENTRALIZATION, false);
        SynchronousResultReceiver<Boolean> recv = SynchronousResultReceiver.get();
        mBinder.setActiveDevice(device, source, recv);
        verify(mA2dpService).setActiveDevice(device);
        mBinder.setActiveDevice(sDevice, sSource, recv);
        verify(mA2dpService).setActiveDevice(sDevice);
    }

    @Test
    public void setActiveDeviceWithAudioRouting() {
        mSetFlagsRule.enableFlags(Flags.FLAG_AUDIO_ROUTING_CENTRALIZATION);

        mFakeFlagsImpl.setFlag(Flags.FLAG_AUDIO_ROUTING_CENTRALIZATION, true);
        recv = SynchronousResultReceiver.get();
        mBinder.setActiveDevice(device, source, recv);
        verify(mAudioRoutingManager).activateDeviceProfile(device, BluetoothProfile.A2DP, recv);
        SynchronousResultReceiver<Boolean> recv = SynchronousResultReceiver.get();
        mBinder.setActiveDevice(sDevice, sSource, recv);
        verify(mAudioRoutingManager).activateDeviceProfile(sDevice, BluetoothProfile.A2DP, recv);
    }

    @Test
    public void setActiveDevice_withNull_callsRemoveActiveDevice() {
        BluetoothDevice device = null;
        AttributionSource source = new AttributionSource.Builder(0).build();
        mSetFlagsRule.disableFlags(Flags.FLAG_AUDIO_ROUTING_CENTRALIZATION);
        final SynchronousResultReceiver<Boolean> recv = SynchronousResultReceiver.get();

        mBinder.setActiveDevice(device, source, recv);
        mBinder.setActiveDevice(null, sSource, recv);
        verify(mA2dpService).removeActiveDevice(false);
    }

    @Test
    public void getActiveDevice() {
        AttributionSource source = new AttributionSource.Builder(0).build();
        final SynchronousResultReceiver<BluetoothDevice> recv = SynchronousResultReceiver.get();

        mBinder.getActiveDevice(source, recv);
        mBinder.getActiveDevice(sSource, recv);
        verify(mA2dpService).getActiveDevice();
    }

    @Test
    public void setConnectionPolicy() {
        BluetoothDevice device = TestUtils.getTestDevice(mAdapter, 0);
        int connectionPolicy = BluetoothProfile.CONNECTION_POLICY_ALLOWED;
        AttributionSource source = new AttributionSource.Builder(0).build();
        final SynchronousResultReceiver<Boolean> recv = SynchronousResultReceiver.get();

        mBinder.setConnectionPolicy(device, connectionPolicy, source, recv);
        verify(mA2dpService).setConnectionPolicy(device, connectionPolicy);
        mBinder.setConnectionPolicy(sDevice, connectionPolicy, sSource, recv);
        verify(mA2dpService).setConnectionPolicy(sDevice, connectionPolicy);
    }

    @Test
    public void getConnectionPolicy() {
        BluetoothDevice device = TestUtils.getTestDevice(mAdapter, 0);
        AttributionSource source = new AttributionSource.Builder(0).build();
        final SynchronousResultReceiver<Integer> recv = SynchronousResultReceiver.get();

        mBinder.getConnectionPolicy(device, source, recv);
        verify(mA2dpService).getConnectionPolicy(device);
        mBinder.getConnectionPolicy(sDevice, sSource, recv);
        verify(mA2dpService).getConnectionPolicy(sDevice);
    }

    @Test
    public void setAvrcpAbsoluteVolume() {
        int volume = 3;
        AttributionSource source = new AttributionSource.Builder(0).build();

        mBinder.setAvrcpAbsoluteVolume(volume, source);
        mBinder.setAvrcpAbsoluteVolume(volume, sSource);
        verify(mA2dpService).setAvrcpAbsoluteVolume(volume);
    }

    @Test
    public void isA2dpPlaying() {
        BluetoothDevice device = TestUtils.getTestDevice(mAdapter, 0);
        AttributionSource source = new AttributionSource.Builder(0).build();
        final SynchronousResultReceiver<Boolean> recv = SynchronousResultReceiver.get();

        mBinder.isA2dpPlaying(device, source, recv);
        verify(mA2dpService).isA2dpPlaying(device);
        mBinder.isA2dpPlaying(sDevice, sSource, recv);
        verify(mA2dpService).isA2dpPlaying(sDevice);
    }

    @Test
    public void getCodecStatus() {
        BluetoothDevice device = TestUtils.getTestDevice(mAdapter, 0);
        AttributionSource source = new AttributionSource.Builder(0).build();
        final SynchronousResultReceiver<BluetoothCodecStatus> recv =
                SynchronousResultReceiver.get();

        mBinder.getCodecStatus(device, source, recv);
        verify(mA2dpService).getCodecStatus(device);
        mBinder.getCodecStatus(sDevice, sSource, recv);
        verify(mA2dpService).getCodecStatus(sDevice);
    }

    @Test
    public void setCodecConfigPreference() {
        BluetoothDevice device = TestUtils.getTestDevice(mAdapter, 0);
        BluetoothCodecConfig config = new BluetoothCodecConfig(SOURCE_CODEC_TYPE_INVALID);
        AttributionSource source = new AttributionSource.Builder(0).build();

        mBinder.setCodecConfigPreference(device, config, source);
        verify(mA2dpService).setCodecConfigPreference(device, config);
        mBinder.setCodecConfigPreference(sDevice, config, sSource);
        verify(mA2dpService).setCodecConfigPreference(sDevice, config);
    }

    @Test
    public void enableOptionalCodecs() {
        BluetoothDevice device = TestUtils.getTestDevice(mAdapter, 0);
        AttributionSource source = new AttributionSource.Builder(0).build();

        mBinder.enableOptionalCodecs(device, source);
        verify(mA2dpService).enableOptionalCodecs(device);
        mBinder.enableOptionalCodecs(sDevice, sSource);
        verify(mA2dpService).enableOptionalCodecs(sDevice);
    }

    @Test
    public void disableOptionalCodecs() {
        BluetoothDevice device = TestUtils.getTestDevice(mAdapter, 0);
        AttributionSource source = new AttributionSource.Builder(0).build();

        mBinder.disableOptionalCodecs(device, source);
        verify(mA2dpService).disableOptionalCodecs(device);
        mBinder.disableOptionalCodecs(sDevice, sSource);
        verify(mA2dpService).disableOptionalCodecs(sDevice);
    }

    @Test
    public void isOptionalCodecsSupported() {
        BluetoothDevice device = TestUtils.getTestDevice(mAdapter, 0);
        AttributionSource source = new AttributionSource.Builder(0).build();
        final SynchronousResultReceiver<Integer> recv = SynchronousResultReceiver.get();

        mBinder.isOptionalCodecsSupported(device, source, recv);
        verify(mA2dpService).getSupportsOptionalCodecs(device);
        mBinder.isOptionalCodecsSupported(sDevice, sSource, recv);
        verify(mA2dpService).getSupportsOptionalCodecs(sDevice);
    }

    @Test
    public void isOptionalCodecsEnabled() {
        BluetoothDevice device = TestUtils.getTestDevice(mAdapter, 0);
        AttributionSource source = new AttributionSource.Builder(0).build();
        final SynchronousResultReceiver<Integer> recv = SynchronousResultReceiver.get();

        mBinder.isOptionalCodecsEnabled(device, source, recv);
        verify(mA2dpService).getOptionalCodecsEnabled(device);
        mBinder.isOptionalCodecsEnabled(sDevice, sSource, recv);
        verify(mA2dpService).getOptionalCodecsEnabled(sDevice);
    }

    @Test
    public void setOptionalCodecsEnabled() {
        BluetoothDevice device = TestUtils.getTestDevice(mAdapter, 0);
        int value = BluetoothA2dp.OPTIONAL_CODECS_PREF_UNKNOWN;
        AttributionSource source = new AttributionSource.Builder(0).build();

        mBinder.setOptionalCodecsEnabled(device, value, source);
        verify(mA2dpService).setOptionalCodecsEnabled(device, value);
        mBinder.setOptionalCodecsEnabled(sDevice, value, sSource);
        verify(mA2dpService).setOptionalCodecsEnabled(sDevice, value);
    }

    @Test
    public void getDynamicBufferSupport() {
        AttributionSource source = new AttributionSource.Builder(0).build();
        final SynchronousResultReceiver<Integer> recv = SynchronousResultReceiver.get();

        mBinder.getDynamicBufferSupport(source, recv);
        mBinder.getDynamicBufferSupport(sSource, recv);
        verify(mA2dpService).getDynamicBufferSupport();
    }

    @Test
    public void getBufferConstraints() {
        AttributionSource source = new AttributionSource.Builder(0).build();
        final SynchronousResultReceiver<BufferConstraints> recv = SynchronousResultReceiver.get();

        mBinder.getBufferConstraints(source, recv);
        mBinder.getBufferConstraints(sSource, recv);
        verify(mA2dpService).getBufferConstraints();
    }

@@ -302,10 +266,9 @@ public class A2dpServiceBinderTest {
    public void setBufferLengthMillis() {
        int codec = 0;
        int value = BluetoothA2dp.OPTIONAL_CODECS_PREF_UNKNOWN;
        AttributionSource source = new AttributionSource.Builder(0).build();
        final SynchronousResultReceiver<Boolean> recv = SynchronousResultReceiver.get();

        mBinder.setBufferLengthMillis(codec, value, source, recv);
        mBinder.setBufferLengthMillis(codec, value, sSource, recv);
        verify(mA2dpService).setBufferLengthMillis(codec, value);
    }
}
+1 −6
Original line number Diff line number Diff line
@@ -44,8 +44,6 @@ import com.android.bluetooth.btservice.ActiveDeviceManager;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.SilenceDeviceManager;
import com.android.bluetooth.btservice.storage.DatabaseManager;
import com.android.bluetooth.flags.FakeFeatureFlagsImpl;
import com.android.bluetooth.flags.Flags;

import org.hamcrest.Matcher;
import org.hamcrest.core.AllOf;
@@ -84,7 +82,6 @@ public class A2dpServiceTest {
    private InOrder mInOrder = null;

    private A2dpService mA2dpService;
    private FakeFeatureFlagsImpl mFakeFlagsImpl;

    @Before
    public void setUp() throws Exception {
@@ -110,9 +107,7 @@ public class A2dpServiceTest {
        doReturn(mActiveDeviceManager).when(mAdapterService).getActiveDeviceManager();
        doReturn(mSilenceDeviceManager).when(mAdapterService).getSilenceDeviceManager();

        mFakeFlagsImpl = new FakeFeatureFlagsImpl();
        mFakeFlagsImpl.setFlag(Flags.FLAG_AUDIO_ROUTING_CENTRALIZATION, false);
        mA2dpService = new A2dpService(mContext, mMockNativeInterface, mFakeFlagsImpl);
        mA2dpService = new A2dpService(mContext, mMockNativeInterface);
        mA2dpService.doStart();

        // Override the timeout value to speed up the test