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

Commit 99884acd authored by Yao Li's avatar Yao Li
Browse files

Mock Resources.getSystem in UserManagerServiceTest

UserManagerService uses Resources.getSystem() to get Resources, along
with other methods.
This CL makes UserManagerServiceTest mock Resources.getSystem, so it is
possible to test with different resource values there, in future CLs.

This CL also moves shouldShowNotificationForBackgroundUserSounds(), so
that we do not have one service class call another.

Bug: 390496349
Flag: EXEMPT refactor
Test: atest FrameworksMockingServicesTests:com.android.server.pm.UserManagerServiceTest
Test: atest FrameworksVibratorServicesTests:com.android.server.vibrator.VibratorManagerServiceTest
Change-Id: Ieb730dd9f3d3c36d1bdf2ff555871f2539971030
parent 96edebcb
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -23,7 +23,9 @@ import android.content.Context;
import android.content.pm.LauncherUserInfo;
import android.content.pm.UserInfo;
import android.content.pm.UserProperties;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.multiuser.Flags;
import android.os.Bundle;
import android.os.UserManager;
import android.util.DebugUtils;
@@ -617,4 +619,14 @@ public abstract class UserManagerInternal {
     * if there is no such user.
     */
    public abstract @UserIdInt int getCommunalProfileId();

    /**
     * Checks whether to show a notification for sounds (e.g., alarms, timers, etc.) from
     * background users.
     */
    public static boolean shouldShowNotificationForBackgroundUserSounds() {
        return Flags.addUiForSoundsFromBackgroundUsers() && Resources.getSystem().getBoolean(
                com.android.internal.R.bool.config_showNotificationForBackgroundUserAlarms)
                && UserManager.supportsMultipleUsers();
    }
}
+1 −13
Original line number Diff line number Diff line
@@ -33,7 +33,6 @@ import static android.os.UserManager.SYSTEM_USER_MODE_EMULATION_PROPERTY;
import static android.os.UserManager.USER_OPERATION_ERROR_UNKNOWN;
import static android.os.UserManager.USER_OPERATION_ERROR_USER_RESTRICTED;
import static android.os.UserManager.USER_TYPE_PROFILE_PRIVATE;
import static android.os.UserManager.supportsMultipleUsers;
import static android.provider.Settings.Secure.HIDE_PRIVATESPACE_ENTRY_POINT;

import static com.android.internal.app.SetScreenLockDialogActivity.EXTRA_ORIGIN_USER_ID;
@@ -60,7 +59,6 @@ import android.annotation.RequiresPermission;
import android.annotation.SpecialUsers.CanBeALL;
import android.annotation.SpecialUsers.CanBeCURRENT;
import android.annotation.SpecialUsers.CanBeNULL;
import android.annotation.SpecialUsers.CannotBeSpecialUser;
import android.annotation.StringRes;
import android.annotation.UserIdInt;
import android.app.ActivityManager;
@@ -1161,7 +1159,7 @@ public class UserManagerService extends IUserManager.Stub {

        showHsumNotificationIfNeeded();

        if (shouldShowNotificationForBackgroundUserSounds()) {
        if (UserManagerInternal.shouldShowNotificationForBackgroundUserSounds()) {
            new BackgroundUserSoundNotifier(mContext);
        }
    }
@@ -8498,16 +8496,6 @@ public class UserManagerService extends IUserManager.Stub {
                .getBoolean(R.bool.config_canSwitchToHeadlessSystemUser);
    }

    /**
     * Checks whether to show a notification for sounds (e.g., alarms, timers, etc.) from
     * background users.
     */
    public static boolean shouldShowNotificationForBackgroundUserSounds() {
        return Flags.addUiForSoundsFromBackgroundUsers() && Resources.getSystem().getBoolean(
                com.android.internal.R.bool.config_showNotificationForBackgroundUserAlarms)
                && supportsMultipleUsers();
    }

    /**
     * Returns instance of {@link com.android.server.pm.UserJourneyLogger}.
     */
+3 −3
Original line number Diff line number Diff line
@@ -79,7 +79,7 @@ import com.android.internal.app.IBatteryStats;
import com.android.internal.util.DumpUtils;
import com.android.server.SystemService;
import com.android.server.pm.BackgroundUserSoundNotifier;
import com.android.server.pm.UserManagerService;
import com.android.server.pm.UserManagerInternal;
import com.android.server.vibrator.VibrationSession.CallerInfo;
import com.android.server.vibrator.VibrationSession.DebugInfo;
import com.android.server.vibrator.VibrationSession.Status;
@@ -201,7 +201,7 @@ public class VibratorManagerService extends IVibratorManagerService.Stub {
                            VibratorManagerService.this::shouldCancelOnScreenOffLocked,
                            Status.CANCELLED_BY_SCREEN_OFF);
                }
            } else if (UserManagerService.shouldShowNotificationForBackgroundUserSounds()
            } else if (UserManagerInternal.shouldShowNotificationForBackgroundUserSounds()
                    && intent.getAction().equals(BackgroundUserSoundNotifier.ACTION_MUTE_SOUND)) {
                synchronized (mLock) {
                    maybeClearCurrentAndNextSessionsLocked(
@@ -325,7 +325,7 @@ public class VibratorManagerService extends IVibratorManagerService.Stub {

        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        if (UserManagerService.shouldShowNotificationForBackgroundUserSounds()) {
        if (UserManagerInternal.shouldShowNotificationForBackgroundUserSounds()) {
            filter.addAction(BackgroundUserSoundNotifier.ACTION_MUTE_SOUND);
        }
        context.registerReceiver(mIntentReceiver, filter, Context.RECEIVER_NOT_EXPORTED);
+2 −0
Original line number Diff line number Diff line
@@ -141,6 +141,7 @@ public final class UserManagerServiceTest {
            .spyStatic(ActivityManager.class)
            .mockStatic(Settings.Global.class)
            .mockStatic(Settings.Secure.class)
            .mockStatic(Resources.class)
            .build();

    @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(
@@ -202,6 +203,7 @@ public final class UserManagerServiceTest {
        doReturn(0)
                .when(mSpyResources)
                .getInteger(com.android.internal.R.integer.config_hsumBootStrategy);
        doReturn(mSpyResources).when(() -> Resources.getSystem());

        // Must construct UserManagerService in the UiThread
        mTestDir = new File(mRealContext.getDataDir(), "umstest");
+3 −3
Original line number Diff line number Diff line
@@ -109,7 +109,7 @@ import com.android.internal.util.test.FakeSettingsProviderRule;
import com.android.server.LocalServices;
import com.android.server.companion.virtual.VirtualDeviceManagerInternal;
import com.android.server.pm.BackgroundUserSoundNotifier;
import com.android.server.pm.UserManagerService;
import com.android.server.pm.UserManagerInternal;
import com.android.server.vibrator.VibrationSession.Status;

import org.junit.After;
@@ -898,7 +898,7 @@ public class VibratorManagerServiceTest {

    @Test
    public void vibrate_thenFgUserRequestsMute_getsCancelled() throws Throwable {
        assumeTrue(UserManagerService.shouldShowNotificationForBackgroundUserSounds());
        assumeTrue(UserManagerInternal.shouldShowNotificationForBackgroundUserSounds());
        mockVibrators(1);
        VibratorManagerService service = createSystemReadyService();

@@ -2760,7 +2760,7 @@ public class VibratorManagerServiceTest {

    @Test
    public void onExternalVibration_thenFgUserRequestsMute_doNotCancelVibration() throws Throwable {
        assumeTrue(UserManagerService.shouldShowNotificationForBackgroundUserSounds());
        assumeTrue(UserManagerInternal.shouldShowNotificationForBackgroundUserSounds());
        mockVibrators(1);
        mVibratorProviders.get(1).setCapabilities(IVibrator.CAP_EXTERNAL_CONTROL);
        VibratorManagerService service = createSystemReadyService();