1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.sync.test.util;
7 import android.accounts.Account;
9 import java.util.HashMap;
13 * This class is used by the {@link MockAccountManager} to hold information about a given
14 * account, such as its password and set of granted auth tokens.
16 public class AccountHolder {
18 private final Account mAccount;
20 private final String mPassword;
22 private final Map<String, String> mAuthTokens;
24 private final Map<String, Boolean> mHasBeenAccepted;
26 private final boolean mAlwaysAccept;
28 private AccountHolder(Account account, String password, Map<String, String> authTokens,
29 Map<String, Boolean> hasBeenAccepted, boolean alwaysAccept) {
30 mAlwaysAccept = alwaysAccept;
31 if (account == null) {
32 throw new IllegalArgumentException("Account can not be null");
36 mAuthTokens = authTokens == null ? new HashMap<String, String>() : authTokens;
37 mHasBeenAccepted = hasBeenAccepted == null ?
38 new HashMap<String, Boolean>() : hasBeenAccepted;
41 public Account getAccount() {
45 public String getPassword() {
49 public boolean hasAuthTokenRegistered(String authTokenType) {
50 return mAuthTokens.containsKey(authTokenType);
53 public String getAuthToken(String authTokenType) {
54 return mAuthTokens.get(authTokenType);
57 public boolean hasBeenAccepted(String authTokenType) {
58 return mAlwaysAccept ||
59 mHasBeenAccepted.containsKey(authTokenType) && mHasBeenAccepted.get(authTokenType);
63 * Removes an auth token from the auth token map.
65 * @param authToken the auth token to remove
66 * @return true if the auth token was found
68 public boolean removeAuthToken(String authToken) {
69 String foundKey = null;
70 for (Map.Entry<String, String> tokenEntry : mAuthTokens.entrySet()) {
71 if (authToken.equals(tokenEntry.getValue())) {
72 foundKey = tokenEntry.getKey();
76 if (foundKey == null) {
79 mAuthTokens.remove(foundKey);
85 public int hashCode() {
86 return mAccount.hashCode();
90 public boolean equals(Object that) {
91 return that != null && that instanceof AccountHolder && mAccount
92 .equals(((AccountHolder) that).getAccount());
95 public static Builder create() {
99 public AccountHolder withPassword(String password) {
100 return copy().password(password).build();
103 public AccountHolder withAuthTokens(Map<String, String> authTokens) {
104 return copy().authTokens(authTokens).build();
107 public AccountHolder withAuthToken(String authTokenType, String authToken) {
108 return copy().authToken(authTokenType, authToken).build();
111 public AccountHolder withHasBeenAccepted(String authTokenType, boolean hasBeenAccepted) {
112 return copy().hasBeenAccepted(authTokenType, hasBeenAccepted).build();
115 public AccountHolder withAlwaysAccept(boolean alwaysAccept) {
116 return copy().alwaysAccept(alwaysAccept).build();
119 private Builder copy() {
120 return create().account(mAccount).password(mPassword).authTokens(mAuthTokens).
121 hasBeenAcceptedMap(mHasBeenAccepted).alwaysAccept(mAlwaysAccept);
124 public static class Builder {
126 private Account mTempAccount;
128 private String mTempPassword;
130 private Map<String, String> mTempAuthTokens;
132 private Map<String, Boolean> mTempHasBeenAccepted;
134 private boolean mTempAlwaysAccept;
136 public Builder account(Account account) {
137 mTempAccount = account;
141 public Builder password(String password) {
142 mTempPassword = password;
146 public Builder authToken(String authTokenType, String authToken) {
147 if (mTempAuthTokens == null) {
148 mTempAuthTokens = new HashMap<String, String>();
150 mTempAuthTokens.put(authTokenType, authToken);
154 public Builder authTokens(Map<String, String> authTokens) {
155 mTempAuthTokens = authTokens;
159 public Builder hasBeenAccepted(String authTokenType, boolean hasBeenAccepted) {
160 if (mTempHasBeenAccepted == null) {
161 mTempHasBeenAccepted = new HashMap<String, Boolean>();
163 mTempHasBeenAccepted.put(authTokenType, hasBeenAccepted);
167 public Builder hasBeenAcceptedMap(Map<String, Boolean> hasBeenAcceptedMap) {
168 mTempHasBeenAccepted = hasBeenAcceptedMap;
172 public Builder alwaysAccept(boolean alwaysAccept) {
173 mTempAlwaysAccept = alwaysAccept;
177 public AccountHolder build() {
178 return new AccountHolder(mTempAccount, mTempPassword, mTempAuthTokens,
179 mTempHasBeenAccepted, mTempAlwaysAccept);