Loading core/java/android/content/pm/UserInfo.java +4 −2 Original line number Diff line number Diff line Loading @@ -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. Loading core/tests/mockingcoretests/Android.bp +1 −0 Original line number Diff line number Diff line Loading @@ -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", Loading core/tests/mockingcoretests/src/android/content/pm/UserInfoTest.java +33 −2 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading @@ -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); Loading Loading @@ -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)); Loading @@ -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 Loading @@ -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)); Loading @@ -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 Loading Loading @@ -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); } } Loading
core/java/android/content/pm/UserInfo.java +4 −2 Original line number Diff line number Diff line Loading @@ -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. Loading
core/tests/mockingcoretests/Android.bp +1 −0 Original line number Diff line number Diff line Loading @@ -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", Loading
core/tests/mockingcoretests/src/android/content/pm/UserInfoTest.java +33 −2 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading @@ -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); Loading Loading @@ -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)); Loading @@ -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 Loading @@ -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)); Loading @@ -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 Loading Loading @@ -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); } }