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 com.android.email.provider.EmailProvider; 20 import com.android.email.provider.EmailContent.Account; 21 import com.android.email.provider.EmailContent.Mailbox; 22 import com.android.exchange.EasSyncService; 23 import com.android.exchange.adapter.EmailSyncAdapter.EasEmailSyncParser; 24 25 import android.content.ContentResolver; 26 import android.content.Context; 27 import android.test.ProviderTestCase2; 28 29 import java.io.ByteArrayInputStream; 30 import java.io.InputStream; 31 import java.lang.reflect.Constructor; 32 import java.lang.reflect.InvocationTargetException; 33 34 public class SyncAdapterTestCase<T extends AbstractSyncAdapter> 35 extends ProviderTestCase2<EmailProvider> { 36 EmailProvider mProvider; 37 Context mMockContext; 38 ContentResolver mMockResolver; 39 Mailbox mMailbox; 40 Account mAccount; 41 EmailSyncAdapter mSyncAdapter; 42 EasEmailSyncParser mSyncParser; 43 44 public SyncAdapterTestCase() { 45 super(EmailProvider.class, EmailProvider.EMAIL_AUTHORITY); 46 } 47 48 @Override 49 public void setUp() throws Exception { 50 super.setUp(); 51 mMockContext = getMockContext(); 52 mMockResolver = mMockContext.getContentResolver(); 53 } 54 55 @Override 56 public void tearDown() throws Exception { 57 super.tearDown(); 58 } 59 60 /** 61 * Create and return a short, simple InputStream that has at least four bytes, which is all 62 * that's required to initialize an EasParser (the parent class of EasEmailSyncParser) 63 * @return the InputStream 64 */ 65 public InputStream getTestInputStream() { 66 return new ByteArrayInputStream(new byte[] {0, 0, 0, 0, 0}); 67 } 68 69 EasSyncService getTestService() { 70 Account account = new Account(); 71 account.mEmailAddress = "__test__ (at) android.com"; 72 account.mId = -1; 73 Mailbox mailbox = new Mailbox(); 74 mailbox.mId = -1; 75 return getTestService(account, mailbox); 76 } 77 78 EasSyncService getTestService(Account account, Mailbox mailbox) { 79 EasSyncService service = new EasSyncService(); 80 service.mContext = mMockContext; 81 service.mMailbox = mailbox; 82 service.mAccount = account; 83 service.mContentResolver = mMockResolver; 84 return service; 85 } 86 87 T getTestSyncAdapter(Class<T> klass) { 88 EasSyncService service = getTestService(); 89 Constructor<T> c; 90 try { 91 c = klass.getDeclaredConstructor(new Class[] {Mailbox.class, EasSyncService.class}); 92 return c.newInstance(service.mMailbox, service); 93 } catch (SecurityException e) { 94 } catch (NoSuchMethodException e) { 95 } catch (IllegalArgumentException e) { 96 } catch (InstantiationException e) { 97 } catch (IllegalAccessException e) { 98 } catch (InvocationTargetException e) { 99 } 100 return null; 101 } 102 103 } 104