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

Commit 3af3d47f authored by Weilin Xu's avatar Weilin Xu
Browse files

Add tests for invalid state radio HAL client

Added more unit tests for broadcast radio HAL client about handling
invalid states and argument in user controller, announcement aggregator
and radio service implementation.

Bug: 262583864
Test: atest com.android.server.broadcastradio
Change-Id: I6341fd32a450cfd895032cf7d6cf0fc902fcf6f8
parent fd723c20
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;

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;
@@ -123,6 +124,16 @@ public final class IRadioServiceAidlImplTest extends ExtendedRadioMockitoTestCas
                .that(tuner).isEqualTo(mTunerMock);
    }

    @Test
    public void openTuner_withNullCallbackForAidlImpl_fails() throws Exception {
        IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class,
                () -> mAidlImpl.openTuner(/* moduleId= */ 0, mBandConfigMock,
                        /* withAudio= */ true, /* callback= */ null, TARGET_SDK_VERSION));

        assertWithMessage("Exception for opening tuner with null callback")
                .that(thrown).hasMessageThat().contains("Callback must not be null");
    }

    @Test
    public void addAnnouncementListener_forAidlImpl() {
        ICloseHandle closeHandle = mAidlImpl.addAnnouncementListener(ENABLE_TYPES, mListenerMock);
+11 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.server.broadcastradio;

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;
@@ -120,6 +121,16 @@ public final class IRadioServiceHidlImplTest {
                .that(tuner).isEqualTo(mHal2TunerMock);
    }

    @Test
    public void openTuner_withNullCallbackForHidlImpl_fails() throws Exception {
        NullPointerException thrown = assertThrows(NullPointerException.class,
                () -> mHidlImpl.openTuner(/* moduleId= */ 0, mBandConfigMock,
                        /* withAudio= */ true, /* callback= */ null, TARGET_SDK_VERSION));

        assertWithMessage("Exception for opening tuner with null callback")
                .that(thrown).hasMessageThat().contains("Callback must not be null");
    }

    @Test
    public void addAnnouncementListener_forHidlImpl() {
        when(mHal2Mock.hasAnyModules()).thenReturn(true);
+9 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.server.broadcastradio;

import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.doThrow;

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

@@ -78,4 +79,12 @@ public final class RadioServiceUserControllerTest extends ExtendedRadioMockitoTe
        assertWithMessage("System user")
                .that(RadioServiceUserController.isCurrentOrSystemUser()).isTrue();
    }

    @Test
    public void isCurrentUser_withActivityManagerFails_returnsFalse() {
        doThrow(new RuntimeException()).when(() -> ActivityManager.getCurrentUser());

        assertWithMessage("User when activity manager fails")
                .that(RadioServiceUserController.isCurrentOrSystemUser()).isFalse();
    }
}
+29 −0
Original line number Diff line number Diff line
@@ -18,9 +18,11 @@ package com.android.server.broadcastradio.aidl;

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.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -107,6 +109,33 @@ public final class AnnouncementAggregatorTest {
        }
    }

    @Test
    public void onListUpdated_afterClosed_notUpdated() throws Exception {
        ArgumentCaptor<IAnnouncementListener> moduleWatcherCaptor =
                ArgumentCaptor.forClass(IAnnouncementListener.class);
        watchModules(/* moduleNumber= */ 1);
        verify(mRadioModuleMocks[0]).addAnnouncementListener(moduleWatcherCaptor.capture(), any());
        mAnnouncementAggregator.close();

        moduleWatcherCaptor.getValue().onListUpdated(Arrays.asList(mAnnouncementMocks[0]));

        verify(mListenerMock, never()).onListUpdated(any());
    }

    @Test
    public void watchModule_afterClosed_throwsException() throws Exception {
        watchModules(/* moduleNumber= */ 1);
        mAnnouncementAggregator.close();

        IllegalStateException thrown = assertThrows(IllegalStateException.class,
                () -> mAnnouncementAggregator.watchModule(mRadioModuleMocks[0],
                        TEST_ENABLED_TYPES));

        assertWithMessage("Exception for watching module after aggregator has been closed")
                .that(thrown).hasMessageThat()
                .contains("announcement aggregator has already been closed");
    }

    @Test
    public void close_withOneModuleWatcher_invokesCloseHandle() throws Exception {
        watchModules(/* moduleNumber= */ 1);
+29 −0
Original line number Diff line number Diff line
@@ -18,9 +18,11 @@ package com.android.server.broadcastradio.hal2;

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.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -108,6 +110,33 @@ public final class AnnouncementAggregatorHidlTest {
        }
    }

    @Test
    public void onListUpdated_afterClosed_notUpdated() throws Exception {
        ArgumentCaptor<IAnnouncementListener> moduleWatcherCaptor =
                ArgumentCaptor.forClass(IAnnouncementListener.class);
        watchModules(/* moduleNumber= */ 1);
        verify(mRadioModuleMocks[0]).addAnnouncementListener(any(), moduleWatcherCaptor.capture());
        mAnnouncementAggregator.close();

        moduleWatcherCaptor.getValue().onListUpdated(Arrays.asList(mAnnouncementMocks[0]));

        verify(mListenerMock, never()).onListUpdated(any());
    }

    @Test
    public void watchModule_afterClosed_throwsException() throws Exception {
        watchModules(/* moduleNumber= */ 1);
        mAnnouncementAggregator.close();

        IllegalStateException thrown = assertThrows(IllegalStateException.class,
                () -> mAnnouncementAggregator.watchModule(mRadioModuleMocks[0],
                        TEST_ENABLED_TYPES));

        assertWithMessage("Exception for watching module after aggregator has been closed")
                .that(thrown).hasMessageThat()
                .contains("announcement aggregator has already been closed");
    }

    @Test
    public void close_withOneModuleWatcher_invokesCloseHandle() throws Exception {
        watchModules(/* moduleNumber= */ 1);
Loading