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

Commit 250014c0 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Refactored UserVisibilityMediator unit tests:"

parents 1697a83b ccd0da6a
Loading
Loading
Loading
Loading
+2 −213
Original line number Diff line number Diff line
@@ -15,19 +15,6 @@
 */
package com.android.server.pm;

import static android.os.UserHandle.USER_NULL;
import static android.view.Display.DEFAULT_DISPLAY;
import static android.view.Display.INVALID_DISPLAY;

import static com.android.server.pm.UserManagerInternal.USER_ASSIGNMENT_RESULT_FAILURE;
import static com.android.server.pm.UserManagerInternal.USER_ASSIGNMENT_RESULT_SUCCESS_INVISIBLE;
import static com.android.server.pm.UserManagerInternal.USER_ASSIGNMENT_RESULT_SUCCESS_VISIBLE;
import static com.android.server.pm.UserVisibilityChangedEvent.onInvisible;
import static com.android.server.pm.UserVisibilityChangedEvent.onVisible;
import static com.android.server.pm.UserVisibilityMediator.INITIAL_CURRENT_USER_ID;

import org.junit.Test;

/**
 * Tests for {@link UserVisibilityMediator} tests for devices that support concurrent Multiple
 * Users on Multiple Displays (A.K.A {@code MUMD}).
@@ -35,208 +22,10 @@ import org.junit.Test;
 * <p>Run as
 * {@code atest FrameworksMockingServicesTests:com.android.server.pm.UserVisibilityMediatorMUMDTest}
 */
public final class UserVisibilityMediatorMUMDTest extends UserVisibilityMediatorTestCase {
public final class UserVisibilityMediatorMUMDTest
        extends UserVisibilityMediatorVisibleBackgroundUserTestCase {

    public UserVisibilityMediatorMUMDTest() throws Exception {
        super(/* usersOnSecondaryDisplaysEnabled= */ true);
    }

    @Test
    public void testStartFgUser_onDefaultDisplay() throws Exception {
        AsyncUserVisibilityListener listener = addListenerForEvents(
                onInvisible(INITIAL_CURRENT_USER_ID),
                onVisible(USER_ID));

        int result = mMediator.assignUserToDisplayOnStart(USER_ID, USER_ID, FG,
                DEFAULT_DISPLAY);
        assertStartUserResult(result, USER_ASSIGNMENT_RESULT_SUCCESS_VISIBLE);

        expectUserIsVisible(USER_ID);
        expectUserIsVisibleOnDisplay(USER_ID, DEFAULT_DISPLAY);
        expectUserIsNotVisibleOnDisplay(USER_ID, INVALID_DISPLAY);
        expectUserIsNotVisibleOnDisplay(USER_ID, SECONDARY_DISPLAY_ID);
        expectVisibleUsers(USER_ID);

        expectDisplayAssignedToUser(USER_ID, DEFAULT_DISPLAY);
        expectUserAssignedToDisplay(DEFAULT_DISPLAY, USER_ID);
        expectUserAssignedToDisplay(INVALID_DISPLAY, USER_ID);
        expectUserAssignedToDisplay(SECONDARY_DISPLAY_ID, USER_ID);

        expectDisplayAssignedToUser(USER_NULL, INVALID_DISPLAY);

        listener.verify();
    }

    @Test
    public void testSwitchFgUser_onDefaultDisplay() throws Exception {
        int previousCurrentUserId = OTHER_USER_ID;
        int currentUserId = USER_ID;
        AsyncUserVisibilityListener listener = addListenerForEvents(
                onInvisible(INITIAL_CURRENT_USER_ID),
                onVisible(previousCurrentUserId),
                onInvisible(previousCurrentUserId),
                onVisible(currentUserId));
        startForegroundUser(previousCurrentUserId);

        int result = mMediator.assignUserToDisplayOnStart(currentUserId, currentUserId, FG,
                DEFAULT_DISPLAY);
        assertStartUserResult(result, USER_ASSIGNMENT_RESULT_SUCCESS_VISIBLE);

        expectUserIsVisible(currentUserId);
        expectUserIsVisibleOnDisplay(currentUserId, DEFAULT_DISPLAY);
        expectUserIsNotVisibleOnDisplay(currentUserId, INVALID_DISPLAY);
        expectUserIsNotVisibleOnDisplay(currentUserId, SECONDARY_DISPLAY_ID);
        expectVisibleUsers(currentUserId);

        expectDisplayAssignedToUser(currentUserId, DEFAULT_DISPLAY);
        expectUserAssignedToDisplay(DEFAULT_DISPLAY, currentUserId);
        expectUserAssignedToDisplay(INVALID_DISPLAY, currentUserId);
        expectUserAssignedToDisplay(SECONDARY_DISPLAY_ID, currentUserId);

        expectUserIsNotVisibleAtAll(previousCurrentUserId);
        expectNoDisplayAssignedToUser(previousCurrentUserId);

        listener.verify();
    }

    @Test
    public void testStartBgProfile_onDefaultDisplay_whenParentIsCurrentUser() throws Exception {
        AsyncUserVisibilityListener listener = addListenerForEvents(
                onInvisible(INITIAL_CURRENT_USER_ID),
                onVisible(PARENT_USER_ID),
                onVisible(PROFILE_USER_ID));
        startForegroundUser(PARENT_USER_ID);

        int result = mMediator.assignUserToDisplayOnStart(PROFILE_USER_ID, PARENT_USER_ID,
                BG_VISIBLE, DEFAULT_DISPLAY);
        assertStartUserResult(result, USER_ASSIGNMENT_RESULT_SUCCESS_VISIBLE);

        expectUserIsVisible(PROFILE_USER_ID);
        expectUserIsNotVisibleOnDisplay(PROFILE_USER_ID, INVALID_DISPLAY);
        expectUserIsNotVisibleOnDisplay(PROFILE_USER_ID, SECONDARY_DISPLAY_ID);
        expectUserIsVisibleOnDisplay(PROFILE_USER_ID, DEFAULT_DISPLAY);
        expectVisibleUsers(PARENT_USER_ID, PROFILE_USER_ID);

        expectDisplayAssignedToUser(PROFILE_USER_ID, DEFAULT_DISPLAY);
        expectUserAssignedToDisplay(DEFAULT_DISPLAY, PARENT_USER_ID);

        listener.verify();
    }

    @Test
    public void testStartFgUser_onInvalidDisplay() throws Exception {
        AsyncUserVisibilityListener listener = addListenerForNoEvents();

        int result = mMediator.assignUserToDisplayOnStart(USER_ID, USER_ID, FG, INVALID_DISPLAY);

        assertStartUserResult(result, USER_ASSIGNMENT_RESULT_FAILURE);

        listener.verify();
    }

    @Test
    public void testStartBgUser_onInvalidDisplay() throws Exception {
        AsyncUserVisibilityListener listener = addListenerForNoEvents();

        int result = mMediator.assignUserToDisplayOnStart(USER_ID, USER_ID, BG_VISIBLE,
                INVALID_DISPLAY);

        assertStartUserResult(result, USER_ASSIGNMENT_RESULT_FAILURE);

        expectUserIsNotVisibleAtAll(USER_ID);

        listener.verify();
    }

    @Test
    public void testStartBgUser_onSecondaryDisplay_displayAvailable() throws Exception {
        AsyncUserVisibilityListener listener = addListenerForEvents(onVisible(USER_ID));

        int result = mMediator.assignUserToDisplayOnStart(USER_ID, USER_ID, BG_VISIBLE,
                SECONDARY_DISPLAY_ID);
        assertStartUserResult(result, USER_ASSIGNMENT_RESULT_SUCCESS_VISIBLE);

        expectUserIsVisible(USER_ID);
        expectUserIsVisibleOnDisplay(USER_ID, SECONDARY_DISPLAY_ID);
        expectUserIsNotVisibleOnDisplay(USER_ID, INVALID_DISPLAY);
        expectUserIsNotVisibleOnDisplay(USER_ID, DEFAULT_DISPLAY);
        expectVisibleUsers(INITIAL_CURRENT_USER_ID, USER_ID);

        expectDisplayAssignedToUser(USER_ID, SECONDARY_DISPLAY_ID);
        expectUserAssignedToDisplay(SECONDARY_DISPLAY_ID, USER_ID);

        listener.verify();
    }

    @Test
    public void testVisibilityOfCurrentUserAndProfilesOnDisplayAssignedToAnotherUser()
            throws Exception {
        startDefaultProfile();

        // Make sure they were visible before
        expectUserIsNotVisibleOnDisplay("before", PARENT_USER_ID, SECONDARY_DISPLAY_ID);
        expectUserIsNotVisibleOnDisplay("before", PROFILE_USER_ID, SECONDARY_DISPLAY_ID);

        int result = mMediator.assignUserToDisplayOnStart(USER_ID, USER_ID, BG_VISIBLE,
                SECONDARY_DISPLAY_ID);
        assertStartUserResult(result, USER_ASSIGNMENT_RESULT_SUCCESS_VISIBLE);

        expectUserIsNotVisibleOnDisplay("after", PARENT_USER_ID, SECONDARY_DISPLAY_ID);
        expectUserIsNotVisibleOnDisplay("after", PROFILE_USER_ID, SECONDARY_DISPLAY_ID);
    }

    @Test
    public void testStartBgUser_onSecondaryDisplay_displayAlreadyAssigned() throws Exception {
        AsyncUserVisibilityListener listener = addListenerForEvents(onVisible(OTHER_USER_ID));
        startUserInSecondaryDisplay(OTHER_USER_ID, SECONDARY_DISPLAY_ID);

        int result = mMediator.assignUserToDisplayOnStart(USER_ID, USER_ID, BG_VISIBLE,
                SECONDARY_DISPLAY_ID);
        assertStartUserResult(result, USER_ASSIGNMENT_RESULT_FAILURE);

        expectUserIsNotVisibleAtAll(USER_ID);
        expectNoDisplayAssignedToUser(USER_ID);
        expectUserAssignedToDisplay(SECONDARY_DISPLAY_ID, OTHER_USER_ID);

        listener.verify();
    }

    @Test
    public void testStartBgUser_onSecondaryDisplay_userAlreadyAssigned() throws Exception {
        AsyncUserVisibilityListener listener = addListenerForEvents(onVisible(USER_ID));
        startUserInSecondaryDisplay(USER_ID, OTHER_SECONDARY_DISPLAY_ID);

        int result = mMediator.assignUserToDisplayOnStart(USER_ID, USER_ID, BG_VISIBLE,
                SECONDARY_DISPLAY_ID);
        assertStartUserResult(result, USER_ASSIGNMENT_RESULT_FAILURE);

        expectUserIsVisible(USER_ID);
        expectUserIsVisibleOnDisplay(USER_ID, OTHER_SECONDARY_DISPLAY_ID);
        expectUserIsNotVisibleOnDisplay(USER_ID, SECONDARY_DISPLAY_ID);
        expectUserIsNotVisibleOnDisplay(USER_ID, INVALID_DISPLAY);
        expectUserIsNotVisibleOnDisplay(USER_ID, DEFAULT_DISPLAY);
        expectVisibleUsers(INITIAL_CURRENT_USER_ID, USER_ID);

        expectDisplayAssignedToUser(USER_ID, OTHER_SECONDARY_DISPLAY_ID);
        expectUserAssignedToDisplay(OTHER_SECONDARY_DISPLAY_ID, USER_ID);

        listener.verify();
    }

    @Test
    public void testStartBgProfile_onDefaultDisplay_whenParentVisibleOnSecondaryDisplay()
            throws Exception {
        AsyncUserVisibilityListener listener = addListenerForEvents(onVisible(PARENT_USER_ID));
        startUserInSecondaryDisplay(PARENT_USER_ID, OTHER_SECONDARY_DISPLAY_ID);

        int result = mMediator.assignUserToDisplayOnStart(PROFILE_USER_ID, PARENT_USER_ID,
                BG_VISIBLE, DEFAULT_DISPLAY);
        assertStartUserResult(result, USER_ASSIGNMENT_RESULT_SUCCESS_INVISIBLE);

        expectUserIsNotVisibleAtAll(PROFILE_USER_ID);
        expectNoDisplayAssignedToUser(PROFILE_USER_ID);
        expectUserAssignedToDisplay(OTHER_SECONDARY_DISPLAY_ID, PARENT_USER_ID);

        listener.verify();
    }
}
+5 −4
Original line number Diff line number Diff line
@@ -61,7 +61,7 @@ public final class UserVisibilityMediatorSUSDTest extends UserVisibilityMediator
        expectUserAssignedToDisplay(INVALID_DISPLAY, USER_ID);
        expectUserAssignedToDisplay(SECONDARY_DISPLAY_ID, USER_ID);

        expectDisplayAssignedToUser(USER_NULL, INVALID_DISPLAY);
        expectNoDisplayAssignedToUser(USER_NULL);

        listener.verify();
    }
@@ -99,7 +99,8 @@ public final class UserVisibilityMediatorSUSDTest extends UserVisibilityMediator
    }

    @Test
    public void testStartBgProfile_onDefaultDisplay_whenParentIsCurrentUser() throws Exception {
    public void testStartVisibleBgProfile_onDefaultDisplay_whenParentIsCurrentUser()
            throws Exception {
        AsyncUserVisibilityListener listener = addListenerForEvents(
                onInvisible(INITIAL_CURRENT_USER_ID),
                onVisible(PARENT_USER_ID),
@@ -123,7 +124,7 @@ public final class UserVisibilityMediatorSUSDTest extends UserVisibilityMediator
    }

    @Test
    public void testStartBgUser_onSecondaryDisplay() throws Exception {
    public void testStartVisibleBgUser_onSecondaryDisplay() throws Exception {
        AsyncUserVisibilityListener listener = addListenerForNoEvents();

        int result = mMediator.assignUserToDisplayOnStart(USER_ID, USER_ID, BG_VISIBLE,
@@ -133,7 +134,7 @@ public final class UserVisibilityMediatorSUSDTest extends UserVisibilityMediator
        expectUserIsNotVisibleAtAll(USER_ID);
        expectNoDisplayAssignedToUser(USER_ID);

        expectNoUserAssignedToDisplay(SECONDARY_DISPLAY_ID);
        expectInitialCurrentUserAssignedToDisplay(SECONDARY_DISPLAY_ID);

        listener.verify();
    }
+26 −21
Original line number Diff line number Diff line
@@ -149,7 +149,7 @@ abstract class UserVisibilityMediatorTestCase extends ExtendedMockitoTestCase {

        expectUserIsNotVisibleAtAll(USER_ID);
        expectNoDisplayAssignedToUser(USER_ID);
        expectNoUserAssignedToDisplay(DEFAULT_DISPLAY);
        expectInitialCurrentUserAssignedToDisplay(DEFAULT_DISPLAY);

        listener.verify();
    }
@@ -163,13 +163,17 @@ abstract class UserVisibilityMediatorTestCase extends ExtendedMockitoTestCase {

        expectUserIsNotVisibleAtAll(USER_ID);
        expectNoDisplayAssignedToUser(USER_ID);
        expectNoUserAssignedToDisplay(DEFAULT_DISPLAY);
        expectInitialCurrentUserAssignedToDisplay(DEFAULT_DISPLAY);

        listener.verify();
    }

    @Test
    public final void testStartBgUser_onDefaultDisplay_visible() throws Exception {
    public final void testStartVisibleBgUser_onDefaultDisplay() throws Exception {
        visibleBgUserCannotBeStartedOnDefaultDisplayTest();
    }

    protected final void visibleBgUserCannotBeStartedOnDefaultDisplayTest() throws Exception {
        AsyncUserVisibilityListener listener = addListenerForNoEvents();

        int result = mMediator.assignUserToDisplayOnStart(USER_ID, USER_ID, BG_VISIBLE,
@@ -183,7 +187,7 @@ abstract class UserVisibilityMediatorTestCase extends ExtendedMockitoTestCase {
    }

    @Test
    public final void testStartBgUser_onSecondaryDisplay_invisible() throws Exception {
    public final void testStartBgUser_onSecondaryDisplay() throws Exception {
        AsyncUserVisibilityListener listener = addListenerForNoEvents();

        int result = mMediator.assignUserToDisplayOnStart(USER_ID, USER_ID, BG,
@@ -217,7 +221,7 @@ abstract class UserVisibilityMediatorTestCase extends ExtendedMockitoTestCase {
    }

    @Test
    public final void testStopVisibleProfile() throws Exception {
    public final void testStopVisibleBgProfile() throws Exception {
        AsyncUserVisibilityListener listener = addListenerForEvents(
                onInvisible(INITIAL_CURRENT_USER_ID),
                onVisible(PARENT_USER_ID),
@@ -235,7 +239,8 @@ abstract class UserVisibilityMediatorTestCase extends ExtendedMockitoTestCase {
    }

    @Test
    public final void testVisibleProfileBecomesInvisibleWhenParentIsSwitchedOut() throws Exception {
    public final void testVisibleBgProfileBecomesInvisibleWhenParentIsSwitchedOut()
            throws Exception {
        AsyncUserVisibilityListener listener = addListenerForEvents(
                onInvisible(INITIAL_CURRENT_USER_ID),
                onVisible(PARENT_USER_ID),
@@ -255,7 +260,7 @@ abstract class UserVisibilityMediatorTestCase extends ExtendedMockitoTestCase {
    }

    @Test
    public final void testStartBgProfile_onDefaultDisplay_whenParentIsNotStarted()
    public final void testStartVisibleBgProfile_onDefaultDisplay_whenParentIsNotStarted()
            throws Exception {
        AsyncUserVisibilityListener listener = addListenerForNoEvents();

@@ -270,7 +275,7 @@ abstract class UserVisibilityMediatorTestCase extends ExtendedMockitoTestCase {
    }

    @Test
    public final void testStartBgProfile_onDefaultDisplay_whenParentIsStartedOnBg()
    public final void testStartVisibleBgProfile_onDefaultDisplay_whenParentIsStartedOnBg()
            throws Exception {
        AsyncUserVisibilityListener listener = addListenerForNoEvents();
        startBackgroundUser(PARENT_USER_ID);
@@ -282,14 +287,14 @@ abstract class UserVisibilityMediatorTestCase extends ExtendedMockitoTestCase {
        expectUserIsNotVisibleAtAll(PROFILE_USER_ID);

        expectNoDisplayAssignedToUser(PROFILE_USER_ID);
        expectNoUserAssignedToDisplay(DEFAULT_DISPLAY);
        expectInitialCurrentUserAssignedToDisplay(DEFAULT_DISPLAY);

        listener.verify();
    }

    // Not supported - profiles can only be started on default display
    @Test
    public final void testStartBgProfile_onSecondaryDisplay() throws Exception {
    public final void testStartVisibleBgProfile_onSecondaryDisplay() throws Exception {
        AsyncUserVisibilityListener listener = addListenerForNoEvents();

        int result = mMediator.assignUserToDisplayOnStart(PROFILE_USER_ID, PARENT_USER_ID,
@@ -298,13 +303,13 @@ abstract class UserVisibilityMediatorTestCase extends ExtendedMockitoTestCase {

        expectUserIsNotVisibleAtAll(PROFILE_USER_ID);
        expectNoDisplayAssignedToUser(PROFILE_USER_ID);
        expectNoUserAssignedToDisplay(SECONDARY_DISPLAY_ID);
        expectInitialCurrentUserAssignedToDisplay(SECONDARY_DISPLAY_ID);

        listener.verify();
    }

    @Test
    public final void testStartBgProfile_onSecondaryDisplay_invisible() throws Exception {
    public final void testStartBgProfile_onSecondaryDisplay() throws Exception {
        AsyncUserVisibilityListener listener = addListenerForNoEvents();

        int result = mMediator.assignUserToDisplayOnStart(PROFILE_USER_ID, PARENT_USER_ID, BG,
@@ -313,7 +318,7 @@ abstract class UserVisibilityMediatorTestCase extends ExtendedMockitoTestCase {

        expectUserIsNotVisibleAtAll(PROFILE_USER_ID);
        expectNoDisplayAssignedToUser(PROFILE_USER_ID);
        expectNoUserAssignedToDisplay(SECONDARY_DISPLAY_ID);
        expectInitialCurrentUserAssignedToDisplay(SECONDARY_DISPLAY_ID);

        listener.verify();
    }
@@ -329,7 +334,7 @@ abstract class UserVisibilityMediatorTestCase extends ExtendedMockitoTestCase {
        expectUserIsNotVisibleAtAll(PROFILE_USER_ID);

        expectNoDisplayAssignedToUser(PROFILE_USER_ID);
        expectNoUserAssignedToDisplay(DEFAULT_DISPLAY);
        expectInitialCurrentUserAssignedToDisplay(DEFAULT_DISPLAY);

        listener.verify();
    }
@@ -344,7 +349,7 @@ abstract class UserVisibilityMediatorTestCase extends ExtendedMockitoTestCase {

        expectUserIsNotVisibleAtAll(PROFILE_USER_ID);
        expectNoDisplayAssignedToUser(PROFILE_USER_ID);
        expectNoUserAssignedToDisplay(SECONDARY_DISPLAY_ID);
        expectInitialCurrentUserAssignedToDisplay(SECONDARY_DISPLAY_ID);

        listener.verify();
    }
@@ -477,7 +482,7 @@ abstract class UserVisibilityMediatorTestCase extends ExtendedMockitoTestCase {
    }

    protected void expectUserIsVisible(@UserIdInt int userId) {
        expectWithMessage("mediator.isUserVisible(%s)", userId)
        expectWithMessage("isUserVisible(%s)", userId)
                .that(mMediator.isUserVisible(userId))
                .isTrue();
    }
@@ -490,13 +495,13 @@ abstract class UserVisibilityMediatorTestCase extends ExtendedMockitoTestCase {
    }

    protected void expectUserIsVisibleOnDisplay(@UserIdInt int userId, int displayId) {
        expectWithMessage("mediator.isUserVisible(%s, %s)", userId, displayId)
        expectWithMessage("isUserVisible(%s, %s)", userId, displayId)
                .that(mMediator.isUserVisible(userId, displayId))
                .isTrue();
    }

    protected void expectUserIsNotVisibleOnDisplay(@UserIdInt int userId, int displayId) {
        expectWithMessage("mediator.isUserVisible(%s, %s)", userId, displayId)
        expectWithMessage("isUserVisible(%s, %s)", userId, displayId)
                .that(mMediator.isUserVisible(userId, displayId))
                .isFalse();
    }
@@ -504,13 +509,13 @@ abstract class UserVisibilityMediatorTestCase extends ExtendedMockitoTestCase {
    protected void expectUserIsNotVisibleOnDisplay(String when, @UserIdInt int userId,
            int displayId) {
        String suffix = TextUtils.isEmpty(when) ? "" : " on " + when;
        expectWithMessage("mediator.isUserVisible(%s, %s)%s", userId, displayId, suffix)
        expectWithMessage("isUserVisible(%s, %s)%s", userId, displayId, suffix)
                .that(mMediator.isUserVisible(userId, displayId))
                .isFalse();
    }

    protected void expectUserIsNotVisibleAtAll(@UserIdInt int userId) {
        expectWithMessage("mediator.isUserVisible(%s)", userId)
        expectWithMessage("isUserVisible(%s)", userId)
                .that(mMediator.isUserVisible(userId))
                .isFalse();
        expectUserIsNotVisibleOnDisplay(userId, DEFAULT_DISPLAY);
@@ -534,7 +539,7 @@ abstract class UserVisibilityMediatorTestCase extends ExtendedMockitoTestCase {
                .that(mMediator.getUserAssignedToDisplay(displayId)).isEqualTo(userId);
    }

    protected void expectNoUserAssignedToDisplay(int displayId) {
    protected void expectInitialCurrentUserAssignedToDisplay(int displayId) {
        expectWithMessage("getUserAssignedToDisplay(%s)", displayId)
                .that(mMediator.getUserAssignedToDisplay(displayId))
                .isEqualTo(INITIAL_CURRENT_USER_ID);
+280 −0

File added.

Preview size limit exceeded, changes collapsed.