OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.chrome.test.util; | 5 package org.chromium.chrome.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.AccountManagerCallback; | 9 import android.accounts.AccountManagerCallback; |
10 import android.accounts.AccountManagerFuture; | 10 import android.accounts.AccountManagerFuture; |
(...skipping 17 matching lines...) Expand all Loading... |
28 /** Bundle tags */ | 28 /** Bundle tags */ |
29 private static final String USERNAME = "username"; | 29 private static final String USERNAME = "username"; |
30 private static final String PASSWORD = "password"; | 30 private static final String PASSWORD = "password"; |
31 private static final String ALLOW_SKIP = "allowSkip"; | 31 private static final String ALLOW_SKIP = "allowSkip"; |
32 | 32 |
33 /** Information for Google OS account */ | 33 /** Information for Google OS account */ |
34 private static final String GOOGLE_ACCOUNT_TYPE = "com.google"; | 34 private static final String GOOGLE_ACCOUNT_TYPE = "com.google"; |
35 | 35 |
36 private AccountManager mAccountManager; | 36 private AccountManager mAccountManager; |
37 private FakeAccountManagerDelegate mFakeAccountManagerDelegate; | 37 private FakeAccountManagerDelegate mFakeAccountManagerDelegate; |
38 private Context mContext; | |
39 private Context mTargetContext; | 38 private Context mTargetContext; |
40 | 39 |
41 /** | 40 /** |
42 * The constructor for SigninUtils for a given test case. | 41 * The constructor for SigninUtils for a given test case. |
43 * | 42 * |
44 * @param instrumentation the {@link android.app.Instrumentation} to perform
signin operations | 43 * @param instrumentation the {@link android.app.Instrumentation} to perform
signin operations |
45 * with. | 44 * with. |
46 */ | 45 */ |
47 public ChromeSigninUtils(Instrumentation instrumentation) { | 46 public ChromeSigninUtils(Instrumentation instrumentation) { |
48 mContext = instrumentation.getContext(); | |
49 mTargetContext = instrumentation.getTargetContext(); | 47 mTargetContext = instrumentation.getTargetContext(); |
50 mAccountManager = AccountManager.get(mTargetContext); | 48 mAccountManager = AccountManager.get(mTargetContext); |
51 mFakeAccountManagerDelegate = new FakeAccountManagerDelegate(mContext); | 49 mFakeAccountManagerDelegate = new FakeAccountManagerDelegate(); |
52 } | 50 } |
53 | 51 |
54 /** | 52 /** |
55 * Adds an account to the app. | 53 * Adds an account to the app. |
56 * | 54 * |
57 * @param username the username for the logged in account; must be a Google
account. | 55 * @param username the username for the logged in account; must be a Google
account. |
58 */ | 56 */ |
59 public void addAccountToApp(String username) { | 57 public void addAccountToApp(String username) { |
60 if (TextUtils.isEmpty(username)) { | 58 if (TextUtils.isEmpty(username)) { |
61 throw new IllegalArgumentException("ERROR: must specify account"); | 59 throw new IllegalArgumentException("ERROR: must specify account"); |
62 } | 60 } |
63 | 61 |
64 if (ChromeSigninController.get().isSignedIn()) { | 62 if (ChromeSigninController.get().isSignedIn()) { |
65 ChromeSigninController.get().setSignedInAccountName(null); | 63 ChromeSigninController.get().setSignedInAccountName(null); |
66 } | 64 } |
67 ChromeSigninController.get().setSignedInAccountName(username); | 65 ChromeSigninController.get().setSignedInAccountName(username); |
68 } | 66 } |
69 | 67 |
70 /** | 68 /** |
71 * Adds a fake account to the OS. | 69 * Adds a fake account to the OS. |
72 */ | 70 */ |
73 public void addFakeAccountToOs(String username, String password) { | 71 public void addFakeAccountToOs(String username, String password) { |
74 if (TextUtils.isEmpty(username)) { | 72 if (TextUtils.isEmpty(username)) { |
75 throw new IllegalArgumentException("ERROR: must specify account"); | 73 throw new IllegalArgumentException("ERROR: must specify account"); |
76 } | 74 } |
77 | 75 |
78 Account account = new Account(username, GOOGLE_ACCOUNT_TYPE); | 76 Account account = new Account(username, GOOGLE_ACCOUNT_TYPE); |
79 mFakeAccountManagerDelegate = new FakeAccountManagerDelegate(mContext, a
ccount); | 77 mFakeAccountManagerDelegate = new FakeAccountManagerDelegate(account); |
80 AccountHolder accountHolder = AccountHolder.builder(account).password(pa
ssword).build(); | 78 AccountHolder accountHolder = AccountHolder.builder(account).password(pa
ssword).build(); |
81 mFakeAccountManagerDelegate.addAccountHolderExplicitly(accountHolder); | 79 mFakeAccountManagerDelegate.addAccountHolderExplicitly(accountHolder); |
82 } | 80 } |
83 | 81 |
84 /** | 82 /** |
85 * Removes all fake accounts from the OS. | 83 * Removes all fake accounts from the OS. |
86 */ | 84 */ |
87 public void removeAllFakeAccountsFromOs() { | 85 public void removeAllFakeAccountsFromOs() { |
88 for (Account acct : mFakeAccountManagerDelegate.getAccountsByType(GOOGLE
_ACCOUNT_TYPE)) { | 86 for (Account acct : mFakeAccountManagerDelegate.getAccountsByType(GOOGLE
_ACCOUNT_TYPE)) { |
89 mFakeAccountManagerDelegate.removeAccountHolderExplicitly( | 87 mFakeAccountManagerDelegate.removeAccountHolderExplicitly( |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 */ | 219 */ |
222 private Bundle buildExceptionBundle(Exception e) { | 220 private Bundle buildExceptionBundle(Exception e) { |
223 Bundle bundle = new Bundle(); | 221 Bundle bundle = new Bundle(); |
224 bundle.putInt(AccountManager.KEY_ERROR_CODE, | 222 bundle.putInt(AccountManager.KEY_ERROR_CODE, |
225 AccountManager.ERROR_CODE_INVALID_RESPONSE); | 223 AccountManager.ERROR_CODE_INVALID_RESPONSE); |
226 bundle.putString(AccountManager.KEY_ERROR_MESSAGE, e.toString()); | 224 bundle.putString(AccountManager.KEY_ERROR_MESSAGE, e.toString()); |
227 return bundle; | 225 return bundle; |
228 } | 226 } |
229 } | 227 } |
230 } | 228 } |
OLD | NEW |