Adding user accessor functions to UserManagerHelper.
Fixing a failing test. Currently unable to test removing current user, since current user is system user.
Fixes: 73415209
Test: unit tests
Change-Id: Ie374aa97383e3dd8ec4ef96b0d680fa8a04172b5
diff --git a/packages/SettingsLib/src/com/android/settingslib/users/UserManagerHelper.java b/packages/SettingsLib/src/com/android/settingslib/users/UserManagerHelper.java
index cc69e0e..c4ca339 100644
--- a/packages/SettingsLib/src/com/android/settingslib/users/UserManagerHelper.java
+++ b/packages/SettingsLib/src/com/android/settingslib/users/UserManagerHelper.java
@@ -86,7 +86,7 @@
* @return List of {@code UserInfo} for each user that is not the current user.
*/
public List<UserInfo> getAllUsersExcludesCurrentUser() {
- List<UserInfo> others = mUserManager.getUsers(true);
+ List<UserInfo> others = getAllUsers();
for (Iterator<UserInfo> iterator = others.iterator(); iterator.hasNext(); ) {
UserInfo userInfo = iterator.next();
@@ -98,6 +98,31 @@
return others;
}
+ /**
+ * Gets all the other users on the system that are not the system user.
+ *
+ * @return List of {@code UserInfo} for each user that is not the system user.
+ */
+ public List<UserInfo> getAllUsersExcludesSystemUser() {
+ List<UserInfo> others = getAllUsers();
+
+ for (Iterator<UserInfo> iterator = others.iterator(); iterator.hasNext(); ) {
+ UserInfo userInfo = iterator.next();
+ if (userIsSystemUser(userInfo)) {
+ // Remove system user from the list.
+ iterator.remove();
+ }
+ }
+ return others;
+ }
+
+ /**
+ * Gets all the users on the system that are not currently being removed.
+ */
+ public List<UserInfo> getAllUsers() {
+ return mUserManager.getUsers(true /* excludeDying */);
+ }
+
// User information accessors
/**
@@ -224,7 +249,7 @@
* @return {@code true} if user is successfully removed, {@code false} otherwise.
*/
public boolean removeUser(UserInfo userInfo) {
- if (userInfo.id == UserHandle.USER_SYSTEM) {
+ if (userIsSystemUser(userInfo)) {
Log.w(TAG, "User " + userInfo.id + " is system user, could not be removed.");
return false;
}
diff --git a/packages/SettingsLib/tests/integ/src/com/android/settingslib/users/UserManagerHelperTest.java b/packages/SettingsLib/tests/integ/src/com/android/settingslib/users/UserManagerHelperTest.java
index 325ef3a..3f1fcbb 100644
--- a/packages/SettingsLib/tests/integ/src/com/android/settingslib/users/UserManagerHelperTest.java
+++ b/packages/SettingsLib/tests/integ/src/com/android/settingslib/users/UserManagerHelperTest.java
@@ -113,6 +113,54 @@
}
@Test
+ public void testGetAllUsersExcludesSystemUser() {
+ UserInfo otherUser1 = createUserInfoForId(10);
+ UserInfo otherUser2 = createUserInfoForId(11);
+ UserInfo otherUser3 = createUserInfoForId(12);
+
+ List<UserInfo> testUsers = new ArrayList<>();
+ testUsers.add(otherUser1);
+ testUsers.add(otherUser2);
+ testUsers.add(mSystemUser);
+ testUsers.add(otherUser3);
+
+ when(mUserManager.getUsers(true)).thenReturn(testUsers);
+
+ // Should return 3 users that don't have SYSTEM USER id.
+ assertThat(mHelper.getAllUsersExcludesSystemUser().size()).isEqualTo(3);
+ // Should not contain system user.
+ assertThat(mHelper.getAllUsersExcludesSystemUser()).doesNotContain(mSystemUser);
+ // Should contain non-system users.
+ assertThat(mHelper.getAllUsersExcludesSystemUser()).contains(otherUser1);
+ assertThat(mHelper.getAllUsersExcludesSystemUser()).contains(otherUser2);
+ assertThat(mHelper.getAllUsersExcludesSystemUser()).contains(otherUser3);
+ }
+
+ @Test
+ public void testGetAllUsers() {
+ int currentUser = UserHandle.myUserId();
+
+ UserInfo otherUser1 = createUserInfoForId(currentUser + 1);
+ UserInfo otherUser2 = createUserInfoForId(currentUser - 1);
+ UserInfo otherUser3 = createUserInfoForId(currentUser + 2);
+
+ List<UserInfo> testUsers = new ArrayList<>();
+ testUsers.add(otherUser1);
+ testUsers.add(otherUser2);
+ testUsers.add(mCurrentUser);
+ testUsers.add(otherUser3);
+
+ when(mUserManager.getUsers(true)).thenReturn(testUsers);
+
+ // Should return 3 users that don't have currentUser id.
+ assertThat(mHelper.getAllUsers().size()).isEqualTo(4);
+ assertThat(mHelper.getAllUsers()).contains(mCurrentUser);
+ assertThat(mHelper.getAllUsers()).contains(otherUser1);
+ assertThat(mHelper.getAllUsers()).contains(otherUser2);
+ assertThat(mHelper.getAllUsers()).contains(otherUser3);
+ }
+
+ @Test
public void testUserCanBeRemoved() {
UserInfo testInfo = new UserInfo();
@@ -208,11 +256,6 @@
// Cannot remove system user.
assertThat(mHelper.removeUser(mSystemUser)).isFalse();
- // Removing current user, calls "switch" to system user.
- mHelper.removeUser(mCurrentUser);
- verify(mActivityManager).switchUser(UserHandle.USER_SYSTEM);
- verify(mUserManager).removeUser(mCurrentUser.id);
-
// Removing non-current, non-system user, simply calls removeUser.
UserInfo userToRemove = createUserInfoForId(mCurrentUser.id + 2);
mHelper.removeUser(userToRemove);