1 /*
2  * Copyright (C) 2010 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 com.android.email;
18 
19 import android.accounts.AccountManager;
20 import android.accounts.AccountManagerFuture;
21 import android.accounts.AuthenticatorException;
22 import android.accounts.OperationCanceledException;
23 import android.database.Cursor;
24 import android.test.ProviderTestCase2;
25 import android.test.suitebuilder.annotation.Suppress;
26 
27 import com.android.email.provider.EmailProvider;
28 import com.android.email.provider.ProviderTestUtils;
29 import com.android.emailcommon.provider.Account;
30 import com.android.emailcommon.provider.EmailContent;
31 
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.HashSet;
35 
36 /**
37  * Base class for unit tests that use {@link android.accounts.Account}.
38  */
39 @Suppress
40 public abstract class AccountTestCase extends ProviderTestCase2<EmailProvider> {
41 
42     protected static final String TEST_ACCOUNT_PREFIX = "__test";
43     protected static final String TEST_ACCOUNT_SUFFIX = "@android.com";
44     protected static final String TEST_ACCOUNT_TYPE = "com.android.test_exchange";
45 
AccountTestCase()46     public AccountTestCase() {
47         super(EmailProvider.class, EmailContent.AUTHORITY);
48     }
49 
getExchangeAccounts()50     protected android.accounts.Account[] getExchangeAccounts() {
51         return AccountManager.get(getContext()).getAccountsByType(TEST_ACCOUNT_TYPE);
52     }
53 
makeAccountManagerAccount(String username)54     protected android.accounts.Account makeAccountManagerAccount(String username) {
55         return new android.accounts.Account(username, TEST_ACCOUNT_TYPE);
56     }
57 
createAccountManagerAccount(String username)58     protected void createAccountManagerAccount(String username) {
59         final android.accounts.Account account = makeAccountManagerAccount(username);
60         AccountManager.get(getContext()).addAccountExplicitly(account, "password", null);
61     }
62 
setupProviderAndAccountManagerAccount(String username)63     protected Account setupProviderAndAccountManagerAccount(String username) {
64         // Note that setupAccount creates the email address username@android.com, so that's what
65         // we need to use for the account manager
66         createAccountManagerAccount(username + TEST_ACCOUNT_SUFFIX);
67         return ProviderTestUtils.setupAccount(username, true, getMockContext());
68     }
69 
makeExchangeServiceAccountList()70     protected ArrayList<Account> makeExchangeServiceAccountList() {
71         ArrayList<Account> accountList = new ArrayList<Account>();
72         Cursor c = getMockContext().getContentResolver().query(Account.CONTENT_URI,
73                 Account.CONTENT_PROJECTION, null, null, null);
74         try {
75             while (c.moveToNext()) {
76                 Account account = new Account();
77                 account.restore(c);
78                 accountList.add(account);
79             }
80         } finally {
81             c.close();
82         }
83         return accountList;
84     }
85 
deleteAccountManagerAccount(android.accounts.Account account)86     protected void deleteAccountManagerAccount(android.accounts.Account account) {
87         AccountManagerFuture<Boolean> future =
88             AccountManager.get(getContext()).removeAccount(account, null, null);
89         try {
90             future.getResult();
91         } catch (OperationCanceledException e) {
92         } catch (AuthenticatorException e) {
93         } catch (IOException e) {
94         }
95     }
96 
deleteTemporaryAccountManagerAccounts()97     protected void deleteTemporaryAccountManagerAccounts() {
98         for (android.accounts.Account accountManagerAccount: getExchangeAccounts()) {
99             if (accountManagerAccount.name.startsWith(TEST_ACCOUNT_PREFIX) &&
100                     accountManagerAccount.name.endsWith(TEST_ACCOUNT_SUFFIX)) {
101                 deleteAccountManagerAccount(accountManagerAccount);
102             }
103         }
104     }
105 
getTestAccountName(String name)106     protected String getTestAccountName(String name) {
107         return TEST_ACCOUNT_PREFIX + name;
108     }
109 
getTestAccountEmailAddress(String name)110     protected String getTestAccountEmailAddress(String name) {
111         return TEST_ACCOUNT_PREFIX + name + TEST_ACCOUNT_SUFFIX;
112     }
113 
114     /**
115      * Helper to retrieve account manager accounts *and* remove any preexisting accounts
116      * from the list, to "hide" them from the reconciler.
117      */
getAccountManagerAccounts( android.accounts.Account[] baseline)118     protected android.accounts.Account[] getAccountManagerAccounts(
119             android.accounts.Account[] baseline) {
120         android.accounts.Account[] rawList = getExchangeAccounts();
121         if (baseline.length == 0) {
122             return rawList;
123         }
124         HashSet<android.accounts.Account> set = new HashSet<android.accounts.Account>();
125         for (android.accounts.Account addAccount : rawList) {
126             set.add(addAccount);
127         }
128         for (android.accounts.Account removeAccount : baseline) {
129             set.remove(removeAccount);
130         }
131         return set.toArray(new android.accounts.Account[0]);
132     }
133 }
134