Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(915)

Side by Side Diff: sync/test/android/javatests/src/org/chromium/sync/test/util/AccountHolder.java

Issue 2130453004: [Sync] Move //sync to //components/sync. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 import android.os.Handler;
9
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
16
17 import javax.annotation.Nullable;
18
19 /**
20 * This class is used by the {@link MockAccountManager} to hold information abou t a given
21 * account, such as its password and set of granted auth tokens.
22 */
23 public class AccountHolder {
24
25 private final Account mAccount;
26
27 private final String mPassword;
28
29 private final Map<String, String> mAuthTokens;
30
31 private final Map<String, Boolean> mHasBeenAccepted;
32
33 private final boolean mAlwaysAccept;
34
35 private Set<String> mFeatures;
36
37 private final List<Runnable> mFeatureCallbacks = new ArrayList<>();
38
39 private AccountHolder(Account account, String password, Map<String, String> authTokens,
40 Map<String, Boolean> hasBeenAccepted, boolean alwaysAccept,
41 @Nullable Set<String> features) {
42 if (account == null) {
43 throw new IllegalArgumentException("Account can not be null");
44 }
45 mAccount = account;
46 mPassword = password;
47 mAuthTokens = authTokens == null ? new HashMap<String, String>() : authT okens;
48 mHasBeenAccepted = hasBeenAccepted == null
49 ? new HashMap<String, Boolean>() : hasBeenAccepted;
50 mAlwaysAccept = alwaysAccept;
51 mFeatures = features;
52 }
53
54 public Account getAccount() {
55 return mAccount;
56 }
57
58 public String getPassword() {
59 return mPassword;
60 }
61
62 public boolean hasAuthTokenRegistered(String authTokenType) {
63 return mAuthTokens.containsKey(authTokenType);
64 }
65
66 public String getAuthToken(String authTokenType) {
67 return mAuthTokens.get(authTokenType);
68 }
69
70 public boolean hasBeenAccepted(String authTokenType) {
71 return mAlwaysAccept || mHasBeenAccepted.containsKey(authTokenType)
72 && mHasBeenAccepted.get(authTokenType);
73 }
74
75 /**
76 * Removes an auth token from the auth token map.
77 *
78 * @param authToken the auth token to remove
79 * @return true if the auth token was found
80 */
81 public boolean removeAuthToken(String authToken) {
82 String foundKey = null;
83 for (Map.Entry<String, String> tokenEntry : mAuthTokens.entrySet()) {
84 if (authToken.equals(tokenEntry.getValue())) {
85 foundKey = tokenEntry.getKey();
86 break;
87 }
88 }
89 if (foundKey == null) {
90 return false;
91 } else {
92 mAuthTokens.remove(foundKey);
93 return true;
94 }
95 }
96
97 /**
98 * @return The set of account features. This method may only be called after the account
99 * features have been fetched.
100 */
101 public Set<String> getFeatures() {
102 assert mFeatures != null;
103 return mFeatures;
104 }
105
106 /**
107 * Adds a callback to be run when the account features have been fetched. If that has already
108 * happened, the callback is run immediately.
109 *
110 * @param callback The callback to be run when the account features have bee n fetched.
111 */
112 public void addFeaturesCallback(Runnable callback) {
113 if (mFeatures == null) {
114 mFeatureCallbacks.add(callback);
115 return;
116 }
117
118 new Handler().post(callback);
119 }
120
121 /**
122 * Notifies this object that the account features have been fetched.
123 *
124 * @param features The set of account features.
125 */
126 public void didFetchFeatures(Set<String> features) {
127 assert features != null;
128 assert mFeatures == null;
129 mFeatures = features;
130 Handler handler = new Handler();
131 for (Runnable r : mFeatureCallbacks) {
132 handler.post(r);
133 }
134 mFeatureCallbacks.clear();
135 }
136
137 @Override
138 public int hashCode() {
139 return mAccount.hashCode();
140 }
141
142 @Override
143 public boolean equals(Object that) {
144 return that instanceof AccountHolder
145 && mAccount.equals(((AccountHolder) that).getAccount());
146 }
147
148 public static Builder create() {
149 return new Builder();
150 }
151
152 public AccountHolder withPassword(String password) {
153 return copy().password(password).build();
154 }
155
156 public AccountHolder withAuthTokens(Map<String, String> authTokens) {
157 return copy().authTokens(authTokens).build();
158 }
159
160 public AccountHolder withAuthToken(String authTokenType, String authToken) {
161 return copy().authToken(authTokenType, authToken).build();
162 }
163
164 public AccountHolder withHasBeenAccepted(String authTokenType, boolean hasBe enAccepted) {
165 return copy().hasBeenAccepted(authTokenType, hasBeenAccepted).build();
166 }
167
168 public AccountHolder withAlwaysAccept(boolean alwaysAccept) {
169 return copy().alwaysAccept(alwaysAccept).build();
170 }
171
172 private Builder copy() {
173 return create().account(mAccount).password(mPassword).authTokens(mAuthTo kens)
174 .hasBeenAcceptedMap(mHasBeenAccepted).alwaysAccept(mAlwaysAccept );
175 }
176
177 /**
178 * Used to construct AccountHolder instances.
179 */
180 public static class Builder {
181
182 private Account mTempAccount;
183
184 private String mTempPassword;
185
186 private Map<String, String> mTempAuthTokens;
187
188 private Map<String, Boolean> mTempHasBeenAccepted;
189
190 private boolean mTempAlwaysAccept;
191
192 private Set<String> mFeatures = new HashSet<>();
193
194 public Builder account(Account account) {
195 mTempAccount = account;
196 return this;
197 }
198
199 public Builder password(String password) {
200 mTempPassword = password;
201 return this;
202 }
203
204 public Builder authToken(String authTokenType, String authToken) {
205 if (mTempAuthTokens == null) {
206 mTempAuthTokens = new HashMap<String, String>();
207 }
208 mTempAuthTokens.put(authTokenType, authToken);
209 return this;
210 }
211
212 public Builder authTokens(Map<String, String> authTokens) {
213 mTempAuthTokens = authTokens;
214 return this;
215 }
216
217 public Builder hasBeenAccepted(String authTokenType, boolean hasBeenAcce pted) {
218 if (mTempHasBeenAccepted == null) {
219 mTempHasBeenAccepted = new HashMap<String, Boolean>();
220 }
221 mTempHasBeenAccepted.put(authTokenType, hasBeenAccepted);
222 return this;
223 }
224
225 public Builder hasBeenAcceptedMap(Map<String, Boolean> hasBeenAcceptedMa p) {
226 mTempHasBeenAccepted = hasBeenAcceptedMap;
227 return this;
228 }
229
230 public Builder alwaysAccept(boolean alwaysAccept) {
231 mTempAlwaysAccept = alwaysAccept;
232 return this;
233 }
234
235 public Builder addFeature(String feature) {
236 if (mFeatures == null) {
237 mFeatures = new HashSet<>();
238 }
239 mFeatures.add(feature);
240 return this;
241 }
242
243 /**
244 * Sets the set of features for this account.
245 *
246 * @param features The set of account features. Can be null to indicate that the account
247 * features have not been fetched yet. In this case,
248 * {@link AccountHolder#didFetchFeatures} should be called on the resulting
249 * {@link AccountHolder} before the features can be accessed.
250 * @return This object, for chaining method calls.
251 */
252 public Builder featureSet(Set<String> features) {
253 mFeatures = features;
254 return this;
255 }
256
257 public AccountHolder build() {
258 return new AccountHolder(mTempAccount, mTempPassword, mTempAuthToken s,
259 mTempHasBeenAccepted, mTempAlwaysAccept, mFeatures);
260 }
261 }
262
263 }
OLDNEW
« no previous file with comments | « sync/test/android/OWNERS ('k') | sync/test/android/javatests/src/org/chromium/sync/test/util/MockAccountManager.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698