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

Commit b0e640c4 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Re-enable the A2DP unit tests"

parents a9d5799e 3f26a83f
Loading
Loading
Loading
Loading
+3 −11
Original line number Diff line number Diff line
@@ -74,8 +74,7 @@ public class A2dpService extends ProfileService {
    // Upper limit of all A2DP devices: Bonded or Connected
    private static final int MAX_A2DP_STATE_MACHINES = 50;
    // Upper limit of all A2DP devices that are Connected or Connecting
    @VisibleForTesting
    int mMaxConnectedAudioDevices = 1;
    private int mMaxConnectedAudioDevices = 1;

    private BroadcastReceiver mBondStateChangedReceiver = null;
    private BroadcastReceiver mConnectionStateChangedReceiver = null;
@@ -282,7 +281,8 @@ public class A2dpService extends ProfileService {
     * @param device the peer device to connect to
     * @return true if connection is allowed, otherwise false
     */
    boolean canConnectToDevice(BluetoothDevice device) {
    @VisibleForTesting
    public boolean canConnectToDevice(BluetoothDevice device) {
        int connected = 0;
        // Count devices that are in the process of connecting or already connected
        synchronized (mStateMachines) {
@@ -816,14 +816,6 @@ public class A2dpService extends ProfileService {
            return null;
        }

        @VisibleForTesting
        A2dpService getServiceForTesting() {
            if (mService != null && mService.isAvailable()) {
                return mService;
            }
            return null;
        }

        BluetoothA2dpBinder(A2dpService svc) {
            mService = svc;
        }
+34 −21
Original line number Diff line number Diff line
@@ -26,7 +26,6 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.os.Looper;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.MediumTest;
@@ -38,7 +37,6 @@ import com.android.bluetooth.btservice.AdapterService;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -53,7 +51,6 @@ import java.util.concurrent.TimeoutException;

@MediumTest
@RunWith(AndroidJUnit4.class)
@Ignore("Test is broken - mocking side-effect of mA2dpService")
public class A2dpServiceTest {
    private BluetoothAdapter mAdapter;
    private Context mTargetContext;
@@ -85,11 +82,12 @@ public class A2dpServiceTest {
                                                               AdapterService.class);
        method.setAccessible(true);
        method.invoke(mAdapterService, mAdapterService);
        doReturn(1).when(mAdapterService).getMaxConnectedAudioDevices();

        mTargetContext = InstrumentationRegistry.getTargetContext();
        mAdapter = BluetoothAdapter.getDefaultAdapter();

        bondService();
        startService();
        mA2dpService.mA2dpNativeInterface = mA2dpNativeInterface;

        // Override the timeout value to speed up the test
@@ -101,29 +99,44 @@ public class A2dpServiceTest {
        mConnectionStateChangedReceiver = new ConnectionStateChangedReceiver();
        mTargetContext.registerReceiver(mConnectionStateChangedReceiver, filter);

        mA2dpService.start();

        // Get a device for testing
        mTestDevice = mAdapter.getRemoteDevice("00:01:02:03:04:05");
        mA2dpService.setPriority(mTestDevice, BluetoothProfile.PRIORITY_UNDEFINED);
    }
    private void bondService() throws TimeoutException {
        IBinder binder = mServiceRule.bindService(
                new Intent(mTargetContext, A2dpService.class));
        mA2dpService = ((A2dpService.BluetoothA2dpBinder) binder).getServiceForTesting();
        Assert.assertNotNull(mA2dpService);
    }

    @After
    public void tearDown() {
        mA2dpService.stop();
        mA2dpService.cleanup();
        mA2dpService = null;

    public void tearDown() throws Exception {
        stopService();
        mTargetContext.unregisterReceiver(mConnectionStateChangedReceiver);
        mConnectionStateChangedQueue.clear();
    }

    private void startService() throws TimeoutException {
        Intent startIntent =
                new Intent(InstrumentationRegistry.getTargetContext(), A2dpService.class);
        startIntent.putExtra(AdapterService.EXTRA_ACTION,
                             AdapterService.ACTION_SERVICE_STATE_CHANGED);
        startIntent.putExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_ON);
        mServiceRule.startService(startIntent);
        verify(mAdapterService, timeout(TIMEOUT_MS)).onProfileServiceStateChanged(
                eq(A2dpService.class.getName()), eq(BluetoothAdapter.STATE_ON));
        mA2dpService = A2dpService.getA2dpService();
        Assert.assertNotNull(mA2dpService);
    }

    private void stopService() throws TimeoutException {
        Intent stopIntent =
                new Intent(InstrumentationRegistry.getTargetContext(), A2dpService.class);
        stopIntent.putExtra(AdapterService.EXTRA_ACTION,
                            AdapterService.ACTION_SERVICE_STATE_CHANGED);
        stopIntent.putExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
        mServiceRule.startService(stopIntent);
        verify(mAdapterService, timeout(TIMEOUT_MS)).onProfileServiceStateChanged(
                eq(A2dpService.class.getName()), eq(BluetoothAdapter.STATE_OFF));
        mA2dpService = A2dpService.getA2dpService();
        Assert.assertNull(mA2dpService);
    }

    private class ConnectionStateChangedReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
@@ -202,8 +215,8 @@ public class A2dpServiceTest {
    public void testOutgoingConnectTimeout() {
        // Update the device priority so okToConnect() returns true
        mA2dpService.setPriority(mTestDevice, BluetoothProfile.PRIORITY_ON);
        when(mA2dpNativeInterface.connectA2dp(any(BluetoothDevice.class))).thenReturn(true);
        when(mA2dpNativeInterface.disconnectA2dp(any(BluetoothDevice.class))).thenReturn(true);
        doReturn(true).when(mA2dpNativeInterface).connectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mA2dpNativeInterface).disconnectA2dp(any(BluetoothDevice.class));

        // Send a connect request
        Assert.assertTrue("Connect failed", mA2dpService.connect(mTestDevice));
@@ -231,8 +244,8 @@ public class A2dpServiceTest {

        // Update the device priority so okToConnect() returns true
        mA2dpService.setPriority(mTestDevice, BluetoothProfile.PRIORITY_ON);
        when(mA2dpNativeInterface.connectA2dp(any(BluetoothDevice.class))).thenReturn(true);
        when(mA2dpNativeInterface.disconnectA2dp(any(BluetoothDevice.class))).thenReturn(true);
        doReturn(true).when(mA2dpNativeInterface).connectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mA2dpNativeInterface).disconnectA2dp(any(BluetoothDevice.class));

        // Send a connect request
        Assert.assertTrue("Connect failed", mA2dpService.connect(mTestDevice));
+23 −12
Original line number Diff line number Diff line
@@ -35,7 +35,6 @@ import org.hamcrest.core.IsInstanceOf;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
@@ -46,7 +45,6 @@ import java.lang.reflect.Method;

@MediumTest
@RunWith(AndroidJUnit4.class)
@Ignore("Test is broken - mocking side-effect of mA2dpService")
public class A2dpStateMachineTest {
    private BluetoothAdapter mAdapter;
    private Context mTargetContext;
@@ -104,14 +102,30 @@ public class A2dpStateMachineTest {
                mA2dpStateMachine.getConnectionState());
    }

    /**
     * Allow/disallow connection to any device.
     *
     * @param allow if true, connection is allowed
     */
    private void allowConnection(boolean allow) {
        if (allow) {
            // Update the device priority so okToConnect() returns true
            doReturn(BluetoothProfile.PRIORITY_ON).when(mA2dpService)
                    .getPriority(any(BluetoothDevice.class));
        } else {
            // Update the device priority so okToConnect() returns false
            doReturn(BluetoothProfile.PRIORITY_OFF).when(mA2dpService)
                    .getPriority(any(BluetoothDevice.class));
        }
        doReturn(true).when(mA2dpService).canConnectToDevice(any(BluetoothDevice.class));
    }

    /**
     * Test that an incoming connection with low priority is rejected
     */
    @Test
    public void testIncomingPriorityReject() {
        // Update the device priority so okToConnect() returns false
        when(mA2dpService.getPriority(any(BluetoothDevice.class))).thenReturn(
                BluetoothProfile.PRIORITY_OFF);
        allowConnection(false);

        // Inject an event for when incoming connection is requested
        A2dpStackEvent connStCh =
@@ -133,9 +147,7 @@ public class A2dpStateMachineTest {
     */
    @Test
    public void testIncomingPriorityAccept() {
        // Update the device priority so okToConnect() returns true
        when(mA2dpService.getPriority(any(BluetoothDevice.class))).thenReturn(
                BluetoothProfile.PRIORITY_ON);
        allowConnection(true);

        // Inject an event for when incoming connection is requested
        A2dpStackEvent connStCh =
@@ -182,12 +194,11 @@ public class A2dpStateMachineTest {
    /**
     * Test that an outgoing connection times out
     */
    @Ignore("Test is broken - mocking side-effect of mA2dpService")
    @Test
    public void testOutgoingTimeout() {
        when(mA2dpService.canConnectToDevice(any(BluetoothDevice.class))).thenReturn(true);
        when(mA2dpNativeInterface.connectA2dp(any(BluetoothDevice.class))).thenReturn(true);
        when(mA2dpNativeInterface.disconnectA2dp(any(BluetoothDevice.class))).thenReturn(true);
        allowConnection(true);
        doReturn(true).when(mA2dpNativeInterface).connectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mA2dpNativeInterface).disconnectA2dp(any(BluetoothDevice.class));

        // Send a connect request
        mA2dpStateMachine.sendMessage(A2dpStateMachine.CONNECT, mTestDevice);