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

Commit 996e12b3 authored by Yiyi Shen's avatar Yiyi Shen Committed by Android (Google) Code Review
Browse files

Merge "[Audiosharing] Increase test coverage for audio sharing" into main

parents 7f55253f 643d6c66
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -57,9 +57,10 @@ public class AudioSharingCompatibilityPreferenceController extends TogglePrefere
    @Nullable private TwoStatePreference mPreference;
    private final Executor mExecutor;
    private final MetricsFeatureProvider mMetricsFeatureProvider;
    private AtomicBoolean mCallbacksRegistered = new AtomicBoolean(false);
    private final AtomicBoolean mCallbacksRegistered = new AtomicBoolean(false);

    private final BluetoothLeBroadcast.Callback mBroadcastCallback =
    @VisibleForTesting
    protected final BluetoothLeBroadcast.Callback mBroadcastCallback =
            new BluetoothLeBroadcast.Callback() {
                @Override
                public void onBroadcastStarted(int reason, int broadcastId) {
+19 −4
Original line number Diff line number Diff line
@@ -20,6 +20,8 @@ import android.app.settings.SettingsEnums;
import android.content.Context;
import android.os.Bundle;

import androidx.annotation.VisibleForTesting;

import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.connecteddevice.audiosharing.audiostreams.AudioStreamsCategoryController;
@@ -31,7 +33,6 @@ public class AudioSharingDashboardFragment extends DashboardFragment
    private static final String TAG = "AudioSharingDashboardFrag";

    SettingsMainSwitchBar mMainSwitchBar;
    private AudioSharingSwitchBarController mSwitchBarController;
    private AudioSharingDeviceVolumeGroupController mAudioSharingDeviceVolumeGroupController;
    private AudioSharingCallAudioPreferenceController mAudioSharingCallAudioPreferenceController;
    private AudioSharingPlaySoundPreferenceController mAudioSharingPlaySoundPreferenceController;
@@ -83,9 +84,10 @@ public class AudioSharingDashboardFragment extends DashboardFragment
        final SettingsActivity activity = (SettingsActivity) getActivity();
        mMainSwitchBar = activity.getSwitchBar();
        mMainSwitchBar.setTitle(getText(R.string.audio_sharing_switch_title));
        mSwitchBarController = new AudioSharingSwitchBarController(activity, mMainSwitchBar, this);
        mSwitchBarController.init(this);
        getSettingsLifecycle().addObserver(mSwitchBarController);
        AudioSharingSwitchBarController switchBarController =
                new AudioSharingSwitchBarController(activity, mMainSwitchBar, this);
        switchBarController.init(this);
        getSettingsLifecycle().addObserver(switchBarController);
        mMainSwitchBar.show();
    }

@@ -99,6 +101,19 @@ public class AudioSharingDashboardFragment extends DashboardFragment
        onProfilesConnectedForAttachedPreferences();
    }

    /** Test only: set mock controllers for the {@link AudioSharingDashboardFragment} */
    @VisibleForTesting
    protected void setControllers(
            AudioSharingDeviceVolumeGroupController volumeGroupController,
            AudioSharingCallAudioPreferenceController callAudioController,
            AudioSharingPlaySoundPreferenceController playSoundController,
            AudioStreamsCategoryController streamsCategoryController) {
        mAudioSharingDeviceVolumeGroupController = volumeGroupController;
        mAudioSharingCallAudioPreferenceController = callAudioController;
        mAudioSharingPlaySoundPreferenceController = playSoundController;
        mAudioStreamsCategoryController = streamsCategoryController;
    }

    private void updateVisibilityForAttachedPreferences() {
        mAudioSharingDeviceVolumeGroupController.updateVisibility();
        mAudioSharingCallAudioPreferenceController.updateVisibility();
+3 −1
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
@@ -50,7 +51,8 @@ public class AudioSharingPreferenceController extends BasePreferenceController
    @Nullable private Preference mPreference;
    private final Executor mExecutor;

    private final BluetoothLeBroadcast.Callback mBroadcastCallback =
    @VisibleForTesting
    protected final BluetoothLeBroadcast.Callback mBroadcastCallback =
            new BluetoothLeBroadcast.Callback() {
                @Override
                public void onBroadcastStarted(int reason, int broadcastId) {
+89 −24
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
@@ -34,6 +35,7 @@ import static org.robolectric.Shadows.shadowOf;
import android.app.settings.SettingsEnums;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothLeBroadcast;
import android.bluetooth.BluetoothLeBroadcastMetadata;
import android.bluetooth.BluetoothStatusCodes;
import android.content.Context;
import android.os.Looper;
@@ -94,28 +96,28 @@ public class AudioSharingCompatibilityPreferenceControllerTest {
    @Mock private LocalBluetoothLeBroadcastAssistant mAssistant;
    @Mock private VolumeControlProfile mVolumeControl;
    @Mock private TwoStatePreference mPreference;
    @Mock private BluetoothLeBroadcastMetadata mMetadata;
    private AudioSharingCompatibilityPreferenceController mController;
    private ShadowBluetoothAdapter mShadowBluetoothAdapter;
    private LocalBluetoothManager mLocalBluetoothManager;
    private FakeFeatureFactory mFeatureFactory;
    private Lifecycle mLifecycle;
    private LifecycleOwner mLifecycleOwner;

    @Before
    public void setUp() {
        mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
        mShadowBluetoothAdapter.setEnabled(true);
        mShadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported(
        ShadowBluetoothAdapter shadowBluetoothAdapter =
                Shadow.extract(BluetoothAdapter.getDefaultAdapter());
        shadowBluetoothAdapter.setEnabled(true);
        shadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported(
                BluetoothStatusCodes.FEATURE_SUPPORTED);
        mShadowBluetoothAdapter.setIsLeAudioBroadcastAssistantSupported(
        shadowBluetoothAdapter.setIsLeAudioBroadcastAssistantSupported(
                BluetoothStatusCodes.FEATURE_SUPPORTED);
        mLifecycleOwner = () -> mLifecycle;
        mLifecycle = new Lifecycle(mLifecycleOwner);
        ShadowBluetoothUtils.sLocalBluetoothManager = mLocalBtManager;
        mLocalBluetoothManager = Utils.getLocalBtManager(mContext);
        LocalBluetoothManager localBluetoothManager = Utils.getLocalBtManager(mContext);
        mFeatureFactory = FakeFeatureFactory.setupForTest();
        when(mLocalBluetoothManager.getEventManager()).thenReturn(mBtEventManager);
        when(mLocalBluetoothManager.getProfileManager()).thenReturn(mBtProfileManager);
        when(localBluetoothManager.getEventManager()).thenReturn(mBtEventManager);
        when(localBluetoothManager.getProfileManager()).thenReturn(mBtProfileManager);
        when(mBtProfileManager.getLeAudioBroadcastProfile()).thenReturn(mBroadcast);
        when(mBtProfileManager.getLeAudioBroadcastAssistantProfile()).thenReturn(mAssistant);
        when(mBtProfileManager.getVolumeControlProfile()).thenReturn(mVolumeControl);
@@ -133,7 +135,7 @@ public class AudioSharingCompatibilityPreferenceControllerTest {
        verify(mBroadcast)
                .registerServiceCallBack(
                        any(Executor.class), any(BluetoothLeBroadcast.Callback.class));
        verify(mBtProfileManager, times(0)).addServiceListener(mController);
        verify(mBtProfileManager, never()).addServiceListener(mController);
    }

    @Test
@@ -141,7 +143,7 @@ public class AudioSharingCompatibilityPreferenceControllerTest {
        mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
        when(mBroadcast.isProfileReady()).thenReturn(false);
        mController.onStart(mLifecycleOwner);
        verify(mBroadcast, times(0))
        verify(mBroadcast, never())
                .registerServiceCallBack(
                        any(Executor.class), any(BluetoothLeBroadcast.Callback.class));
        verify(mBtProfileManager).addServiceListener(mController);
@@ -151,7 +153,7 @@ public class AudioSharingCompatibilityPreferenceControllerTest {
    public void onStart_flagOff_doNothing() {
        mSetFlagsRule.disableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
        mController.onStart(mLifecycleOwner);
        verify(mBroadcast, times(0))
        verify(mBroadcast, never())
                .registerServiceCallBack(
                        any(Executor.class), any(BluetoothLeBroadcast.Callback.class));
    }
@@ -170,9 +172,9 @@ public class AudioSharingCompatibilityPreferenceControllerTest {
        mSetFlagsRule.disableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
        mController.setCallbacksRegistered(true);
        mController.onStop(mLifecycleOwner);
        verify(mBroadcast, times(0))
        verify(mBroadcast, never())
                .unregisterServiceCallBack(any(BluetoothLeBroadcast.Callback.class));
        verify(mBtProfileManager, times(0)).removeServiceListener(mController);
        verify(mBtProfileManager, never()).removeServiceListener(mController);
    }

    @Test
@@ -224,11 +226,10 @@ public class AudioSharingCompatibilityPreferenceControllerTest {
        mController.displayPreference(mScreen);
        shadowOf(Looper.getMainLooper()).idle();
        verify(mPreference).setEnabled(false);
        verify(mPreference)
                .setSummary(
                        eq(mContext.getString(
                                R.string
                                        .audio_sharing_stream_compatibility_disabled_description)));
        String expected =
                mContext.getString(
                        R.string.audio_sharing_stream_compatibility_disabled_description);
        verify(mPreference).setSummary(eq(expected));
    }

    @Test
@@ -237,10 +238,9 @@ public class AudioSharingCompatibilityPreferenceControllerTest {
        mController.displayPreference(mScreen);
        shadowOf(Looper.getMainLooper()).idle();
        verify(mPreference).setEnabled(true);
        verify(mPreference)
                .setSummary(
                        eq(mContext.getString(
                                R.string.audio_sharing_stream_compatibility_description)));
        String expected =
                mContext.getString(R.string.audio_sharing_stream_compatibility_description);
        verify(mPreference).setSummary(eq(expected));
    }

    @Test
@@ -272,8 +272,73 @@ public class AudioSharingCompatibilityPreferenceControllerTest {
    public void setCheckedToCurrentValue_returnsFalse() {
        when(mBroadcast.getImproveCompatibility()).thenReturn(true);
        boolean setChecked = mController.setChecked(true);
        verify(mBroadcast, times(0)).setImproveCompatibility(anyBoolean());
        verify(mBroadcast, never()).setImproveCompatibility(anyBoolean());
        verifyNoInteractions(mFeatureFactory.metricsFeatureProvider);
        assertThat(setChecked).isFalse();
    }

    @Test
    public void testBluetoothLeBroadcastCallbacks_refreshPreference() {
        when(mBroadcast.isEnabled(any())).thenReturn(false);
        mController.displayPreference(mScreen);
        shadowOf(Looper.getMainLooper()).idle();
        verify(mPreference).setEnabled(true);
        String expected =
                mContext.getString(R.string.audio_sharing_stream_compatibility_description);
        verify(mPreference).setSummary(eq(expected));

        when(mBroadcast.isEnabled(any())).thenReturn(true);
        mController.mBroadcastCallback.onBroadcastStarted(/* reason= */ 1, /* broadcastId= */ 1);
        shadowOf(Looper.getMainLooper()).idle();
        verify(mPreference).setEnabled(false);
        expected =
                mContext.getString(
                        R.string.audio_sharing_stream_compatibility_disabled_description);
        verify(mPreference).setSummary(eq(expected));

        when(mBroadcast.isEnabled(any())).thenReturn(false);
        mController.mBroadcastCallback.onBroadcastStopped(/* reason= */ 1, /* broadcastId= */ 1);
        shadowOf(Looper.getMainLooper()).idle();

        // Verify one extra setEnabled/setSummary is called other than the first call in
        // displayPreference.
        verify(mPreference, times(2)).setEnabled(true);
        expected = mContext.getString(R.string.audio_sharing_stream_compatibility_description);
        verify(mPreference, times(2)).setSummary(eq(expected));
    }

    @Test
    public void testBluetoothLeBroadcastCallbacks_doNothing() {
        when(mBroadcast.isEnabled(any())).thenReturn(false);
        mController.displayPreference(mScreen);
        shadowOf(Looper.getMainLooper()).idle();
        verify(mPreference).setEnabled(true);
        String expected =
                mContext.getString(R.string.audio_sharing_stream_compatibility_description);
        verify(mPreference).setSummary(eq(expected));

        // Verify no extra setEnabled/setSummary is called other than call in displayPreference.
        mController.mBroadcastCallback.onBroadcastMetadataChanged(/* reason= */ 1, mMetadata);
        verify(mPreference).setEnabled(anyBoolean());
        verify(mPreference).setSummary(any());
        mController.mBroadcastCallback.onBroadcastUpdated(/* reason= */ 1, /* broadcastId= */ 1);
        verify(mPreference).setEnabled(anyBoolean());
        verify(mPreference).setSummary(any());
        mController.mBroadcastCallback.onPlaybackStarted(/* reason= */ 1, /* broadcastId= */ 1);
        verify(mPreference).setEnabled(anyBoolean());
        verify(mPreference).setSummary(any());
        mController.mBroadcastCallback.onPlaybackStopped(/* reason= */ 1, /* broadcastId= */ 1);
        verify(mPreference).setEnabled(anyBoolean());
        verify(mPreference).setSummary(any());
        mController.mBroadcastCallback.onBroadcastStartFailed(/* reason= */ 1);
        verify(mPreference).setEnabled(anyBoolean());
        verify(mPreference).setSummary(any());
        mController.mBroadcastCallback.onBroadcastStopFailed(/* reason= */ 1);
        verify(mPreference).setEnabled(anyBoolean());
        verify(mPreference).setSummary(any());
        mController.mBroadcastCallback.onBroadcastUpdateFailed(
                /* reason= */ 1, /* broadcastId= */ 1);
        verify(mPreference).setEnabled(anyBoolean());
        verify(mPreference).setSummary(any());
    }
}
+60 −2
Original line number Diff line number Diff line
@@ -18,22 +18,45 @@ package com.android.settings.connecteddevice.audiosharing;

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

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

import android.app.settings.SettingsEnums;
import android.content.Context;
import android.os.Bundle;

import androidx.test.core.app.ApplicationProvider;

import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.connecteddevice.audiosharing.audiostreams.AudioStreamsCategoryController;
import com.android.settings.testutils.shadow.ShadowFragment;
import com.android.settings.widget.SettingsMainSwitchBar;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowFragment.class})
public class AudioSharingDashboardFragmentTest {

    @Rule public final MockitoRule mMockitoRule = MockitoJUnit.rule();

    @Mock private SettingsActivity mActivity;
    @Mock private SettingsMainSwitchBar mSwitchBar;
    @Mock private AudioSharingDeviceVolumeGroupController mVolumeGroupController;
    @Mock private AudioSharingCallAudioPreferenceController mCallAudioController;
    @Mock private AudioSharingPlaySoundPreferenceController mPlaySoundController;
    @Mock private AudioStreamsCategoryController mStreamsCategoryController;
    private final Context mContext = ApplicationProvider.getApplicationContext();
    private AudioSharingDashboardFragment mFragment;

    @Before
@@ -59,7 +82,42 @@ public class AudioSharingDashboardFragmentTest {

    @Test
    public void getHelpResource_returnsCorrectResource() {
        assertThat(mFragment.getHelpResource())
                .isEqualTo(R.string.help_url_audio_sharing);
        assertThat(mFragment.getHelpResource()).isEqualTo(R.string.help_url_audio_sharing);
    }

    @Test
    public void onActivityCreated_showSwitchBar() {
        doReturn(mSwitchBar).when(mActivity).getSwitchBar();
        mFragment = spy(new AudioSharingDashboardFragment());
        doReturn(mActivity).when(mFragment).getActivity();
        doReturn(mContext).when(mFragment).getContext();
        mFragment.onAttach(mContext);
        mFragment.onActivityCreated(new Bundle());
        verify(mSwitchBar).show();
    }

    @Test
    public void onAudioSharingStateChanged_updateVisibilityForControllers() {
        mFragment.setControllers(
                mVolumeGroupController,
                mCallAudioController,
                mPlaySoundController,
                mStreamsCategoryController);
        mFragment.onAudioSharingStateChanged();
        verify(mVolumeGroupController).updateVisibility();
        verify(mCallAudioController).updateVisibility();
        verify(mPlaySoundController).updateVisibility();
        verify(mStreamsCategoryController).updateVisibility();
    }

    @Test
    public void onAudioSharingProfilesConnected_registerCallbacksForVolumeGroupController() {
        mFragment.setControllers(
                mVolumeGroupController,
                mCallAudioController,
                mPlaySoundController,
                mStreamsCategoryController);
        mFragment.onAudioSharingProfilesConnected();
        verify(mVolumeGroupController).onAudioSharingProfilesConnected();
    }
}
Loading