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. 4 5 package org.chromium.sync.test.util; 6 7 import android.accounts.Account; 8 9 import java.util.HashMap; 10 import java.util.Map; 11 12 /** 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. 15 */ 16 public class AccountHolder { 17 18 private final Account mAccount; 19 20 private final String mPassword; 21 22 private final Map<String, String> mAuthTokens; 23 24 private final Map<String, Boolean> mHasBeenAccepted; 25 26 private final boolean mAlwaysAccept; 27 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"); 33 } 34 mAccount = account; 35 mPassword = password; 36 mAuthTokens = authTokens == null ? new HashMap<String, String>() : authTokens; 37 mHasBeenAccepted = hasBeenAccepted == null ? 38 new HashMap<String, Boolean>() : hasBeenAccepted; 39 } 40 41 public Account getAccount() { 42 return mAccount; 43 } 44 45 public String getPassword() { 46 return mPassword; 47 } 48 49 public boolean hasAuthTokenRegistered(String authTokenType) { 50 return mAuthTokens.containsKey(authTokenType); 51 } 52 53 public String getAuthToken(String authTokenType) { 54 return mAuthTokens.get(authTokenType); 55 } 56 57 public boolean hasBeenAccepted(String authTokenType) { 58 return mAlwaysAccept || 59 mHasBeenAccepted.containsKey(authTokenType) && mHasBeenAccepted.get(authTokenType); 60 } 61 62 /** 63 * Removes an auth token from the auth token map. 64 * 65 * @param authToken the auth token to remove 66 * @return true if the auth token was found 67 */ 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(); 73 break; 74 } 75 } 76 if (foundKey == null) { 77 return false; 78 } else { 79 mAuthTokens.remove(foundKey); 80 return true; 81 } 82 } 83 84 @Override 85 public int hashCode() { 86 return mAccount.hashCode(); 87 } 88 89 @Override 90 public boolean equals(Object that) { 91 return that != null && that instanceof AccountHolder && mAccount 92 .equals(((AccountHolder) that).getAccount()); 93 } 94 95 public static Builder create() { 96 return new Builder(); 97 } 98 99 public AccountHolder withPassword(String password) { 100 return copy().password(password).build(); 101 } 102 103 public AccountHolder withAuthTokens(Map<String, String> authTokens) { 104 return copy().authTokens(authTokens).build(); 105 } 106 107 public AccountHolder withAuthToken(String authTokenType, String authToken) { 108 return copy().authToken(authTokenType, authToken).build(); 109 } 110 111 public AccountHolder withHasBeenAccepted(String authTokenType, boolean hasBeenAccepted) { 112 return copy().hasBeenAccepted(authTokenType, hasBeenAccepted).build(); 113 } 114 115 public AccountHolder withAlwaysAccept(boolean alwaysAccept) { 116 return copy().alwaysAccept(alwaysAccept).build(); 117 } 118 119 private Builder copy() { 120 return create().account(mAccount).password(mPassword).authTokens(mAuthTokens). 121 hasBeenAcceptedMap(mHasBeenAccepted).alwaysAccept(mAlwaysAccept); 122 } 123 124 public static class Builder { 125 126 private Account mTempAccount; 127 128 private String mTempPassword; 129 130 private Map<String, String> mTempAuthTokens; 131 132 private Map<String, Boolean> mTempHasBeenAccepted; 133 134 private boolean mTempAlwaysAccept; 135 136 public Builder account(Account account) { 137 mTempAccount = account; 138 return this; 139 } 140 141 public Builder password(String password) { 142 mTempPassword = password; 143 return this; 144 } 145 146 public Builder authToken(String authTokenType, String authToken) { 147 if (mTempAuthTokens == null) { 148 mTempAuthTokens = new HashMap<String, String>(); 149 } 150 mTempAuthTokens.put(authTokenType, authToken); 151 return this; 152 } 153 154 public Builder authTokens(Map<String, String> authTokens) { 155 mTempAuthTokens = authTokens; 156 return this; 157 } 158 159 public Builder hasBeenAccepted(String authTokenType, boolean hasBeenAccepted) { 160 if (mTempHasBeenAccepted == null) { 161 mTempHasBeenAccepted = new HashMap<String, Boolean>(); 162 } 163 mTempHasBeenAccepted.put(authTokenType, hasBeenAccepted); 164 return this; 165 } 166 167 public Builder hasBeenAcceptedMap(Map<String, Boolean> hasBeenAcceptedMap) { 168 mTempHasBeenAccepted = hasBeenAcceptedMap; 169 return this; 170 } 171 172 public Builder alwaysAccept(boolean alwaysAccept) { 173 mTempAlwaysAccept = alwaysAccept; 174 return this; 175 } 176 177 public AccountHolder build() { 178 return new AccountHolder(mTempAccount, mTempPassword, mTempAuthTokens, 179 mTempHasBeenAccepted, mTempAlwaysAccept); 180 } 181 } 182 183 } 184