Skip to content

Commit

Permalink
Implement AccountManager.removeAccount(...) with Activity parameter.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 233759954
  • Loading branch information
Googler authored and copybara-robolectric committed Feb 13, 2019
1 parent 3e02b9b commit 63816d7
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 9 deletions.
Expand Up @@ -345,6 +345,42 @@ public void removeAccount_does() throws Exception {
assertThat(testAccountManagerCallback.accountManagerFuture).isNotNull();
}

@Test
@Config(minSdk = LOLLIPOP_MR1)
public void removeAccount_withActivity() throws Exception {
Account account = new Account("name", "type");
shadowOf(am).addAccount(account);

TestAccountManagerCallback<Bundle> testAccountManagerCallback =
new TestAccountManagerCallback<>();
AccountManagerFuture<Bundle> future =
am.removeAccount(account, activity, testAccountManagerCallback, null);
Bundle result = future.getResult();

assertThat(result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT)).isTrue();
Intent removeAccountIntent = result.getParcelable(AccountManager.KEY_INTENT);
assertThat(removeAccountIntent).isNull();
}

@Test
@Config(minSdk = LOLLIPOP_MR1)
public void removeAccount_withActivity_doesNotRemoveButReturnsIntent() throws Exception {
Account account = new Account("name", "type");
shadowOf(am).addAccount(account);
Intent intent = new Intent().setAction("remove-account-action");
shadowOf(am).setRemoveAccountIntent(intent);

TestAccountManagerCallback<Bundle> testAccountManagerCallback =
new TestAccountManagerCallback<>();
AccountManagerFuture<Bundle> future =
am.removeAccount(account, activity, testAccountManagerCallback, null);
Bundle result = future.getResult();

assertThat(result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT)).isFalse();
Intent removeAccountIntent = result.getParcelable(AccountManager.KEY_INTENT);
assertThat(removeAccountIntent.getAction()).isEqualTo(intent.getAction());
}

@Test
public void removeAccount_notifiesListenersIfSuccessful() {
Account account = new Account("name", "type");
Expand Down Expand Up @@ -468,12 +504,12 @@ public void invalidateAuthToken_multipleAccounts() {
assertThat(am.peekAuthToken(account1, "token_type_1")).isEqualTo("token1");
assertThat(am.peekAuthToken(account2, "token_type_1")).isEqualTo("token1");

// invalidate token for type1 account
// invalidate token for type1 account
am.invalidateAuthToken("type1", "token1");
assertThat(am.peekAuthToken(account1, "token_type_1")).isNull();
assertThat(am.peekAuthToken(account2, "token_type_1")).isEqualTo("token1");

// invalidate token for type2 account
// invalidate token for type2 account
am.invalidateAuthToken("type2", "token1");
assertThat(am.peekAuthToken(account1, "token_type_1")).isNull();
assertThat(am.peekAuthToken(account2, "token_type_1")).isNull();
Expand Down Expand Up @@ -906,11 +942,11 @@ private static class TestAccountManagerCallback<T> implements AccountManagerCall
public void run(AccountManagerFuture<T> accountManagerFuture) {
this.accountManagerFuture = accountManagerFuture;
}

boolean hasBeenCalled() {
return accountManagerFuture != null;
}

T getResult() throws Exception {
return accountManagerFuture.getResult();
}
Expand Down
Expand Up @@ -51,6 +51,7 @@ public class ShadowAccountManager {
private Handler mainHandler;
private RoboAccountManagerFuture pendingAddFuture;
private boolean authenticationErrorOnNextResponse = false;
private Intent removeAccountIntent;

@Implementation
protected void __constructor__(Context context, IAccountManager service) {
Expand Down Expand Up @@ -90,9 +91,9 @@ protected Account[] getAccountsByType(String type) {

@Implementation
protected synchronized void setAuthToken(Account account, String tokenType, String authToken) {
if(accounts.contains(account)) {
if (accounts.contains(account)) {
Map<String, String> tokenMap = authTokens.get(account);
if(tokenMap == null) {
if (tokenMap == null) {
tokenMap = new HashMap<>();
authTokens.put(account, tokenMap);
}
Expand All @@ -103,7 +104,7 @@ protected synchronized void setAuthToken(Account account, String tokenType, Stri
@Implementation
protected String peekAuthToken(Account account, String tokenType) {
Map<String, String> tokenMap = authTokens.get(account);
if(tokenMap != null) {
if (tokenMap != null) {
return tokenMap.get(tokenType);
}
return null;
Expand All @@ -115,7 +116,7 @@ protected boolean addAccountExplicitly(Account account, String password, Bundle
if (account == null) {
throw new IllegalArgumentException("account is null");
}
for (Account a: getAccountsByType(account.type)) {
for (Account a : getAccountsByType(account.type)) {
if (a.name.equals(account.name)) {
return false;
}
Expand All @@ -127,7 +128,7 @@ protected boolean addAccountExplicitly(Account account, String password, Bundle

setPassword(account, password);

if(userdata != null) {
if (userdata != null) {
for (String key : userdata.keySet()) {
setUserData(account, key, userdata.get(key).toString());
}
Expand Down Expand Up @@ -176,6 +177,37 @@ public Boolean doWork()
});
}

/**
* Removes the account unless {@link #setRemoveAccountIntent} has been set. If set, the future
* Bundle will include the Intent and {@link AccountManager#KEY_BOOLEAN_RESULT} will be false.
*/
@Implementation(minSdk = LOLLIPOP_MR1)
protected AccountManagerFuture<Bundle> removeAccount(
Account account,
Activity activity,
AccountManagerCallback<Bundle> callback,
Handler handler) {
if (account == null) {
throw new IllegalArgumentException("account is null");
}
return start(
new BaseRoboAccountManagerFuture<Bundle>(callback, handler) {
@Override
public Bundle doWork()
throws OperationCanceledException, IOException, AuthenticatorException {
Bundle result = new Bundle();
if (removeAccountIntent == null) {
result.putBoolean(
AccountManager.KEY_BOOLEAN_RESULT, removeAccountExplicitly(account));
} else {
result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false);
result.putParcelable(AccountManager.KEY_INTENT, removeAccountIntent);
}
return result;
}
});
}

@Implementation(minSdk = LOLLIPOP_MR1)
protected boolean removeAccountExplicitly(Account account) {
passwords.remove(account);
Expand Down Expand Up @@ -585,6 +617,15 @@ public void setAuthenticationErrorOnNextResponse(boolean authenticationErrorOnNe
this.authenticationErrorOnNextResponse = authenticationErrorOnNextResponse;
}

/**
* Sets the intent to include in Bundle result from {@link #removeAccount} if Activity is given.
*
* @param removeAccountIntent the intent to surface as {@link AccountManager#KEY_INTENT}.
*/
public void setRemoveAccountIntent(Intent removeAccountIntent) {
this.removeAccountIntent = removeAccountIntent;
}

private abstract class BaseRoboAccountManagerFuture<T> implements AccountManagerFuture<T> {
protected final AccountManagerCallback<T> callback;
private final Handler handler;
Expand Down

0 comments on commit 63816d7

Please sign in to comment.