Merge "Fix account deletion not updating account display"
This commit is contained in:
committed by
Android (Google) Code Review
commit
9f553bc863
@@ -16,6 +16,7 @@
|
||||
package com.android.settings.accounts;
|
||||
|
||||
import android.accounts.Account;
|
||||
import android.accounts.AccountManager;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
@@ -89,6 +90,28 @@ public class AccountDetailDashboardFragment extends DashboardFragment {
|
||||
updateUi();
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
if (!accountExists) {
|
||||
finish();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
finishIfAccountMissing();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
return MetricsEvent.ACCOUNT;
|
||||
|
@@ -157,10 +157,6 @@ public class RemoveAccountPreferenceController extends AbstractPreferenceControl
|
||||
new AccountManagerCallback<Bundle>() {
|
||||
@Override
|
||||
public void run(AccountManagerFuture<Bundle> future) {
|
||||
// If already out of this screen, don't proceed.
|
||||
if (!getTargetFragment().isResumed()) {
|
||||
return;
|
||||
}
|
||||
boolean failed = true;
|
||||
try {
|
||||
if (future.getResult()
|
||||
|
@@ -23,9 +23,13 @@ import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.accounts.Account;
|
||||
import android.accounts.AccountManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ActivityInfo;
|
||||
@@ -49,6 +53,8 @@ import org.junit.runner.RunWith;
|
||||
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.util.ReflectionHelpers;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@@ -73,10 +79,11 @@ public class AccountDetailDashboardFragmentTest {
|
||||
final Bundle args = new Bundle();
|
||||
args.putParcelable(METADATA_USER_HANDLE, UserHandle.CURRENT);
|
||||
|
||||
mFragment = new AccountDetailDashboardFragment();
|
||||
mFragment = spy(new AccountDetailDashboardFragment());
|
||||
mFragment.setArguments(args);
|
||||
mFragment.mAccountType = "com.abc";
|
||||
mFragment.mAccount = new Account("name1@abc.com", "com.abc");
|
||||
when(mFragment.getContext()).thenReturn(mContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -143,4 +150,20 @@ public class AccountDetailDashboardFragmentTest {
|
||||
|
||||
assertThat(intent.getStringExtra("extra.accountName")).isEqualTo("name1@abc.com");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(shadows = {ShadowAccountManager.class})
|
||||
public void onResume_accountMissing_shouldFinish() {
|
||||
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);
|
||||
mFragment.finishIfAccountMissing();
|
||||
verify(mFragment, never()).finish();
|
||||
}
|
||||
}
|
||||
|
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package com.android.settings.accounts;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.ArgumentMatchers.nullable;
|
||||
import static org.mockito.Matchers.any;
|
||||
@@ -28,7 +30,10 @@ import static org.mockito.Mockito.when;
|
||||
import android.accounts.Account;
|
||||
import android.accounts.AccountManager;
|
||||
import android.accounts.AccountManagerCallback;
|
||||
import android.accounts.AccountManagerFuture;
|
||||
import android.accounts.AuthenticatorDescription;
|
||||
import android.accounts.AuthenticatorException;
|
||||
import android.accounts.OperationCanceledException;
|
||||
import android.app.Activity;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
@@ -56,12 +61,14 @@ import com.android.settings.testutils.shadow.ShadowUserManager;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.shadows.ShadowApplication;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -157,7 +164,8 @@ public class RemoveAccountPreferenceControllerTest {
|
||||
|
||||
@Test
|
||||
@Config(shadows = {ShadowAccountManager.class, ShadowContentResolver.class})
|
||||
public void confirmRemove_shouldRemoveAccount() {
|
||||
public void confirmRemove_shouldRemoveAccount()
|
||||
throws AuthenticatorException, OperationCanceledException, IOException {
|
||||
when(mFragment.isAdded()).thenReturn(true);
|
||||
FragmentActivity activity = mock(FragmentActivity.class);
|
||||
when(activity.getSystemService(Context.ACCOUNT_SERVICE)).thenReturn(mAccountManager);
|
||||
@@ -170,7 +178,18 @@ public class RemoveAccountPreferenceControllerTest {
|
||||
mFragment, account, userHandle);
|
||||
dialog.onCreate(new Bundle());
|
||||
dialog.onClick(null, 0);
|
||||
ArgumentCaptor<AccountManagerCallback<Bundle>> callbackCaptor = ArgumentCaptor.forClass(
|
||||
AccountManagerCallback.class);
|
||||
verify(mAccountManager).removeAccountAsUser(eq(account), nullable(Activity.class),
|
||||
nullable(AccountManagerCallback.class), nullable(Handler.class), eq(userHandle));
|
||||
callbackCaptor.capture(), nullable(Handler.class), eq(userHandle));
|
||||
|
||||
AccountManagerCallback<Bundle> callback = callbackCaptor.getValue();
|
||||
assertThat(callback).isNotNull();
|
||||
AccountManagerFuture<Bundle> future = mock(AccountManagerFuture.class);
|
||||
Bundle resultBundle = new Bundle();
|
||||
resultBundle.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true);
|
||||
when(future.getResult()).thenReturn(resultBundle);
|
||||
callback.run(future);
|
||||
verify(activity).finish();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user