Fix MailService unit tests

* These broke when we split Email/Exchange
* We now use a test authenticator and a test account type, which
  is actually far cleaner than dealing with actual accounts

Change-Id: Ib7c9f884eac484c33642a8036f47466ce641c567
This commit is contained in:
Marc Blank 2011-06-21 12:11:57 -07:00
parent 369905c95d
commit cbc842c5c9
5 changed files with 195 additions and 21 deletions

View File

@ -40,8 +40,7 @@
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.GET_ACCOUNTS" />
<uses-permission
android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/>
<uses-permission
android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission
@ -398,6 +397,21 @@
android:resource="@xml/eas_authenticator"
/>
</service>
<!--Required stanza to register the EasTestAuthenticatorService with AccountManager -->
<service
android:name=".service.EasTestAuthenticatorService"
android:exported="true"
android:enabled="false"
>
<intent-filter>
<action
android:name="android.accounts.AccountAuthenticator" />
</intent-filter>
<meta-data
android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/eastest_authenticator"
/>
</service>
<!--
EasAuthenticatorService with the alternative label. Disabled by default,
and OneTimeInitializer enables it if the vendor policy tells so.

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
/**
* Copyright (c) 2011, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-->
<!-- The attributes in this XML file provide configuration information -->
<!-- for the Account Manager. -->
<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="com.android.test_exchange"
android:icon="@drawable/ic_exchange_selected"
android:smallIcon="@drawable/ic_exchange_minitab_selected"
android:label="com.android.test_exchange"
android:accountPreferences="@xml/account_preferences"
/>

View File

@ -0,0 +1,122 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.email.service;
import android.accounts.AbstractAccountAuthenticator;
import android.accounts.Account;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager;
import android.accounts.NetworkErrorException;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import com.android.email.activity.setup.AccountSetupBasics;
/**
* Anauthenticator service for reconciliation tests; it simply adds the account to AccountManager
* directly with a username and password.
*/
public class EasTestAuthenticatorService extends Service {
public static final String OPTIONS_USERNAME = "username";
public static final String OPTIONS_PASSWORD = "password";
private static final String TEST_ACCOUNT_TYPE = "com.android.test_exchange";
class EasAuthenticator extends AbstractAccountAuthenticator {
public EasAuthenticator(Context context) {
super(context);
}
@Override
public Bundle addAccount(AccountAuthenticatorResponse response, String accountType,
String authTokenType, String[] requiredFeatures, Bundle options)
throws NetworkErrorException {
// There are two cases here:
// 1) We are called with a username/password; this comes from the traditional email
// app UI; we simply create the account and return the proper bundle
if (options != null && options.containsKey(OPTIONS_PASSWORD)
&& options.containsKey(OPTIONS_USERNAME)) {
final Account account = new Account(options.getString(OPTIONS_USERNAME),
TEST_ACCOUNT_TYPE);
AccountManager.get(EasTestAuthenticatorService.this).addAccountExplicitly(
account, options.getString(OPTIONS_PASSWORD), null);
Bundle b = new Bundle();
b.putString(AccountManager.KEY_ACCOUNT_NAME, TEST_ACCOUNT_TYPE);
return b;
// 2) The other case is that we're creating a new account from an Account manager
// activity. In this case, we add an intent that will be used to gather the
// account information...
} else {
Bundle b = new Bundle();
Intent intent =
AccountSetupBasics.actionSetupExchangeIntent(EasTestAuthenticatorService.this);
intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
b.putParcelable(AccountManager.KEY_INTENT, intent);
return b;
}
}
@Override
public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account,
Bundle options) {
return null;
}
@Override
public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
return null;
}
@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account,
String authTokenType, Bundle loginOptions) throws NetworkErrorException {
return null;
}
@Override
public String getAuthTokenLabel(String authTokenType) {
// null means we don't have compartmentalized authtoken types
return null;
}
@Override
public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account,
String[] features) throws NetworkErrorException {
return null;
}
@Override
public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account,
String authTokenType, Bundle loginOptions) {
return null;
}
}
@Override
public IBinder onBind(Intent intent) {
if (AccountManager.ACTION_AUTHENTICATOR_INTENT.equals(intent.getAction())) {
return new EasAuthenticator(this).getIBinder();
} else {
return null;
}
}
}

View File

@ -16,12 +16,6 @@
package com.android.email;
import com.android.email.provider.EmailProvider;
import com.android.email.provider.ProviderTestUtils;
import com.android.emailcommon.AccountManagerTypes;
import com.android.emailcommon.provider.Account;
import com.android.emailcommon.provider.EmailContent;
import android.accounts.AccountManager;
import android.accounts.AccountManagerFuture;
import android.accounts.AuthenticatorException;
@ -29,6 +23,11 @@ import android.accounts.OperationCanceledException;
import android.database.Cursor;
import android.test.ProviderTestCase2;
import com.android.email.provider.EmailProvider;
import com.android.email.provider.ProviderTestUtils;
import com.android.emailcommon.provider.Account;
import com.android.emailcommon.provider.EmailContent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
@ -40,18 +39,18 @@ public abstract class AccountTestCase extends ProviderTestCase2<EmailProvider> {
protected static final String TEST_ACCOUNT_PREFIX = "__test";
protected static final String TEST_ACCOUNT_SUFFIX = "@android.com";
protected static final String TEST_ACCOUNT_TYPE = "com.android.test_exchange";
public AccountTestCase() {
super(EmailProvider.class, EmailContent.AUTHORITY);
}
protected android.accounts.Account[] getExchangeAccounts() {
return AccountManager.get(getContext())
.getAccountsByType(AccountManagerTypes.TYPE_EXCHANGE);
return AccountManager.get(getContext()).getAccountsByType(TEST_ACCOUNT_TYPE);
}
protected android.accounts.Account makeAccountManagerAccount(String username) {
return new android.accounts.Account(username, AccountManagerTypes.TYPE_EXCHANGE);
return new android.accounts.Account(username, TEST_ACCOUNT_TYPE);
}
protected void createAccountManagerAccount(String username) {

View File

@ -16,22 +16,22 @@
package com.android.email.service;
import android.accounts.AccountManager;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.PackageManager;
import com.android.email.AccountTestCase;
import com.android.email.Controller;
import com.android.email.provider.EmailProvider;
import com.android.email.provider.ProviderTestUtils;
import com.android.email.service.MailService.AccountSyncReport;
import com.android.emailcommon.AccountManagerTypes;
import com.android.emailcommon.provider.Account;
import com.android.emailcommon.provider.EmailContent;
import com.android.emailcommon.provider.HostAuth;
import com.android.emailcommon.utility.Utility;
import android.accounts.AccountManager;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import java.util.HashMap;
@ -53,6 +53,11 @@ public class MailServiceTests extends AccountTestCase {
@Override
public void setUp() throws Exception {
super.setUp();
PackageManager pm = getContext().getPackageManager();
pm.setComponentEnabledSetting(
new ComponentName(getContext(), EasTestAuthenticatorService.class),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
mMockContext = getMockContext();
// Delete any test accounts we might have created earlier
deleteTemporaryAccountManagerAccounts();
@ -63,6 +68,11 @@ public class MailServiceTests extends AccountTestCase {
super.tearDown();
// Delete any test accounts we might have created earlier
deleteTemporaryAccountManagerAccounts();
PackageManager pm = getContext().getPackageManager();
pm.setComponentEnabledSetting(
new ComponentName(getContext(), EasTestAuthenticatorService.class),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
/**
@ -87,7 +97,7 @@ public class MailServiceTests extends AccountTestCase {
context.getContentResolver().delete(firstAccount.getUri(), null, null);
// delete the account manager account
android.accounts.Account[] accountManagerAccounts = AccountManager.get(context)
.getAccountsByType(AccountManagerTypes.TYPE_EXCHANGE);
.getAccountsByType(TEST_ACCOUNT_TYPE);
for (android.accounts.Account accountManagerAccount: accountManagerAccounts) {
if ((TEST_USER_ACCOUNT + TEST_ACCOUNT_SUFFIX)
.equals(accountManagerAccount.name)) {
@ -112,7 +122,7 @@ public class MailServiceTests extends AccountTestCase {
// Capture the baseline (account manager accounts) so we can measure the changes
// we're making, irrespective of the number of actual accounts, and not destroy them
android.accounts.Account[] baselineAccounts =
AccountManager.get(context).getAccountsByType(AccountManagerTypes.TYPE_EXCHANGE);
AccountManager.get(context).getAccountsByType(TEST_ACCOUNT_TYPE);
// Set up three accounts, both in AccountManager and in EmailProvider
Account firstAccount = setupProviderAndAccountManagerAccount(getTestAccountName("1"));