• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.car.settings.testutils;
18 
19 import android.annotation.UserIdInt;
20 import android.content.pm.UserInfo;
21 import android.graphics.Bitmap;
22 import android.os.UserHandle;
23 import android.os.UserManager;
24 import android.util.ArrayMap;
25 
26 import org.robolectric.annotation.Implementation;
27 import org.robolectric.annotation.Implements;
28 import org.robolectric.annotation.Resetter;
29 
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Map;
34 
35 @Implements(UserManager.class)
36 public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager {
37 
38     private static boolean sIsHeadlessSystemUserMode = true;
39     private static boolean sCanAddMoreUsers = true;
40     private static Map<Integer, List<UserInfo>> sProfiles = new ArrayMap<>();
41     private static Map<Integer, Bitmap> sUserIcons = new ArrayMap<>();
42     private static int sMaxSupportedUsers = 10;
43 
44     @Implementation
getProfileIdsWithDisabled(int userId)45     protected int[] getProfileIdsWithDisabled(int userId) {
46         if (sProfiles.containsKey(userId)) {
47             return sProfiles.get(userId).stream().mapToInt(userInfo -> userInfo.id).toArray();
48         }
49         return new int[]{};
50     }
51 
52     @Implementation
getProfiles(int userHandle)53     protected List<UserInfo> getProfiles(int userHandle) {
54         if (sProfiles.containsKey(userHandle)) {
55             return new ArrayList<>(sProfiles.get(userHandle));
56         }
57         return Collections.emptyList();
58     }
59 
60     /** Adds a profile to be returned by {@link #getProfiles(int)}. **/
addProfile( int userHandle, int profileUserHandle, String profileName, int profileFlags)61     public void addProfile(
62             int userHandle, int profileUserHandle, String profileName, int profileFlags) {
63         sProfiles.putIfAbsent(userHandle, new ArrayList<>());
64         sProfiles.get(userHandle).add(new UserInfo(profileUserHandle, profileName, profileFlags));
65     }
66 
67     @Implementation
setUserRestriction(String key, boolean value, UserHandle userHandle)68     protected void setUserRestriction(String key, boolean value, UserHandle userHandle) {
69         setUserRestriction(userHandle, key, value);
70     }
71 
72     @Implementation
getUsers(boolean excludeDying)73     protected List<UserInfo> getUsers(boolean excludeDying) {
74         return super.getUsers();
75     }
76 
77     @Implementation
isHeadlessSystemUserMode()78     protected static boolean isHeadlessSystemUserMode() {
79         return sIsHeadlessSystemUserMode;
80     }
81 
setIsHeadlessSystemUserMode(boolean isEnabled)82     public static void setIsHeadlessSystemUserMode(boolean isEnabled) {
83         sIsHeadlessSystemUserMode = isEnabled;
84     }
85 
86     @Implementation
canAddMoreUsers()87     protected static boolean canAddMoreUsers() {
88         return sCanAddMoreUsers;
89     }
90 
setCanAddMoreUsers(boolean isEnabled)91     public static void setCanAddMoreUsers(boolean isEnabled) {
92         sCanAddMoreUsers = isEnabled;
93     }
94 
95     @Implementation
getUserIcon(@serIdInt int userId)96     public Bitmap getUserIcon(@UserIdInt int userId) {
97         return sUserIcons.get(userId);
98     }
99 
setUserIcon(@serIdInt int userId, Bitmap icon)100     public static void setUserIcon(@UserIdInt int userId, Bitmap icon) {
101         sUserIcons.put(userId, icon);
102     }
103 
104     @Implementation
getMaxSupportedUsers()105     public static int getMaxSupportedUsers() {
106         return sMaxSupportedUsers;
107     }
108 
setMaxSupportedUsers(int maxSupportedUsers)109     public static void setMaxSupportedUsers(int maxSupportedUsers) {
110         sMaxSupportedUsers = maxSupportedUsers;
111     }
112 
113     @Resetter
reset()114     public static void reset() {
115         org.robolectric.shadows.ShadowUserManager.reset();
116         sIsHeadlessSystemUserMode = true;
117         sCanAddMoreUsers = true;
118         sProfiles.clear();
119         sUserIcons.clear();
120     }
121 }
122