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

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

A2dpService: stop calling NativeInterface in test

Bug: 295237486
Test: atest A2dpServiceTest
Change-Id: Ie4f90641b7ae8672c191ae1f8d7b43228a74c301
parent e04bf5c1
Loading
Loading
Loading
Loading
+20 −8
Original line number Diff line number Diff line
@@ -47,11 +47,16 @@ public class A2dpNativeInterface {

    @GuardedBy("INSTANCE_LOCK")
    private static A2dpNativeInterface 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();
        }
    }

    @VisibleForTesting
    private A2dpNativeInterface() {
@@ -75,22 +80,29 @@ public class A2dpNativeInterface {
        }
    }

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

    /**
     * Initializes the native interface.
     *
     * @param maxConnectedAudioDevices maximum number of A2DP Sink devices that can be connected
     *     simultaneously
     * @param codecConfigPriorities an array with the codec configuration
     * priorities to configure.
     * @param codecConfigPriorities an array with the codec configuration priorities to configure.
     */
    public void init(int maxConnectedAudioDevices, BluetoothCodecConfig[] codecConfigPriorities,
    public void init(
            int maxConnectedAudioDevices,
            BluetoothCodecConfig[] codecConfigPriorities,
            BluetoothCodecConfig[] codecConfigOffloading) {
        initNative(maxConnectedAudioDevices, codecConfigPriorities, codecConfigOffloading);
    }

    /**
     * Cleanup the native interface.
     */
    /** Cleanup the native interface. */
    public void cleanup() {
        cleanupNative();
    }
+39 −28
Original line number Diff line number Diff line
@@ -23,6 +23,8 @@ import static com.android.bluetooth.Utils.enforceBluetoothPrivilegedPermission;
import static com.android.bluetooth.Utils.enforceCdmAssociation;
import static com.android.bluetooth.Utils.hasBluetoothPrivilegedPermission;

import static java.util.Objects.requireNonNull;

import android.annotation.NonNull;
import android.annotation.RequiresPermission;
import android.bluetooth.BluetoothA2dp;
@@ -72,11 +74,10 @@ import java.util.concurrent.ConcurrentMap;

/**
 * Provides Bluetooth A2DP profile, as a service in the Bluetooth application.
 * @hide
 */
public class A2dpService extends ProfileService {
    private static final String TAG = A2dpService.class.getSimpleName();
    private static final boolean DBG = true;
    private static final String TAG = "A2dpService";

    // TODO(b/240635097): remove in U
    private static final int SOURCE_CODEC_TYPE_OPUS = 6;
@@ -87,8 +88,7 @@ public class A2dpService extends ProfileService {
    private DatabaseManager mDatabaseManager;
    private HandlerThread mStateMachinesThread;

    @VisibleForTesting
    A2dpNativeInterface mA2dpNativeInterface;
    private final A2dpNativeInterface mNativeInterface;
    @VisibleForTesting
    ServiceFactory mFactory = new ServiceFactory();
    @VisibleForTesting
@@ -116,6 +116,17 @@ public class A2dpService extends ProfileService {

    private BroadcastReceiver mBondStateChangedReceiver;

    A2dpService() {
        mNativeInterface = requireNonNull(A2dpNativeInterface.getInstance());
    }

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

    public static boolean isEnabled() {
        return BluetoothProperties.isProfileA2dpSourceEnabled().orElse(false);
    }
@@ -139,16 +150,17 @@ public class A2dpService extends ProfileService {

        // Step 1: Get AdapterService, A2dpNativeInterface, DatabaseManager, AudioManager.
        // None of them can be null.
        mAdapterService = Objects.requireNonNull(AdapterService.getAdapterService(),
        mAdapterService =
                requireNonNull(
                        AdapterService.getAdapterService(),
                        "AdapterService cannot be null when A2dpService starts");
        mA2dpNativeInterface = Objects.requireNonNull(A2dpNativeInterface.getInstance(),
                "A2dpNativeInterface cannot be null when A2dpService starts");
        mDatabaseManager = Objects.requireNonNull(mAdapterService.getDatabase(),
        mDatabaseManager =
                requireNonNull(
                        mAdapterService.getDatabase(),
                        "DatabaseManager cannot be null when A2dpService starts");
        mAudioManager = getSystemService(AudioManager.class);
        mCompanionDeviceManager = getSystemService(CompanionDeviceManager.class);
        Objects.requireNonNull(mAudioManager,
                               "AudioManager cannot be null when A2dpService starts");
        requireNonNull(mAudioManager, "AudioManager cannot be null when A2dpService starts");

        // Step 2: Get maximum number of connected audio devices
        mMaxConnectedAudioDevices = mAdapterService.getMaxConnectedAudioDevices();
@@ -160,10 +172,11 @@ public class A2dpService extends ProfileService {
        mStateMachinesThread.start();

        // Step 4: Setup codec config
        mA2dpCodecConfig = new A2dpCodecConfig(this, mA2dpNativeInterface);
        mA2dpCodecConfig = new A2dpCodecConfig(this, mNativeInterface);

        // Step 5: Initialize native interface
        mA2dpNativeInterface.init(mMaxConnectedAudioDevices,
        mNativeInterface.init(
                mMaxConnectedAudioDevices,
                mA2dpCodecConfig.codecConfigPriorities(),
                mA2dpCodecConfig.codecConfigOffloading());

@@ -218,8 +231,7 @@ public class A2dpService extends ProfileService {
        mBondStateChangedReceiver = null;

        // Step 6: Cleanup native interface
        mA2dpNativeInterface.cleanup();
        mA2dpNativeInterface = null;
        mNativeInterface.cleanup();

        // Step 5: Clear codec config
        mA2dpCodecConfig = null;
@@ -245,9 +257,8 @@ public class A2dpService extends ProfileService {
        // Step 2: Reset maximum number of connected audio devices
        mMaxConnectedAudioDevices = 1;

        // Step 1: Clear AdapterService, A2dpNativeInterface, AudioManager
        // Step 1: Clear AdapterService, AudioManager
        mAudioManager = null;
        mA2dpNativeInterface = null;
        mAdapterService = null;

        return true;
@@ -514,7 +525,7 @@ public class A2dpService extends ProfileService {

            synchronized (mStateMachines) {
                // Make sure the Active device in native layer is set to null and audio is off
                if (!mA2dpNativeInterface.setActiveDevice(null)) {
                if (!mNativeInterface.setActiveDevice(null)) {
                    Log.w(TAG, "setActiveDevice(null): Cannot remove active device in native "
                            + "layer");
                    return false;
@@ -542,7 +553,7 @@ public class A2dpService extends ProfileService {
            // Set the device as the active device if currently no active device.
            setActiveDevice(device);
        }
        if (!mA2dpNativeInterface.setSilenceDevice(device, silence)) {
        if (!mNativeInterface.setSilenceDevice(device, silence)) {
            Log.e(TAG, "Cannot set " + device + " silence mode " + silence + " in native layer");
            return false;
        }
@@ -599,7 +610,7 @@ public class A2dpService extends ProfileService {

            BluetoothDevice newActiveDevice = null;
            synchronized (mStateMachines) {
                if (!mA2dpNativeInterface.setActiveDevice(device)) {
                if (!mNativeInterface.setActiveDevice(device)) {
                    Log.e(TAG, "setActiveDevice(" + device + "): Cannot set as active in native "
                            + "layer");
                    // Remove active device and stop playing audio.
@@ -945,8 +956,7 @@ public class A2dpService extends ProfileService {

    // Handle messages from native (JNI) to Java
    void messageFromNative(A2dpStackEvent stackEvent) {
        Objects.requireNonNull(stackEvent.device,
                               "Device should never be null, event: " + stackEvent);
        requireNonNull(stackEvent.device, "Device should never be null, event: " + stackEvent);
        synchronized (mStateMachines) {
            BluetoothDevice device = stackEvent.device;
            A2dpStateMachine sm = mStateMachines.get(device);
@@ -1041,8 +1051,9 @@ public class A2dpService extends ProfileService {
            if (DBG) {
                Log.d(TAG, "Creating a new state machine for " + device);
            }
            sm = A2dpStateMachine.make(device, this, mA2dpNativeInterface,
                                       mStateMachinesThread.getLooper());
            sm =
                    A2dpStateMachine.make(
                            device, this, mNativeInterface, mStateMachinesThread.getLooper());
            mStateMachines.put(device, sm);
            return sm;
        }
@@ -1098,7 +1109,7 @@ public class A2dpService extends ProfileService {
            int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,
                                           BluetoothDevice.ERROR);
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Objects.requireNonNull(device, "ACTION_BOND_STATE_CHANGED with no EXTRA_DEVICE");
            requireNonNull(device, "ACTION_BOND_STATE_CHANGED with no EXTRA_DEVICE");
            bondStateChanged(device, state);
        }
    }
+47 −72
Original line number Diff line number Diff line
@@ -58,7 +58,6 @@ import java.util.Arrays;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeoutException;

@MediumTest
@RunWith(AndroidJUnit4.class)
@@ -78,7 +77,7 @@ public class A2dpServiceTest {

    @Mock private AdapterService mAdapterService;
    @Mock private ActiveDeviceManager mActiveDeviceManager;
    @Mock private A2dpNativeInterface mA2dpNativeInterface;
    @Mock private A2dpNativeInterface mMockNativeInterface;
    @Mock private DatabaseManager mDatabaseManager;

    @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule();
@@ -103,8 +102,8 @@ public class A2dpServiceTest {

        mAdapter = BluetoothAdapter.getDefaultAdapter();

        startService();
        mA2dpService.mA2dpNativeInterface = mA2dpNativeInterface;
        mA2dpService = new A2dpService(mTargetContext, mMockNativeInterface);
        mA2dpService.doStart();

        // Override the timeout value to speed up the test
        A2dpStateMachine.sConnectTimeoutMs = TIMEOUT_MS;    // 1s
@@ -128,7 +127,7 @@ public class A2dpServiceTest {

    @After
    public void tearDown() throws Exception {
        stopService();
        mA2dpService.doStop();
        mTargetContext.unregisterReceiver(mA2dpIntentReceiver);
        mConnectionStateChangedQueue.clear();
        mAudioStateChangedQueue.clear();
@@ -136,19 +135,6 @@ public class A2dpServiceTest {
        TestUtils.clearAdapterService(mAdapterService);
    }

    private void startService() throws TimeoutException {
        TestUtils.startService(mServiceRule, A2dpService.class);
        mA2dpService = A2dpService.getA2dpService();
        Assert.assertNotNull(mA2dpService);
        verify(mAdapterService).notifyActivityAttributionInfo(any(), any());
    }

    private void stopService() throws TimeoutException {
        TestUtils.stopService(mServiceRule, A2dpService.class);
        mA2dpService = A2dpService.getA2dpService();
        Assert.assertNull(mA2dpService);
    }

    private class A2dpIntentReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
@@ -225,39 +211,25 @@ public class A2dpServiceTest {
        Assert.assertNull(intent);
    }

    /**
     * Test getting A2DP Service: getA2dpService()
     */
    @Test
    public void testGetA2dpService() {
        Assert.assertEquals(mA2dpService, A2dpService.getA2dpService());
    }

    /**
     * Test stop A2DP Service
     */
    @Test
    public void testStopA2dpService() {
        // Prepare: connect and set active device
        doReturn(true).when(mA2dpNativeInterface).setActiveDevice(any(BluetoothDevice.class));
        doReturn(true).when(mMockNativeInterface).setActiveDevice(any(BluetoothDevice.class));
        connectDevice(mTestDevice);
        Assert.assertTrue(mA2dpService.setActiveDevice(mTestDevice));
        verify(mA2dpNativeInterface).setActiveDevice(mTestDevice);
        // A2DP Service is already running: test stop(). Note: must be done on the main thread.
        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
            public void run() {
        verify(mMockNativeInterface).setActiveDevice(mTestDevice);

        Assert.assertTrue(mA2dpService.stop());
            }
        });

        // Verify that setActiveDevice(null) was called during shutdown
        verify(mA2dpNativeInterface).setActiveDevice(null);
        // Try to restart the service. Note: must be done on the main thread.
        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
            public void run() {
        verify(mMockNativeInterface).setActiveDevice(null);
        Assert.assertTrue(mA2dpService.start());
    }
        });
    }

    /**
     * Test get priority for BluetoothDevice
@@ -333,8 +305,8 @@ public class A2dpServiceTest {
        // Update the device priority so okToConnect() returns true
        when(mDatabaseManager.getProfileConnectionPolicy(mTestDevice, BluetoothProfile.A2DP))
                .thenReturn(BluetoothProfile.CONNECTION_POLICY_ALLOWED);
        doReturn(true).when(mA2dpNativeInterface).connectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mA2dpNativeInterface).disconnectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mMockNativeInterface).connectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mMockNativeInterface).disconnectA2dp(any(BluetoothDevice.class));

        // Return AudioSource UUID instead of AudioSink
        doReturn(new ParcelUuid[]{BluetoothUuid.A2DP_SOURCE}).when(mAdapterService)
@@ -349,8 +321,8 @@ public class A2dpServiceTest {
     */
    @Test
    public void testOutgoingConnectPriorityOff() {
        doReturn(true).when(mA2dpNativeInterface).connectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mA2dpNativeInterface).disconnectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mMockNativeInterface).connectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mMockNativeInterface).disconnectA2dp(any(BluetoothDevice.class));

        // Set the device priority to PRIORITY_OFF so connect() should fail
        when(mDatabaseManager.getProfileConnectionPolicy(mTestDevice, BluetoothProfile.A2DP))
@@ -368,8 +340,8 @@ public class A2dpServiceTest {
        // Update the device priority so okToConnect() returns true
        when(mDatabaseManager.getProfileConnectionPolicy(mTestDevice, BluetoothProfile.A2DP))
                .thenReturn(BluetoothProfile.CONNECTION_POLICY_ALLOWED);
        doReturn(true).when(mA2dpNativeInterface).connectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mA2dpNativeInterface).disconnectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mMockNativeInterface).connectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mMockNativeInterface).disconnectA2dp(any(BluetoothDevice.class));

        // Send a connect request
        Assert.assertTrue("Connect failed", mA2dpService.connect(mTestDevice));
@@ -398,8 +370,8 @@ public class A2dpServiceTest {
        // Update the device priority so okToConnect() returns true
        when(mDatabaseManager.getProfileConnectionPolicy(mTestDevice, BluetoothProfile.A2DP))
                .thenReturn(BluetoothProfile.CONNECTION_POLICY_ALLOWED);
        doReturn(true).when(mA2dpNativeInterface).connectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mA2dpNativeInterface).disconnectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mMockNativeInterface).connectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mMockNativeInterface).disconnectA2dp(any(BluetoothDevice.class));

        // Send a connect request
        Assert.assertTrue("Connect failed", mA2dpService.connect(mTestDevice));
@@ -459,8 +431,8 @@ public class A2dpServiceTest {
        BluetoothDevice[] testDevices = new BluetoothDevice[MAX_CONNECTED_AUDIO_DEVICES];
        BluetoothDevice extraTestDevice;

        doReturn(true).when(mA2dpNativeInterface).connectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mA2dpNativeInterface).disconnectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mMockNativeInterface).connectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mMockNativeInterface).disconnectA2dp(any(BluetoothDevice.class));

        // Prepare and connect all test devices
        for (int i = 0; i < MAX_CONNECTED_AUDIO_DEVICES; i++) {
@@ -510,8 +482,8 @@ public class A2dpServiceTest {
        // Update the device priority so okToConnect() returns true
        when(mDatabaseManager.getProfileConnectionPolicy(mTestDevice, BluetoothProfile.A2DP))
                .thenReturn(BluetoothProfile.CONNECTION_POLICY_ALLOWED);
        doReturn(true).when(mA2dpNativeInterface).connectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mA2dpNativeInterface).disconnectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mMockNativeInterface).connectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mMockNativeInterface).disconnectA2dp(any(BluetoothDevice.class));

        // A2DP stack event: CONNECTION_STATE_CONNECTING - state machine should be created
        generateConnectionMessageFromNative(mTestDevice, BluetoothProfile.STATE_CONNECTING,
@@ -589,8 +561,8 @@ public class A2dpServiceTest {
        // Update the device priority so okToConnect() returns true
        when(mDatabaseManager.getProfileConnectionPolicy(mTestDevice, BluetoothProfile.A2DP))
                .thenReturn(BluetoothProfile.CONNECTION_POLICY_ALLOWED);
        doReturn(true).when(mA2dpNativeInterface).connectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mA2dpNativeInterface).disconnectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mMockNativeInterface).connectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mMockNativeInterface).disconnectA2dp(any(BluetoothDevice.class));

        // A2DP stack event: EVENT_TYPE_AUDIO_STATE_CHANGED - state machine should not be created
        generateUnexpectedAudioMessageFromNative(mTestDevice, A2dpStackEvent.AUDIO_STATE_STARTED,
@@ -653,8 +625,8 @@ public class A2dpServiceTest {
        // Update the device priority so okToConnect() returns true
        when(mDatabaseManager.getProfileConnectionPolicy(mTestDevice, BluetoothProfile.A2DP))
                .thenReturn(BluetoothProfile.CONNECTION_POLICY_ALLOWED);
        doReturn(true).when(mA2dpNativeInterface).connectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mA2dpNativeInterface).disconnectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mMockNativeInterface).connectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mMockNativeInterface).disconnectA2dp(any(BluetoothDevice.class));

        // A2DP stack event: CONNECTION_STATE_CONNECTING - state machine should be created
        generateConnectionMessageFromNative(mTestDevice, BluetoothProfile.STATE_CONNECTING,
@@ -711,8 +683,8 @@ public class A2dpServiceTest {
        // Update the device priority so okToConnect() returns true
        when(mDatabaseManager.getProfileConnectionPolicy(mTestDevice, BluetoothProfile.A2DP))
                .thenReturn(BluetoothProfile.CONNECTION_POLICY_ALLOWED);
        doReturn(true).when(mA2dpNativeInterface).connectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mA2dpNativeInterface).disconnectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mMockNativeInterface).connectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mMockNativeInterface).disconnectA2dp(any(BluetoothDevice.class));

        // A2DP stack event: CONNECTION_STATE_CONNECTING - state machine should be created
        generateConnectionMessageFromNative(mTestDevice, BluetoothProfile.STATE_CONNECTING,
@@ -756,32 +728,33 @@ public class A2dpServiceTest {
        BluetoothDevice otherDevice = mAdapter.getRemoteDevice("05:04:03:02:01:00");
        connectDevice(mTestDevice);
        connectDevice(otherDevice);
        doReturn(true).when(mA2dpNativeInterface).setActiveDevice(any(BluetoothDevice.class));
        doReturn(true).when(mA2dpNativeInterface).setSilenceDevice(any(BluetoothDevice.class),
                anyBoolean());
        doReturn(true).when(mMockNativeInterface).setActiveDevice(any(BluetoothDevice.class));
        doReturn(true)
                .when(mMockNativeInterface)
                .setSilenceDevice(any(BluetoothDevice.class), anyBoolean());

        // Test whether active device been removed after enable silence mode.
        Assert.assertTrue(mA2dpService.setActiveDevice(mTestDevice));
        Assert.assertEquals(mTestDevice, mA2dpService.getActiveDevice());
        Assert.assertTrue(mA2dpService.setSilenceMode(mTestDevice, true));
        verify(mA2dpNativeInterface).setSilenceDevice(mTestDevice, true);
        verify(mMockNativeInterface).setSilenceDevice(mTestDevice, true);
        Assert.assertNull(mA2dpService.getActiveDevice());

        // Test whether active device been resumeed after disable silence mode.
        Assert.assertTrue(mA2dpService.setSilenceMode(mTestDevice, false));
        verify(mA2dpNativeInterface).setSilenceDevice(mTestDevice, false);
        verify(mMockNativeInterface).setSilenceDevice(mTestDevice, false);
        Assert.assertEquals(mTestDevice, mA2dpService.getActiveDevice());

        // Test that active device should not be changed when silence a non-active device
        Assert.assertTrue(mA2dpService.setActiveDevice(mTestDevice));
        Assert.assertEquals(mTestDevice, mA2dpService.getActiveDevice());
        Assert.assertTrue(mA2dpService.setSilenceMode(otherDevice, true));
        verify(mA2dpNativeInterface).setSilenceDevice(otherDevice, true);
        verify(mMockNativeInterface).setSilenceDevice(otherDevice, true);
        Assert.assertEquals(mTestDevice, mA2dpService.getActiveDevice());

        // Test that active device should not be changed when another device exits silence mode
        Assert.assertTrue(mA2dpService.setSilenceMode(otherDevice, false));
        verify(mA2dpNativeInterface).setSilenceDevice(otherDevice, false);
        verify(mMockNativeInterface).setSilenceDevice(otherDevice, false);
        Assert.assertEquals(mTestDevice, mA2dpService.getActiveDevice());
    }

@@ -799,7 +772,7 @@ public class A2dpServiceTest {
    @Test
    public void testRemoveActiveDevice_whenStopAudioIsFalse_suppressNoisyIntent() {
        connectDevice(mTestDevice);
        doReturn(true).when(mA2dpNativeInterface).setActiveDevice(any(BluetoothDevice.class));
        doReturn(true).when(mMockNativeInterface).setActiveDevice(any(BluetoothDevice.class));
        Assert.assertTrue(mA2dpService.setActiveDevice(mTestDevice));
        Assert.assertEquals(mTestDevice, mA2dpService.getActiveDevice());
        AudioManager audioManager = mock(AudioManager.class);
@@ -828,7 +801,7 @@ public class A2dpServiceTest {
    @Test
    public void testRemoveActiveDevice_whenStopAudioIsFalse_doesNotSuppressNoisyIntent() {
        connectDevice(mTestDevice);
        doReturn(true).when(mA2dpNativeInterface).setActiveDevice(any(BluetoothDevice.class));
        doReturn(true).when(mMockNativeInterface).setActiveDevice(any(BluetoothDevice.class));
        Assert.assertTrue(mA2dpService.setActiveDevice(mTestDevice));
        Assert.assertEquals(mTestDevice, mA2dpService.getActiveDevice());
        AudioManager audioManager = mock(AudioManager.class);
@@ -941,7 +914,7 @@ public class A2dpServiceTest {
     */
    @Test
    public void testSendPreferredAudioProfileChangeToAudioFramework() {
        doReturn(true).when(mA2dpNativeInterface).setActiveDevice(any(BluetoothDevice.class));
        doReturn(true).when(mMockNativeInterface).setActiveDevice(any(BluetoothDevice.class));
        Assert.assertTrue(mA2dpService.removeActiveDevice(true));
        Assert.assertNull(mA2dpService.getActiveDevice());

@@ -973,9 +946,11 @@ public class A2dpServiceTest {
        // Update the device priority so okToConnect() returns true
        when(mDatabaseManager.getProfileConnectionPolicy(device, BluetoothProfile.A2DP))
                .thenReturn(BluetoothProfile.CONNECTION_POLICY_ALLOWED);
        doReturn(true).when(mA2dpNativeInterface).connectA2dp(device);
        doReturn(true).when(mA2dpNativeInterface).disconnectA2dp(device);
        doReturn(true).when(mA2dpNativeInterface).setCodecConfigPreference(
        doReturn(true).when(mMockNativeInterface).connectA2dp(device);
        doReturn(true).when(mMockNativeInterface).disconnectA2dp(device);
        doReturn(true)
                .when(mMockNativeInterface)
                .setCodecConfigPreference(
                        any(BluetoothDevice.class), any(BluetoothCodecConfig[].class));

        // Send a connect request
@@ -1119,7 +1094,7 @@ public class A2dpServiceTest {
    private void testUpdateOptionalCodecsSupportCase(int previousSupport, boolean support,
            int previousEnabled, int verifySupportTime, int verifyNotSupportTime,
            int verifyEnabledTime) {
        doReturn(true).when(mA2dpNativeInterface).setActiveDevice(any(BluetoothDevice.class));
        doReturn(true).when(mMockNativeInterface).setActiveDevice(any(BluetoothDevice.class));

        BluetoothCodecConfig codecConfigSbc =
                buildBluetoothCodecConfig(
+4 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import androidx.test.rule.ServiceTestRule;
import androidx.test.runner.AndroidJUnit4;

import com.android.bluetooth.TestUtils;
import com.android.bluetooth.a2dp.A2dpNativeInterface;
import com.android.bluetooth.btservice.storage.DatabaseManager;
import com.android.bluetooth.gatt.GattService;
import com.android.bluetooth.pan.PanNativeInterface;
@@ -83,6 +84,7 @@ public class ProfileServiceTest {
        mServiceTestRule.startService(startIntent);
    }

    @Mock private A2dpNativeInterface mA2dpNativeInterface;
    @Mock private PanNativeInterface mPanNativeInterface;

    private void setAllProfilesState(int state, int invocationNumber) throws TimeoutException {
@@ -163,6 +165,7 @@ public class ProfileServiceTest {
                false /* is_common_criteria_mode */, 0 /* config_compare_result */,
                new String[0], false, "");
        mMockAdapterService.enableNative();
        A2dpNativeInterface.setInstance(mA2dpNativeInterface);
        PanNativeInterface.setInstance(mPanNativeInterface);
    }

@@ -175,6 +178,7 @@ public class ProfileServiceTest {
        TestUtils.clearAdapterService(mMockAdapterService);
        mMockAdapterService = null;
        mProfiles = null;
        A2dpNativeInterface.setInstance(null);
        PanNativeInterface.setInstance(null);
    }