| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.sync.test.util; | 5 package org.chromium.components.sync.test.util; |
| 6 | 6 |
| 7 import android.accounts.Account; | 7 import android.accounts.Account; |
| 8 import android.accounts.AccountManager; | 8 import android.accounts.AccountManager; |
| 9 import android.accounts.AuthenticatorDescription; | 9 import android.accounts.AuthenticatorDescription; |
| 10 import android.content.Context; | 10 import android.content.Context; |
| 11 import android.content.Intent; | 11 import android.content.Intent; |
| 12 import android.os.AsyncTask; | 12 import android.os.AsyncTask; |
| 13 | 13 |
| 14 import org.chromium.base.Callback; | 14 import org.chromium.base.Callback; |
| 15 import org.chromium.base.Log; | 15 import org.chromium.base.Log; |
| 16 import org.chromium.base.VisibleForTesting; | 16 import org.chromium.base.VisibleForTesting; |
| 17 import org.chromium.sync.signin.AccountManagerDelegate; | 17 import org.chromium.components.sync.signin.AccountManagerDelegate; |
| 18 import org.chromium.sync.signin.AccountManagerHelper; | 18 import org.chromium.components.sync.signin.AccountManagerHelper; |
| 19 | 19 |
| 20 import java.util.ArrayList; | 20 import java.util.ArrayList; |
| 21 import java.util.HashSet; | 21 import java.util.HashSet; |
| 22 import java.util.Set; | 22 import java.util.Set; |
| 23 import java.util.UUID; | 23 import java.util.UUID; |
| 24 import java.util.concurrent.LinkedBlockingDeque; | 24 import java.util.concurrent.LinkedBlockingDeque; |
| 25 import java.util.concurrent.ThreadPoolExecutor; | 25 import java.util.concurrent.ThreadPoolExecutor; |
| 26 import java.util.concurrent.TimeUnit; | 26 import java.util.concurrent.TimeUnit; |
| 27 | 27 |
| 28 /** | 28 /** |
| 29 * The MockAccountManager helps out if you want to mock out all calls to the And
roid AccountManager. | 29 * The MockAccountManager helps out if you want to mock out all calls to the And
roid AccountManager. |
| 30 * | 30 * |
| 31 * You should provide a set of accounts as a constructor argument, or use the mo
re direct approach | 31 * You should provide a set of accounts as a constructor argument, or use the mo
re direct approach |
| 32 * and provide an array of AccountHolder objects. | 32 * and provide an array of AccountHolder objects. |
| 33 * | 33 * |
| 34 * Currently, this implementation supports adding and removing accounts, handlin
g credentials | 34 * Currently, this implementation supports adding and removing accounts, handlin
g credentials |
| 35 * (including confirming them), and handling of dummy auth tokens. | 35 * (including confirming them), and handling of dummy auth tokens. |
| 36 * | 36 * |
| 37 * If you want to auto-approve a given authtokentype, use addAccountHolderExplic
itly(...) with | 37 * If you want to auto-approve a given authtokentype, use addAccountHolderExplic
itly(...) with |
| 38 * an AccountHolder you have built with hasBeenAccepted("yourAuthTokenType", tru
e). | 38 * an AccountHolder you have built with hasBeenAccepted("yourAuthTokenType", tru
e). |
| 39 * | 39 * |
| 40 * If you want to auto-approve all auth token types for a given account, use the
{@link | 40 * If you want to auto-approve all auth token types for a given account, use the
{@link |
| 41 * AccountHolder} builder method alwaysAccept(true). | 41 * AccountHolder} builder method alwaysAccept(true). |
| 42 */ | 42 */ |
| 43 public class MockAccountManager implements AccountManagerDelegate { | 43 public class MockAccountManager implements AccountManagerDelegate { |
| 44 | |
| 45 private static final String TAG = "MockAccountManager"; | 44 private static final String TAG = "MockAccountManager"; |
| 46 | 45 |
| 47 protected final Context mContext; | 46 protected final Context mContext; |
| 48 | 47 |
| 49 private final Set<AccountHolder> mAccounts; | 48 private final Set<AccountHolder> mAccounts; |
| 50 | 49 |
| 51 // Tracks the number of in-progress getAccountsByType() tasks so that tests
can wait for | 50 // Tracks the number of in-progress getAccountsByType() tasks so that tests
can wait for |
| 52 // their completion. | 51 // their completion. |
| 53 private final ZeroCounter mGetAccountsTaskCounter; | 52 private final ZeroCounter mGetAccountsTaskCounter; |
| 54 | 53 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 return addAccountHolderExplicitly(accountHolder, false); | 121 return addAccountHolderExplicitly(accountHolder, false); |
| 123 } | 122 } |
| 124 | 123 |
| 125 /** | 124 /** |
| 126 * Add an AccountHolder directly. | 125 * Add an AccountHolder directly. |
| 127 * | 126 * |
| 128 * @param accountHolder the account holder to add | 127 * @param accountHolder the account holder to add |
| 129 * @param broadcastEvent whether to broadcast an AccountChangedEvent | 128 * @param broadcastEvent whether to broadcast an AccountChangedEvent |
| 130 * @return whether the account holder was added successfully | 129 * @return whether the account holder was added successfully |
| 131 */ | 130 */ |
| 132 public boolean addAccountHolderExplicitly(AccountHolder accountHolder, | 131 public boolean addAccountHolderExplicitly(AccountHolder accountHolder, boole
an broadcastEvent) { |
| 133 boolean broadcastEvent) { | |
| 134 boolean result = mAccounts.add(accountHolder); | 132 boolean result = mAccounts.add(accountHolder); |
| 135 if (broadcastEvent) { | 133 if (broadcastEvent) { |
| 136 postAsyncAccountChangedEvent(); | 134 postAsyncAccountChangedEvent(); |
| 137 } | 135 } |
| 138 return result; | 136 return result; |
| 139 } | 137 } |
| 140 | 138 |
| 141 @VisibleForTesting | 139 @VisibleForTesting |
| 142 public boolean removeAccountHolderExplicitly(AccountHolder accountHolder) { | 140 public boolean removeAccountHolderExplicitly(AccountHolder accountHolder) { |
| 143 return removeAccountHolderExplicitly(accountHolder, false); | 141 return removeAccountHolderExplicitly(accountHolder, false); |
| 144 } | 142 } |
| 145 | 143 |
| 146 /** | 144 /** |
| 147 * Remove an AccountHolder directly. | 145 * Remove an AccountHolder directly. |
| 148 * | 146 * |
| 149 * @param accountHolder the account holder to remove | 147 * @param accountHolder the account holder to remove |
| 150 * @param broadcastEvent whether to broadcast an AccountChangedEvent | 148 * @param broadcastEvent whether to broadcast an AccountChangedEvent |
| 151 * @return whether the account holder was removed successfully | 149 * @return whether the account holder was removed successfully |
| 152 */ | 150 */ |
| 153 public boolean removeAccountHolderExplicitly(AccountHolder accountHolder, | 151 public boolean removeAccountHolderExplicitly( |
| 154 boolean broadcastEvent) { | 152 AccountHolder accountHolder, boolean broadcastEvent) { |
| 155 boolean result = mAccounts.remove(accountHolder); | 153 boolean result = mAccounts.remove(accountHolder); |
| 156 if (broadcastEvent) { | 154 if (broadcastEvent) { |
| 157 postAsyncAccountChangedEvent(); | 155 postAsyncAccountChangedEvent(); |
| 158 } | 156 } |
| 159 return result; | 157 return result; |
| 160 } | 158 } |
| 161 | 159 |
| 162 @Override | 160 @Override |
| 163 public String getAuthToken(Account account, String authTokenScope) { | 161 public String getAuthToken(Account account, String authTokenScope) { |
| 164 AccountHolder ah = getAccountHolder(account); | 162 AccountHolder ah = getAccountHolder(account); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 188 break; | 186 break; |
| 189 } | 187 } |
| 190 } | 188 } |
| 191 } | 189 } |
| 192 | 190 |
| 193 @Override | 191 @Override |
| 194 public AuthenticatorDescription[] getAuthenticatorTypes() { | 192 public AuthenticatorDescription[] getAuthenticatorTypes() { |
| 195 AuthenticatorDescription googleAuthenticator = new AuthenticatorDescript
ion( | 193 AuthenticatorDescription googleAuthenticator = new AuthenticatorDescript
ion( |
| 196 AccountManagerHelper.GOOGLE_ACCOUNT_TYPE, "p1", 0, 0, 0, 0); | 194 AccountManagerHelper.GOOGLE_ACCOUNT_TYPE, "p1", 0, 0, 0, 0); |
| 197 | 195 |
| 198 return new AuthenticatorDescription[] { googleAuthenticator }; | 196 return new AuthenticatorDescription[] {googleAuthenticator}; |
| 199 } | 197 } |
| 200 | 198 |
| 201 @Override | 199 @Override |
| 202 public void hasFeatures( | 200 public void hasFeatures( |
| 203 Account account, final String[] features, final Callback<Boolean> ca
llback) { | 201 Account account, final String[] features, final Callback<Boolean> ca
llback) { |
| 204 final AccountHolder accountHolder = getAccountHolder(account); | 202 final AccountHolder accountHolder = getAccountHolder(account); |
| 205 accountHolder.addFeaturesCallback(new Runnable() { | 203 accountHolder.addFeaturesCallback(new Runnable() { |
| 206 @Override | 204 @Override |
| 207 public void run() { | 205 public void run() { |
| 208 Set<String> accountFeatures = accountHolder.getFeatures(); | 206 Set<String> accountFeatures = accountHolder.getFeatures(); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 | 264 |
| 267 public void waitUntilZero() throws InterruptedException { | 265 public void waitUntilZero() throws InterruptedException { |
| 268 synchronized (mLock) { | 266 synchronized (mLock) { |
| 269 while (mCount != 0) { | 267 while (mCount != 0) { |
| 270 mLock.wait(WAIT_TIMEOUT_MS); | 268 mLock.wait(WAIT_TIMEOUT_MS); |
| 271 } | 269 } |
| 272 } | 270 } |
| 273 } | 271 } |
| 274 } | 272 } |
| 275 } | 273 } |
| OLD | NEW |