Heemin Seog | 5f1ec27 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 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; |
Anthony Hugh | 492f184 | 2019-07-11 13:50:15 -0700 | [diff] [blame] | 21 | import android.os.UserHandle; |
Heemin Seog | 5f1ec27 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 22 | import android.os.UserManager; |
davidln | 086f1ec | 2019-02-06 10:45:25 -0800 | [diff] [blame] | 23 | import android.util.ArrayMap; |
Heemin Seog | 5f1ec27 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 24 | |
| 25 | import org.robolectric.annotation.Implementation; |
| 26 | import org.robolectric.annotation.Implements; |
| 27 | import org.robolectric.annotation.Resetter; |
| 28 | |
davidln | 086f1ec | 2019-02-06 10:45:25 -0800 | [diff] [blame] | 29 | import java.util.ArrayList; |
| 30 | import java.util.Collections; |
| 31 | import java.util.List; |
| 32 | import java.util.Map; |
| 33 | |
Heemin Seog | 5f1ec27 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 34 | @Implements(UserManager.class) |
| 35 | public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager { |
| 36 | private static UserManager sInstance; |
| 37 | |
davidln | 086f1ec | 2019-02-06 10:45:25 -0800 | [diff] [blame] | 38 | private Map<Integer, List<UserInfo>> mProfiles = new ArrayMap<>(); |
| 39 | |
Heemin Seog | 5f1ec27 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 40 | 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 | |
davidln | 086f1ec | 2019-02-06 10:45:25 -0800 | [diff] [blame] | 49 | @Implementation |
davidln | afe538d | 2019-02-06 15:58:57 -0800 | [diff] [blame] | 50 | 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 |
davidln | 086f1ec | 2019-02-06 10:45:25 -0800 | [diff] [blame] | 58 | 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 Hugh | 492f184 | 2019-07-11 13:50:15 -0700 | [diff] [blame] | 72 | @Implementation |
| 73 | protected void setUserRestriction(String key, boolean value, UserHandle userHandle) { |
| 74 | setUserRestriction(userHandle, key, value); |
| 75 | } |
| 76 | |
Heemin Seog | 5f1ec27 | 2018-11-13 10:20:49 -0800 | [diff] [blame] | 77 | @Resetter |
| 78 | public static void reset() { |
| 79 | sInstance = null; |
| 80 | } |
| 81 | } |