1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.accounts.cts; 18 19 import android.accounts.AbstractAccountAuthenticator; 20 import android.accounts.Account; 21 import android.accounts.AccountAuthenticatorResponse; 22 import android.accounts.AccountManager; 23 import android.accounts.NetworkErrorException; 24 import android.content.Context; 25 import android.os.Bundle; 26 27 import java.util.ArrayList; 28 29 /** 30 * A simple Mock Account Authenticator 31 */ 32 public class MockAccountAuthenticator extends AbstractAccountAuthenticator { 33 34 private AccountAuthenticatorResponse mResponse; 35 private String mAccountType; 36 private String mAuthTokenType; 37 private String[] mRequiredFeatures; 38 public Bundle mOptionsUpdateCredentials; 39 public Bundle mOptionsConfirmCredentials; 40 public Bundle mOptionsAddAccount; 41 public Bundle mOptionsGetAuthToken; 42 private Account mAccount; 43 private String[] mFeatures; 44 45 private final ArrayList<String> mockFeatureList = new ArrayList<String>(); 46 47 public MockAccountAuthenticator(Context context) { 48 super(context); 49 50 // Create some mock features 51 mockFeatureList.add(AccountManagerTest.FEATURE_1); 52 mockFeatureList.add(AccountManagerTest.FEATURE_2); 53 } 54 55 public AccountAuthenticatorResponse getResponse() { 56 return mResponse; 57 } 58 59 public String getAccountType() { 60 return mAccountType; 61 } 62 63 public String getAuthTokenType() { 64 return mAuthTokenType; 65 } 66 67 public String[] getRequiredFeatures() { 68 return mRequiredFeatures; 69 } 70 71 public Account getAccount() { 72 return mAccount; 73 } 74 75 public String[] getFeatures() { 76 return mFeatures; 77 } 78 79 public void clearData() { 80 mResponse = null; 81 mAccountType = null; 82 mAuthTokenType = null; 83 mRequiredFeatures = null; 84 mOptionsUpdateCredentials = null; 85 mOptionsAddAccount = null; 86 mOptionsGetAuthToken = null; 87 mOptionsConfirmCredentials = null; 88 mAccount = null; 89 mFeatures = null; 90 } 91 92 private Bundle createResultBundle() { 93 Bundle result = new Bundle(); 94 result.putString(AccountManager.KEY_ACCOUNT_NAME, AccountManagerTest.ACCOUNT_NAME); 95 result.putString(AccountManager.KEY_ACCOUNT_TYPE, AccountManagerTest.ACCOUNT_TYPE); 96 result.putString(AccountManager.KEY_AUTHTOKEN, AccountManagerTest.AUTH_TOKEN); 97 98 return result; 99 } 100 101 /** 102 * Adds an account of the specified accountType. 103 */ 104 @Override 105 public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, 106 String authTokenType, String[] requiredFeatures, Bundle options) 107 throws NetworkErrorException { 108 109 this.mResponse = response; 110 this.mAccountType = accountType; 111 this.mAuthTokenType = authTokenType; 112 this.mRequiredFeatures = requiredFeatures; 113 this.mOptionsAddAccount = options; 114 115 return createResultBundle(); 116 } 117 118 /** 119 * Update the locally stored credentials for an account. 120 */ 121 @Override 122 public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, 123 String authTokenType, Bundle options) throws NetworkErrorException { 124 125 this.mResponse = response; 126 this.mAccount = account; 127 this.mAuthTokenType = authTokenType; 128 this.mOptionsUpdateCredentials = options; 129 130 return createResultBundle(); 131 } 132 133 /** 134 * Returns a Bundle that contains the Intent of the activity that can be used to edit the 135 * properties. In order to indicate success the activity should call response.setResult() 136 * with a non-null Bundle. 137 */ 138 @Override 139 public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) { 140 141 this.mResponse = response; 142 this.mAccountType = accountType; 143 144 return createResultBundle(); 145 } 146 147 /** 148 * Checks that the user knows the credentials of an account. 149 */ 150 @Override 151 public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, 152 Bundle options) throws NetworkErrorException { 153 154 this.mResponse = response; 155 this.mAccount = account; 156 this.mOptionsConfirmCredentials = options; 157 158 Bundle result = new Bundle(); 159 result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true); 160 161 return result; 162 } 163 164 /** 165 * Gets the authtoken for an account. 166 */ 167 @Override 168 public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, 169 String authTokenType, Bundle options) throws NetworkErrorException { 170 171 this.mResponse = response; 172 this.mAccount = account; 173 this.mAuthTokenType = authTokenType; 174 this.mOptionsGetAuthToken = options; 175 176 return createResultBundle(); 177 } 178 179 /** 180 * Ask the authenticator for a localized label for the given authTokenType. 181 */ 182 @Override 183 public String getAuthTokenLabel(String authTokenType) { 184 this.mAuthTokenType = authTokenType; 185 186 return AccountManagerTest.AUTH_TOKEN_LABEL; 187 } 188 189 /** 190 * Checks if the account supports all the specified authenticator specific features. 191 */ 192 @Override 193 public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, 194 String[] features) throws NetworkErrorException { 195 196 this.mResponse = response; 197 this.mAccount = account; 198 this.mFeatures = features; 199 200 Bundle result = new Bundle(); 201 if (null == features) { 202 result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true); 203 } 204 else { 205 boolean booleanResult = true; 206 for (String feature: features) { 207 if (!mockFeatureList.contains(feature)) { 208 booleanResult = false; 209 break; 210 } 211 } 212 result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, booleanResult); 213 } 214 return result; 215 } 216 }