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

Commit 43fccacb authored by Weilin Xu's avatar Weilin Xu
Browse files

Add unit tests about throwing expection for radio

Bug: 262583864
Test: atest android.hardware.radio.tests.unittests
Test: com.android.server.broadcastradio
Change-Id: I0264c0541cca4a026e4b4d3ab37cd4301f9887ab
parent 58141fbd
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -275,6 +275,19 @@ public final class ProgramListTest {
        assertWithMessage("Chunks").that(chunks).hasLength(CREATOR_ARRAY_SIZE);
    }

    @Test
    public void getProgramList_forTunerAdapterWhenServiceDied_fails() throws Exception {
        Map<String, String> parameters = Map.of("ParameterKeyMock", "ParameterValueMock");
        createRadioTuner();
        doThrow(new RemoteException()).when(mTunerMock).startProgramListUpdates(any());

        RuntimeException thrown = assertThrows(RuntimeException.class,
                () -> mRadioTuner.getProgramList(parameters));

        assertWithMessage("Exception for getting program list when service is dead")
                .that(thrown).hasMessageThat().contains("Service died");
    }

    @Test
    public void getDynamicProgramList_forTunerAdapter() throws Exception {
        createRadioTuner();
+58 −0
Original line number Diff line number Diff line
@@ -18,7 +18,9 @@ package android.hardware.radio.tests.unittests;

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

import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
@@ -1005,6 +1007,35 @@ public final class RadioManagerTest {
                .that(modules).containsExactly(AMFM_PROPERTIES);
    }

    @Test
    public void listModules_forRadioManagerWithNullListAsInput_fails() throws Exception {
        createRadioManager();

        assertWithMessage("Status when listing module with empty list input")
                .that(mRadioManager.listModules(null)).isEqualTo(RadioManager.STATUS_BAD_VALUE);
    }

    @Test
    public void listModules_withNullListFromService_fails() throws Exception {
        createRadioManager();
        when(mRadioServiceMock.listModules()).thenReturn(null);
        List<RadioManager.ModuleProperties> modules = new ArrayList<>();

        assertWithMessage("Status for listing module when getting null list from HAL client")
                .that(mRadioManager.listModules(modules)).isEqualTo(RadioManager.STATUS_ERROR);
    }

    @Test
    public void listModules_whenServiceDied_fails() throws Exception {
        createRadioManager();
        when(mRadioServiceMock.listModules()).thenThrow(new RemoteException());
        List<RadioManager.ModuleProperties> modules = new ArrayList<>();

        assertWithMessage("Status for listing module when HAL client service is dead")
                .that(mRadioManager.listModules(modules))
                .isEqualTo(RadioManager.STATUS_DEAD_OBJECT);
    }

    @Test
    public void openTuner_forRadioModule() throws Exception {
        createRadioManager();
@@ -1018,6 +1049,18 @@ public final class RadioManagerTest {
                anyInt());
    }

    @Test
    public void openTuner_whenServiceDied_returnsNull() throws Exception {
        createRadioManager();
        when(mRadioServiceMock.openTuner(anyInt(), any(), anyBoolean(), any(), anyInt()))
                .thenThrow(new RemoteException());

        RadioTuner nullTuner = mRadioManager.openTuner(/* moduleId= */ 0, FM_BAND_CONFIG,
                /* withAudio= */ true, mCallbackMock, /* handler= */ null);

        assertWithMessage("Radio tuner when service is dead").that(nullTuner).isNull();
    }

    @Test
    public void addAnnouncementListener_withListenerNotAddedBefore() throws Exception {
        createRadioManager();
@@ -1048,6 +1091,21 @@ public final class RadioManagerTest {
        verify(mCloseHandleMock).close();
    }

    @Test
    public void addAnnouncementListener_whenServiceDied_throwException() throws Exception {
        createRadioManager();
        String exceptionMessage = "service is dead";
        when(mRadioServiceMock.addAnnouncementListener(any(), any()))
                .thenThrow(new RemoteException(exceptionMessage));
        Set<Integer> enableTypeSet = createAnnouncementTypeSet(EVENT_ANNOUNCEMENT_TYPE);

        RuntimeException thrown = assertThrows(RuntimeException.class,
                () -> mRadioManager.addAnnouncementListener(enableTypeSet, mEventListener));

        assertWithMessage("Exception for adding announcement listener with dead service")
                .that(thrown).hasMessageThat().contains(exceptionMessage);
    }

    @Test
    public void removeAnnouncementListener_withListenerNotAddedBefore_ignores() throws Exception {
        createRadioManager();
+315 −6

File changed.

Preview size limit exceeded, changes collapsed.