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