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

Commit c8776157 authored by Benjamin Franz's avatar Benjamin Franz
Browse files

Allow removing non-enabled profiles

Bug: 31668514
Test: runtest -c
com.android.server.pm.UserManagerServiceCreateProfileTest
frameworks-services
Change-Id: If8db670d6f253a44b2aa91df97349dd0ffd85f02
parent e86e6d75
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -1523,7 +1523,7 @@ public class UserManagerService extends IUserManager.Stub {
            return false;
        }
        // Limit number of managed profiles that can be created
        final int managedProfilesCount = getProfiles(userId, true).size() - 1;
        final int managedProfilesCount = getProfiles(userId, false).size() - 1;
        final int profilesRemovedCount = managedProfilesCount > 0 && allowedToRemoveOne ? 1 : 0;
        if (managedProfilesCount - profilesRemovedCount >= getMaxManagedProfiles()) {
            return false;
+123 −84
Original line number Diff line number Diff line
@@ -16,27 +16,27 @@

package com.android.server.pm;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import android.app.ApplicationPackageManager;
import android.content.pm.UserInfo;
import android.os.Looper;
import android.os.UserManagerInternal;
import android.os.UserHandle;
import android.os.UserManagerInternal;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.filters.MediumTest;
import android.support.test.runner.AndroidJUnit4;

import com.android.server.LocalServices;

import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * <p>Run with:<pre>
 * runtest -c com.android.server.pm.UserManagerServiceCreateProfileTest frameworks-services
@@ -64,9 +64,13 @@ public class UserManagerServiceCreateProfileTest {
                UserHandle.USER_SYSTEM, users.get(0).id);
    }

    @After
    public void tearDown() {
        removeUsers();
    }

    @Test
    public void testGetProfiles() {
        try {
        // Pretend we have a secondary user with a profile.
        UserInfo secondaryUser = addUser();
        UserInfo profile = addProfile(secondaryUser);
@@ -85,14 +89,10 @@ public class UserManagerServiceCreateProfileTest {
                || users.get(1).id == secondaryUser.id);
        assertTrue("Missing profile user id", users.get(0).id == profile.id
                || users.get(1).id == profile.id);
        } finally {
            removeUsers();
        }
    }

    @Test
    public void testProfileBadge() {
        try {
        // First profile for system user should get badge 0
        assertEquals("First profile isn't given badge index 0", 0,
                mUserManagerService.getFreeProfileBadgeLU(UserHandle.USER_SYSTEM));
@@ -114,14 +114,10 @@ public class UserManagerServiceCreateProfileTest {
        // Shouldn't have impacted the badge for the system user
        assertEquals("First profile isn't given badge index 0 in secondary user", 0,
                mUserManagerService.getFreeProfileBadgeLU(UserHandle.USER_SYSTEM));
        } finally {
            removeUsers();
        }
    }

    @Test
    public void testProfileBadgeUnique() {
        try {
        List<UserInfo> users = mUserManagerService.getUsers(/* excludeDying */ false);
        UserInfo system = users.get(0);
        // Badges should get allocated 0 -> max
@@ -131,14 +127,10 @@ public class UserManagerServiceCreateProfileTest {
            UserInfo profile = addProfile(system);
            profile.profileBadge = nextBadge;
        }
        } finally {
            removeUsers();
        }
    }

    @Test
    public void testProfileBadgeReuse() {
        try {
        // Pretend we have a secondary user with a profile.
        UserInfo secondaryUser = addUser();
        UserInfo profile = addProfile(secondaryUser);
@@ -157,9 +149,6 @@ public class UserManagerServiceCreateProfileTest {
            assertEquals("Lower index not used", 0,
                    mUserManagerService.getFreeProfileBadgeLU(secondaryUser.id));
        }
        } finally {
            removeUsers();
        }
    }

    @Test
@@ -172,6 +161,49 @@ public class UserManagerServiceCreateProfileTest {
                ApplicationPackageManager.CORP_BADGE_LABEL_RES_ID.length);
    }

    @Test
    public void testCanAddMoreManagedProfiles_removeProfile() {
        // if device is low-ram or doesn't support managed profiles for some other reason, just
        // skip the test
        if (!mUserManagerService.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM,
                false /* disallow remove */)) {
            return;
        }

        // GIVEN we've reached the limit of managed profiles possible on the system user
        while (mUserManagerService.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM,
                false /* disallow remove */)) {
            addProfile(mUserManagerService.getPrimaryUser());
        }

        // THEN you should be able to add a new profile if you remove an existing one
        assertTrue("Cannot add a managed profile by removing another one",
                mUserManagerService.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM,
                        true /* allow remove */));
    }

    @Test
    public void testCanAddMoreManagedProfiles_removeDisabledProfile() {
        // if device is low-ram or doesn't support managed profiles for some other reason, just
        // skip the test
        if (!mUserManagerService.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM,
                false /* disallow remove */)) {
            return;
        }

        // GIVEN we've reached the limit of managed profiles possible on the system user
        // GIVEN that the profiles are not enabled yet
        while (mUserManagerService.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM,
                false /* disallow remove */)) {
            addProfile(mUserManagerService.getPrimaryUser(), true /* disabled */);
        }

        // THEN you should be able to add a new profile if you remove an existing one
        assertTrue("Cannot add a managed profile by removing another one",
                mUserManagerService.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM,
                        true /* allow remove */));
    }

    private void removeUsers() {
        List<UserInfo> users = mUserManagerService.getUsers(/* excludeDying */ false);
        for (UserInfo user: users) {
@@ -182,10 +214,17 @@ public class UserManagerServiceCreateProfileTest {
    }

    private UserInfo addProfile(UserInfo user) {
        return addProfile(user, false);
    }

    private UserInfo addProfile(UserInfo user, boolean disabled) {
        user.profileGroupId = user.id;
        int flags = UserInfo.FLAG_MANAGED_PROFILE;
        if (disabled) {
            flags |= UserInfo.FLAG_DISABLED;
        }
        UserInfo profile = new UserInfo(
                mUserManagerService.getNextAvailableId(), "profile",
                UserInfo.FLAG_MANAGED_PROFILE);
                mUserManagerService.getNextAvailableId(), "profile", flags);
        profile.profileGroupId = user.id;
        mUserManagerService.putUserInfo(profile);
        return profile;