Home | History | Annotate | Download | only in adapter
      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.exchange.adapter;
     18 
     19 import android.content.ContentResolver;
     20 import android.content.ContentUris;
     21 import android.content.Context;
     22 import android.test.AndroidTestCase;
     23 
     24 import com.android.emailcommon.provider.Account;
     25 import com.android.emailcommon.provider.Mailbox;
     26 import com.android.exchange.EasSyncService;
     27 import com.android.exchange.adapter.AbstractSyncAdapter;
     28 import com.android.exchange.adapter.EmailSyncAdapter;
     29 import com.android.exchange.adapter.EmailSyncAdapter.EasEmailSyncParser;
     30 
     31 import java.io.ByteArrayInputStream;
     32 import java.io.InputStream;
     33 import java.lang.reflect.Constructor;
     34 import java.lang.reflect.InvocationTargetException;
     35 
     36 public class SyncAdapterTestCase<T extends AbstractSyncAdapter> extends AndroidTestCase {
     37     public Context mContext;
     38     public Context mProviderContext;
     39     public ContentResolver mResolver;
     40     public Mailbox mMailbox;
     41     public Account mAccount;
     42     public EmailSyncAdapter mSyncAdapter;
     43     public EasEmailSyncParser mSyncParser;
     44 
     45     public SyncAdapterTestCase() {
     46     }
     47 
     48     @Override
     49     public void setUp() throws Exception {
     50         super.setUp();
     51         mContext = getContext();
     52         // This could be used with a MockContext if we switch over
     53         mProviderContext = mContext;
     54         mResolver = mContext.getContentResolver();
     55     }
     56 
     57     @Override
     58     public void tearDown() throws Exception {
     59         super.tearDown();
     60         // If we've created and saved an account, delete it
     61         if (mAccount != null && mAccount.mId > 0) {
     62             mResolver.delete(ContentUris.withAppendedId(Account.CONTENT_URI, mAccount.mId), null,
     63                     null);
     64         }
     65     }
     66 
     67     /**
     68      * Create and return a short, simple InputStream that has at least four bytes, which is all
     69      * that's required to initialize an EasParser (the parent class of EasEmailSyncParser)
     70      * @return the InputStream
     71      */
     72     public InputStream getTestInputStream() {
     73         return new ByteArrayInputStream(new byte[] {0, 0, 0, 0, 0});
     74     }
     75 
     76     EasSyncService getTestService() {
     77         mAccount = new Account();
     78         mAccount.mEmailAddress = "__test__ (at) android.com";
     79         mAccount.mId = -1;
     80         Mailbox mailbox = new Mailbox();
     81         mailbox.mId = -1;
     82         return getTestService(mAccount, mailbox);
     83     }
     84 
     85     EasSyncService getTestService(Account account, Mailbox mailbox) {
     86         EasSyncService service = new EasSyncService();
     87         service.mContext = mContext;
     88         service.mMailbox = mailbox;
     89         service.mAccount = account;
     90         service.mContentResolver = mContext.getContentResolver();
     91         return service;
     92     }
     93 
     94     protected T getTestSyncAdapter(Class<T> klass) {
     95         EasSyncService service = getTestService();
     96         Constructor<T> c;
     97         try {
     98             c = klass.getDeclaredConstructor(new Class[] {EasSyncService.class});
     99             return c.newInstance(service);
    100         } catch (SecurityException e) {
    101         } catch (NoSuchMethodException e) {
    102         } catch (IllegalArgumentException e) {
    103         } catch (InstantiationException e) {
    104         } catch (IllegalAccessException e) {
    105         } catch (InvocationTargetException e) {
    106         }
    107         return null;
    108     }
    109 
    110 }
    111