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

Commit 905c0af4 authored by Olivier Nshimiye's avatar Olivier Nshimiye Committed by Android (Google) Code Review
Browse files

Merge "Fix NPE exception in Utils#isPrivateProfile" into main

parents 6cc8c036 67c45fd0
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -1510,8 +1510,7 @@ public final class Utils extends com.android.settingslib.Utils {
    public static boolean isPrivateProfile(int userId, @NonNull Context context) {
        final UserManager userManager = context.getSystemService(UserManager.class);
        UserInfo userInfo = userManager.getUserInfo(userId);
        return Flags.allowPrivateProfile() && android.multiuser.Flags.enablePrivateSpaceFeatures()
                && userInfo.isPrivateProfile();
        return !Objects.isNull(userInfo) && userInfo.isPrivateProfile();
    }

    /**
+23 −0
Original line number Diff line number Diff line
@@ -727,6 +727,29 @@ public class UtilsTest {
                ConfirmDeviceCredentialActivity.InternalActivity.class.getName());
    }

    @Test
    public void testIsPrivateProfile_nullUserInfo() {
        Context mockContext = mock(Context.class);
        UserInfo mockUserInfo = mock(UserInfo.class);
        int mockUserId = 12;
        when(mockContext.getSystemService(UserManager.class)).thenReturn(mMockUserManager);
        when(mMockUserManager.getUserInfo(mockUserId)).thenReturn(null);

        assertThat(Utils.isPrivateProfile(mockUserId, mockContext)).isFalse();
    }

    @Test
    public void testIsPrivateProfile_validUserInfo() {
        Context mockContext = mock(Context.class);
        UserInfo mockUserInfo = mock(UserInfo.class);
        int mockUserId = 12;
        when(mockContext.getSystemService(UserManager.class)).thenReturn(mMockUserManager);
        when(mockUserInfo.isPrivateProfile()).thenReturn(true);
        when(mMockUserManager.getUserInfo(mockUserId)).thenReturn(mockUserInfo);

        assertThat(Utils.isPrivateProfile(mockUserId, mockContext)).isTrue();
    }

    private void setUpForConfirmCredentialString(boolean isEffectiveUserManagedProfile) {
        when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mMockUserManager);
        when(mMockUserManager.getCredentialOwnerProfile(USER_ID)).thenReturn(USER_ID);