1 /*
2  * Copyright (C) 2019 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.testing.shadows;
18 
19 import android.annotation.NonNull;
20 import android.annotation.UserIdInt;
21 import android.os.UserManager;
22 
23 import org.robolectric.annotation.Implementation;
24 import org.robolectric.annotation.Implements;
25 
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.Map;
29 import java.util.Set;
30 
31 /** Shadow for {@link UserManager}. */
32 @Implements(UserManager.class)
33 public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager {
34     private final Map<Integer, Set<Integer>> profileIds = new HashMap<>();
35 
36     /** @see UserManager#isUserUnlocked() */
37     @Implementation
isUserUnlocked(@serIdInt int userId)38     public boolean isUserUnlocked(@UserIdInt int userId) {
39         return false;
40     }
41 
42     /** @see UserManager#getProfileIds(int, boolean) () */
43     @Implementation
44     @NonNull
getProfileIds(@serIdInt int userId, boolean enabledOnly)45     public int[] getProfileIds(@UserIdInt int userId, boolean enabledOnly) {
46         // Currently, enabledOnly is ignored.
47         if (!profileIds.containsKey(userId)) {
48             return new int[] {userId};
49         }
50         return profileIds.get(userId).stream().mapToInt(Number::intValue).toArray();
51     }
52 
53     /** Add a collection of profile IDs, all within the same profile group. */
addProfileIds(@serIdInt int... userIds)54     public void addProfileIds(@UserIdInt int... userIds) {
55         final Set<Integer> profileGroup = new HashSet<>();
56         for (int userId : userIds) {
57             profileGroup.add(userId);
58             profileIds.put(userId, profileGroup);
59         }
60     }
61 }
62