Loading core/tests/coretests/src/android/content/pm/UserInfoTest.java +110 −0 Original line number Diff line number Diff line Loading @@ -17,6 +17,7 @@ package android.content.pm; import static android.content.pm.UserInfo.FLAG_DEMO; import static android.content.pm.UserInfo.FLAG_DISABLED; import static android.content.pm.UserInfo.FLAG_FULL; import static android.content.pm.UserInfo.FLAG_GUEST; import static android.content.pm.UserInfo.FLAG_MAIN; Loading @@ -26,7 +27,9 @@ 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 android.annotation.UserIdInt; import android.content.pm.UserInfo.UserInfoFlag; import android.os.Parcel; import android.os.UserHandle; import android.platform.test.annotations.DisableFlags; import android.platform.test.annotations.EnableFlags; Loading Loading @@ -121,6 +124,70 @@ public final class UserInfoTest { createTestUserInfo(USER_TYPE_SYSTEM_HEADLESS, FLAG_SYSTEM)); } @Test public void testParcelUnparcelUserInfo() throws Exception { UserInfo info = createUserWithAllFields(); Parcel out = Parcel.obtain(); info.writeToParcel(out, 0); byte[] data = out.marshall(); out.recycle(); Parcel in = Parcel.obtain(); try { in.unmarshall(data, 0, data.length); in.setDataPosition(0); UserInfo read = UserInfo.CREATOR.createFromParcel(in); assertUserInfoEquals(info, read, /* parcelCopy= */ true); } finally { in.recycle(); } } @Test public void testCopyConstructor() throws Exception { UserInfo info = createUserWithAllFields(); UserInfo copy = new UserInfo(info); assertUserInfoEquals(info, copy, /* parcelCopy= */ false); } @Test public void testSupportSwitchTo_partial() throws Exception { UserInfo userInfo = createUser(100, FLAG_FULL, /* userType= */ null); userInfo.partial = true; expect.withMessage("Supports switch to a partial user").that(userInfo.supportsSwitchTo()) .isFalse(); } @Test public void testSupportSwitchTo_disabled() throws Exception { UserInfo userInfo = createUser(100, FLAG_DISABLED, /* userType= */ null); expect.withMessage("Supports switch to a DISABLED user").that(userInfo.supportsSwitchTo()) .isFalse(); } @Test public void testSupportSwitchTo_preCreated() throws Exception { UserInfo userInfo = createUser(100, FLAG_FULL, /* userType= */ null); userInfo.preCreated = true; expect.withMessage("Supports switch to a pre-created user") .that(userInfo.supportsSwitchTo()) .isFalse(); userInfo.preCreated = false; expect.withMessage("Supports switch to a full, real user").that(userInfo.supportsSwitchTo()) .isTrue(); } @Test public void testSupportSwitchTo_profile() throws Exception { UserInfo userInfo = createUser(100, FLAG_PROFILE, /* userType= */ null); expect.withMessage("Supports switch to a profile").that(userInfo.supportsSwitchTo()) .isFalse(); } /** * Creates a new {@link UserInfo} with id {@code 10}, name {@code Test}, and the given * {@code flags}. Loading @@ -137,6 +204,49 @@ public final class UserInfoTest { return new UserInfo(10, "Test", /* iconPath= */ null, flags, userType); } /** Creates a UserInfo with the given flags and userType. */ private UserInfo createUser(@UserIdInt int userId, @UserInfoFlag int flags, String userType) { return new UserInfo(userId, "A Name", "A path", flags, userType); } private UserInfo createUserWithAllFields() { UserInfo user = new UserInfo(/*id= */ 21, "A Name", "A path", /*flags*/ 0x0ff0ff, "A type"); user.serialNumber = 5; user.creationTime = 4L << 32; user.lastLoggedInTime = 5L << 32; user.lastLoggedInFingerprint = "afingerprint"; user.profileGroupId = 45; user.restrictedProfileParentId = 4; user.profileBadge = 2; user.partial = true; user.guestToRemove = true; user.preCreated = true; user.convertedFromPreCreated = true; return user; } private void assertUserInfoEquals(UserInfo one, UserInfo two, boolean parcelCopy) { expect.withMessage("Id").that(two.id).isEqualTo(one.id); expect.withMessage("Name").that(two.name).isEqualTo(one.name); expect.withMessage("Icon path").that(two.iconPath).isEqualTo(one.iconPath); expect.withMessage("Flags").that(two.flags).isEqualTo(one.flags); expect.withMessage("UserType").that(two.userType).isEqualTo(one.userType); expect.withMessage("profile group").that(two.profileGroupId).isEqualTo(one.profileGroupId); expect.withMessage("restricted profile parent").that(two.restrictedProfileParentId) .isEqualTo(one.restrictedProfileParentId); expect.withMessage("profile badge").that(two.profileBadge).isEqualTo(one.profileBadge); expect.withMessage("partial").that(two.partial).isEqualTo(one.partial); expect.withMessage("guestToRemove").that(two.guestToRemove).isEqualTo(one.guestToRemove); expect.withMessage("preCreated").that(two.preCreated).isEqualTo(one.preCreated); if (parcelCopy) { expect.withMessage("convertedFromPreCreated").that(two.convertedFromPreCreated) .isFalse(); } else { expect.withMessage("convertedFromPreCreated").that(two.convertedFromPreCreated) .isEqualTo(one.convertedFromPreCreated); } } private void expectCanHaveProfile(String description, UserInfo user) { expect.withMessage("canHaveProfile() on %s (%s)", description, user) .that(user.canHaveProfile()).isTrue(); Loading services/tests/servicestests/src/com/android/server/pm/UserManagerServiceUserInfoTest.java +0 −76 Original line number Diff line number Diff line Loading @@ -16,12 +16,10 @@ package com.android.server.pm; import static android.content.pm.UserInfo.FLAG_DEMO; import static android.content.pm.UserInfo.FLAG_DISABLED; import static android.content.pm.UserInfo.FLAG_EPHEMERAL; import static android.content.pm.UserInfo.FLAG_FULL; import static android.content.pm.UserInfo.FLAG_GUEST; import static android.content.pm.UserInfo.FLAG_INITIALIZED; import static android.content.pm.UserInfo.FLAG_MAIN; import static android.content.pm.UserInfo.FLAG_MANAGED_PROFILE; import static android.content.pm.UserInfo.FLAG_PROFILE; import static android.content.pm.UserInfo.FLAG_RESTRICTED; Loading @@ -43,7 +41,6 @@ import android.content.pm.UserInfo.UserInfoFlag; import android.content.res.Resources; import android.multiuser.Flags; import android.os.Looper; import android.os.Parcel; import android.os.UserHandle; import android.os.UserManager; import android.platform.test.annotations.Presubmit; Loading Loading @@ -182,33 +179,6 @@ public final class UserManagerServiceUserInfoTest { .that(mUserManagerService.hasUserRestrictionOnAnyUser(local)).isEqualTo(enabled); } @Test public void testParcelUnparcelUserInfo() throws Exception { UserInfo info = createUser(); Parcel out = Parcel.obtain(); info.writeToParcel(out, 0); byte[] data = out.marshall(); out.recycle(); Parcel in = Parcel.obtain(); in.unmarshall(data, 0, data.length); in.setDataPosition(0); UserInfo read = UserInfo.CREATOR.createFromParcel(in); in.recycle(); assertUserInfoEquals(info, read, /* parcelCopy= */ true); } @Test public void testCopyConstructor() throws Exception { UserInfo info = createUser(); UserInfo copy = new UserInfo(info); assertUserInfoEquals(info, copy, /* parcelCopy= */ false); } @Test public void testGetUserName() throws Exception { expect.withMessage("System user name is set") Loading Loading @@ -245,52 +215,6 @@ public final class UserManagerServiceUserInfoTest { .that(mUserManagerService.isUserOfType(testId, typeName)).isTrue(); } /** Test UserInfo.supportsSwitchTo() for partial user. */ @Test public void testSupportSwitchTo_partial() throws Exception { UserInfo userInfo = createUser(100, FLAG_FULL, /* userType= */ null); userInfo.partial = true; expect.withMessage("Supports switch to a partial user").that(userInfo.supportsSwitchTo()) .isFalse(); } /** Test UserInfo.supportsSwitchTo() for disabled user. */ @Test public void testSupportSwitchTo_disabled() throws Exception { UserInfo userInfo = createUser(100, FLAG_DISABLED, /* userType= */ null); expect.withMessage("Supports switch to a DISABLED user").that(userInfo.supportsSwitchTo()) .isFalse(); } /** Test UserInfo.supportsSwitchTo() for precreated users. */ @Test public void testSupportSwitchTo_preCreated() throws Exception { UserInfo userInfo = createUser(100, FLAG_FULL, /* userType= */ null); userInfo.preCreated = true; expect.withMessage("Supports switch to a pre-created user") .that(userInfo.supportsSwitchTo()) .isFalse(); userInfo.preCreated = false; expect.withMessage("Supports switch to a full, real user").that(userInfo.supportsSwitchTo()) .isTrue(); } /** Test UserInfo.supportsSwitchTo() for profiles. */ @Test public void testSupportSwitchTo_profile() throws Exception { UserInfo userInfo = createUser(100, FLAG_PROFILE, /* userType= */ null); expect.withMessage("Supports switch to a profile").that(userInfo.supportsSwitchTo()) .isFalse(); } /** Test UserInfo.canHaveProfile for main user */ @Test public void testCanHaveProfile() throws Exception { UserInfo userInfo = createUser(100, FLAG_FULL | FLAG_MAIN, /* userType= */ null); expect.withMessage("Main users can have profile").that(userInfo.canHaveProfile()).isTrue(); } /** Tests upgradeIfNecessaryLP (but without locking) for upgrading from version 8 to 9+. */ @Test public void testUpgradeIfNecessaryLP_9() { Loading Loading
core/tests/coretests/src/android/content/pm/UserInfoTest.java +110 −0 Original line number Diff line number Diff line Loading @@ -17,6 +17,7 @@ package android.content.pm; import static android.content.pm.UserInfo.FLAG_DEMO; import static android.content.pm.UserInfo.FLAG_DISABLED; import static android.content.pm.UserInfo.FLAG_FULL; import static android.content.pm.UserInfo.FLAG_GUEST; import static android.content.pm.UserInfo.FLAG_MAIN; Loading @@ -26,7 +27,9 @@ 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 android.annotation.UserIdInt; import android.content.pm.UserInfo.UserInfoFlag; import android.os.Parcel; import android.os.UserHandle; import android.platform.test.annotations.DisableFlags; import android.platform.test.annotations.EnableFlags; Loading Loading @@ -121,6 +124,70 @@ public final class UserInfoTest { createTestUserInfo(USER_TYPE_SYSTEM_HEADLESS, FLAG_SYSTEM)); } @Test public void testParcelUnparcelUserInfo() throws Exception { UserInfo info = createUserWithAllFields(); Parcel out = Parcel.obtain(); info.writeToParcel(out, 0); byte[] data = out.marshall(); out.recycle(); Parcel in = Parcel.obtain(); try { in.unmarshall(data, 0, data.length); in.setDataPosition(0); UserInfo read = UserInfo.CREATOR.createFromParcel(in); assertUserInfoEquals(info, read, /* parcelCopy= */ true); } finally { in.recycle(); } } @Test public void testCopyConstructor() throws Exception { UserInfo info = createUserWithAllFields(); UserInfo copy = new UserInfo(info); assertUserInfoEquals(info, copy, /* parcelCopy= */ false); } @Test public void testSupportSwitchTo_partial() throws Exception { UserInfo userInfo = createUser(100, FLAG_FULL, /* userType= */ null); userInfo.partial = true; expect.withMessage("Supports switch to a partial user").that(userInfo.supportsSwitchTo()) .isFalse(); } @Test public void testSupportSwitchTo_disabled() throws Exception { UserInfo userInfo = createUser(100, FLAG_DISABLED, /* userType= */ null); expect.withMessage("Supports switch to a DISABLED user").that(userInfo.supportsSwitchTo()) .isFalse(); } @Test public void testSupportSwitchTo_preCreated() throws Exception { UserInfo userInfo = createUser(100, FLAG_FULL, /* userType= */ null); userInfo.preCreated = true; expect.withMessage("Supports switch to a pre-created user") .that(userInfo.supportsSwitchTo()) .isFalse(); userInfo.preCreated = false; expect.withMessage("Supports switch to a full, real user").that(userInfo.supportsSwitchTo()) .isTrue(); } @Test public void testSupportSwitchTo_profile() throws Exception { UserInfo userInfo = createUser(100, FLAG_PROFILE, /* userType= */ null); expect.withMessage("Supports switch to a profile").that(userInfo.supportsSwitchTo()) .isFalse(); } /** * Creates a new {@link UserInfo} with id {@code 10}, name {@code Test}, and the given * {@code flags}. Loading @@ -137,6 +204,49 @@ public final class UserInfoTest { return new UserInfo(10, "Test", /* iconPath= */ null, flags, userType); } /** Creates a UserInfo with the given flags and userType. */ private UserInfo createUser(@UserIdInt int userId, @UserInfoFlag int flags, String userType) { return new UserInfo(userId, "A Name", "A path", flags, userType); } private UserInfo createUserWithAllFields() { UserInfo user = new UserInfo(/*id= */ 21, "A Name", "A path", /*flags*/ 0x0ff0ff, "A type"); user.serialNumber = 5; user.creationTime = 4L << 32; user.lastLoggedInTime = 5L << 32; user.lastLoggedInFingerprint = "afingerprint"; user.profileGroupId = 45; user.restrictedProfileParentId = 4; user.profileBadge = 2; user.partial = true; user.guestToRemove = true; user.preCreated = true; user.convertedFromPreCreated = true; return user; } private void assertUserInfoEquals(UserInfo one, UserInfo two, boolean parcelCopy) { expect.withMessage("Id").that(two.id).isEqualTo(one.id); expect.withMessage("Name").that(two.name).isEqualTo(one.name); expect.withMessage("Icon path").that(two.iconPath).isEqualTo(one.iconPath); expect.withMessage("Flags").that(two.flags).isEqualTo(one.flags); expect.withMessage("UserType").that(two.userType).isEqualTo(one.userType); expect.withMessage("profile group").that(two.profileGroupId).isEqualTo(one.profileGroupId); expect.withMessage("restricted profile parent").that(two.restrictedProfileParentId) .isEqualTo(one.restrictedProfileParentId); expect.withMessage("profile badge").that(two.profileBadge).isEqualTo(one.profileBadge); expect.withMessage("partial").that(two.partial).isEqualTo(one.partial); expect.withMessage("guestToRemove").that(two.guestToRemove).isEqualTo(one.guestToRemove); expect.withMessage("preCreated").that(two.preCreated).isEqualTo(one.preCreated); if (parcelCopy) { expect.withMessage("convertedFromPreCreated").that(two.convertedFromPreCreated) .isFalse(); } else { expect.withMessage("convertedFromPreCreated").that(two.convertedFromPreCreated) .isEqualTo(one.convertedFromPreCreated); } } private void expectCanHaveProfile(String description, UserInfo user) { expect.withMessage("canHaveProfile() on %s (%s)", description, user) .that(user.canHaveProfile()).isTrue(); Loading
services/tests/servicestests/src/com/android/server/pm/UserManagerServiceUserInfoTest.java +0 −76 Original line number Diff line number Diff line Loading @@ -16,12 +16,10 @@ package com.android.server.pm; import static android.content.pm.UserInfo.FLAG_DEMO; import static android.content.pm.UserInfo.FLAG_DISABLED; import static android.content.pm.UserInfo.FLAG_EPHEMERAL; import static android.content.pm.UserInfo.FLAG_FULL; import static android.content.pm.UserInfo.FLAG_GUEST; import static android.content.pm.UserInfo.FLAG_INITIALIZED; import static android.content.pm.UserInfo.FLAG_MAIN; import static android.content.pm.UserInfo.FLAG_MANAGED_PROFILE; import static android.content.pm.UserInfo.FLAG_PROFILE; import static android.content.pm.UserInfo.FLAG_RESTRICTED; Loading @@ -43,7 +41,6 @@ import android.content.pm.UserInfo.UserInfoFlag; import android.content.res.Resources; import android.multiuser.Flags; import android.os.Looper; import android.os.Parcel; import android.os.UserHandle; import android.os.UserManager; import android.platform.test.annotations.Presubmit; Loading Loading @@ -182,33 +179,6 @@ public final class UserManagerServiceUserInfoTest { .that(mUserManagerService.hasUserRestrictionOnAnyUser(local)).isEqualTo(enabled); } @Test public void testParcelUnparcelUserInfo() throws Exception { UserInfo info = createUser(); Parcel out = Parcel.obtain(); info.writeToParcel(out, 0); byte[] data = out.marshall(); out.recycle(); Parcel in = Parcel.obtain(); in.unmarshall(data, 0, data.length); in.setDataPosition(0); UserInfo read = UserInfo.CREATOR.createFromParcel(in); in.recycle(); assertUserInfoEquals(info, read, /* parcelCopy= */ true); } @Test public void testCopyConstructor() throws Exception { UserInfo info = createUser(); UserInfo copy = new UserInfo(info); assertUserInfoEquals(info, copy, /* parcelCopy= */ false); } @Test public void testGetUserName() throws Exception { expect.withMessage("System user name is set") Loading Loading @@ -245,52 +215,6 @@ public final class UserManagerServiceUserInfoTest { .that(mUserManagerService.isUserOfType(testId, typeName)).isTrue(); } /** Test UserInfo.supportsSwitchTo() for partial user. */ @Test public void testSupportSwitchTo_partial() throws Exception { UserInfo userInfo = createUser(100, FLAG_FULL, /* userType= */ null); userInfo.partial = true; expect.withMessage("Supports switch to a partial user").that(userInfo.supportsSwitchTo()) .isFalse(); } /** Test UserInfo.supportsSwitchTo() for disabled user. */ @Test public void testSupportSwitchTo_disabled() throws Exception { UserInfo userInfo = createUser(100, FLAG_DISABLED, /* userType= */ null); expect.withMessage("Supports switch to a DISABLED user").that(userInfo.supportsSwitchTo()) .isFalse(); } /** Test UserInfo.supportsSwitchTo() for precreated users. */ @Test public void testSupportSwitchTo_preCreated() throws Exception { UserInfo userInfo = createUser(100, FLAG_FULL, /* userType= */ null); userInfo.preCreated = true; expect.withMessage("Supports switch to a pre-created user") .that(userInfo.supportsSwitchTo()) .isFalse(); userInfo.preCreated = false; expect.withMessage("Supports switch to a full, real user").that(userInfo.supportsSwitchTo()) .isTrue(); } /** Test UserInfo.supportsSwitchTo() for profiles. */ @Test public void testSupportSwitchTo_profile() throws Exception { UserInfo userInfo = createUser(100, FLAG_PROFILE, /* userType= */ null); expect.withMessage("Supports switch to a profile").that(userInfo.supportsSwitchTo()) .isFalse(); } /** Test UserInfo.canHaveProfile for main user */ @Test public void testCanHaveProfile() throws Exception { UserInfo userInfo = createUser(100, FLAG_FULL | FLAG_MAIN, /* userType= */ null); expect.withMessage("Main users can have profile").that(userInfo.canHaveProfile()).isTrue(); } /** Tests upgradeIfNecessaryLP (but without locking) for upgrading from version 8 to 9+. */ @Test public void testUpgradeIfNecessaryLP_9() { Loading