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

Commit e2810a11 authored by Felipe Leme's avatar Felipe Leme
Browse files

Changed UserInfo.canHaveProfile() to use config_supportProfilesOnNonMainUser.

Test: atest FrameworksMockingCoreTests:UserInfoTest
Bug: 374832167
Flag: android.multiuser.profiles_for_all

Change-Id: I362e9d35ee73a478db1fc45263817a03786eb3a0
parent 90a077a3
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -500,8 +500,10 @@ public class UserInfo implements Parcelable {
        // NOTE: profiles used to be restricted just to the system user (and later to the main
        // user), but from the framework point of view there is no need for such restriction, hence
        // it's lifted
        // TODO(b/374832167): check value of config_supportProfilesOnNonMainUser
        return isMain() || android.multiuser.Flags.profilesForAll();
        return isMain()
                || (android.multiuser.Flags.profilesForAll()
                        && Resources.getSystem().getBoolean(
                                com.android.internal.R.bool.config_supportProfilesOnNonMainUser));
    }

    // TODO(b/142482943): Get rid of this (after removing it from all tests) if feasible.
+1 −0
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ android_test {
        "androidx.test.rules",
        "androidx.test.ext.junit",
        "mockito-target-extended-minus-junit4",
        "modules-utils-extended-mockito-rule",
        "platform-test-annotations",
        "truth",
        "testables",
+33 −2
Original line number Diff line number Diff line
@@ -27,8 +27,14 @@ import static android.os.UserManager.USER_TYPE_FULL_RESTRICTED;
import static android.os.UserManager.USER_TYPE_FULL_SYSTEM;
import static android.os.UserManager.USER_TYPE_SYSTEM_HEADLESS;

import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import android.annotation.UserIdInt;
import android.content.pm.UserInfo.UserInfoFlag;
import android.content.res.Resources;
import android.os.Parcel;
import android.os.UserHandle;
import android.platform.test.annotations.DisableFlags;
@@ -37,6 +43,8 @@ import android.platform.test.flag.junit.SetFlagsRule;

import androidx.test.filters.SmallTest;

import com.android.modules.utils.testing.ExtendedMockitoRule;

import com.google.common.truth.Expect;

import org.junit.Rule;
@@ -52,6 +60,11 @@ public final class UserInfoTest {

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

    @Rule
    public final ExtendedMockitoRule extendedMockito = new ExtendedMockitoRule.Builder(this)
            .spyStatic(Resources.class)
            .build();

    @Test
    public void testSimple() throws Exception {
        UserInfo ui = createTestUserInfo(FLAG_GUEST);
@@ -96,7 +109,6 @@ public final class UserInfoTest {
        expectCannotHaveProfile("non-full user", createTestUserInfo(/* flags= */ 0));
        expectCannotHaveProfile("guest user", createTestUserInfo(FLAG_FULL | FLAG_GUEST));
        expectCanHaveProfile("main user", createTestUserInfo(FLAG_FULL | FLAG_MAIN));
        expectCannotHaveProfile("non-main user", createTestUserInfo(FLAG_FULL));
        expectCannotHaveProfile("demo user", createTestUserInfo(FLAG_FULL | FLAG_DEMO));
        expectCannotHaveProfile("restricted user",
                createTestUserInfo(USER_TYPE_FULL_RESTRICTED, FLAG_FULL));
@@ -105,6 +117,13 @@ public final class UserInfoTest {
                createTestUserInfo(USER_TYPE_FULL_SYSTEM, FLAG_FULL | FLAG_SYSTEM | FLAG_MAIN));
        expectCannotHaveProfile("headless system user that's not main user",
                createTestUserInfo(USER_TYPE_SYSTEM_HEADLESS, FLAG_SYSTEM));

        // Non-main user depends on config_supportProfilesOnNonMainUser, but should be disabled
        // anyways because of the flag
        mockConfigSupportProfilesOnNonMainUser(false);
        expectCannotHaveProfile("non-main user", createTestUserInfo(FLAG_FULL));
        mockConfigSupportProfilesOnNonMainUser(true);
        expectCannotHaveProfile("non-main user", createTestUserInfo(FLAG_FULL));
    }

    @Test
@@ -113,7 +132,6 @@ public final class UserInfoTest {
        expectCannotHaveProfile("non-full user", createTestUserInfo(/* flags= */ 0));
        expectCannotHaveProfile("guest user", createTestUserInfo(FLAG_FULL | FLAG_GUEST));
        expectCanHaveProfile("main user", createTestUserInfo(FLAG_FULL | FLAG_MAIN));
        expectCanHaveProfile("non-main user", createTestUserInfo(FLAG_FULL));
        expectCannotHaveProfile("demo user", createTestUserInfo(FLAG_FULL | FLAG_DEMO));
        expectCannotHaveProfile("restricted user",
                createTestUserInfo(USER_TYPE_FULL_RESTRICTED, FLAG_FULL));
@@ -122,6 +140,12 @@ public final class UserInfoTest {
                createTestUserInfo(USER_TYPE_FULL_SYSTEM, FLAG_FULL | FLAG_SYSTEM | FLAG_MAIN));
        expectCannotHaveProfile("headless system user that's not main user",
                createTestUserInfo(USER_TYPE_SYSTEM_HEADLESS, FLAG_SYSTEM));

        // Non-main user depends on config_supportProfilesOnNonMainUser
        mockConfigSupportProfilesOnNonMainUser(false);
        expectCannotHaveProfile("non-main user (config disabled)", createTestUserInfo(FLAG_FULL));
        mockConfigSupportProfilesOnNonMainUser(true);
        expectCanHaveProfile("non-main user (config enabled)", createTestUserInfo(FLAG_FULL));
    }

    @Test
@@ -256,4 +280,11 @@ public final class UserInfoTest {
        expect.withMessage("canHaveProfile() on %s (%s)", description, user)
                .that(user.canHaveProfile()).isFalse();
    }

    private void mockConfigSupportProfilesOnNonMainUser(boolean value) {
        Resources resources = mock(Resources.class);
        int config = com.android.internal.R.bool.config_supportProfilesOnNonMainUser;
        when(resources.getBoolean(config)).thenReturn(value);
        doReturn(resources).when(Resources::getSystem);
    }
}