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