From ce5f9c07dac577ab58cf053a9499f704f95c604a Mon Sep 17 00:00:00 2001 From: Doris Ling Date: Mon, 23 Jul 2018 16:13:39 -0700 Subject: [PATCH] Fix new account not shown for work profile. When refreshing the Accounts settings UI, we uses the cached user info for checking user status. However, when the work profile is being updated, the UserInfo obejct for the user might be updated even the user id is the same. Using the cached data causes stale info to be returned for the user and results in the latest account data not being shown properly for the user. Update the cache to the latest user info retrieved from user manager. Change-Id: Ic0127842203f0288f2fdea6c6346cd11e42a8bf0 Fix: 38302246 Test: make RunSettingsRoboTests --- .../accounts/AccountPreferenceController.java | 11 +++--- .../accounts/AccountPreferenceControllerTest.java | 39 ++++++++++++++++++++++ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/com/android/settings/accounts/AccountPreferenceController.java b/src/com/android/settings/accounts/AccountPreferenceController.java index 904dea32a7..46a3e6b777 100644 --- a/src/com/android/settings/accounts/AccountPreferenceController.java +++ b/src/com/android/settings/accounts/AccountPreferenceController.java @@ -192,12 +192,10 @@ public class AccountPreferenceController extends AbstractPreferenceController data.screenTitle = screenTitle; rawData.add(data); } - { - SearchIndexableRaw data = new SearchIndexableRaw(mContext); - data.title = res.getString(R.string.managed_profile_settings_title); - data.screenTitle = screenTitle; - rawData.add(data); - } + SearchIndexableRaw data = new SearchIndexableRaw(mContext); + data.title = res.getString(R.string.managed_profile_settings_title); + data.screenTitle = screenTitle; + rawData.add(data); } } } @@ -300,6 +298,7 @@ public class AccountPreferenceController extends AbstractPreferenceController final ProfileData data = mProfiles.get(userInfo.id); if (data != null) { data.pendingRemoval = false; + data.userInfo = userInfo; if (userInfo.isEnabled()) { // recreate the authentication helper to refresh the list of enabled accounts data.authenticatorHelper = diff --git a/tests/robotests/src/com/android/settings/accounts/AccountPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/accounts/AccountPreferenceControllerTest.java index de558b34d1..e790218209 100644 --- a/tests/robotests/src/com/android/settings/accounts/AccountPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/accounts/AccountPreferenceControllerTest.java @@ -563,6 +563,45 @@ public class AccountPreferenceControllerTest { verify(preferenceGroup, times(1)).removePreference(argThat(titleMatches("Acct12"))); } + @Test + public void onResume_userReEnabled_shouldAddOneAccountPreference() { + final List infos = new ArrayList<>(); + infos.add(new UserInfo(1, "user 1", UserInfo.FLAG_DISABLED)); + when(mUserManager.isManagedProfile()).thenReturn(false); + when(mUserManager.isRestrictedProfile()).thenReturn(false); + when(mUserManager.getProfiles(anyInt())).thenReturn(infos); + + Account[] accounts = {new Account("Acct1", "com.acct1")}; + when(mAccountManager.getAccountsAsUser(1)).thenReturn(accounts); + when(mAccountManager.getAccountsByTypeAsUser(eq("com.acct1"), any(UserHandle.class))) + .thenReturn(accounts); + + AuthenticatorDescription[] authDescs = { + new AuthenticatorDescription("com.acct1", "com.android.settings", + R.string.account_settings_title, 0 /* iconId */, 0 /* smallIconId */, + 0 /* prefId */, false /* customTokens */) + }; + when(mAccountManager.getAuthenticatorTypesAsUser(anyInt())).thenReturn(authDescs); + + AccessiblePreferenceCategory preferenceGroup = mock(AccessiblePreferenceCategory.class); + when(preferenceGroup.getPreferenceManager()).thenReturn(mock(PreferenceManager.class)); + when(mAccountHelper.createAccessiblePreferenceCategory(any(Context.class))) + .thenReturn(preferenceGroup); + + // First time resume will build the UI with no account + mController.onResume(); + verify(preferenceGroup, never()).addPreference(argThat(titleMatches("Acct1"))); + + // Enable the user + infos.remove(0 /* index */); + infos.add(new UserInfo(1, "user 1", 0 /* flags */)); + + // Resume should show the account for the user + mController.onResume(); + + verify(preferenceGroup).addPreference(argThat(titleMatches("Acct1"))); + } + private static ArgumentMatcher titleMatches(String expected) { return preference -> TextUtils.equals(expected, preference.getTitle()); } -- 2.11.0