Index: chrome/test/android/javatests/src/org/chromium/chrome/test/util/SigninUtils.java |
diff --git a/chrome/test/android/javatests/src/org/chromium/chrome/test/util/SigninUtils.java b/chrome/test/android/javatests/src/org/chromium/chrome/test/util/SigninUtils.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..380598893a666b184a266e245a208dce49925944 |
--- /dev/null |
+++ b/chrome/test/android/javatests/src/org/chromium/chrome/test/util/SigninUtils.java |
@@ -0,0 +1,240 @@ |
+// 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.util; |
+ |
+import android.accounts.Account; |
+import android.accounts.AccountManager; |
+import android.accounts.AccountManagerCallback; |
+import android.accounts.AccountManagerFuture; |
+import android.accounts.AuthenticatorException; |
+import android.accounts.OperationCanceledException; |
+import android.content.Context; |
+import android.os.Bundle; |
+import android.text.TextUtils; |
+ |
+import org.chromium.base.test.BaseActivityInstrumentationTestCase; |
+import org.chromium.chrome.test.services.accountauthenticator.AccountConstants; |
+import org.chromium.sync.signin.ChromeSigninController; |
+ |
+import java.io.IOException; |
+ |
+/** |
+ * A tool used for running tests as signed in. Currently this only works for instrumentation tests |
+ * that extened {@link ChromeActivityTestCaseBase} and only signs in to the Chrome app. |
+ */ |
+public class SigninUtils { |
+ private AccountManager mAccountManager; |
+ private Context mTargetContext; |
+ |
+ /** |
+ * The constructor for SigninUtils for a given test case. |
+ * |
+ * @param testCase the test case to perform signin operations with. |
+ */ |
+ public SigninUtils(BaseActivityInstrumentationTestCase testCase) { |
+ mTargetContext = testCase.getInstrumentation().getTargetContext(); |
+ mAccountManager = AccountManager.get(testCase.getInstrumentation().getContext()); |
+ } |
+ |
+ /** |
+ * Removes account from the app. |
+ */ |
+ public void removeAccountFromApp() { |
+ ChromeSigninController.get(mTargetContext).clearSignedInUser(); |
+ } |
+ |
+ /** |
+ * Checks if an existing fake account is on the app. |
+ * |
+ * @return true if fake account is signed in on app, false otherwise. |
+ */ |
+ public boolean isExistingAccountOnApp() { |
+ return ChromeSigninController.get(mTargetContext).isSignedIn(); |
+ } |
+ |
+ /** |
+ * Adds an account to the app. |
+ * |
+ * @param username the username for the logged in account. must be a gaia account. |
+ */ |
+ public void addAccountToApp(String username) { |
+ if (isExistingAccountOnApp()) { |
+ removeAccountFromApp(); |
+ } |
+ ChromeSigninController.get(mTargetContext).setSignedInAccountName(username); |
+ } |
+ |
+ /** |
+ * Adds a fake account to the OS. |
+ */ |
+ public void addFakeAccountToOs(String username, String password) { |
+ addAccountToOs(username, password, AccountConstants.FAKE_ACCOUNT_TYPE); |
+ } |
+ |
+ /** |
+ * Removes a fake account from the OS. |
+ */ |
+ public void removeFakeAccountFromOs(String username) { |
+ Account account = new Account(username, AccountConstants.FAKE_ACCOUNT_TYPE); |
+ mAccountManager.removeAccountExplicitly(account); |
+ } |
+ |
+ /** |
+ * Removes all fake accounts from the OS. |
+ */ |
+ public void removeAllFakeAccountsFromOs() { |
+ removeAllAccountsOfTypeFromOs(AccountConstants.FAKE_ACCOUNT_TYPE); |
+ } |
+ |
+ /** |
+ * Checks if an existing fake account is on the OS. |
+ * |
+ * @return true if fake account is on OS, false otherwise. |
+ */ |
+ public boolean isExistingFakeAccountOnOs() { |
+ return mAccountManager.getAccountsByType(AccountConstants.FAKE_ACCOUNT_TYPE).length > 0; |
+ } |
+ |
+ /** |
+ * Adds a Gaia account to the OS. |
+ */ |
+ public void addGaiaAccountToOs(String username, String password, String type) { |
+ final Bundle options = new Bundle(); |
+ options.putString(AccountConstants.USERNAME, username); |
+ options.putString(AccountConstants.PASSWORD, password); |
+ options.putBoolean(AccountConstants.ALLOW_SKIP, true); |
+ |
+ AuthenticationCallback authCallback = new AuthenticationCallback(); |
+ mAccountManager.addAccount(AccountConstants.GOOGLE_ACCOUNT_TYPE, type, null, options, null, |
+ authCallback, null); |
+ |
+ Bundle authResult = authCallback.waitForAuthCompletion(); |
+ |
+ if (authResult.containsKey(AccountManager.KEY_ERROR_CODE)) { |
+ throw new IllegalStateException(String.format("AddAccount failed. Reason: %s", |
+ authResult.get(AccountManager.KEY_ERROR_MESSAGE))); |
+ } |
+ } |
+ |
+ /** |
+ * Checks to see if an existing Gaia account is on OS. |
+ * |
+ * @return {@code true} if one or more gaia accounts exists on OS, {@code false} otherwise |
+ */ |
+ public boolean isExistingGaiaAccountOnOs() { |
+ return mAccountManager.getAccountsByType(AccountConstants.GOOGLE_ACCOUNT_TYPE).length > 0; |
+ } |
+ |
+ /** |
+ * Removes all Gaia accounts from the OS. |
+ */ |
+ public void removeAllGaiaAccountsFromOs() { |
+ removeAllAccountsOfTypeFromOs(AccountConstants.GOOGLE_ACCOUNT_TYPE); |
+ } |
+ |
+ /** |
+ * Helper class for adding Gaia accounts to OS. |
+ * |
+ * Usage: Use this as the callback parameter when using {@link addAccount} in |
+ * {@link android.accounts.AccountManager}. |
+ */ |
+ private class AuthenticationCallback implements AccountManagerCallback<Bundle> { |
+ /** Stores the result of account authentication. Null means not finished. */ |
+ private Bundle mResultBundle = null; |
+ |
+ /** |
+ * Block and wait for the authentication callback to complete. |
+ * |
+ * @return the {@link Bundle} result from the authentication. |
+ */ |
+ public synchronized Bundle waitForAuthCompletion() { |
+ while (mResultBundle == null) { |
+ try { |
+ wait(); |
+ } catch (InterruptedException e) { |
+ // ignore |
+ } |
+ } |
+ return mResultBundle; |
+ } |
+ |
+ public void run(AccountManagerFuture<Bundle> future) { |
+ try { |
+ mResultBundle = future.getResult(); |
+ } catch (OperationCanceledException e) { |
+ mResultBundle = buildExceptionBundle(e); |
+ } catch (IOException e) { |
+ mResultBundle = buildExceptionBundle(e); |
+ } catch (AuthenticatorException e) { |
+ mResultBundle = buildExceptionBundle(e); |
+ } |
+ synchronized (this) { |
+ notifyAll(); |
+ } |
+ } |
+ |
+ /** |
+ * Create a result bundle for a given exception. |
+ * |
+ * @param e the exception to be made into a {@link Bundle} |
+ * @return the {@link Bundle} for the given exception e. |
+ */ |
+ private Bundle buildExceptionBundle(Exception e) { |
+ Bundle bundle = new Bundle(); |
+ bundle.putInt(AccountManager.KEY_ERROR_CODE, |
+ AccountManager.ERROR_CODE_INVALID_RESPONSE); |
+ bundle.putString(AccountManager.KEY_ERROR_MESSAGE, e.toString()); |
+ return bundle; |
+ } |
+ } |
+ |
+ /** |
+ * Adds a user account. If the account already exists, we do nothing. |
+ * |
+ * @param username the username of the account. |
+ * @param password the password of the account. |
+ * @param type the type of the account. |
+ * @throws RuntimeException if the account creation fails. |
+ */ |
+ public void addAccountToOs(String username, String password, String type) { |
+ Account account = new Account(username, type); |
+ mAccountManager.addAccountExplicitly(account, password, new Bundle()); |
+ } |
+ |
+ /** |
+ * Checks if an existing account is on the OS. |
+ * |
+ * @param username the username of the account signed in (e.g. test@example.com). |
+ * @param type the type of account on the OS (e.g. com.example). |
+ * @return true if account is signed in on OS, false otherwise. |
+ * @throws IllegalArgumentException if username is empty. |
+ */ |
+ private boolean isExistingAccountOnOs(String username, String type) { |
+ Account account = new Account(username, type); |
+ |
+ if (TextUtils.isEmpty(username)) { |
+ throw new IllegalArgumentException("ERROR: must specify account"); |
+ } |
+ |
+ // If the account already exists, return false |
+ for (Account acct : mAccountManager.getAccountsByType(type)) { |
+ if (acct.name.equals(username) && acct.type.equals(type)) { |
+ return true; |
+ } |
+ } |
+ return false; |
+ } |
+ |
+ /** |
+ * Removes all accounts of a certain type from OS. |
+ * |
+ * @param type the type of account on the OS (e.g. com.example). |
+ */ |
+ private void removeAllAccountsOfTypeFromOs(String type) { |
+ for (Account acct : mAccountManager.getAccountsByType(type)) { |
+ mAccountManager.removeAccount(acct, null, null); |
+ } |
+ } |
+} |