Index: chrome/test/android/javatests/src/org/chromium/chrome/test/services/accountauthenticator/AccountAuthenticatorService.java |
diff --git a/chrome/test/android/javatests/src/org/chromium/chrome/test/services/accountauthenticator/AccountAuthenticatorService.java b/chrome/test/android/javatests/src/org/chromium/chrome/test/services/accountauthenticator/AccountAuthenticatorService.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6d0c1ff7a3fab0c69c898e7b66ef1f53dc5be098 |
--- /dev/null |
+++ b/chrome/test/android/javatests/src/org/chromium/chrome/test/services/accountauthenticator/AccountAuthenticatorService.java |
@@ -0,0 +1,117 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.chrome.test.services.accountauthenticator; |
+ |
+import android.accounts.AbstractAccountAuthenticator; |
+import android.accounts.Account; |
+import android.accounts.AccountAuthenticatorResponse; |
+import android.accounts.AccountManager; |
+import android.app.Service; |
+import android.content.ContentResolver; |
+import android.content.Context; |
+import android.content.Intent; |
+import android.os.Bundle; |
+import android.os.IBinder; |
+ |
+/** |
+ * Authenticator {@link Service} for test accounts. Allows adding fake accounts without password |
+ * validation. |
+ */ |
+public class AccountAuthenticatorService extends Service { |
+ |
+ @Override |
+ public IBinder onBind(Intent intent) { |
+ return new AccountAuthenticator(this).getIBinder(); |
+ } |
+ |
+ /** |
+ * Account authenticator implementation for test accounts. |
+ */ |
+ private static final class AccountAuthenticator extends AbstractAccountAuthenticator { |
+ private static final String AUTH_FAILURE = "AUTH_FAILS"; |
+ private static final String AUTH_TOKEN = "myToken"; |
+ |
+ private final ContentResolver mContentResolver; |
+ private final Context mContext; |
+ |
+ public AccountAuthenticator(Context context) { |
+ super(context); |
+ mContext = context; |
+ mContentResolver = mContext.getContentResolver(); |
+ } |
+ |
+ @Override |
+ public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, |
+ String authTokenType, String[] requiredFeatures, Bundle options) { |
+ Intent intent = new Intent(mContext, null); |
+ intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response); |
+ |
+ Bundle result = new Bundle(); |
+ result.putParcelable(AccountManager.KEY_INTENT, intent); |
+ return result; |
+ } |
+ |
+ @Override |
+ public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, |
+ Bundle options) { |
+ Bundle result = new Bundle(); |
+ result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true); |
+ return result; |
+ } |
+ |
+ @Override |
+ public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) { |
+ // Properties aren't supported |
+ return Bundle.EMPTY; |
+ } |
+ |
+ @Override |
+ public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, |
+ String authTokenType, Bundle options) { |
+ AccountManager accountManager = AccountManager.get(mContext); |
+ |
+ String authToken = accountManager.getUserData(account, AccountManager.KEY_AUTHTOKEN); |
+ if (authToken == null) { |
+ authToken = AUTH_TOKEN; |
+ } |
+ |
+ accountManager.setUserData(account, AccountManager.KEY_AUTHTOKEN, authToken); |
+ Bundle result = new Bundle(); |
+ result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
+ result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
+ result.putString(AccountManager.KEY_AUTHTOKEN, authToken); |
+ return result; |
+ } |
+ |
+ @Override |
+ public String getAuthTokenLabel(String authTokenType) { |
+ return authTokenType; |
+ } |
+ |
+ @Override |
+ public Bundle hasFeatures( |
+ AccountAuthenticatorResponse response, Account account, String[] features) { |
+ Bundle result = new Bundle(); |
+ result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true); |
+ return result; |
+ } |
+ |
+ @Override |
+ public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, |
+ String authTokenType, Bundle options) { |
+ Bundle result = new Bundle(); |
+ if (!shouldFailAuth(account)) { |
+ result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
+ result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
+ } |
+ return result; |
+ } |
+ |
+ private boolean shouldFailAuth(Account account) { |
+ AccountManager accountManager = AccountManager.get(mContext); |
+ return AUTH_FAILURE.equals(accountManager.getPassword(account)); |
+ } |
+ } |
+} |