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.car.userlib.CarUserManagerHelper; 20 import android.car.userlib.CarUserManagerHelper.OnUsersUpdateListener; 21 import android.content.pm.UserInfo; 22 23 import org.robolectric.annotation.Implementation; 24 import org.robolectric.annotation.Implements; 25 import org.robolectric.annotation.Resetter; 26 27 import java.util.HashMap; 28 import java.util.List; 29 import java.util.Map; 30 31 /** 32 * Shadow for {@link CarUserManagerHelper} 33 */ 34 @Implements(CarUserManagerHelper.class) 35 public class ShadowCarUserManagerHelper { 36 // First Map keys from user id to map of restrictions. Second Map keys from restriction id to 37 // bool. 38 private static Map<Integer, Map<String, Boolean>> sUserRestrictionMap = new HashMap<>(); 39 private static CarUserManagerHelper sMockInstance; 40 setMockInstance(CarUserManagerHelper userManagerHelper)41 public static void setMockInstance(CarUserManagerHelper userManagerHelper) { 42 sMockInstance = userManagerHelper; 43 } 44 45 @Resetter reset()46 public static void reset() { 47 sMockInstance = null; 48 sUserRestrictionMap.clear(); 49 } 50 51 @Implementation setUserName(UserInfo user, String name)52 protected void setUserName(UserInfo user, String name) { 53 sMockInstance.setUserName(user, name); 54 } 55 56 @Implementation getCurrentProcessUserInfo()57 protected UserInfo getCurrentProcessUserInfo() { 58 return sMockInstance.getCurrentProcessUserInfo(); 59 } 60 61 @Implementation getCurrentForegroundUserInfo()62 protected UserInfo getCurrentForegroundUserInfo() { 63 return sMockInstance.getCurrentForegroundUserInfo(); 64 } 65 66 @Implementation getCurrentProcessUserId()67 protected int getCurrentProcessUserId() { 68 return sMockInstance.getCurrentProcessUserId(); 69 } 70 71 @Implementation isCurrentProcessUser(UserInfo userInfo)72 protected boolean isCurrentProcessUser(UserInfo userInfo) { 73 return sMockInstance.isCurrentProcessUser(userInfo); 74 } 75 76 @Implementation getAllSwitchableUsers()77 protected List<UserInfo> getAllSwitchableUsers() { 78 return sMockInstance.getAllSwitchableUsers(); 79 } 80 81 @Implementation getAllUsers()82 protected List<UserInfo> getAllUsers() { 83 return sMockInstance.getAllUsers(); 84 } 85 86 @Implementation getAllPersistentUsers()87 protected List<UserInfo> getAllPersistentUsers() { 88 return sMockInstance.getAllPersistentUsers(); 89 } 90 91 @Implementation createNewNonAdminUser(String userName)92 protected UserInfo createNewNonAdminUser(String userName) { 93 return sMockInstance.createNewNonAdminUser(userName); 94 } 95 96 @Implementation registerOnUsersUpdateListener(OnUsersUpdateListener listener)97 protected void registerOnUsersUpdateListener(OnUsersUpdateListener listener) { 98 sMockInstance.registerOnUsersUpdateListener(listener); 99 } 100 101 @Implementation unregisterOnUsersUpdateListener(OnUsersUpdateListener listener)102 protected void unregisterOnUsersUpdateListener(OnUsersUpdateListener listener) { 103 sMockInstance.unregisterOnUsersUpdateListener(listener); 104 } 105 106 @Implementation isUserLimitReached()107 protected boolean isUserLimitReached() { 108 return sMockInstance.isUserLimitReached(); 109 } 110 111 @Implementation canCurrentProcessModifyAccounts()112 protected boolean canCurrentProcessModifyAccounts() { 113 return sMockInstance.canCurrentProcessModifyAccounts(); 114 } 115 116 @Implementation canCurrentProcessAddUsers()117 protected boolean canCurrentProcessAddUsers() { 118 return sMockInstance.canCurrentProcessAddUsers(); 119 } 120 121 @Implementation getMaxSupportedRealUsers()122 protected int getMaxSupportedRealUsers() { 123 return sMockInstance.getMaxSupportedRealUsers(); 124 } 125 126 @Implementation canCurrentProcessRemoveUsers()127 protected boolean canCurrentProcessRemoveUsers() { 128 return sMockInstance.canCurrentProcessRemoveUsers(); 129 } 130 131 @Implementation canUserBeRemoved(UserInfo userInfo)132 protected boolean canUserBeRemoved(UserInfo userInfo) { 133 return sMockInstance.canUserBeRemoved(userInfo); 134 } 135 136 @Implementation grantAdminPermissions(UserInfo user)137 protected void grantAdminPermissions(UserInfo user) { 138 sMockInstance.grantAdminPermissions(user); 139 } 140 141 @Implementation isCurrentProcessDemoUser()142 protected boolean isCurrentProcessDemoUser() { 143 return sMockInstance.isCurrentProcessDemoUser(); 144 } 145 146 @Implementation isCurrentProcessAdminUser()147 protected boolean isCurrentProcessAdminUser() { 148 return sMockInstance.isCurrentProcessAdminUser(); 149 } 150 151 @Implementation isCurrentProcessGuestUser()152 protected boolean isCurrentProcessGuestUser() { 153 return sMockInstance.isCurrentProcessGuestUser(); 154 } 155 156 @Implementation isCurrentProcessUserHasRestriction(String restriction)157 protected boolean isCurrentProcessUserHasRestriction(String restriction) { 158 return sMockInstance.isCurrentProcessUserHasRestriction(restriction); 159 } 160 161 @Implementation removeUser(UserInfo userInfo, String guestUserName)162 protected boolean removeUser(UserInfo userInfo, String guestUserName) { 163 return sMockInstance.removeUser(userInfo, guestUserName); 164 } 165 166 @Implementation setUserRestriction(UserInfo userInfo, String restriction, boolean enable)167 protected void setUserRestriction(UserInfo userInfo, String restriction, boolean enable) { 168 Map<String, Boolean> permissionsMap = sUserRestrictionMap.getOrDefault(userInfo.id, 169 new HashMap<>()); 170 permissionsMap.put(restriction, enable); 171 sUserRestrictionMap.put(userInfo.id, permissionsMap); 172 } 173 174 @Implementation hasUserRestriction(String restriction, UserInfo userInfo)175 protected boolean hasUserRestriction(String restriction, UserInfo userInfo) { 176 // False by default, if nothing was added to our map, 177 if (!sUserRestrictionMap.containsKey(userInfo.id)) { 178 return false; 179 } 180 return sUserRestrictionMap.get(userInfo.id).getOrDefault(restriction, false); 181 } 182 } 183