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

Commit 0a3441be authored by Weilin Xu's avatar Weilin Xu Committed by Android Build Cherrypicker Worker
Browse files

Move binder to radio service user controller

Bug: 363965571
Flag: EXEMPT refactor
Test: atest BroadcastRadioTests
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:2545348f6036e6743d06b8e9c156b726bd0225b2)
Merged-In: I1315393a0a1bfa0b0c05f48b23f51b171e1b2ff0
Change-Id: I1315393a0a1bfa0b0c05f48b23f51b171e1b2ff0
parent f4b27d13
Loading
Loading
Loading
Loading
+21 −11
Original line number Diff line number Diff line
@@ -19,18 +19,18 @@ 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;

import static org.mockito.Mockito.when;

import android.app.ActivityManager;
import android.app.compat.CompatChanges;
import android.os.Binder;
import android.os.UserHandle;

import com.android.dx.mockito.inline.extended.StaticMockitoSessionBuilder;

import com.google.common.truth.Expect;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;

@@ -46,10 +46,12 @@ public final class RadioServiceUserControllerTest extends ExtendedRadioMockitoTe
    @Mock
    private UserHandle mUserHandleMock;

    @Rule
    public final Expect expect = Expect.create();

    @Override
    protected void initializeSession(StaticMockitoSessionBuilder builder) {
        builder.spyStatic(ActivityManager.class).spyStatic(Binder.class)
                .spyStatic(CompatChanges.class);
        builder.spyStatic(ActivityManager.class).spyStatic(Binder.class);
    }

    @Before
@@ -63,7 +65,7 @@ public final class RadioServiceUserControllerTest extends ExtendedRadioMockitoTe
    public void isCurrentOrSystemUser_forCurrentUser_returnsFalse() {
        when(mUserHandleMock.getIdentifier()).thenReturn(USER_ID_1);

        assertWithMessage("Current user")
        expect.withMessage("Current user")
                .that(mUserController.isCurrentOrSystemUser()).isTrue();
    }

@@ -71,7 +73,7 @@ public final class RadioServiceUserControllerTest extends ExtendedRadioMockitoTe
    public void isCurrentOrSystemUser_forNonCurrentUser_returnsFalse() {
        when(mUserHandleMock.getIdentifier()).thenReturn(USER_ID_2);

        assertWithMessage("Non-current user")
        expect.withMessage("Non-current user")
                .that(mUserController.isCurrentOrSystemUser()).isFalse();
    }

@@ -80,7 +82,7 @@ public final class RadioServiceUserControllerTest extends ExtendedRadioMockitoTe
        when(mUserHandleMock.getIdentifier()).thenReturn(USER_ID_1);
        when(mUserHandleMock.getIdentifier()).thenReturn(UserHandle.USER_SYSTEM);

        assertWithMessage("System user")
        expect.withMessage("System user")
                .that(mUserController.isCurrentOrSystemUser()).isTrue();
    }

@@ -89,13 +91,13 @@ public final class RadioServiceUserControllerTest extends ExtendedRadioMockitoTe
        when(mUserHandleMock.getIdentifier()).thenReturn(USER_ID_1);
        doThrow(new RuntimeException()).when(ActivityManager::getCurrentUser);

        assertWithMessage("User when activity manager fails")
        expect.withMessage("User when activity manager fails")
                .that(mUserController.isCurrentOrSystemUser()).isFalse();
    }

    @Test
    public void getCurrentUser() {
        assertWithMessage("Current user")
        expect.withMessage("Current user")
                .that(mUserController.getCurrentUser()).isEqualTo(USER_ID_1);
    }

@@ -104,7 +106,15 @@ public final class RadioServiceUserControllerTest extends ExtendedRadioMockitoTe
        when(mUserHandleMock.getIdentifier()).thenReturn(USER_ID_1);
        doThrow(new RuntimeException()).when(ActivityManager::getCurrentUser);

        assertWithMessage("Current user when activity manager fails")
        expect.withMessage("Current user when activity manager fails")
                .that(mUserController.getCurrentUser()).isEqualTo(UserHandle.USER_NULL);
    }

    @Test
    public void getCallingUserId() {
        when(mUserHandleMock.getIdentifier()).thenReturn(USER_ID_1);

        expect.withMessage("Calling user id")
                .that(mUserController.getCallingUserId()).isEqualTo(USER_ID_1);
    }
}
+2 −7
Original line number Diff line number Diff line
@@ -49,12 +49,10 @@ import android.hardware.radio.ProgramSelector;
import android.hardware.radio.RadioManager;
import android.hardware.radio.RadioTuner;
import android.hardware.radio.UniqueProgramIdentifier;
import android.os.Binder;
import android.os.DeadObjectException;
import android.os.ParcelableException;
import android.os.RemoteException;
import android.os.ServiceSpecificException;
import android.os.UserHandle;
import android.platform.test.flag.junit.SetFlagsRule;
import android.util.ArrayMap;
import android.util.ArraySet;
@@ -147,8 +145,6 @@ public final class TunerSessionTest extends ExtendedRadioMockitoTestCase {

    // Mocks
    @Mock
    private UserHandle mUserHandleMock;
    @Mock
    private IBroadcastRadio mBroadcastRadioMock;
    private android.hardware.radio.ITunerCallback[] mAidlTunerCallbackMocks;
    @Mock
@@ -171,7 +167,7 @@ public final class TunerSessionTest extends ExtendedRadioMockitoTestCase {

    @Override
    protected void initializeSession(StaticMockitoSessionBuilder builder) {
        builder.spyStatic(CompatChanges.class).spyStatic(Binder.class);
        builder.spyStatic(CompatChanges.class);
    }

    @Before
@@ -182,8 +178,7 @@ public final class TunerSessionTest extends ExtendedRadioMockitoTestCase {

        doReturn(true).when(() -> CompatChanges.isChangeEnabled(
                eq(ConversionUtils.RADIO_U_VERSION_REQUIRED), anyInt()));
        doReturn(USER_ID_1).when(mUserHandleMock).getIdentifier();
        doReturn(mUserHandleMock).when(() -> Binder.getCallingUserHandle());
        doReturn(USER_ID_1).when(mUserControllerMock).getCallingUserId();
        doReturn(true).when(mUserControllerMock).isCurrentOrSystemUser();
        doReturn(USER_ID_1).when(mUserControllerMock).getCurrentUser();

+2 −14
Original line number Diff line number Diff line
@@ -44,16 +44,12 @@ import android.hardware.radio.ProgramList;
import android.hardware.radio.ProgramSelector;
import android.hardware.radio.RadioManager;
import android.hardware.radio.RadioTuner;
import android.os.Binder;
import android.os.DeadObjectException;
import android.os.ParcelableException;
import android.os.RemoteException;
import android.os.UserHandle;
import android.util.ArrayMap;
import android.util.ArraySet;

import com.android.dx.mockito.inline.extended.StaticMockitoSessionBuilder;
import com.android.server.broadcastradio.ExtendedRadioMockitoTestCase;
import com.android.server.broadcastradio.RadioServiceUserController;

import com.google.common.truth.Expect;
@@ -76,7 +72,7 @@ import java.util.Map;
 * Tests for HIDL HAL TunerSession.
 */
@RunWith(MockitoJUnitRunner.class)
public final class TunerSessionHidlTest extends ExtendedRadioMockitoTestCase {
public final class TunerSessionHidlTest {

    private static final int USER_ID_1 = 11;
    private static final int USER_ID_2 = 12;
@@ -103,8 +99,6 @@ public final class TunerSessionHidlTest extends ExtendedRadioMockitoTestCase {
    @Rule
    public final Expect mExpect = Expect.create();

    @Mock
    private UserHandle mUserHandleMock;
    @Mock
    private IBroadcastRadio mBroadcastRadioMock;
    @Mock
@@ -113,15 +107,9 @@ public final class TunerSessionHidlTest extends ExtendedRadioMockitoTestCase {
    @Mock
    private RadioServiceUserController mUserControllerMock;

    @Override
    protected void initializeSession(StaticMockitoSessionBuilder builder) {
        builder.spyStatic(Binder.class);
    }

    @Before
    public void setup() throws Exception {
        doReturn(USER_ID_1).when(mUserHandleMock).getIdentifier();
        doReturn(mUserHandleMock).when(() -> Binder.getCallingUserHandle());
        doReturn(USER_ID_1).when(mUserControllerMock).getCallingUserId();
        doReturn(true).when(mUserControllerMock).isCurrentOrSystemUser();
        doReturn(USER_ID_1).when(mUserControllerMock).getCurrentUser();

+8 −0
Original line number Diff line number Diff line
@@ -37,4 +37,12 @@ public interface RadioServiceUserController {
     * @return foreground user id.
     */
    int getCurrentUser();

    /**
     * Get id of the user handle assigned to the process that sent the binder transaction that is
     * being processed
     *
     * @return Id of the user handle
     */
    int getCallingUserId();
}
 No newline at end of file
+9 −1
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ public final class RadioServiceUserControllerImpl implements RadioServiceUserCon
     */
    @Override
    public boolean isCurrentOrSystemUser() {
        int callingUser = Binder.getCallingUserHandle().getIdentifier();
        int callingUser = getCallingUserId();
        return callingUser == getCurrentUser() || callingUser == UserHandle.USER_SYSTEM;
    }

@@ -51,4 +51,12 @@ public final class RadioServiceUserControllerImpl implements RadioServiceUserCon
        }
        return userId;
    }

    /**
     * @see RadioServiceUserController#getCallingUserId()
     */
    @Override
    public int getCallingUserId() {
        return Binder.getCallingUserHandle().getIdentifier();
    }
}
Loading