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

Commit 0e7702f7 authored by Felipe Leme's avatar Felipe Leme
Browse files

Added more unit tests for UserManagerService.

This CL adds tests for the following 3 methods:

- boolean isUserVisible(int userId)
- boolean isUserVisibleOnDisplay(int userId, int displayId)
- int getDisplayAssignedToUser(int userId)

It also adds UnitManagerInternalTest and refactors the test infra
so tests for methods whose implementation is the same for UMS / UMI
(i.e., where the method of the former calls the method of the latter
or vice versa) are written just once.

Bug: 244452695
Test: atest FrameworksMockingServicesTests:com.android.server.pm.UserManagerServiceTest UserManagerInternalTest

Change-Id: I95b53935b8fa01edeb64816e2afda57d4852c888
parent effb3653
Loading
Loading
Loading
Loading
+21 −8
Original line number Diff line number Diff line
@@ -630,7 +630,7 @@ public class UserManagerService extends IUserManager.Stub {
    /**
     * Set on on devices that support background users (key) running on secondary displays (value).
     */
    // TODO(b/239982558): move such logic to a different class (like UserDisplayAssigner)
    // TODO(b/244644281): move such logic to a different class (like UserDisplayAssigner)
    @Nullable
    @GuardedBy("mUsersOnSecondaryDisplays")
    private final SparseIntArray mUsersOnSecondaryDisplays;
@@ -710,7 +710,8 @@ public class UserManagerService extends IUserManager.Stub {
    @VisibleForTesting
    UserManagerService(Context context) {
        this(context, /* pm= */ null, /* userDataPreparer= */ null,
                /* packagesLock= */ new Object(), context.getCacheDir(), /* users= */ null);
                /* packagesLock= */ new Object(), context.getCacheDir(), /* users= */ null,
                /* usersOnSecondaryDisplays= */ null);
    }

    /**
@@ -721,13 +722,13 @@ public class UserManagerService extends IUserManager.Stub {
    UserManagerService(Context context, PackageManagerService pm, UserDataPreparer userDataPreparer,
            Object packagesLock) {
        this(context, pm, userDataPreparer, packagesLock, Environment.getDataDirectory(),
                /* users= */ null);
                /* users= */ null, /* usersOnSecondaryDisplays= */ null);
    }

    @VisibleForTesting
    UserManagerService(Context context, PackageManagerService pm,
            UserDataPreparer userDataPreparer, Object packagesLock, File dataDir,
            SparseArray<UserData> users) {
            SparseArray<UserData> users, @Nullable SparseIntArray usersOnSecondaryDisplays) {
        mContext = context;
        mPm = pm;
        mPackagesLock = packagesLock;
@@ -758,7 +759,13 @@ public class UserManagerService extends IUserManager.Stub {
        mUser0Allocations = DBG_ALLOCATION ? new AtomicInteger() : null;
        emulateSystemUserModeIfNeeded();
        mUsersOnSecondaryDisplaysEnabled = UserManager.isUsersOnSecondaryDisplaysEnabled();
        mUsersOnSecondaryDisplays = mUsersOnSecondaryDisplaysEnabled ? new SparseIntArray() : null;
        if (mUsersOnSecondaryDisplaysEnabled) {
            mUsersOnSecondaryDisplays = usersOnSecondaryDisplays == null
                    ? new SparseIntArray() // default behavior
                    : usersOnSecondaryDisplays; // passed by unit test
        } else {
            mUsersOnSecondaryDisplays = null;
        }
    }

    void systemReady() {
@@ -1720,6 +1727,11 @@ public class UserManagerService extends IUserManager.Stub {
        return userId == getCurrentUserId();
    }

    @VisibleForTesting
    boolean isUsersOnSecondaryDisplaysEnabled() {
        return mUsersOnSecondaryDisplaysEnabled;
    }

    @Override
    public boolean isUserVisible(@UserIdInt int userId) {
        int callingUserId = UserHandle.getCallingUserId();
@@ -1733,7 +1745,8 @@ public class UserManagerService extends IUserManager.Stub {
        return isUserVisibleUnchecked(userId);
    }

    private boolean isUserVisibleUnchecked(@UserIdInt int userId) {
    @VisibleForTesting
    boolean isUserVisibleUnchecked(@UserIdInt int userId) {
        // First check current foreground user and their profiles (on main display)
        if (isCurrentUserOrRunningProfileOfCurrentUser(userId)) {
            return true;
@@ -1750,8 +1763,8 @@ public class UserManagerService extends IUserManager.Stub {
        }
    }

    // TODO(b/239982558): add unit test
    private int getDisplayAssignedToUser(@UserIdInt int userId) {
    @VisibleForTesting
    int getDisplayAssignedToUser(@UserIdInt int userId) {
        if (isCurrentUserOrRunningProfileOfCurrentUser(userId)) {
            return Display.DEFAULT_DISPLAY;
        }
+37 −0
Original line number Diff line number Diff line
/*
 * 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.pm;

/**
 * Run as {@code atest FrameworksMockingServicesTests:com.android.server.pm.UserManagerInternalTest}
 */
public final class UserManagerInternalTest extends UserManagerServiceOrInternalTestCase {

    @Override
    protected boolean isUserVisible(int userId) {
        return mUmi.isUserVisible(userId);
    }

    @Override
    protected boolean isUserVisibleOnDisplay(int userId, int displayId) {
        return mUmi.isUserVisible(userId, displayId);
    }

    @Override
    protected int getDisplayAssignedToUser(int userId) {
        return mUmi.getDisplayAssignedToUser(userId);
    }
}
+524 −0

File added.

Preview size limit exceeded, changes collapsed.

+36 −152

File changed.

Preview size limit exceeded, changes collapsed.