• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 package android.car.test.util;
17 
18 import static com.android.compatibility.common.util.ShellUtils.runShellCommand;
19 
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.UserIdInt;
23 import android.content.pm.UserInfo;
24 import android.content.pm.UserInfo.UserInfoFlag;
25 import android.os.UserManager;
26 
27 import com.android.internal.util.Preconditions;
28 
29 import java.util.Arrays;
30 import java.util.List;
31 import java.util.stream.Collectors;
32 
33 /**
34  * Provides utilities for Android User related tasks.
35  */
36 public final class UserTestingHelper {
37 
38     /**
39      * Creates a simple {@link UserInfo}, containing just the given {@code userId}.
40      */
41     @NonNull
newUser(@serIdInt int userId)42     public static UserInfo newUser(@UserIdInt int userId) {
43         return new UserInfoBuilder(userId).build();
44     }
45 
46     /**
47      * Creates a list of {@link UserInfo UserInfos}, each containing just the given user ids.
48      */
49     @NonNull
newUsers(@serIdInt int... userIds)50     public static List<UserInfo> newUsers(@UserIdInt int... userIds) {
51         return Arrays.stream(userIds)
52                 .mapToObj(id -> newUser(id))
53                 .collect(Collectors.toList());
54     }
55 
56     /**
57      * Creates a list of {@link UserInfo UserInfos}.
58      */
59     @NonNull
toList(@onNull UserInfo... users)60     public static List<UserInfo> toList(@NonNull UserInfo... users) {
61         return Arrays.stream(users).collect(Collectors.toList());
62     }
63 
64     /**
65      * Creates a {@link UserInfo} with the type explicitly set and with the given {@code userId}.
66      */
67     @NonNull
newSecondaryUser(@serIdInt int userId)68     public static UserInfo newSecondaryUser(@UserIdInt int userId) {
69         return new UserInfoBuilder(userId).setType(UserManager.USER_TYPE_FULL_SECONDARY).build();
70     }
71 
72     /**
73      * Creates a new guest with the given {@code userId} and proper flags and types set.
74      */
75     @NonNull
newGuestUser(@serIdInt int userId, boolean ephemeral)76     public static UserInfo newGuestUser(@UserIdInt int userId, boolean ephemeral) {
77         return new UserInfoBuilder(userId).setGuest(true).setEphemeral(ephemeral).build();
78     }
79 
80     /**
81      * Creates a new guest with the given {@code userId} and without any flag..
82      */
83     @NonNull
newGuestUser(@serIdInt int userId)84     public static UserInfo newGuestUser(@UserIdInt int userId) {
85         return new UserInfoBuilder(userId).setGuest(true).build();
86     }
87 
88     /**
89      * Gets the default {@link UserInfo#userType} for a guest / regular user.
90      */
91     @NonNull
getDefaultUserType(boolean isGuest)92     public static String getDefaultUserType(boolean isGuest) {
93         return isGuest ? UserManager.USER_TYPE_FULL_GUEST : UserManager.USER_TYPE_FULL_SECONDARY;
94     }
95 
96     /**
97      * Sets the property that defines the maximum number of uses allowed in the device.
98      */
setMaxSupportedUsers(int max)99     public static void setMaxSupportedUsers(int max) {
100         runShellCommand("setprop fw.max_users %d", max);
101     }
102 
103     /**
104      * Configures the user to use PIN credentials.
105      */
setUserLockCredentials(@serIdInt int userId, int pin)106     public static void setUserLockCredentials(@UserIdInt int userId, int pin) {
107         runShellCommand("locksettings set-pin %s --user %d ", pin, userId);
108     }
109 
110     /**
111      * Clears the user credentials using current PIN.
112      */
clearUserLockCredentials(@serIdInt int userId, int pin)113     public static void clearUserLockCredentials(@UserIdInt int userId, int pin) {
114         runShellCommand("locksettings clear --old %d --user %d ", pin, userId);
115     }
116 
117     /**
118      * Builder for {@link UserInfo} objects.
119      */
120     public static final class UserInfoBuilder {
121 
122         @UserIdInt
123         private final int mUserId;
124 
125         @UserInfoFlag
126         private int mFlags;
127 
128         @Nullable
129         private String mName;
130 
131         @Nullable
132         private String mType;
133 
134         private boolean mGuest;
135         private boolean mEphemeral;
136         private boolean mAdmin;
137         private boolean mPreCreated;
138         private boolean mInitialized;
139 
140         /**
141          * Default constructor.
142          */
UserInfoBuilder(@serIdInt int userId)143         public UserInfoBuilder(@UserIdInt int userId) {
144             mUserId = userId;
145         }
146 
147         /**
148          * Sets the user name.
149          */
150         @NonNull
setName(@ullable String name)151         public UserInfoBuilder setName(@Nullable String name) {
152             mName = name;
153             return this;
154         }
155 
156         /**
157          * Sets the user type.
158          */
159         @NonNull
setType(@ullable String type)160         public UserInfoBuilder setType(@Nullable String type) {
161             Preconditions.checkState(!mGuest, "cannot set type (" + mType + ") after setting it as "
162                     + "guest");
163             mType = type;
164             return this;
165         }
166 
167         /**
168          * Sets whether the user is a guest.
169          */
170         @NonNull
setGuest(boolean guest)171         public UserInfoBuilder setGuest(boolean guest) {
172             Preconditions.checkState(mType == null, "cannot set guest after setting type (" + mType
173                     + ")");
174             mGuest = guest;
175             return this;
176         }
177 
178         /**
179          * Sets the user flags
180          */
181         @NonNull
setFlags(@serInfoFlag int flags)182         public UserInfoBuilder setFlags(@UserInfoFlag int flags) {
183             mFlags = flags;
184             return this;
185         }
186 
187         /**
188          * Sets whether the user is ephemeral.
189          */
190         @NonNull
setEphemeral(boolean ephemeral)191         public UserInfoBuilder setEphemeral(boolean ephemeral) {
192             mEphemeral = ephemeral;
193             return this;
194         }
195 
196         /**
197          * Sets whether the user is an admin.
198          */
199         @NonNull
setAdmin(boolean admin)200         public UserInfoBuilder setAdmin(boolean admin) {
201             mAdmin = admin;
202             return this;
203         }
204 
205         /**
206          * Sets whether the user is an pre-created.
207          */
208         @NonNull
setPreCreated(boolean preCreated)209         public UserInfoBuilder setPreCreated(boolean preCreated) {
210             mPreCreated = preCreated;
211             return this;
212         }
213 
214         /**
215          * Sets whether the user is initialized.
216          */
217         @NonNull
setInitialized(boolean initialized)218         public UserInfoBuilder setInitialized(boolean initialized) {
219             mInitialized = initialized;
220             return this;
221         }
222 
223         /**
224          * Creates a new {@link UserInfo}.
225          */
226         @NonNull
build()227         public UserInfo build() {
228             int flags = mFlags;
229             if (mEphemeral) {
230                 flags |= UserInfo.FLAG_EPHEMERAL;
231             }
232             if (mAdmin) {
233                 flags |= UserInfo.FLAG_ADMIN;
234             }
235             if (mInitialized) {
236                 flags |= UserInfo.FLAG_INITIALIZED;
237             }
238             if (mGuest) {
239                 mType = UserManager.USER_TYPE_FULL_GUEST;
240             }
241             UserInfo info = new UserInfo(mUserId, mName, /* iconPath= */ null, flags, mType);
242             info.preCreated = mPreCreated;
243             return info;
244         }
245     }
246 
UserTestingHelper()247     private UserTestingHelper() {
248         throw new UnsupportedOperationException("contains only static methods");
249     }
250 }
251