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