Check accounts in all profiles on account detail dashboard
In cl/5074783 I fixed a problem with account deletion where the account didn't appear to be deleted even when it was. Part of that fix broke the ability to make changes to work profile accounts - this CL fixes that by making sure not to early exit the account detail dashboard unless we've checked the accounts owned by all profiles for the user. Bug: 117965642 Test: make -j40 RunSettingsRoboTests Change-Id: Id034418beb4aec8bd4d10191b4924509d27e55d4
This commit is contained in:
@@ -92,18 +92,18 @@ public class AccountDetailDashboardFragment extends DashboardFragment {
|
||||
|
||||
@VisibleForTesting
|
||||
void finishIfAccountMissing() {
|
||||
AccountManager accountManager = (AccountManager) getContext().getSystemService(
|
||||
Context.ACCOUNT_SERVICE);
|
||||
boolean accountExists = false;
|
||||
for (Account account : accountManager.getAccountsByType(mAccount.type)) {
|
||||
if (account.equals(mAccount)) {
|
||||
accountExists = true;
|
||||
break;
|
||||
final Context context = getContext();
|
||||
final UserManager um = context.getSystemService(UserManager.class);
|
||||
final AccountManager accountManager = (AccountManager) context.getSystemService(
|
||||
AccountManager.class);
|
||||
for (UserHandle userHandle : um.getUserProfiles()) {
|
||||
for (Account account : accountManager.getAccountsAsUser(userHandle.getIdentifier())) {
|
||||
if (account.equals(mAccount)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!accountExists) {
|
||||
finish();
|
||||
}
|
||||
finish();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -177,4 +177,4 @@ public class AccountDetailDashboardFragment extends DashboardFragment {
|
||||
accountTypePreferenceLoader.updatePreferenceIntents(prefs, mAccountType, mAccount);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -35,8 +35,10 @@ import android.content.Intent;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.content.pm.UserInfo;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
import androidx.preference.Preference;
|
||||
@@ -44,9 +46,12 @@ import androidx.preference.Preference;
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settings.dashboard.DashboardFeatureProviderImpl;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.testutils.shadow.ShadowAccountManager;
|
||||
import com.android.settings.testutils.shadow.ShadowUserManager;
|
||||
import com.android.settingslib.drawer.CategoryKey;
|
||||
import com.android.settingslib.drawer.Tile;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -54,10 +59,11 @@ import org.robolectric.Robolectric;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.Shadows;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.shadows.ShadowAccountManager;
|
||||
import org.robolectric.shadow.api.Shadow;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(shadows = {ShadowAccountManager.class, ShadowUserManager.class})
|
||||
public class AccountDetailDashboardFragmentTest {
|
||||
|
||||
private static final String METADATA_CATEGORY = "com.android.settings.category";
|
||||
@@ -86,6 +92,11 @@ public class AccountDetailDashboardFragmentTest {
|
||||
when(mFragment.getContext()).thenReturn(mContext);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
ShadowAccountManager.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCategory_isAccountDetail() {
|
||||
assertThat(new AccountDetailDashboardFragment().getCategoryKey())
|
||||
@@ -152,17 +163,45 @@ public class AccountDetailDashboardFragmentTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(shadows = {ShadowAccountManager.class})
|
||||
public void onResume_accountMissing_shouldFinish() {
|
||||
ShadowUserManager userManager = Shadow.extract(
|
||||
mContext.getSystemService(UserManager.class));
|
||||
ShadowAccountManager acctMgr = Shadow.extract(
|
||||
mContext.getSystemService(AccountManager.class));
|
||||
|
||||
userManager.addProfile(new UserInfo(1, null, 0));
|
||||
acctMgr.addAccountForUser(1, new Account("test@test.com", "com.test"));
|
||||
|
||||
mFragment.finishIfAccountMissing();
|
||||
verify(mFragment).finish();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(shadows = {ShadowAccountManager.class})
|
||||
public void onResume_accountPresent_shouldNotFinish() {
|
||||
AccountManager mgr = mContext.getSystemService(AccountManager.class);
|
||||
Shadows.shadowOf(mgr).addAccount(mFragment.mAccount);
|
||||
public void onResume_accountPresentOneProfile_shouldNotFinish() {
|
||||
ShadowUserManager userManager = Shadow.extract(
|
||||
mContext.getSystemService(UserManager.class));
|
||||
ShadowAccountManager acctMgr = Shadow.extract(
|
||||
mContext.getSystemService(AccountManager.class));
|
||||
|
||||
userManager.addProfile(new UserInfo(1, null, 0));
|
||||
acctMgr.addAccountForUser(1, mFragment.mAccount);
|
||||
|
||||
mFragment.finishIfAccountMissing();
|
||||
verify(mFragment, never()).finish();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onResume_accountPresentTwoProfiles_shouldNotFinish() {
|
||||
ShadowUserManager userManager = Shadow.extract(
|
||||
mContext.getSystemService(UserManager.class));
|
||||
ShadowAccountManager acctMgr = Shadow.extract(
|
||||
mContext.getSystemService(AccountManager.class));
|
||||
|
||||
userManager.addProfile(new UserInfo(1, null, 0));
|
||||
userManager.addProfile(new UserInfo(2, null, 0));
|
||||
acctMgr.addAccountForUser(1, new Account("test@test.com", "com.test"));
|
||||
acctMgr.addAccountForUser(2, mFragment.mAccount);
|
||||
|
||||
mFragment.finishIfAccountMissing();
|
||||
verify(mFragment, never()).finish();
|
||||
}
|
||||
|
@@ -76,7 +76,7 @@ public class ChooseAccountPreferenceControllerTest {
|
||||
@After
|
||||
public void tearDown() {
|
||||
ShadowContentResolver.reset();
|
||||
ShadowAccountManager.resetAuthenticator();
|
||||
ShadowAccountManager.reset();
|
||||
ShadowRestrictedLockUtilsInternal.clearDisabledTypes();
|
||||
}
|
||||
|
||||
|
@@ -16,19 +16,24 @@
|
||||
|
||||
package com.android.settings.testutils.shadow;
|
||||
|
||||
import android.accounts.Account;
|
||||
import android.accounts.AccountManager;
|
||||
import android.accounts.AuthenticatorDescription;
|
||||
import android.annotation.NonNull;
|
||||
|
||||
import org.robolectric.annotation.Implementation;
|
||||
import org.robolectric.annotation.Implements;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Implements(AccountManager.class)
|
||||
public class ShadowAccountManager{
|
||||
|
||||
private static final Map<String, AuthenticatorDescription> sAuthenticators = new HashMap<>();
|
||||
private static final Map<Integer, List<Account>> sAccountsByUserId = new HashMap<>();
|
||||
|
||||
@Implementation
|
||||
public AuthenticatorDescription[] getAuthenticatorTypesAsUser(int userId) {
|
||||
@@ -39,7 +44,24 @@ public class ShadowAccountManager{
|
||||
sAuthenticators.put(authenticator.type, authenticator);
|
||||
}
|
||||
|
||||
public static void resetAuthenticator() {
|
||||
public static void reset() {
|
||||
sAuthenticators.clear();
|
||||
sAccountsByUserId.clear();
|
||||
}
|
||||
|
||||
@Implementation @NonNull
|
||||
public Account[] getAccountsAsUser(int userId) {
|
||||
if (sAccountsByUserId.containsKey(userId)) {
|
||||
return sAccountsByUserId.get(userId).toArray(new Account[0]);
|
||||
} else {
|
||||
return new Account[0];
|
||||
}
|
||||
}
|
||||
|
||||
public static void addAccountForUser(int userId, Account account) {
|
||||
if (!sAccountsByUserId.containsKey(userId)) {
|
||||
sAccountsByUserId.put(userId, new ArrayList<>());
|
||||
}
|
||||
sAccountsByUserId.get(userId).add(account);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user