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
|
@VisibleForTesting
|
||||||
void finishIfAccountMissing() {
|
void finishIfAccountMissing() {
|
||||||
AccountManager accountManager = (AccountManager) getContext().getSystemService(
|
final Context context = getContext();
|
||||||
Context.ACCOUNT_SERVICE);
|
final UserManager um = context.getSystemService(UserManager.class);
|
||||||
boolean accountExists = false;
|
final AccountManager accountManager = (AccountManager) context.getSystemService(
|
||||||
for (Account account : accountManager.getAccountsByType(mAccount.type)) {
|
AccountManager.class);
|
||||||
if (account.equals(mAccount)) {
|
for (UserHandle userHandle : um.getUserProfiles()) {
|
||||||
accountExists = true;
|
for (Account account : accountManager.getAccountsAsUser(userHandle.getIdentifier())) {
|
||||||
break;
|
if (account.equals(mAccount)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!accountExists) {
|
finish();
|
||||||
finish();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -35,8 +35,10 @@ import android.content.Intent;
|
|||||||
import android.content.pm.ActivityInfo;
|
import android.content.pm.ActivityInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.content.pm.ResolveInfo;
|
import android.content.pm.ResolveInfo;
|
||||||
|
import android.content.pm.UserInfo;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
|
import android.os.UserManager;
|
||||||
|
|
||||||
import androidx.fragment.app.FragmentActivity;
|
import androidx.fragment.app.FragmentActivity;
|
||||||
import androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
@@ -44,9 +46,12 @@ import androidx.preference.Preference;
|
|||||||
import com.android.internal.logging.nano.MetricsProto;
|
import com.android.internal.logging.nano.MetricsProto;
|
||||||
import com.android.settings.dashboard.DashboardFeatureProviderImpl;
|
import com.android.settings.dashboard.DashboardFeatureProviderImpl;
|
||||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
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.CategoryKey;
|
||||||
import com.android.settingslib.drawer.Tile;
|
import com.android.settingslib.drawer.Tile;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@@ -54,10 +59,11 @@ import org.robolectric.Robolectric;
|
|||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
import org.robolectric.Shadows;
|
import org.robolectric.Shadows;
|
||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
import org.robolectric.shadows.ShadowAccountManager;
|
import org.robolectric.shadow.api.Shadow;
|
||||||
import org.robolectric.util.ReflectionHelpers;
|
import org.robolectric.util.ReflectionHelpers;
|
||||||
|
|
||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
|
@Config(shadows = {ShadowAccountManager.class, ShadowUserManager.class})
|
||||||
public class AccountDetailDashboardFragmentTest {
|
public class AccountDetailDashboardFragmentTest {
|
||||||
|
|
||||||
private static final String METADATA_CATEGORY = "com.android.settings.category";
|
private static final String METADATA_CATEGORY = "com.android.settings.category";
|
||||||
@@ -86,6 +92,11 @@ public class AccountDetailDashboardFragmentTest {
|
|||||||
when(mFragment.getContext()).thenReturn(mContext);
|
when(mFragment.getContext()).thenReturn(mContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
ShadowAccountManager.reset();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCategory_isAccountDetail() {
|
public void testCategory_isAccountDetail() {
|
||||||
assertThat(new AccountDetailDashboardFragment().getCategoryKey())
|
assertThat(new AccountDetailDashboardFragment().getCategoryKey())
|
||||||
@@ -152,17 +163,45 @@ public class AccountDetailDashboardFragmentTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Config(shadows = {ShadowAccountManager.class})
|
|
||||||
public void onResume_accountMissing_shouldFinish() {
|
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();
|
mFragment.finishIfAccountMissing();
|
||||||
verify(mFragment).finish();
|
verify(mFragment).finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Config(shadows = {ShadowAccountManager.class})
|
public void onResume_accountPresentOneProfile_shouldNotFinish() {
|
||||||
public void onResume_accountPresent_shouldNotFinish() {
|
ShadowUserManager userManager = Shadow.extract(
|
||||||
AccountManager mgr = mContext.getSystemService(AccountManager.class);
|
mContext.getSystemService(UserManager.class));
|
||||||
Shadows.shadowOf(mgr).addAccount(mFragment.mAccount);
|
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();
|
mFragment.finishIfAccountMissing();
|
||||||
verify(mFragment, never()).finish();
|
verify(mFragment, never()).finish();
|
||||||
}
|
}
|
||||||
|
@@ -76,7 +76,7 @@ public class ChooseAccountPreferenceControllerTest {
|
|||||||
@After
|
@After
|
||||||
public void tearDown() {
|
public void tearDown() {
|
||||||
ShadowContentResolver.reset();
|
ShadowContentResolver.reset();
|
||||||
ShadowAccountManager.resetAuthenticator();
|
ShadowAccountManager.reset();
|
||||||
ShadowRestrictedLockUtilsInternal.clearDisabledTypes();
|
ShadowRestrictedLockUtilsInternal.clearDisabledTypes();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -16,19 +16,24 @@
|
|||||||
|
|
||||||
package com.android.settings.testutils.shadow;
|
package com.android.settings.testutils.shadow;
|
||||||
|
|
||||||
|
import android.accounts.Account;
|
||||||
import android.accounts.AccountManager;
|
import android.accounts.AccountManager;
|
||||||
import android.accounts.AuthenticatorDescription;
|
import android.accounts.AuthenticatorDescription;
|
||||||
|
import android.annotation.NonNull;
|
||||||
|
|
||||||
import org.robolectric.annotation.Implementation;
|
import org.robolectric.annotation.Implementation;
|
||||||
import org.robolectric.annotation.Implements;
|
import org.robolectric.annotation.Implements;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@Implements(AccountManager.class)
|
@Implements(AccountManager.class)
|
||||||
public class ShadowAccountManager{
|
public class ShadowAccountManager{
|
||||||
|
|
||||||
private static final Map<String, AuthenticatorDescription> sAuthenticators = new HashMap<>();
|
private static final Map<String, AuthenticatorDescription> sAuthenticators = new HashMap<>();
|
||||||
|
private static final Map<Integer, List<Account>> sAccountsByUserId = new HashMap<>();
|
||||||
|
|
||||||
@Implementation
|
@Implementation
|
||||||
public AuthenticatorDescription[] getAuthenticatorTypesAsUser(int userId) {
|
public AuthenticatorDescription[] getAuthenticatorTypesAsUser(int userId) {
|
||||||
@@ -39,7 +44,24 @@ public class ShadowAccountManager{
|
|||||||
sAuthenticators.put(authenticator.type, authenticator);
|
sAuthenticators.put(authenticator.type, authenticator);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void resetAuthenticator() {
|
public static void reset() {
|
||||||
sAuthenticators.clear();
|
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