• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 android.system.helpers;
18 
19 import android.content.Context;
20 import android.os.UserManager;
21 import android.support.test.InstrumentationRegistry;
22 import android.system.helpers.CommandsHelper;
23 import android.util.Log;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26 import junit.framework.Assert;
27 
28 /**
29  * Implement common helper methods for user.
30  */
31 public class UserHelper {
32     private static final String TAG = UserHelper.class.getSimpleName();
33     private static UserHelper sInstance = null;
34     private Context mContext = null;
35 
36     public static final int INVALID_USER_ID = -1;
37 
38 
UserHelper()39     public UserHelper() {
40         mContext = InstrumentationRegistry.getTargetContext();
41     }
42 
getInstance()43     public static UserHelper getInstance() {
44         if (sInstance == null) {
45             sInstance = new UserHelper();
46         }
47         return sInstance;
48     }
49 
getUserManager()50     public UserManager getUserManager() {
51         return (UserManager)mContext.getSystemService(Context.USER_SERVICE);
52     }
53 
54     /**
55      * Creates a test user
56      * @return id for created secondary user
57      */
createSecondaryUser(String userName)58     public int createSecondaryUser(String userName) {
59         // Create user
60         String cmdOut = CommandsHelper.execute("pm create-user " + userName);
61         // Find user id from user-create output
62         // output format : "Success: created user id 10"
63         final Pattern pattern = Pattern.compile("Success: created user id (\\d+)");
64         Matcher matcher = pattern.matcher(cmdOut);
65         int userId = INVALID_USER_ID;
66         if (matcher.find()) {
67             userId = Integer.parseInt(matcher.group(1));
68             Log.i(TAG, String.format("User Name:%s User ID:%d", userName, userId));
69         }
70         return userId;
71     }
72 
73     /**
74      * Returns id for first secondary user
75      * @return userid
76      */
getSecondaryUserId()77     public int getSecondaryUserId() {
78         String cmdOut = CommandsHelper.execute("pm list users");
79         // Assume that the a user with ID 0 is a primary user. Otherwise secondary users
80         final Pattern USERS_REGEX = Pattern.compile("UserInfo\\{([1-9]\\d*):[\\w\\s]+:(\\d+)\\}");
81         Matcher matcher = USERS_REGEX.matcher(cmdOut);
82         int userId = INVALID_USER_ID;
83         if (matcher.find()) {
84             userId = Integer.parseInt(matcher.group(1));    // 1 = id 2 = flag
85             Log.i(TAG, String.format("The userId is %d", userId));
86         }
87         return userId;
88     }
89 
removeSecondaryUser(int userId)90     public void removeSecondaryUser(int userId) {
91         int prevUserCount = getUserCount();
92         CommandsHelper.execute("pm remove-user " + userId);
93         Assert.assertTrue("User hasn't been removed", getUserCount() == (prevUserCount - 1));
94     }
95 
getUserCount()96     public int getUserCount() {
97         return getUserManager().getUserCount();
98     }
99 }