DBTestHelper.java revision d1ee5b8fa5fe92df1ded5953a9e3f001b38a1ac7
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 17package com.android.email; 18 19import com.android.email.provider.AttachmentProvider; 20import com.android.email.provider.EmailContent; 21import com.android.email.provider.EmailProvider; 22 23import android.content.ContentProvider; 24import android.content.ContentResolver; 25import android.content.ContentValues; 26import android.content.Context; 27import android.content.res.Resources; 28import android.database.Cursor; 29import android.net.Uri; 30import android.test.IsolatedContext; 31import android.test.RenamingDelegatingContext; 32import android.test.mock.MockContentResolver; 33import android.test.mock.MockContext; 34import android.test.mock.MockCursor; 35 36import java.io.File; 37 38import junit.framework.Assert; 39 40/** 41 * Helper classes (and possibly methods) for database related tests. 42 */ 43public final class DBTestHelper { 44 private DBTestHelper() { // Utility class. No instantiation. 45 } 46 47 /** 48 * A simple {@link Context} that returns {@link MyProvider} as the email content provider. 49 */ 50 public static class MyContext extends MockContext { 51 private final MockContentResolver mContentResolver; 52 private final MyProvider mProvider; 53 54 public MyContext() { 55 mProvider = new MyProvider(); 56 mContentResolver = new MockContentResolver(); 57 mContentResolver.addProvider(EmailProvider.EMAIL_AUTHORITY, mProvider); 58 } 59 60 @Override 61 public ContentResolver getContentResolver() { 62 return mContentResolver; 63 } 64 65 public MyProvider getMyProvider() { 66 return mProvider; 67 } 68 } 69 70 /** 71 * A simply {@link ContentProvider} to mock out {@link ContentProvider#query}. 72 */ 73 public static class MyProvider extends ContentProvider { 74 public Cursor mQueryPresetResult; 75 public Uri mPassedUri; 76 public String[] mPassedProjection; 77 public String mPassedSelection; 78 public String[] mPassedSelectionArgs; 79 public String mPassedSortOrder; 80 81 public void reset() { 82 mQueryPresetResult = null; 83 mPassedUri = null; 84 mPassedProjection = null; 85 mPassedSelection = null; 86 mPassedSelectionArgs = null; 87 mPassedSortOrder = null; 88 } 89 90 @Override 91 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 92 String sortOrder) { 93 mPassedUri = uri; 94 mPassedProjection = projection; 95 mPassedSelection = selection; 96 mPassedSelectionArgs = selectionArgs; 97 mPassedSortOrder = sortOrder; 98 return mQueryPresetResult; 99 } 100 101 @Override 102 public int delete(Uri uri, String selection, String[] selectionArgs) { 103 throw new UnsupportedOperationException(); 104 } 105 106 @Override 107 public String getType(Uri uri) { 108 throw new UnsupportedOperationException(); 109 } 110 111 @Override 112 public Uri insert(Uri uri, ContentValues values) { 113 throw new UnsupportedOperationException(); 114 } 115 116 @Override 117 public boolean onCreate() { 118 throw new UnsupportedOperationException(); 119 } 120 121 @Override 122 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { 123 throw new UnsupportedOperationException(); 124 } 125 } 126 127 /** 128 * Simple {@link MockCursor} subclass that implements common methods. 129 */ 130 public static class EasyMockCursor extends MockCursor { 131 public int mCount; 132 public boolean mClosed; 133 134 public EasyMockCursor(int count) { 135 mCount = count; 136 } 137 138 @Override 139 public int getCount() { 140 return mCount; 141 } 142 143 @Override 144 public void close() { 145 mClosed = true; 146 } 147 } 148 149 /** 150 * This class has only one method, that creats an isolated {@link Context} similar to what 151 * {@link android.test.ProviderTestCase2} provides. 152 * 153 * The method also creates a {@link ContentProvider} of {@code providerClass}, and add it to 154 * the context. See the javadoc on android.test.ProviderTestCase2 for the details. 155 */ 156 public static class ProviderContextSetupHelper { 157 // Based on ProviderTestCase2.MockContext2. 158 private static class MockContext2 extends MockContext { 159 private final Context mBaseContext; 160 161 public MockContext2(Context baseContext) { 162 mBaseContext = baseContext; 163 } 164 165 @Override 166 public Resources getResources() { 167 return mBaseContext.getResources(); 168 } 169 170 @Override 171 public File getDir(String name, int mode) { 172 return mBaseContext.getDir("mockcontext2_" + name, mode); 173 } 174 } 175 176 /** {@link IsolatedContext} + getApplicationContext() */ 177 private static class MyIsolatedContext extends IsolatedContext { 178 public MyIsolatedContext(ContentResolver resolver, Context targetContext) { 179 super(resolver, targetContext); 180 } 181 182 @Override 183 public Context getApplicationContext() { 184 return this; 185 } 186 } 187 188 // Based on ProviderTestCase2.setUp(). 189 public static Context getProviderContext(Context context) throws Exception { 190 MockContentResolver resolver = new MockContentResolver(); 191 final String filenamePrefix = "test."; 192 RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext( 193 new MockContext2(context), // The context that most methods are delegated to 194 context, // The context that file methods are delegated to 195 filenamePrefix); 196 final Context providerContext = new MyIsolatedContext(resolver, targetContextWrapper); 197 providerContext.getContentResolver(); 198 199 // register EmailProvider and AttachmentProvider. 200 final EmailProvider ep = new EmailProvider(); 201 ep.attachInfo(providerContext, null); 202 resolver.addProvider(EmailContent.AUTHORITY, ep); 203 204 final AttachmentProvider ap = new AttachmentProvider(); 205 ap.attachInfo(providerContext, null); 206 resolver.addProvider(AttachmentProvider.AUTHORITY, ap); 207 return providerContext; 208 } 209 } 210} 211