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