1 /* 2 * Copyright (C) 2021 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.settings.users; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.app.Instrumentation; 22 import android.content.Intent; 23 import android.content.pm.UserInfo; 24 import android.os.UserManager; 25 import android.util.Log; 26 27 import androidx.fragment.app.Fragment; 28 import androidx.fragment.app.FragmentActivity; 29 import androidx.test.core.app.ActivityScenario; 30 import androidx.test.ext.junit.rules.ActivityScenarioRule; 31 import androidx.test.ext.junit.runners.AndroidJUnit4; 32 import androidx.test.filters.SmallTest; 33 import androidx.test.platform.app.InstrumentationRegistry; 34 35 import com.android.settings.Settings; 36 import com.android.settings.testutils.AdbUtils; 37 import com.android.settings.testutils.UiUtils; 38 39 import org.junit.After; 40 import org.junit.Before; 41 import org.junit.Rule; 42 import org.junit.Test; 43 import org.junit.runner.RunWith; 44 45 import java.io.IOException; 46 import java.util.ArrayList; 47 import java.util.Random; 48 import java.util.stream.Collectors; 49 50 @RunWith(AndroidJUnit4.class) 51 @SmallTest 52 public class UserSettingsComponentTest { 53 public static final int TIMEOUT = 2000; 54 private static final int USER_TYPE_RESTRICTED_PROFILE = 2; 55 public final String TAG = this.getClass().getName(); 56 private final Instrumentation mInstrumentation = InstrumentationRegistry.getInstrumentation(); 57 private final ArrayList<Integer> mOriginUserIds = new ArrayList<>(); 58 private final UserManager mUserManager = 59 (UserManager) mInstrumentation.getTargetContext().getSystemService("user"); 60 @Rule 61 public ActivityScenarioRule<Settings.UserSettingsActivity> 62 rule = new ActivityScenarioRule<>( 63 new Intent(android.provider.Settings.ACTION_USER_SETTINGS) 64 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); 65 66 @Before setUp()67 public void setUp() { 68 for (UserInfo info : mUserManager.getUsers()) { 69 mOriginUserIds.add(info.id); 70 } 71 72 // Enable multiple user switch. 73 if (!mUserManager.isUserSwitcherEnabled()) { 74 android.provider.Settings.Global.putInt( 75 mInstrumentation.getTargetContext().getContentResolver(), 76 android.provider.Settings.Global.USER_SWITCHER_ENABLED, 1); 77 } 78 } 79 80 @Test test_new_user_on_multiple_setting_page()81 public void test_new_user_on_multiple_setting_page() throws IOException { 82 String randomUserName = gendrate_random_name(10); 83 ActivityScenario scenario = rule.getScenario(); 84 scenario.onActivity(activity -> { 85 Fragment f = 86 ((FragmentActivity) activity).getSupportFragmentManager().getFragments().get(0); 87 UserSettings us = (UserSettings) f; 88 Log.d(TAG, "Start to add user :" + randomUserName); 89 us.createUser(USER_TYPE_RESTRICTED_PROFILE, randomUserName); 90 }); 91 92 assertThat( 93 UiUtils.waitUntilCondition(5000, () -> mUserManager.getAliveUsers().stream().filter( 94 (user) -> user.name.equals( 95 randomUserName)).findFirst().isPresent())).isTrue(); 96 } 97 98 @After tearDown()99 public void tearDown() { 100 int retryNumber = 5; 101 for (int i = 0; i < retryNumber; ++i) { 102 int currentUsersCount = mUserManager.getUserCount(); 103 if (currentUsersCount == mOriginUserIds.size()) { 104 break; 105 } else if (i != 0) { 106 Log.d(TAG, "[tearDown] User not fully removed. Retry #" + (i = 1) + " of total " 107 + mOriginUserIds.size()); 108 } 109 110 for (UserInfo info : mUserManager.getUsers()) { 111 if (mOriginUserIds.contains(info.id)) { 112 continue; 113 } 114 Log.d(TAG, "[tearDown] Clean up user {" + info.id + "}:" + info.name); 115 try { 116 AdbUtils.shell("pm remove-user " + info.id); 117 } catch (Exception e) { 118 Log.w(TAG, "[tearDown] Error occurs while removing user. " + e.toString()); 119 } 120 } 121 } 122 } 123 gendrate_random_name(int length)124 private String gendrate_random_name(int length) { 125 String seed = "abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQSTUVWXYZ"; 126 Random r1 = new Random(); 127 String result = ""; 128 for (int i = 0; i < length; ++i) { 129 result = result + seed.charAt(r1.nextInt(seed.length() - 1)); 130 } 131 if (mUserManager.getAliveUsers().stream().map(user -> user.name).collect( 132 Collectors.toList()).contains(result)) { 133 Log.d(TAG, "Name repeated! add padding 'rpt' in the end of name."); 134 result += "rpt"; 135 } 136 return result; 137 } 138 139 } 140