1 /* 2 * Copyright (C) 2007 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 android.test; 18 19 import android.content.ContentProvider; 20 import android.content.ContentResolver; 21 import android.content.Context; 22 import android.test.mock.MockContext; 23 import android.test.mock.MockContentResolver; 24 import android.database.DatabaseUtils; 25 26 /** 27 * If you would like to test a single content provider with an 28 * {@link InstrumentationTestCase}, this provides some of the boiler plate in {@link #setUp} and 29 * {@link #tearDown}. 30 * 31 * @deprecated this class extends InstrumentationTestCase but should extend AndroidTestCase. Use 32 * ProviderTestCase2, which corrects this problem, instead. 33 */ 34 @Deprecated 35 public abstract class ProviderTestCase<T extends ContentProvider> 36 extends InstrumentationTestCase { 37 38 Class<T> mProviderClass; 39 String mProviderAuthority; 40 41 private IsolatedContext mProviderContext; 42 private MockContentResolver mResolver; 43 ProviderTestCase(Class<T> providerClass, String providerAuthority)44 public ProviderTestCase(Class<T> providerClass, String providerAuthority) { 45 mProviderClass = providerClass; 46 mProviderAuthority = providerAuthority; 47 } 48 49 /** 50 * The content provider that will be set up for use in each test method. 51 */ 52 private T mProvider; 53 getProvider()54 public T getProvider() { 55 return mProvider; 56 } 57 58 @Override setUp()59 protected void setUp() throws Exception { 60 super.setUp(); 61 62 mResolver = new MockContentResolver(); 63 final String filenamePrefix = "test."; 64 RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext( 65 new MockContext(), // The context that most methods are delegated to 66 getInstrumentation().getTargetContext(), // The context that file methods are delegated to 67 filenamePrefix); 68 mProviderContext = new IsolatedContext(mResolver, targetContextWrapper); 69 70 mProvider = ProviderTestCase2.createProviderForTest( 71 mProviderContext, mProviderClass, mProviderAuthority); 72 mResolver.addProvider(mProviderAuthority, getProvider()); 73 } 74 75 /** 76 * Tears down the environment for the test fixture. 77 * <p> 78 * Calls {@link android.content.ContentProvider#shutdown()} on the 79 * {@link android.content.ContentProvider} represented by mProvider. 80 */ 81 @Override tearDown()82 protected void tearDown() throws Exception { 83 mProvider.shutdown(); 84 super.tearDown(); 85 } 86 getMockContentResolver()87 public MockContentResolver getMockContentResolver() { 88 return mResolver; 89 } 90 getMockContext()91 public IsolatedContext getMockContext() { 92 return mProviderContext; 93 } 94 newResolverWithContentProviderFromSql( Context targetContext, Class<T> providerClass, String authority, String databaseName, int databaseVersion, String sql)95 public static <T extends ContentProvider> ContentResolver newResolverWithContentProviderFromSql( 96 Context targetContext, Class<T> providerClass, String authority, 97 String databaseName, int databaseVersion, String sql) 98 throws IllegalAccessException, InstantiationException { 99 final String filenamePrefix = "test."; 100 MockContentResolver resolver = new MockContentResolver(); 101 RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext( 102 new MockContext(), // The context that most methods are delegated to 103 targetContext, // The context that file methods are delegated to 104 filenamePrefix); 105 Context context = new IsolatedContext( 106 resolver, targetContextWrapper); 107 DatabaseUtils.createDbFromSqlStatements(context, databaseName, databaseVersion, sql); 108 109 T provider = ProviderTestCase2.createProviderForTest(context, providerClass, authority); 110 resolver.addProvider(authority, provider); 111 112 return resolver; 113 } 114 } 115