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

Commit acf32500 authored by William Escande's avatar William Escande Committed by Automerger Merge Worker
Browse files

Merge "AvrcpVolumeManager: Fix flaky test" into main am: 44da2cb9

parents 61abfcc2 44da2cb9
Loading
Loading
Loading
Loading
+21 −14
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@@ -35,21 +36,22 @@ import androidx.test.InstrumentationRegistry;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;


import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;


@SmallTest
@RunWith(AndroidJUnit4.class)
public class AvrcpVolumeManagerTest {
    private static final String REMOTE_DEVICE_ADDRESS = "00:01:02:03:04:05";
    private static final int TEST_DEVICE_MAX_VOUME = 25;
    private static final int TEST_DEVICE_MAX_VOLUME = 25;

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

@@ -67,7 +69,7 @@ public class AvrcpVolumeManagerTest {
    public void setUp() throws Exception {
        mContext = InstrumentationRegistry.getTargetContext();
        when(mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC))
                .thenReturn(TEST_DEVICE_MAX_VOUME);
                .thenReturn(TEST_DEVICE_MAX_VOLUME);
        mRemoteDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(REMOTE_DEVICE_ADDRESS);
        mAvrcpVolumeManager = new AvrcpVolumeManager(mContext, mAudioManager, mNativeInterface);
    }
@@ -81,10 +83,10 @@ public class AvrcpVolumeManagerTest {
    public void avrcpVolumeConversion() {
        assertThat(AvrcpVolumeManager.avrcpToSystemVolume(0)).isEqualTo(0);
        assertThat(AvrcpVolumeManager.avrcpToSystemVolume(AVRCP_MAX_VOL))
                .isEqualTo(TEST_DEVICE_MAX_VOUME);
                .isEqualTo(TEST_DEVICE_MAX_VOLUME);

        assertThat(AvrcpVolumeManager.systemToAvrcpVolume(0)).isEqualTo(0);
        assertThat(AvrcpVolumeManager.systemToAvrcpVolume(TEST_DEVICE_MAX_VOUME))
        assertThat(AvrcpVolumeManager.systemToAvrcpVolume(TEST_DEVICE_MAX_VOLUME))
                .isEqualTo(AVRCP_MAX_VOL);
    }

@@ -98,7 +100,7 @@ public class AvrcpVolumeManagerTest {

    @Test
    public void sendVolumeChanged() {
        mAvrcpVolumeManager.sendVolumeChanged(mRemoteDevice, TEST_DEVICE_MAX_VOUME);
        mAvrcpVolumeManager.sendVolumeChanged(mRemoteDevice, TEST_DEVICE_MAX_VOLUME);

        verify(mNativeInterface).sendVolumeChanged(mRemoteDevice, AVRCP_MAX_VOL);
    }
@@ -107,25 +109,30 @@ public class AvrcpVolumeManagerTest {
    public void setVolume() {
        mAvrcpVolumeManager.setVolume(mRemoteDevice, AVRCP_MAX_VOL);

        verify(mAudioManager).setStreamVolume(eq(AudioManager.STREAM_MUSIC),
                eq(TEST_DEVICE_MAX_VOUME), anyInt());
        verify(mAudioManager)
                .setStreamVolume(
                        eq(AudioManager.STREAM_MUSIC), eq(TEST_DEVICE_MAX_VOLUME), anyInt());
    }

    @Test
    public void switchVolumeDevice() {
    public void switchVolumeDevice() throws InterruptedException {
        mAvrcpVolumeManager.volumeDeviceSwitched(mRemoteDevice);
        mAvrcpVolumeManager.deviceConnected(mRemoteDevice, true);

        // verify whether switchVolumeDevice is called by checking
        // mAudioManager.setDeviceVolumeBehavior().
        verify(mAudioManager).setDeviceVolumeBehavior(any(), anyInt());
        // Since it's done in an async thread, we need to add a timeout to await completion
        verify(mAudioManager, timeout(1_000)).setDeviceVolumeBehavior(any(), anyInt());
    }

        Mockito.clearInvocations(mAudioManager);
        mAvrcpVolumeManager.deviceDisconnected(mRemoteDevice);
        // verify one more time when deviceConnected event comes first.
    @Test
    public void switchVolumeDevice_reverseEventOrder() throws InterruptedException {
        mAvrcpVolumeManager.deviceConnected(mRemoteDevice, true);
        mAvrcpVolumeManager.volumeDeviceSwitched(mRemoteDevice);

        verify(mAudioManager).setDeviceVolumeBehavior(any(), anyInt());
        // verify whether switchVolumeDevice is called by checking
        // mAudioManager.setDeviceVolumeBehavior().
        // Since it's done in an async thread, we need to add a timeout to await completion
        verify(mAudioManager, timeout(1_000)).setDeviceVolumeBehavior(any(), anyInt());
    }
}