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 package com.android.server.pm;
17 
18 import static android.os.UserHandle.USER_NULL;
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_VISIBLE;
24 import static com.android.server.pm.UserVisibilityChangedEvent.onInvisible;
25 import static com.android.server.pm.UserVisibilityChangedEvent.onVisible;
26 import static com.android.server.pm.UserVisibilityMediator.INITIAL_CURRENT_USER_ID;
27 
28 import org.junit.Test;
29 
30 /**
31  * Tests for {@link UserVisibilityMediator} tests for devices that DO NOT support concurrent
32  * multiple users on multiple displays (A.K.A {@code SUSD} - Single User on Single Device).
33  *
34  * <p>Run as
35  * {@code atest FrameworksMockingServicesTests:com.android.server.pm.UserVisibilityMediatorSUSDTest}
36  */
37 public final class UserVisibilityMediatorSUSDTest extends UserVisibilityMediatorTestCase {
38 
UserVisibilityMediatorSUSDTest()39     public UserVisibilityMediatorSUSDTest() {
40         super(/* backgroundUsersOnDisplaysEnabled= */ false,
41                 /* backgroundUserOnDefaultDisplayAllowed= */ false);
42     }
43 
44     @Test
testStartVisibleBgUser_onDefaultDisplay()45     public void testStartVisibleBgUser_onDefaultDisplay() throws Exception {
46         int userId = visibleBgUserCannotBeStartedOnDefaultDisplayTest();
47 
48         assertInvisibleUserCannotBeAssignedExtraDisplay(userId, SECONDARY_DISPLAY_ID);
49     }
50 
51     @Test
testStartFgUser_onDefaultDisplay()52     public void testStartFgUser_onDefaultDisplay() throws Exception {
53         AsyncUserVisibilityListener listener = addListenerForEvents(
54                 onInvisible(INITIAL_CURRENT_USER_ID),
55                 onVisible(USER_ID));
56 
57         int result = mMediator.assignUserToDisplayOnStart(USER_ID, USER_ID, FG,
58                 DEFAULT_DISPLAY);
59         assertStartUserResult(result, USER_ASSIGNMENT_RESULT_SUCCESS_VISIBLE);
60         expectUserCannotBeUnassignedFromDisplay(USER_ID, DEFAULT_DISPLAY);
61 
62         expectUserIsVisible(USER_ID);
63         expectUserIsNotVisibleOnDisplay(USER_ID, INVALID_DISPLAY);
64         expectUserIsVisibleOnDisplay(USER_ID, DEFAULT_DISPLAY);
65         expectUserIsVisibleOnDisplay(USER_ID, SECONDARY_DISPLAY_ID);
66         expectVisibleUsers(USER_ID);
67 
68         expectDisplayAssignedToUser(USER_ID, DEFAULT_DISPLAY);
69         expectUserAssignedToDisplay(DEFAULT_DISPLAY, USER_ID);
70         expectUserAssignedToDisplay(INVALID_DISPLAY, USER_ID);
71         expectUserAssignedToDisplay(SECONDARY_DISPLAY_ID, USER_ID);
72 
73         expectNoDisplayAssignedToUser(USER_NULL);
74 
75         listener.verify();
76     }
77 
78     @Test
testSwitchFgUser_onDefaultDisplay()79     public void testSwitchFgUser_onDefaultDisplay() throws Exception {
80         int previousCurrentUserId = OTHER_USER_ID;
81         int currentUserId = USER_ID;
82         AsyncUserVisibilityListener listener = addListenerForEvents(
83                 onInvisible(INITIAL_CURRENT_USER_ID),
84                 onVisible(previousCurrentUserId),
85                 onInvisible(previousCurrentUserId),
86                 onVisible(currentUserId));
87         startForegroundUser(previousCurrentUserId);
88 
89         int result = mMediator.assignUserToDisplayOnStart(currentUserId, currentUserId, FG,
90                 DEFAULT_DISPLAY);
91         assertStartUserResult(result, USER_ASSIGNMENT_RESULT_SUCCESS_VISIBLE);
92         expectUserCannotBeUnassignedFromDisplay(currentUserId, DEFAULT_DISPLAY);
93 
94         expectUserIsVisible(currentUserId);
95         expectUserIsNotVisibleOnDisplay(currentUserId, INVALID_DISPLAY);
96         expectUserIsVisibleOnDisplay(currentUserId, DEFAULT_DISPLAY);
97         expectUserIsVisibleOnDisplay(currentUserId, SECONDARY_DISPLAY_ID);
98         expectVisibleUsers(currentUserId);
99 
100         expectDisplayAssignedToUser(currentUserId, DEFAULT_DISPLAY);
101         expectUserAssignedToDisplay(DEFAULT_DISPLAY, currentUserId);
102         expectUserAssignedToDisplay(INVALID_DISPLAY, currentUserId);
103         expectUserAssignedToDisplay(SECONDARY_DISPLAY_ID, currentUserId);
104 
105         expectUserIsNotVisibleAtAll(previousCurrentUserId);
106         expectNoDisplayAssignedToUser(previousCurrentUserId);
107 
108         listener.verify();
109     }
110 
111     @Test
testStartBgUser_onDefaultDisplay_visible()112     public void testStartBgUser_onDefaultDisplay_visible() throws Exception {
113         visibleBgUserCannotBeStartedOnDefaultDisplayTest();
114     }
115 
116     @Test
testStartVisibleBgProfile_onDefaultDisplay_whenParentIsCurrentUser()117     public void testStartVisibleBgProfile_onDefaultDisplay_whenParentIsCurrentUser()
118             throws Exception {
119         AsyncUserVisibilityListener listener = addListenerForEvents(
120                 onInvisible(INITIAL_CURRENT_USER_ID),
121                 onVisible(PARENT_USER_ID),
122                 onVisible(PROFILE_USER_ID));
123         startForegroundUser(PARENT_USER_ID);
124 
125         int result = mMediator.assignUserToDisplayOnStart(PROFILE_USER_ID, PARENT_USER_ID,
126                 BG_VISIBLE, DEFAULT_DISPLAY);
127         assertStartUserResult(result, USER_ASSIGNMENT_RESULT_SUCCESS_VISIBLE);
128         expectUserCannotBeUnassignedFromDisplay(PROFILE_USER_ID, DEFAULT_DISPLAY);
129 
130         expectUserIsVisible(PROFILE_USER_ID);
131         expectUserIsNotVisibleOnDisplay(PROFILE_USER_ID, INVALID_DISPLAY);
132         expectUserIsVisibleOnDisplay(PROFILE_USER_ID, DEFAULT_DISPLAY);
133         expectUserIsVisibleOnDisplay(PROFILE_USER_ID, SECONDARY_DISPLAY_ID);
134         expectVisibleUsers(PARENT_USER_ID, PROFILE_USER_ID);
135 
136         expectDisplayAssignedToUser(PROFILE_USER_ID, DEFAULT_DISPLAY);
137         expectUserAssignedToDisplay(DEFAULT_DISPLAY, PARENT_USER_ID);
138 
139         listener.verify();
140     }
141 
142     @Test
testStartVisibleBgUser_onSecondaryDisplay()143     public void testStartVisibleBgUser_onSecondaryDisplay() throws Exception {
144         AsyncUserVisibilityListener listener = addListenerForNoEvents();
145 
146         int result = mMediator.assignUserToDisplayOnStart(USER_ID, USER_ID, BG_VISIBLE,
147                 SECONDARY_DISPLAY_ID);
148         assertStartUserResult(result, USER_ASSIGNMENT_RESULT_FAILURE);
149 
150         expectUserIsNotVisibleAtAll(USER_ID);
151         expectNoDisplayAssignedToUser(USER_ID);
152 
153         expectInitialCurrentUserAssignedToDisplay(SECONDARY_DISPLAY_ID);
154 
155         listener.verify();
156     }
157 }
158