1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.pm; 18 19 import static android.view.Display.DEFAULT_DISPLAY; 20 import static android.view.Display.INVALID_DISPLAY; 21 22 import static com.android.server.pm.UserManagerInternal.USER_ASSIGNMENT_RESULT_FAILURE; 23 import static com.android.server.pm.UserManagerInternal.USER_ASSIGNMENT_RESULT_SUCCESS_ALREADY_VISIBLE; 24 import static com.android.server.pm.UserManagerInternal.USER_ASSIGNMENT_RESULT_SUCCESS_INVISIBLE; 25 import static com.android.server.pm.UserManagerInternal.USER_ASSIGNMENT_RESULT_SUCCESS_VISIBLE; 26 import static com.android.server.pm.UserVisibilityChangedEvent.onInvisible; 27 import static com.android.server.pm.UserVisibilityChangedEvent.onVisible; 28 import static com.android.server.pm.UserVisibilityMediator.INITIAL_CURRENT_USER_ID; 29 30 import org.junit.Test; 31 32 /** 33 * Tests for {@link UserVisibilityMediator} tests for devices that support not only concurrent 34 * Multiple Users on Multiple Displays, but also let background users to be visible in the default 35 * display (A.K.A {@code MUPAND} - MUltiple Passengers, No Driver). 36 * 37 * <p> Run as {@code 38 * atest FrameworksMockingServicesTests:com.android.server.pm.UserVisibilityMediatorMUPANDTest} 39 */ 40 public final class UserVisibilityMediatorMUPANDTest 41 extends UserVisibilityMediatorVisibleBackgroundUserTestCase { 42 UserVisibilityMediatorMUPANDTest()43 public UserVisibilityMediatorMUPANDTest() throws Exception { 44 super(/* backgroundUsersOnDisplaysEnabled= */ true, 45 /* backgroundUserOnDefaultDisplayAllowed= */ true); 46 } 47 48 @Test testStartVisibleBgUser_onDefaultDisplay_initialCurrentUserId()49 public void testStartVisibleBgUser_onDefaultDisplay_initialCurrentUserId() 50 throws Exception { 51 int currentUserId = INITIAL_CURRENT_USER_ID; 52 int visibleBgUserId = USER_ID; 53 int otherUserId = OTHER_USER_ID; 54 55 AsyncUserVisibilityListener listener = addListenerForEvents(onVisible(visibleBgUserId)); 56 57 int result = mMediator.assignUserToDisplayOnStart(visibleBgUserId, visibleBgUserId, 58 BG_VISIBLE, DEFAULT_DISPLAY); 59 assertStartUserResult(result, USER_ASSIGNMENT_RESULT_SUCCESS_VISIBLE); 60 expectVisibleUsers(currentUserId, visibleBgUserId); 61 62 // Assert bg user visibility 63 expectUserIsVisible(visibleBgUserId); 64 expectUserIsVisibleOnDisplay(visibleBgUserId, DEFAULT_DISPLAY); 65 expectUserIsNotVisibleOnDisplay(visibleBgUserId, INVALID_DISPLAY); 66 expectDisplayAssignedToUser(visibleBgUserId, DEFAULT_DISPLAY); 67 expectUserAssignedToDisplay(DEFAULT_DISPLAY, visibleBgUserId); 68 69 // Assert current user visibility 70 expectUserIsVisible(currentUserId); 71 expectUserIsVisibleOnDisplay(currentUserId, DEFAULT_DISPLAY); 72 expectUserIsNotVisibleOnDisplay(currentUserId, INVALID_DISPLAY); 73 expectDisplayAssignedToUser(currentUserId, INVALID_DISPLAY); 74 75 assertUserCanBeAssignedExtraDisplay(USER_ID, OTHER_SECONDARY_DISPLAY_ID); 76 77 // Make sure another user cannot be started on default display 78 int result2 = mMediator.assignUserToDisplayOnStart(otherUserId, otherUserId, BG_VISIBLE, 79 DEFAULT_DISPLAY); 80 assertStartUserResult(result2, USER_ASSIGNMENT_RESULT_FAILURE, 81 "when user (%d) is starting on default display after it was started by user %d", 82 otherUserId, visibleBgUserId); 83 expectVisibleUsers(currentUserId, visibleBgUserId); 84 85 listener.verify(); 86 } 87 88 @Test testStartVisibleBgUser_onDefaultDisplay_nonInitialCurrentUserId()89 public void testStartVisibleBgUser_onDefaultDisplay_nonInitialCurrentUserId() 90 throws Exception { 91 int currentUserId = OTHER_USER_ID; 92 int visibleBgUserId = USER_ID; 93 int otherUserId = YET_ANOTHER_USER_ID; 94 95 AsyncUserVisibilityListener listener = addListenerForEvents( 96 onInvisible(INITIAL_CURRENT_USER_ID), 97 onVisible(currentUserId), 98 onVisible(visibleBgUserId)); 99 startForegroundUser(currentUserId); 100 101 int result = mMediator.assignUserToDisplayOnStart(visibleBgUserId, visibleBgUserId, 102 BG_VISIBLE, DEFAULT_DISPLAY); 103 assertStartUserResult(result, USER_ASSIGNMENT_RESULT_SUCCESS_VISIBLE); 104 expectVisibleUsers(currentUserId, visibleBgUserId); 105 106 // Assert bg user visibility 107 expectUserIsVisible(visibleBgUserId); 108 expectUserIsVisibleOnDisplay(visibleBgUserId, DEFAULT_DISPLAY); 109 expectUserIsNotVisibleOnDisplay(visibleBgUserId, INVALID_DISPLAY); 110 expectDisplayAssignedToUser(visibleBgUserId, DEFAULT_DISPLAY); 111 expectUserAssignedToDisplay(DEFAULT_DISPLAY, visibleBgUserId); 112 113 // Assert current user visibility 114 expectUserIsVisible(currentUserId); 115 expectUserIsVisibleOnDisplay(currentUserId, DEFAULT_DISPLAY); 116 expectUserIsNotVisibleOnDisplay(currentUserId, INVALID_DISPLAY); 117 expectDisplayAssignedToUser(currentUserId, INVALID_DISPLAY); 118 119 assertUserCanBeAssignedExtraDisplay(USER_ID, OTHER_SECONDARY_DISPLAY_ID); 120 121 // Make sure another user cannot be started on default display 122 int result2 = mMediator.assignUserToDisplayOnStart(otherUserId, otherUserId, BG_VISIBLE, 123 DEFAULT_DISPLAY); 124 assertStartUserResult(result2, USER_ASSIGNMENT_RESULT_FAILURE, 125 "when user (%d) is starting on default display after it was started by user %d", 126 otherUserId, visibleBgUserId); 127 expectVisibleUsers(currentUserId, visibleBgUserId); 128 129 listener.verify(); 130 } 131 132 @Test 133 public void testStartVisibleBgProfile_onDefaultDisplay_whenParentIsStartedVisibleOnBgOnSecondaryDisplay()134 testStartVisibleBgProfile_onDefaultDisplay_whenParentIsStartedVisibleOnBgOnSecondaryDisplay() 135 throws Exception { 136 AsyncUserVisibilityListener listener = addListenerForEvents(onVisible(PARENT_USER_ID)); 137 startUserInSecondaryDisplay(PARENT_USER_ID, OTHER_SECONDARY_DISPLAY_ID); 138 139 int result = mMediator.assignUserToDisplayOnStart(PROFILE_USER_ID, PARENT_USER_ID, 140 BG_VISIBLE, DEFAULT_DISPLAY); 141 assertStartUserResult(result, USER_ASSIGNMENT_RESULT_FAILURE); 142 143 expectUserIsNotVisibleAtAll(PROFILE_USER_ID); 144 expectNoDisplayAssignedToUser(PROFILE_USER_ID); 145 expectUserAssignedToDisplay(OTHER_SECONDARY_DISPLAY_ID, PARENT_USER_ID); 146 147 assertInvisibleUserCannotBeAssignedExtraDisplay(PROFILE_USER_ID, SECONDARY_DISPLAY_ID); 148 149 listener.verify(); 150 } 151 152 @Test 153 public void testStartVisibleBgProfile_onDefaultDisplay_whenParentIsStartedVisibleOnBgOnDefaultDisplay()154 testStartVisibleBgProfile_onDefaultDisplay_whenParentIsStartedVisibleOnBgOnDefaultDisplay() 155 throws Exception { 156 AsyncUserVisibilityListener listener = addListenerForEvents( 157 onVisible(PARENT_USER_ID), 158 onVisible(PROFILE_USER_ID)); 159 startUserInSecondaryDisplay(PARENT_USER_ID, DEFAULT_DISPLAY); 160 161 int result = mMediator.assignUserToDisplayOnStart(PROFILE_USER_ID, PARENT_USER_ID, 162 BG_VISIBLE, DEFAULT_DISPLAY); 163 assertStartUserResult(result, USER_ASSIGNMENT_RESULT_SUCCESS_VISIBLE); 164 165 // Assert parent user visibility 166 expectUserIsVisible(PARENT_USER_ID); 167 expectUserIsVisibleOnDisplay(PARENT_USER_ID, DEFAULT_DISPLAY); 168 expectUserIsNotVisibleOnDisplay(PARENT_USER_ID, INVALID_DISPLAY); 169 expectDisplayAssignedToUser(PARENT_USER_ID, DEFAULT_DISPLAY); 170 expectUserAssignedToDisplay(DEFAULT_DISPLAY, PARENT_USER_ID); 171 assertUserCanBeAssignedExtraDisplay(PARENT_USER_ID, OTHER_SECONDARY_DISPLAY_ID); 172 173 // Assert profile user visibility 174 expectUserIsVisible(PROFILE_USER_ID); 175 expectUserIsVisibleOnDisplay(PROFILE_USER_ID, DEFAULT_DISPLAY); 176 expectUserIsNotVisibleOnDisplay(PROFILE_USER_ID, INVALID_DISPLAY); 177 // Only full user (parent) is assigned to the display 178 expectDisplayAssignedToUser(PROFILE_USER_ID, INVALID_DISPLAY); 179 assertUserCannotBeAssignedExtraDisplay(PROFILE_USER_ID, OTHER_SECONDARY_DISPLAY_ID); 180 181 listener.verify(); 182 } 183 184 @Test testStartBgProfile_onDefaultDisplay_whenParentIsStartedVisibleOnBg()185 public void testStartBgProfile_onDefaultDisplay_whenParentIsStartedVisibleOnBg() 186 throws Exception { 187 AsyncUserVisibilityListener listener = addListenerForEvents(onVisible(PARENT_USER_ID)); 188 startUserInSecondaryDisplay(PARENT_USER_ID, DEFAULT_DISPLAY); 189 190 int result = mMediator.assignUserToDisplayOnStart(PROFILE_USER_ID, PARENT_USER_ID, BG, 191 DEFAULT_DISPLAY); 192 assertStartUserResult(result, USER_ASSIGNMENT_RESULT_SUCCESS_INVISIBLE); 193 194 // Assert parent user visibility 195 expectUserIsVisible(PARENT_USER_ID); 196 expectUserIsVisibleOnDisplay(PARENT_USER_ID, DEFAULT_DISPLAY); 197 expectUserIsNotVisibleOnDisplay(PARENT_USER_ID, INVALID_DISPLAY); 198 expectDisplayAssignedToUser(PARENT_USER_ID, DEFAULT_DISPLAY); 199 expectUserAssignedToDisplay(DEFAULT_DISPLAY, PARENT_USER_ID); 200 assertUserCanBeAssignedExtraDisplay(PARENT_USER_ID, OTHER_SECONDARY_DISPLAY_ID); 201 202 // Assert profile user visibility 203 expectUserIsNotVisibleAtAll(PROFILE_USER_ID); 204 expectNoDisplayAssignedToUser(PROFILE_USER_ID); 205 assertUserCannotBeAssignedExtraDisplay(PROFILE_USER_ID, OTHER_SECONDARY_DISPLAY_ID); 206 listener.verify(); 207 } 208 209 @Test testStartVisibleBgUser_onDefaultDisplay_currentUserId()210 public void testStartVisibleBgUser_onDefaultDisplay_currentUserId() throws Exception { 211 int currentUserId = INITIAL_CURRENT_USER_ID; 212 213 AsyncUserVisibilityListener listener = addListenerForNoEvents(); 214 215 int result = mMediator.assignUserToDisplayOnStart(currentUserId, currentUserId, 216 BG_VISIBLE, DEFAULT_DISPLAY); 217 assertStartUserResult(result, USER_ASSIGNMENT_RESULT_SUCCESS_ALREADY_VISIBLE); 218 219 // Assert current user visibility 220 expectUserIsVisible(currentUserId); 221 expectUserIsVisibleOnDisplay(currentUserId, DEFAULT_DISPLAY); 222 expectUserIsNotVisibleOnDisplay(currentUserId, INVALID_DISPLAY); 223 expectDisplayAssignedToUser(currentUserId, DEFAULT_DISPLAY); 224 225 assertUserCanBeAssignedExtraDisplay(currentUserId, OTHER_SECONDARY_DISPLAY_ID); 226 227 listener.verify(); 228 } 229 } 230