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

Commit 2a34e820 authored by Sal Savage's avatar Sal Savage
Browse files

Replace flag rule with flag annotations

Flag rule based calls happen exactly when they're made, and allow for
setup code to be ran without the properly set flag values. This is
errorprone and can cause subtle bugs where the flag values don't appear
to be set properly.

Instead, we should use the new annotations, which are executed _before_
setup is called.

Bug: 352982293
Flag: EXEMPT, test only change
Test: atest BrowseTreeTest.java
Test: atest AvrcpControllerServiceTest.java
Test: atest AvrcpControllerStateMachineTest.java
Change-Id: Iff1c55f96b51269bbc74492d464135e7cc579744
parent 84d8b49d
Loading
Loading
Loading
Loading
+4 −6
Original line number Diff line number Diff line
@@ -30,8 +30,8 @@ import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.media.AudioManager;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.support.v4.media.session.PlaybackStateCompat;

@@ -40,7 +40,6 @@ 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.a2dpsink.A2dpSinkService;
import com.android.bluetooth.avrcpcontroller.BluetoothMediaBrowserService.BrowseResult;
@@ -67,11 +66,11 @@ public class AvrcpControllerServiceTest {
    private static final String REMOTE_DEVICE_ADDRESS = "00:00:00:00:00:00";
    private static final String REMOTE_DEVICE_ADDRESS_2 = "11:11:11:11:11:11";

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

    private AvrcpControllerService mService = null;
    private BluetoothAdapter mAdapter = null;

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

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

@@ -517,9 +516,8 @@ public class AvrcpControllerServiceTest {
     * first device by checking that it has remained as the active device.
     */
    @Test
    @EnableFlags(Flags.FLAG_RANDOMIZE_DEVICE_LEVEL_MEDIA_IDS)
    public void testActiveDeviceMaintainsAudioFocusWhenOtherDeviceConnects_audioFocusMaintained() {
        mSetFlagsRule.enableFlags(Flags.FLAG_RANDOMIZE_DEVICE_LEVEL_MEDIA_IDS);

        mService.onConnectionStateChanged(true, true, mRemoteDevice);
        // check set active device is called
        verify(mA2dpSinkService).setActiveDevice(mRemoteDevice);
+6 −4
Original line number Diff line number Diff line
@@ -31,6 +31,8 @@ import android.content.res.Resources;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Looper;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaControllerCompat;
@@ -74,10 +76,10 @@ public class AvrcpControllerStateMachineTest {
    private static final int UUID_START = 0;
    private static final int UUID_LENGTH = 25;

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

    private BluetoothAdapter mAdapter;

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

    @Rule public MockitoRule mockitoRule = MockitoJUnit.rule();

    @Mock private AdapterService mAdapterService;
@@ -466,8 +468,8 @@ public class AvrcpControllerStateMachineTest {

    /** Get the root of the device */
    @Test
    @DisableFlags(Flags.FLAG_RANDOMIZE_DEVICE_LEVEL_MEDIA_IDS)
    public void testGetDeviceRootNode_flagRandomDeviceIdDisabled_rootNodeMatchesUuidFormat() {
        mSetFlagsRule.disableFlags(Flags.FLAG_RANDOMIZE_DEVICE_LEVEL_MEDIA_IDS);
        // create new state machine to follow current flags rule
        mAvrcpStateMachine = makeStateMachine(mTestDevice);
        setUpConnectedState(true, true);
@@ -479,8 +481,8 @@ public class AvrcpControllerStateMachineTest {

    /** Get the root of the device */
    @Test
    @EnableFlags(Flags.FLAG_RANDOMIZE_DEVICE_LEVEL_MEDIA_IDS)
    public void testGetDeviceRootNode_flagRandomDeviceIdEnabled_rootNodeMatchesUuidFormat() {
        mSetFlagsRule.enableFlags(Flags.FLAG_RANDOMIZE_DEVICE_LEVEL_MEDIA_IDS);
        // create new state machine to follow current flags rule
        mAvrcpStateMachine = makeStateMachine(mTestDevice);
        setUpConnectedState(true, true);
+5 −11
Original line number Diff line number Diff line
@@ -20,6 +20,8 @@ import static com.google.common.truth.Truth.assertThat;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;

import com.android.bluetooth.avrcpcontroller.BrowseTree.BrowseNode;
@@ -100,8 +102,8 @@ public class BrowseTreeTest {
    }

    @Test
    @EnableFlags(Flags.FLAG_RANDOMIZE_DEVICE_LEVEL_MEDIA_IDS)
    public void sameDeviceDifferentBrowseTrees_uniqueMediaIds() {
        mSetFlagsRule.enableFlags(Flags.FLAG_RANDOMIZE_DEVICE_LEVEL_MEDIA_IDS);
        BrowseTree browseTree1 = new BrowseTree(mTestDevice);
        BrowseTree browseTree2 = new BrowseTree(mTestDevice);

@@ -118,21 +120,13 @@ public class BrowseTreeTest {
    }

    @Test
    @DisableFlags(Flags.FLAG_RANDOMIZE_DEVICE_LEVEL_MEDIA_IDS)
    public void findBrowseNodeByIDForDevice() {
        mSetFlagsRule.disableFlags(Flags.FLAG_RANDOMIZE_DEVICE_LEVEL_MEDIA_IDS);
        BrowseTree browseTree = new BrowseTree(mTestDevice);
        final String deviceId = BrowseTree.ROOT + mTestDevice.getAddress().toString();
        assertThat(browseTree.findBrowseNodeByID(deviceId)).isEqualTo(browseTree.mRootNode);
    }

    @Test
    public void findBrowseNodeByIDForDevice_flagEnabled() {
        mSetFlagsRule.enableFlags(Flags.FLAG_RANDOMIZE_DEVICE_LEVEL_MEDIA_IDS);
        BrowseTree browseTree = new BrowseTree(mTestDevice);
        final String deviceId = browseTree.mRootNode.getID();
        assertThat(browseTree.findBrowseNodeByID(deviceId)).isEqualTo(browseTree.mRootNode);
    }

    @Test
    public void findBrowseNodeByIDForIllegalId() {
        BrowseTree browseTree = new BrowseTree(mTestDevice);
@@ -149,8 +143,8 @@ public class BrowseTreeTest {
    }

    @Test
    @EnableFlags(Flags.FLAG_RANDOMIZE_DEVICE_LEVEL_MEDIA_IDS)
    public void findBrowseNodeByIDForDevice_withRandomDeviceID_nodeIsFound() {
        mSetFlagsRule.enableFlags(Flags.FLAG_RANDOMIZE_DEVICE_LEVEL_MEDIA_IDS);
        BrowseTree browseTree = new BrowseTree(mTestDevice);
        final String deviceId = browseTree.mRootNode.getID();
        assertThat(browseTree.findBrowseNodeByID(deviceId)).isEqualTo(browseTree.mRootNode);