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

Commit a3886099 authored by Yan Han's avatar Yan Han
Browse files

Fix volume behavior unit tests on differently configured devices

- Configure AudioService for a non-fixed volume device
- Only set/check 0 or max volume index, so tests work regardless of
  STREAM_MUSIC's max volume

Bug: 269129645
Test: atest AbsoluteVolumeBehaviorTest

Change-Id: I0311b7f091c1399f48b6649776fec363b4b901be
parent 8efc8ae9
Loading
Loading
Loading
Loading
+36 −17
Original line number Original line Diff line number Diff line
@@ -26,8 +26,10 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;


import android.content.Context;
import android.content.Context;
import android.content.res.Resources;
import android.media.AudioDeviceAttributes;
import android.media.AudioDeviceAttributes;
import android.media.AudioDeviceInfo;
import android.media.AudioDeviceInfo;
import android.media.AudioDeviceVolumeManager;
import android.media.AudioDeviceVolumeManager;
@@ -38,7 +40,7 @@ import android.media.VolumeInfo;
import android.os.RemoteException;
import android.os.RemoteException;
import android.os.test.TestLooper;
import android.os.test.TestLooper;


import androidx.test.InstrumentationRegistry;
import androidx.test.core.app.ApplicationProvider;


import libcore.junit.util.compat.CoreCompatChangeRule;
import libcore.junit.util.compat.CoreCompatChangeRule;


@@ -60,6 +62,7 @@ public class AbsoluteVolumeBehaviorTest {
            AudioDeviceAttributes.ROLE_OUTPUT, AudioDeviceInfo.TYPE_BUILTIN_SPEAKER, "");
            AudioDeviceAttributes.ROLE_OUTPUT, AudioDeviceInfo.TYPE_BUILTIN_SPEAKER, "");


    private Context mContext;
    private Context mContext;
    private Resources mResources;
    private String mPackageName;
    private String mPackageName;
    private AudioSystemAdapter mSpyAudioSystem;
    private AudioSystemAdapter mSpyAudioSystem;
    private SystemServerAdapter mSystemServer;
    private SystemServerAdapter mSystemServer;
@@ -76,12 +79,19 @@ public class AbsoluteVolumeBehaviorTest {
    @Before
    @Before
    public void setUp() throws Exception {
    public void setUp() throws Exception {
        mTestLooper = new TestLooper();
        mTestLooper = new TestLooper();
        mContext = InstrumentationRegistry.getTargetContext();

        mContext = spy(ApplicationProvider.getApplicationContext());
        mResources = spy(mContext.getResources());
        mPackageName = mContext.getOpPackageName();
        mPackageName = mContext.getOpPackageName();
        mSpyAudioSystem = spy(new NoOpAudioSystemAdapter());


        when(mContext.getResources()).thenReturn(mResources);
        when(mResources.getBoolean(com.android.internal.R.bool.config_useFixedVolume))
                .thenReturn(false);

        mSpyAudioSystem = spy(new NoOpAudioSystemAdapter());
        mSystemServer = new NoOpSystemServerAdapter();
        mSystemServer = new NoOpSystemServerAdapter();
        mSettingsAdapter = new NoOpSettingsAdapter();
        mSettingsAdapter = new NoOpSettingsAdapter();

        mAudioService = new AudioService(mContext, mSpyAudioSystem, mSystemServer,
        mAudioService = new AudioService(mContext, mSpyAudioSystem, mSystemServer,
                mSettingsAdapter, mMockAudioPolicy, mTestLooper.getLooper()) {
                mSettingsAdapter, mMockAudioPolicy, mTestLooper.getLooper()) {
            @Override
            @Override
@@ -134,8 +144,8 @@ public class AbsoluteVolumeBehaviorTest {
        List<VolumeInfo> volumes = Collections.singletonList(
        List<VolumeInfo> volumes = Collections.singletonList(
                new VolumeInfo.Builder(AudioManager.STREAM_MUSIC)
                new VolumeInfo.Builder(AudioManager.STREAM_MUSIC)
                        .setMinVolumeIndex(0)
                        .setMinVolumeIndex(0)
                        .setMaxVolumeIndex(250) // Max index is 10 times that of STREAM_MUSIC
                        .setMaxVolumeIndex(250)
                        .setVolumeIndex(50)
                        .setVolumeIndex(250)
                        .build());
                        .build());


        mAudioService.registerDeviceVolumeDispatcherForAbsoluteVolume(true,
        mAudioService.registerDeviceVolumeDispatcherForAbsoluteVolume(true,
@@ -144,7 +154,7 @@ public class AbsoluteVolumeBehaviorTest {
        mTestLooper.dispatchAll();
        mTestLooper.dispatchAll();


        assertThat(mAudioService.getStreamVolume(AudioManager.STREAM_MUSIC))
        assertThat(mAudioService.getStreamVolume(AudioManager.STREAM_MUSIC))
                .isEqualTo(5);
                .isEqualTo(mAudioService.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
    }
    }


    @Test
    @Test
@@ -167,7 +177,9 @@ public class AbsoluteVolumeBehaviorTest {
        assertThat(mAudioService.getDeviceVolumeBehavior(DEVICE_SPEAKER_OUT))
        assertThat(mAudioService.getDeviceVolumeBehavior(DEVICE_SPEAKER_OUT))
                .isEqualTo(AudioManager.DEVICE_VOLUME_BEHAVIOR_VARIABLE);
                .isEqualTo(AudioManager.DEVICE_VOLUME_BEHAVIOR_VARIABLE);


        mAudioService.setStreamVolume(AudioManager.STREAM_MUSIC, 15, 0, mPackageName);
        mAudioService.setStreamVolume(AudioManager.STREAM_MUSIC,
                /*index*/ mAudioService.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
                /*flags*/ 0, mPackageName);
        mTestLooper.dispatchAll();
        mTestLooper.dispatchAll();


        verify(mMockDispatcher, never()).dispatchDeviceVolumeChanged(
        verify(mMockDispatcher, never()).dispatchDeviceVolumeChanged(
@@ -188,7 +200,9 @@ public class AbsoluteVolumeBehaviorTest {
                AudioManager.DEVICE_VOLUME_BEHAVIOR_FULL, mPackageName);
                AudioManager.DEVICE_VOLUME_BEHAVIOR_FULL, mPackageName);
        mTestLooper.dispatchAll();
        mTestLooper.dispatchAll();


        mAudioService.setStreamVolume(AudioManager.STREAM_MUSIC, 15, 0, mPackageName);
        mAudioService.setStreamVolume(AudioManager.STREAM_MUSIC,
                /*index*/ mAudioService.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
                /*flags*/ 0, mPackageName);
        mTestLooper.dispatchAll();
        mTestLooper.dispatchAll();


        verify(mMockDispatcher, never()).dispatchDeviceVolumeChanged(
        verify(mMockDispatcher, never()).dispatchDeviceVolumeChanged(
@@ -199,8 +213,8 @@ public class AbsoluteVolumeBehaviorTest {
    public void setStreamVolume_noAbsVolFlag_dispatchesVolumeChanged() throws RemoteException {
    public void setStreamVolume_noAbsVolFlag_dispatchesVolumeChanged() throws RemoteException {
        VolumeInfo volumeInfo = new VolumeInfo.Builder(AudioManager.STREAM_MUSIC)
        VolumeInfo volumeInfo = new VolumeInfo.Builder(AudioManager.STREAM_MUSIC)
                .setMinVolumeIndex(0)
                .setMinVolumeIndex(0)
                .setMaxVolumeIndex(250) // Max index is 10 times that of STREAM_MUSIC
                .setMaxVolumeIndex(250)
                .setVolumeIndex(50)
                .setVolumeIndex(0)
                .build();
                .build();


        mAudioService.registerDeviceVolumeDispatcherForAbsoluteVolume(true,
        mAudioService.registerDeviceVolumeDispatcherForAbsoluteVolume(true,
@@ -210,12 +224,14 @@ public class AbsoluteVolumeBehaviorTest {
        mTestLooper.dispatchAll();
        mTestLooper.dispatchAll();


        // Set stream volume without FLAG_ABSOLUTE_VOLUME
        // Set stream volume without FLAG_ABSOLUTE_VOLUME
        mAudioService.setStreamVolume(AudioManager.STREAM_MUSIC, 15, 0, mPackageName);
        mAudioService.setStreamVolume(AudioManager.STREAM_MUSIC,
                /*index*/ mAudioService.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
                /*flags*/ 0, mPackageName);
        mTestLooper.dispatchAll();
        mTestLooper.dispatchAll();


        // Dispatched volume index is scaled to the range in the initial VolumeInfo
        // Dispatched volume index is scaled to the range in the initial VolumeInfo
        verify(mMockDispatcher).dispatchDeviceVolumeChanged(DEVICE_SPEAKER_OUT,
        verify(mMockDispatcher).dispatchDeviceVolumeChanged(DEVICE_SPEAKER_OUT,
                new VolumeInfo.Builder(volumeInfo).setVolumeIndex(150).build());
                new VolumeInfo.Builder(volumeInfo).setVolumeIndex(250).build());
    }
    }


    @Test
    @Test
@@ -224,7 +240,7 @@ public class AbsoluteVolumeBehaviorTest {
        VolumeInfo volumeInfo = new VolumeInfo.Builder(AudioManager.STREAM_MUSIC)
        VolumeInfo volumeInfo = new VolumeInfo.Builder(AudioManager.STREAM_MUSIC)
                .setMinVolumeIndex(0)
                .setMinVolumeIndex(0)
                .setMaxVolumeIndex(250)
                .setMaxVolumeIndex(250)
                .setVolumeIndex(50)
                .setVolumeIndex(0)
                .build();
                .build();


        mAudioService.registerDeviceVolumeDispatcherForAbsoluteVolume(true,
        mAudioService.registerDeviceVolumeDispatcherForAbsoluteVolume(true,
@@ -234,8 +250,9 @@ public class AbsoluteVolumeBehaviorTest {
        mTestLooper.dispatchAll();
        mTestLooper.dispatchAll();


        // Set stream volume with FLAG_ABSOLUTE_VOLUME
        // Set stream volume with FLAG_ABSOLUTE_VOLUME
        mAudioService.setStreamVolume(AudioManager.STREAM_MUSIC, 15,
        mAudioService.setStreamVolume(AudioManager.STREAM_MUSIC,
                AudioManager.FLAG_ABSOLUTE_VOLUME, mPackageName);
                /*index*/ mAudioService.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
                /*flags*/ AudioManager.FLAG_ABSOLUTE_VOLUME, mPackageName);
        mTestLooper.dispatchAll();
        mTestLooper.dispatchAll();


        verify(mMockDispatcher, never()).dispatchDeviceVolumeChanged(eq(DEVICE_SPEAKER_OUT),
        verify(mMockDispatcher, never()).dispatchDeviceVolumeChanged(eq(DEVICE_SPEAKER_OUT),
@@ -353,13 +370,15 @@ public class AbsoluteVolumeBehaviorTest {
        mTestLooper.dispatchAll();
        mTestLooper.dispatchAll();


        // Set stream volume without FLAG_ABSOLUTE_VOLUME
        // Set stream volume without FLAG_ABSOLUTE_VOLUME
        mAudioService.setStreamVolume(AudioManager.STREAM_MUSIC, 15, 0, mPackageName);
        mAudioService.setStreamVolume(AudioManager.STREAM_MUSIC,
                /*index*/ mAudioService.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
                /*flags*/ 0, mPackageName);
        mTestLooper.dispatchAll();
        mTestLooper.dispatchAll();


        // Volume change not dispatched for absolute volume listener
        // Volume change not dispatched for absolute volume listener
        verify(mMockDispatcher, never()).dispatchDeviceVolumeChanged(eq(DEVICE_SPEAKER_OUT), any());
        verify(mMockDispatcher, never()).dispatchDeviceVolumeChanged(eq(DEVICE_SPEAKER_OUT), any());
        // Volume changed dispatched for adjust-only absolute volume listener
        // Volume changed dispatched for adjust-only absolute volume listener
        verify(mMockAdjustOnlyAbsoluteVolumeDispatcher).dispatchDeviceVolumeChanged(
        verify(mMockAdjustOnlyAbsoluteVolumeDispatcher).dispatchDeviceVolumeChanged(
                DEVICE_SPEAKER_OUT, new VolumeInfo.Builder(volumeInfo).setVolumeIndex(150).build());
                DEVICE_SPEAKER_OUT, new VolumeInfo.Builder(volumeInfo).setVolumeIndex(250).build());
    }
    }
}
}