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

Commit a55c0b07 authored by Vlad Popa's avatar Vlad Popa
Browse files

Fix VolumeHelperTest on automotive

On automotive platforms the hardening enforcer is blocking a lot of
set/adjust stream volume calls. Skipping those tests.

Also added some minor improvements regarding the assertEqual checking
order.

Flag: TEST_ONLY
Test: atest VolumeHelperTest
Bug: 369155290
Change-Id: I4eebedadbbdb367123938f6c67283a986bad0518
parent 73e56ea3
Loading
Loading
Loading
Loading
+31 −23
Original line number Diff line number Diff line
@@ -289,6 +289,9 @@ public class VolumeHelperTest {
    // --------------- Volume Stream APIs ---------------
    @Test
    public void setStreamVolume_callsASSetStreamVolumeIndex() throws Exception {
        assumeFalse("Skipping setStreamVolume_callsASSetStreamVolumeIndex on automotive",
                mIsAutomotive);

        int newIndex = circularNoMinMaxIncrementVolume(STREAM_MUSIC);

        mAudioService.setDeviceForStream(STREAM_MUSIC, DEVICE_OUT_USB_DEVICE);
@@ -312,6 +315,9 @@ public class VolumeHelperTest {

    @Test
    public void adjustStreamVolume_callsASSetStreamVolumeIndex() throws Exception {
        assumeFalse("Skipping adjustStreamVolume_callsASSetStreamVolumeIndex on automotive",
                mIsAutomotive);

        mAudioService.setDeviceForStream(STREAM_MUSIC, DEVICE_OUT_USB_DEVICE);
        mAudioService.adjustStreamVolume(STREAM_MUSIC, ADJUST_LOWER, /*flags=*/0,
                mContext.getOpPackageName());
@@ -323,6 +329,9 @@ public class VolumeHelperTest {

    @Test
    public void handleVolumeKey_callsASSetStreamVolumeIndex() throws Exception {
        assumeFalse("Skipping handleVolumeKey_callsASSetStreamVolumeIndex on automotive",
                mIsAutomotive);

        final KeyEvent keyEvent = new KeyEvent(ACTION_DOWN, KEYCODE_VOLUME_UP);

        mAudioService.setDeviceForStream(STREAM_MUSIC, DEVICE_OUT_USB_DEVICE);
@@ -338,6 +347,9 @@ public class VolumeHelperTest {

    @Test
    public void setVolumeGroupVolumeIndex_callsASSetVolumeIndexForAttributes() throws Exception {
        assumeFalse(
                "Skipping setVolumeGroupVolumeIndex_callsASSetVolumeIndexForAttributes on "
                        + "automotive", mIsAutomotive);
        assumeNotNull(mAudioMusicVolumeGroup);

        mAudioService.setDeviceForStream(STREAM_MUSIC, DEVICE_OUT_USB_DEVICE);
@@ -346,8 +358,8 @@ public class VolumeHelperTest {
                mContext.getOpPackageName(),  /*attributionTag*/null);
        mTestLooper.dispatchAll();

        verify(mSpyAudioSystem).setVolumeIndexForAttributes(
                any(), anyInt(), eq(DEVICE_OUT_USB_DEVICE));
        verify(mSpyAudioSystem).setVolumeIndexForAttributes(any(), anyInt(),
                eq(DEVICE_OUT_USB_DEVICE));
    }

    @Test
@@ -365,6 +377,7 @@ public class VolumeHelperTest {

    @Test
    public void check_getVolumeGroupVolumeIndex() throws Exception {
        assumeFalse("Skipping check_getVolumeGroupVolumeIndex on automotive", mIsAutomotive);
        assumeNotNull(mAudioMusicVolumeGroup);

        int newIndex = circularNoMinMaxIncrementVolume(STREAM_MUSIC);
@@ -373,10 +386,9 @@ public class VolumeHelperTest {
                newIndex, /*flags=*/0, mContext.getOpPackageName(),  /*attributionTag*/null);
        mTestLooper.dispatchAll();

        assertEquals(mAudioService.getVolumeGroupVolumeIndex(mAudioMusicVolumeGroup.getId()),
                newIndex);
        assertEquals(mAudioService.getStreamVolume(STREAM_MUSIC),
                newIndex);
        assertEquals(newIndex,
                mAudioService.getVolumeGroupVolumeIndex(mAudioMusicVolumeGroup.getId()));
        assertEquals(newIndex, mAudioService.getStreamVolume(STREAM_MUSIC));
    }

    @Test
@@ -431,6 +443,7 @@ public class VolumeHelperTest {

    @Test
    public void flagAbsVolume_onBtDevice_changesVolume() throws Exception {
        assumeFalse("Skipping flagAbsVolume_onBtDevice_changesVolume on automotive", mIsAutomotive);
        mAudioService.setDeviceForStream(STREAM_NOTIFICATION, DEVICE_OUT_BLE_SPEAKER);

        int newIndex = circularNoMinMaxIncrementVolume(STREAM_NOTIFICATION);
@@ -493,7 +506,7 @@ public class VolumeHelperTest {
                mContext.getOpPackageName());
        mTestLooper.dispatchAll();

        assertEquals(mAudioService.getRingerModeInternal(), RINGER_MODE_VIBRATE);
        assertEquals(RINGER_MODE_VIBRATE, mAudioService.getRingerModeInternal());
    }

    // --------------------- Permission tests ---------------------
@@ -537,6 +550,7 @@ public class VolumeHelperTest {
    // ----------------- AudioDeviceVolumeManager -----------------
    @Test
    public void setDeviceVolume_checkIndex() {
        assumeFalse("Skipping setDeviceVolume_checkIndex on automotive", mIsAutomotive);
        final int minIndex = mAm.getStreamMinVolume(STREAM_MUSIC);
        final int maxIndex = mAm.getStreamMaxVolume(STREAM_MUSIC);
        final int midIndex = (minIndex + maxIndex) / 2;
@@ -552,21 +566,17 @@ 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);
        }
        assertEquals(volMin, mAudioService.getDeviceVolume(volMin, usbDevice,
                mContext.getOpPackageName()));
        verify(mSpyAudioSystem, atLeast(1)).setStreamVolumeIndexAS(
                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);
        }
        assertEquals(volMid, mAudioService.getDeviceVolume(volMid, usbDevice,
                mContext.getOpPackageName()));
        verify(mSpyAudioSystem, atLeast(1)).setStreamVolumeIndexAS(
                eq(STREAM_MUSIC), anyInt(), eq(AudioSystem.DEVICE_OUT_USB_DEVICE));
    }
@@ -602,9 +612,8 @@ public class VolumeHelperTest {
            mAudioService.setDeviceVolume(volCur, bleDevice, mContext.getOpPackageName());
            mTestLooper.dispatchAll();

            assertEquals(
                    mAudioService.getDeviceVolume(volCur, bleDevice, mContext.getOpPackageName()),
                    volCur);
            assertEquals(volCur,
                    mAudioService.getDeviceVolume(volCur, bleDevice, mContext.getOpPackageName()));
            // Stream volume changes
            verify(mSpyAudioSystem, atLeast(1)).setStreamVolumeIndexAS(
                    STREAM_MUSIC, targetIndex,
@@ -617,9 +626,8 @@ public class VolumeHelperTest {
        mAudioService.setDeviceVolume(volIndex4, bleDevice, mContext.getOpPackageName());
        mTestLooper.dispatchAll();

        assertEquals(
                mAudioService.getDeviceVolume(volIndex4, bleDevice, mContext.getOpPackageName()),
                volIndex4);
        assertEquals(volIndex4,
                mAudioService.getDeviceVolume(volIndex4, bleDevice, mContext.getOpPackageName()));
        verify(mSpyAudioSystem, atLeast(1)).setStreamVolumeIndexAS(
                STREAM_MUSIC, maxIndex,
                AudioSystem.DEVICE_OUT_BLE_HEADSET);