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

Commit 38fb7a0b authored by Vlad Popa's avatar Vlad Popa
Browse files

Fix VolumeHelperTest on automotive

Skipping ringer mode tests

Min/max volumes are not registered properly on auto. Making sure to
avoid checks that depend on mid values

Flag: EXEMPT bugfix
Test: atest VolumeHelperTest (on Seahawk)
Bug: 331244597
Change-Id: I67a3e41b20655b0b9b3ab2c098ae8f8ea29b21f5
parent 5ecbc5f7
Loading
Loading
Loading
Loading
+29 −15
Original line number Diff line number Diff line
@@ -111,6 +111,9 @@ public class VolumeHelperTest {
    private static final AudioDeviceAttributes DEVICE_SPEAKER_OUT = new AudioDeviceAttributes(
            AudioDeviceAttributes.ROLE_OUTPUT, AudioDeviceInfo.TYPE_BUILTIN_SPEAKER, "");

    /** Choose a default stream volume value which does not depend on min/max. */
    private static final int DEFAULT_STREAM_VOLUME = 2;

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

@@ -144,6 +147,8 @@ public class VolumeHelperTest {

    private TestLooper mTestLooper;

    private boolean mIsAutomotive;

    public static final int[] BASIC_VOLUME_BEHAVIORS = {
            AudioManager.DEVICE_VOLUME_BEHAVIOR_VARIABLE,
            AudioManager.DEVICE_VOLUME_BEHAVIOR_FULL,
@@ -232,9 +237,10 @@ public class VolumeHelperTest {
                || packageManager.hasSystemFeature(PackageManager.FEATURE_TELEVISION));
        final boolean isSingleVolume = mContext.getResources().getBoolean(
                Resources.getSystem().getIdentifier("config_single_volume", "bool", "android"));
        final boolean automotiveHardened = mContext.getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_AUTOMOTIVE) && autoPublicVolumeApiHardening();
        assumeFalse("Skipping test for fixed, TV, single volume and auto devices",
        mIsAutomotive = mContext.getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_AUTOMOTIVE);
        final boolean automotiveHardened = mIsAutomotive && autoPublicVolumeApiHardening();
        assumeFalse("Skipping test for fixed, TV, single volume and auto hardened devices",
                useFixedVolume || isTelevision || isSingleVolume || automotiveHardened);

        InstrumentationRegistry.getInstrumentation().getUiAutomation()
@@ -249,16 +255,15 @@ public class VolumeHelperTest {
                {STREAM_MUSIC, STREAM_NOTIFICATION, STREAM_RING, STREAM_ALARM, STREAM_SYSTEM,
                        STREAM_VOICE_CALL, STREAM_ACCESSIBILITY};
        for (int streamType : usedStreamTypes) {
            final int streamVolume = (mAm.getStreamMinVolume(streamType) + mAm.getStreamMaxVolume(
                    streamType)) / 2;

            mAudioService.setStreamVolume(streamType, streamVolume, /*flags=*/0,
            mAudioService.setStreamVolume(streamType, DEFAULT_STREAM_VOLUME, /*flags=*/0,
                    mContext.getOpPackageName());
        }

        if (!mIsAutomotive) {
            mAudioService.setRingerModeInternal(RINGER_MODE_NORMAL, mContext.getOpPackageName());
            mAudioService.setRingerModeExternal(RINGER_MODE_NORMAL, mContext.getOpPackageName());
        }
    }

    private AudioVolumeGroup getStreamTypeVolumeGroup(int streamType) {
        // get the volume group from the AudioManager to pass permission checks
@@ -297,6 +302,7 @@ public class VolumeHelperTest {

    @Test
    public void setStreamRingVolume0_setsRingerModeVibrate() throws Exception {
        assumeFalse("Skipping ringer mode test on automotive", mIsAutomotive);
        mAudioService.setStreamVolume(STREAM_RING, 0, /*flags=*/0,
                mContext.getOpPackageName());
        mTestLooper.dispatchAll();
@@ -462,6 +468,7 @@ public class VolumeHelperTest {

    @Test
    public void flagAllowRingerModes_onSystemStreams_changesMode() throws Exception {
        assumeFalse("Skipping ringer mode test on automotive", mIsAutomotive);
        mAudioService.setStreamVolume(STREAM_SYSTEM,
                mAudioService.getStreamMinVolume(STREAM_SYSTEM), /*flags=*/0,
                mContext.getOpPackageName());
@@ -476,6 +483,7 @@ public class VolumeHelperTest {

    @Test
    public void flagAllowRingerModesAbsent_onNonSystemStreams_noModeChange() throws Exception {
        assumeFalse("Skipping ringer mode test on automotive", mIsAutomotive);
        mAudioService.setStreamVolume(STREAM_MUSIC,
                mAudioService.getStreamMinVolume(STREAM_MUSIC), /*flags=*/0,
                mContext.getOpPackageName());
@@ -544,17 +552,23 @@ public class VolumeHelperTest {
        mAudioService.setDeviceVolume(volMin, usbDevice, mContext.getOpPackageName());
        mTestLooper.dispatchAll();

        if (!mIsAutomotive) {
            // there is a min/max index mismatch in automotive
            assertEquals(mAudioService.getDeviceVolume(volMin, usbDevice,
                    mContext.getOpPackageName()), volMin);
        }
        verify(mSpyAudioSystem, atLeast(1)).setStreamVolumeIndexAS(
                STREAM_MUSIC, minIndex, AudioSystem.DEVICE_OUT_USB_DEVICE);
                eq(STREAM_MUSIC), anyInt(), eq(AudioSystem.DEVICE_OUT_USB_DEVICE));

        mAudioService.setDeviceVolume(volMid, usbDevice, mContext.getOpPackageName());
        mTestLooper.dispatchAll();
        if (!mIsAutomotive) {
            // there is a min/max index mismatch in automotive
            assertEquals(mAudioService.getDeviceVolume(volMid, usbDevice,
                    mContext.getOpPackageName()), volMid);
        }
        verify(mSpyAudioSystem, atLeast(1)).setStreamVolumeIndexAS(
                STREAM_MUSIC, midIndex, AudioSystem.DEVICE_OUT_USB_DEVICE);
                eq(STREAM_MUSIC), anyInt(), eq(AudioSystem.DEVICE_OUT_USB_DEVICE));
    }

    @Test