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

Commit 21f607ad authored by Sandeep Samdaria's avatar Sandeep Samdaria
Browse files

Fix flaky tests related to bluetooth browser media service.

Problem: Some of the test randomly fails while trying to assert
the value of BluetoothMediaBrowserService's session state.

Solution: Test's setup should wait until BluetoothBrowserMediaService
is up and running. Using ServiceTestRule makes the starService call a
blocking call. Once the service is up, rest of the test case proceeds
with the test logic.

Bug: 275458774
Test: Ran a custom script which runs each test 20 times to ensure it
doesn't fail.
Test: atest GoogleBluetoothInstrumentationTests:com.android.bluetooth
.a2dpsink.A2dpSinkStreamHandlerTest
Test: atest GoogleBluetoothInstrumentationTests:com.android.bluetooth
.avrcpcontroller.AvrcpBipClientTest
Test: atest GoogleBluetoothInstrumentationTests:com.android.bluetooth
.a2dpsink.A2dpSinkStreamHandlerTest
Tag: #stability

Change-Id: I7c8fc877e50f4b0e40f6c4463b05e9292d3fa656
parent 36e4fb4c
Loading
Loading
Loading
Loading
+15 −1
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
 */
package com.android.bluetooth;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

import static org.mockito.ArgumentMatchers.eq;
@@ -29,11 +30,12 @@ import android.content.res.Resources;
import android.os.Handler;
import android.os.Looper;
import android.os.MessageQueue;
import android.os.Process;
import android.service.media.MediaBrowserService;

import androidx.test.InstrumentationRegistry;
import androidx.test.rule.ServiceTestRule;

import com.android.bluetooth.avrcpcontroller.BluetoothMediaBrowserService;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.ProfileService;

@@ -366,6 +368,18 @@ public class TestUtils {
        return adapterConfig;
    }

    /**
     * Prepare the intent to start bluetooth browser media service.
     *
     * @return intent with the appropriate component & action set.
     */
    public static Intent prepareIntentToStartBluetoothBrowserMediaService() {
        final Intent intent = new Intent(InstrumentationRegistry.getTargetContext(),
                BluetoothMediaBrowserService.class);
        intent.setAction(MediaBrowserService.SERVICE_INTERFACE);
        return intent;
    }

    /**
     * Helper class used to run synchronously a runnable action on a looper.
     */
+8 −3
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.*;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.media.AudioManager;
@@ -39,7 +40,6 @@ import com.android.bluetooth.avrcpcontroller.BluetoothMediaBrowserService;
import com.android.bluetooth.btservice.AdapterService;

import org.junit.After;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -68,6 +68,9 @@ public class A2dpSinkStreamHandlerTest {
    @Rule
    public final ServiceTestRule mServiceRule = new ServiceTestRule();

    @Rule
    public final ServiceTestRule mBluetoothBrowserMediaServiceTestRule = new ServiceTestRule();

    @Mock
    private AdapterService mAdapterService;

@@ -82,6 +85,9 @@ public class A2dpSinkStreamHandlerTest {
        TestUtils.setAdapterService(mAdapterService);
        doReturn(true, false).when(mAdapterService).isStartedProfile(anyString());
        TestUtils.startService(mServiceRule, AvrcpControllerService.class);
        final Intent bluetoothBrowserMediaServiceStartIntent =
                TestUtils.prepareIntentToStartBluetoothBrowserMediaService();
        mBluetoothBrowserMediaServiceTestRule.startService(bluetoothBrowserMediaServiceStartIntent);

        mHandlerThread = new HandlerThread("A2dpSinkStreamHandlerTest");
        mHandlerThread.start();
@@ -101,7 +107,6 @@ public class A2dpSinkStreamHandlerTest {
        when(mMockPackageManager.hasSystemFeature(any())).thenReturn(false);

        mStreamHandler = spy(new A2dpSinkStreamHandler(mMockA2dpSink, mMockNativeInterface));
        BluetoothMediaBrowserService.setActive(false);
    }

    @After
+7 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import static org.mockito.Mockito.doReturn;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.content.Intent;

import androidx.test.filters.SmallTest;
import androidx.test.rule.ServiceTestRule;
@@ -49,6 +50,9 @@ public class AvrcpBipClientTest {
    @Rule
    public final ServiceTestRule mServiceRule = new ServiceTestRule();

    @Rule
    public final ServiceTestRule mBluetoothBrowserMediaServiceTestRule = new ServiceTestRule();

    @Mock
    private AdapterService mAdapterService;

@@ -65,6 +69,9 @@ public class AvrcpBipClientTest {
        doReturn(true, false).when(mAdapterService).isStartedProfile(anyString());
        TestUtils.startService(mServiceRule, AvrcpControllerService.class);
        mService = AvrcpControllerService.getAvrcpControllerService();
        final Intent bluetoothBrowserMediaServiceStartIntent =
                TestUtils.prepareIntentToStartBluetoothBrowserMediaService();
        mBluetoothBrowserMediaServiceTestRule.startService(bluetoothBrowserMediaServiceStartIntent);

        mAdapter = BluetoothAdapter.getDefaultAdapter();
        mTestDevice = mAdapter.getRemoteDevice("00:01:02:03:04:05");
+7 −2
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.content.Intent;
import android.media.AudioManager;
import android.support.v4.media.session.PlaybackStateCompat;

@@ -60,6 +61,8 @@ public class AvrcpControllerServiceTest {
    private BluetoothAdapter mAdapter = null;

    @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule();
    @Rule
    public final ServiceTestRule mBluetoothBrowserMediaServiceTestRule = new ServiceTestRule();

    @Mock private AdapterService mAdapterService;
    @Mock private AvrcpControllerStateMachine mStateMachine;
@@ -79,7 +82,9 @@ public class AvrcpControllerServiceTest {
        assertThat(mAdapter).isNotNull();
        mRemoteDevice = mAdapter.getRemoteDevice(REMOTE_DEVICE_ADDRESS);
        mService.mDeviceStateMap.put(mRemoteDevice, mStateMachine);
        BluetoothMediaBrowserService.setActive(false);
        final Intent bluetoothBrowserMediaServiceStartIntent =
                TestUtils.prepareIntentToStartBluetoothBrowserMediaService();
        mBluetoothBrowserMediaServiceTestRule.startService(bluetoothBrowserMediaServiceStartIntent);
    }

    @After