blob: b7c502db012942b9c4b14f0d7ff4696226075415 [file] [log] [blame]
Heemin Seog5f1ec272018-11-13 10:20:49 -08001/*
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
17package com.android.car.settings.testutils;
18
19import android.annotation.UserIdInt;
20import android.content.pm.UserInfo;
Anthony Hugh492f1842019-07-11 13:50:15 -070021import android.os.UserHandle;
Heemin Seog5f1ec272018-11-13 10:20:49 -080022import android.os.UserManager;
davidln086f1ec2019-02-06 10:45:25 -080023import android.util.ArrayMap;
Heemin Seog5f1ec272018-11-13 10:20:49 -080024
25import org.robolectric.annotation.Implementation;
26import org.robolectric.annotation.Implements;
27import org.robolectric.annotation.Resetter;
28
davidln086f1ec2019-02-06 10:45:25 -080029import java.util.ArrayList;
30import java.util.Collections;
31import java.util.List;
32import java.util.Map;
33
Heemin Seog5f1ec272018-11-13 10:20:49 -080034@Implements(UserManager.class)
35public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager {
36 private static UserManager sInstance;
37
davidln086f1ec2019-02-06 10:45:25 -080038 private Map<Integer, List<UserInfo>> mProfiles = new ArrayMap<>();
39
Heemin Seog5f1ec272018-11-13 10:20:49 -080040 public static void setInstance(UserManager manager) {
41 sInstance = manager;
42 }
43
44 @Implementation
45 protected UserInfo getUserInfo(@UserIdInt int userHandle) {
46 return sInstance.getUserInfo(userHandle);
47 }
48
davidln086f1ec2019-02-06 10:45:25 -080049 @Implementation
davidlnafe538d2019-02-06 15:58:57 -080050 protected int[] getProfileIdsWithDisabled(int userId) {
51 if (mProfiles.containsKey(userId)) {
52 return mProfiles.get(userId).stream().mapToInt(userInfo -> userInfo.id).toArray();
53 }
54 return new int[]{};
55 }
56
57 @Implementation
davidln086f1ec2019-02-06 10:45:25 -080058 protected List<UserInfo> getProfiles(int userHandle) {
59 if (mProfiles.containsKey(userHandle)) {
60 return new ArrayList<>(mProfiles.get(userHandle));
61 }
62 return Collections.emptyList();
63 }
64
65 /** Adds a profile to be returned by {@link #getProfiles(int)}. **/
66 public void addProfile(
67 int userHandle, int profileUserHandle, String profileName, int profileFlags) {
68 mProfiles.putIfAbsent(userHandle, new ArrayList<>());
69 mProfiles.get(userHandle).add(new UserInfo(profileUserHandle, profileName, profileFlags));
70 }
71
Anthony Hugh492f1842019-07-11 13:50:15 -070072 @Implementation
73 protected void setUserRestriction(String key, boolean value, UserHandle userHandle) {
74 setUserRestriction(userHandle, key, value);
75 }
76
Heemin Seog5f1ec272018-11-13 10:20:49 -080077 @Resetter
78 public static void reset() {
79 sInstance = null;
80 }
81}