diff options
5 files changed, 177 insertions, 0 deletions
diff --git a/core/tests/BroadcastRadioTests/src/com/android/server/broadcastradio/RadioServiceUserControllerTest.java b/core/tests/BroadcastRadioTests/src/com/android/server/broadcastradio/RadioServiceUserControllerTest.java new file mode 100644 index 000000000000..a2d8467d2a67 --- /dev/null +++ b/core/tests/BroadcastRadioTests/src/com/android/server/broadcastradio/RadioServiceUserControllerTest.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.broadcastradio; + +import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; + +import static com.google.common.truth.Truth.assertWithMessage; + +import static org.mockito.Mockito.when; + +import android.app.ActivityManager; +import android.os.Binder; +import android.os.UserHandle; + +import com.android.dx.mockito.inline.extended.StaticMockitoSessionBuilder; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; + +/** + * Tests for {@link com.android.server.broadcastradio.RadioServiceUserController} + */ +public final class RadioServiceUserControllerTest extends ExtendedRadioMockitoTestCase { + + private static final int USER_ID_1 = 11; + private static final int USER_ID_2 = 12; + + @Mock + private UserHandle mUserHandleMock; + + @Override + protected void initializeSession(StaticMockitoSessionBuilder builder) { + builder.spyStatic(ActivityManager.class) + .spyStatic(Binder.class); + } + + @Before + public void setUp() { + doReturn(mUserHandleMock).when(() -> Binder.getCallingUserHandle()); + doReturn(USER_ID_1).when(() -> ActivityManager.getCurrentUser()); + } + + @Test + public void isCurrentUser_forCurrentUser_returnsFalse() { + when(mUserHandleMock.getIdentifier()).thenReturn(USER_ID_1); + + assertWithMessage("Current user") + .that(RadioServiceUserController.isCurrentOrSystemUser()).isTrue(); + } + + @Test + public void isCurrentUser_forNonCurrentUser_returnsFalse() { + when(mUserHandleMock.getIdentifier()).thenReturn(USER_ID_2); + + assertWithMessage("Non-current user") + .that(RadioServiceUserController.isCurrentOrSystemUser()).isFalse(); + } + + @Test + public void isCurrentUser_forSystemUser_returnsTrue() { + when(mUserHandleMock.getIdentifier()).thenReturn(UserHandle.USER_SYSTEM); + + assertWithMessage("System user") + .that(RadioServiceUserController.isCurrentOrSystemUser()).isTrue(); + } +} diff --git a/core/tests/BroadcastRadioTests/src/com/android/server/broadcastradio/aidl/BroadcastRadioServiceImplTest.java b/core/tests/BroadcastRadioTests/src/com/android/server/broadcastradio/aidl/BroadcastRadioServiceImplTest.java index 93214e5a6944..36aa915a67ec 100644 --- a/core/tests/BroadcastRadioTests/src/com/android/server/broadcastradio/aidl/BroadcastRadioServiceImplTest.java +++ b/core/tests/BroadcastRadioTests/src/com/android/server/broadcastradio/aidl/BroadcastRadioServiceImplTest.java @@ -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.anyInt; import static org.mockito.ArgumentMatchers.anyString; @@ -28,6 +29,9 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import android.hardware.broadcastradio.IBroadcastRadio; +import android.hardware.radio.Announcement; +import android.hardware.radio.IAnnouncementListener; +import android.hardware.radio.ICloseHandle; import android.hardware.radio.ITuner; import android.hardware.radio.ITunerCallback; import android.hardware.radio.RadioManager; @@ -54,6 +58,7 @@ public final class BroadcastRadioServiceImplTest extends ExtendedRadioMockitoTes private static final int DAB_RADIO_MODULE_ID = 1; private static final ArrayList<String> SERVICE_LIST = new ArrayList<>(Arrays.asList("FmService", "DabService")); + private static final int[] TEST_ENABLED_TYPES = new int[]{Announcement.TYPE_TRAFFIC}; private BroadcastRadioServiceImpl mBroadcastRadioService; private IBinder.DeathRecipient mFmDeathRecipient; @@ -78,6 +83,14 @@ public final class BroadcastRadioServiceImplTest extends ExtendedRadioMockitoTes private TunerSession mFmTunerSessionMock; @Mock private ITunerCallback mTunerCallbackMock; + @Mock + private ICloseHandle mFmCloseHandleMock; + @Mock + private ICloseHandle mDabCloseHandleMock; + @Mock + private IAnnouncementListener mAnnouncementListenerMock; + @Mock + private IBinder mListenerBinderMock; @Override protected void initializeSession(StaticMockitoSessionBuilder builder) { @@ -141,6 +154,19 @@ public final class BroadcastRadioServiceImplTest extends ExtendedRadioMockitoTes } @Test + public void openSession_forNonCurrentUser_throwsException() throws Exception { + createBroadcastRadioService(); + doReturn(false).when(() -> RadioServiceUserController.isCurrentOrSystemUser()); + + IllegalStateException thrown = assertThrows(IllegalStateException.class, + () -> mBroadcastRadioService.openSession(FM_RADIO_MODULE_ID, + /* legacyConfig= */ null, /* withAudio= */ true, mTunerCallbackMock)); + + assertWithMessage("Exception for opening session by non-current user") + .that(thrown).hasMessageThat().contains("Cannot open session for non-current user"); + } + + @Test public void binderDied_forDeathRecipient() throws Exception { createBroadcastRadioService(); @@ -151,6 +177,22 @@ public final class BroadcastRadioServiceImplTest extends ExtendedRadioMockitoTes .that(mBroadcastRadioService.hasModule(FM_RADIO_MODULE_ID)).isFalse(); } + @Test + public void addAnnouncementListener_addsOnAllRadioModules() throws Exception { + createBroadcastRadioService(); + when(mAnnouncementListenerMock.asBinder()).thenReturn(mListenerBinderMock); + when(mFmRadioModuleMock.addAnnouncementListener(any(), any())) + .thenReturn(mFmCloseHandleMock); + when(mDabRadioModuleMock.addAnnouncementListener(any(), any())) + .thenReturn(mDabCloseHandleMock); + + mBroadcastRadioService.addAnnouncementListener(TEST_ENABLED_TYPES, + mAnnouncementListenerMock); + + verify(mFmRadioModuleMock).addAnnouncementListener(any(), any()); + verify(mDabRadioModuleMock).addAnnouncementListener(any(), any()); + } + private void createBroadcastRadioService() throws RemoteException { doReturn(true).when(() -> RadioServiceUserController.isCurrentOrSystemUser()); mockServiceManager(); diff --git a/core/tests/BroadcastRadioTests/src/com/android/server/broadcastradio/aidl/TunerSessionTest.java b/core/tests/BroadcastRadioTests/src/com/android/server/broadcastradio/aidl/TunerSessionTest.java index a29e9c5c9bb1..87d0ea473665 100644 --- a/core/tests/BroadcastRadioTests/src/com/android/server/broadcastradio/aidl/TunerSessionTest.java +++ b/core/tests/BroadcastRadioTests/src/com/android/server/broadcastradio/aidl/TunerSessionTest.java @@ -330,6 +330,20 @@ public final class TunerSessionTest extends ExtendedRadioMockitoTestCase { } @Test + public void tune_forCurrentUser_doesNotTune() throws Exception { + openAidlClients(/* numClients= */ 1); + doReturn(false).when(() -> RadioServiceUserController.isCurrentOrSystemUser()); + ProgramSelector initialSel = AidlTestUtils.makeFmSelector(AM_FM_FREQUENCY_LIST[1]); + RadioManager.ProgramInfo tuneInfo = + AidlTestUtils.makeProgramInfo(initialSel, SIGNAL_QUALITY); + + mTunerSessions[0].tune(initialSel); + + verify(mAidlTunerCallbackMocks[0], CALLBACK_TIMEOUT.times(0)) + .onCurrentProgramInfoChanged(tuneInfo); + } + + @Test public void step_withDirectionUp() throws Exception { long initFreq = AM_FM_FREQUENCY_LIST[1]; ProgramSelector initialSel = AidlTestUtils.makeFmSelector(initFreq); diff --git a/core/tests/BroadcastRadioTests/src/com/android/server/broadcastradio/hal2/BroadcastRadioServiceHidlTest.java b/core/tests/BroadcastRadioTests/src/com/android/server/broadcastradio/hal2/BroadcastRadioServiceHidlTest.java index 99e70436ac95..0b7bbeaab28e 100644 --- a/core/tests/BroadcastRadioTests/src/com/android/server/broadcastradio/hal2/BroadcastRadioServiceHidlTest.java +++ b/core/tests/BroadcastRadioTests/src/com/android/server/broadcastradio/hal2/BroadcastRadioServiceHidlTest.java @@ -156,6 +156,19 @@ public final class BroadcastRadioServiceHidlTest extends ExtendedRadioMockitoTes } @Test + public void openSession_forNonCurrentUser_throwsException() throws Exception { + createBroadcastRadioService(); + doReturn(false).when(() -> RadioServiceUserController.isCurrentOrSystemUser()); + + IllegalStateException thrown = assertThrows(IllegalStateException.class, + () -> mBroadcastRadioService.openSession(FM_RADIO_MODULE_ID, + /* legacyConfig= */ null, /* withAudio= */ true, mTunerCallbackMock)); + + assertWithMessage("Exception for opening session by non-current user") + .that(thrown).hasMessageThat().contains("Cannot open session for non-current user"); + } + + @Test public void addAnnouncementListener_addsOnAllRadioModules() throws Exception { createBroadcastRadioService(); when(mAnnouncementListenerMock.asBinder()).thenReturn(mBinderMock); diff --git a/core/tests/BroadcastRadioTests/src/com/android/server/broadcastradio/hal2/TunerSessionHidlTest.java b/core/tests/BroadcastRadioTests/src/com/android/server/broadcastradio/hal2/TunerSessionHidlTest.java index 8884053bb58c..b7da5d038f77 100644 --- a/core/tests/BroadcastRadioTests/src/com/android/server/broadcastradio/hal2/TunerSessionHidlTest.java +++ b/core/tests/BroadcastRadioTests/src/com/android/server/broadcastradio/hal2/TunerSessionHidlTest.java @@ -27,6 +27,7 @@ import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.timeout; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -337,6 +338,20 @@ public final class TunerSessionHidlTest extends ExtendedRadioMockitoTestCase { } @Test + public void tune_forCurrentUser_doesNotTune() throws Exception { + openAidlClients(/* numClients= */ 1); + doReturn(false).when(() -> RadioServiceUserController.isCurrentOrSystemUser()); + ProgramSelector initialSel = TestUtils.makeFmSelector(AM_FM_FREQUENCY_LIST[1]); + RadioManager.ProgramInfo tuneInfo = + TestUtils.makeProgramInfo(initialSel, SIGNAL_QUALITY); + + mTunerSessions[0].tune(initialSel); + + verify(mAidlTunerCallbackMocks[0], CALLBACK_TIMEOUT.times(0)) + .onCurrentProgramInfoChanged(tuneInfo); + } + + @Test public void step_withDirectionUp() throws Exception { long initFreq = AM_FM_FREQUENCY_LIST[1]; ProgramSelector initialSel = TestUtils.makeFmSelector(initFreq); @@ -427,6 +442,18 @@ public final class TunerSessionHidlTest extends ExtendedRadioMockitoTestCase { } @Test + public void cancel_forNonCurrentUser() throws Exception { + openAidlClients(/* numClients= */ 1); + ProgramSelector initialSel = TestUtils.makeFmSelector(AM_FM_FREQUENCY_LIST[1]); + mTunerSessions[0].tune(initialSel); + doReturn(false).when(() -> RadioServiceUserController.isCurrentOrSystemUser()); + + mTunerSessions[0].cancel(); + + verify(mHalTunerSessionMock, never()).cancel(); + } + + @Test public void getImage_withInvalidId_throwsIllegalArgumentException() throws Exception { openAidlClients(/* numClients= */ 1); int imageId = Constants.INVALID_IMAGE; |