Merge "Add unit test for broadcast radio user control"
This commit is contained in:
committed by
Android (Google) Code Review
commit
23a3b64bda
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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) {
|
||||
@@ -140,6 +153,19 @@ public final class BroadcastRadioServiceImplTest extends ExtendedRadioMockitoTes
|
||||
assertWithMessage("Session opened with id not found").that(session).isNull();
|
||||
}
|
||||
|
||||
@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();
|
||||
|
||||
@@ -329,6 +329,20 @@ public final class TunerSessionTest extends ExtendedRadioMockitoTestCase {
|
||||
.that(thrown).hasMessageThat().contains("tune: NOT_SUPPORTED");
|
||||
}
|
||||
|
||||
@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];
|
||||
|
||||
@@ -155,6 +155,19 @@ public final class BroadcastRadioServiceHidlTest extends ExtendedRadioMockitoTes
|
||||
.that(thrown).hasMessageThat().contains("Invalid module ID");
|
||||
}
|
||||
|
||||
@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();
|
||||
|
||||
@@ -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;
|
||||
@@ -336,6 +337,20 @@ public final class TunerSessionHidlTest extends ExtendedRadioMockitoTestCase {
|
||||
.that(thrown).hasMessageThat().contains("tune: NOT_SUPPORTED");
|
||||
}
|
||||
|
||||
@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];
|
||||
@@ -426,6 +441,18 @@ public final class TunerSessionHidlTest extends ExtendedRadioMockitoTestCase {
|
||||
verify(mHalTunerSessionMock).cancel();
|
||||
}
|
||||
|
||||
@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);
|
||||
|
||||
Reference in New Issue
Block a user