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 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 54 public T getProvider() { 55 return mProvider; 56 } 57 58 @Override 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 = mProviderClass.newInstance(); 71 mProvider.attachInfoForTesting(mProviderContext, null); 72 assertNotNull(mProvider); 73 mResolver.addProvider(mProviderAuthority, getProvider()); 74 } 75 76 /** 77 * Tears down the environment for the test fixture. 78 * <p> 79 * Calls {@link android.content.ContentProvider#shutdown()} on the 80 * {@link android.content.ContentProvider} represented by mProvider. 81 */ 82 @Override 83 protected void tearDown() throws Exception { 84 mProvider.shutdown(); 85 super.tearDown(); 86 } 87 88 public MockContentResolver getMockContentResolver() { 89 return mResolver; 90 } 91 92 public IsolatedContext getMockContext() { 93 return mProviderContext; 94 } 95 96 public static <T extends ContentProvider> ContentResolver newResolverWithContentProviderFromSql( 97 Context targetContext, Class<T> providerClass, String authority, 98 String databaseName, int databaseVersion, String sql) 99 throws IllegalAccessException, InstantiationException { 100 final String filenamePrefix = "test."; 101 MockContentResolver resolver = new MockContentResolver(); 102 RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext( 103 new MockContext(), // The context that most methods are delegated to 104 targetContext, // The context that file methods are delegated to 105 filenamePrefix); 106 Context context = new IsolatedContext( 107 resolver, targetContextWrapper); 108 DatabaseUtils.createDbFromSqlStatements(context, databaseName, databaseVersion, sql); 109 110 T provider = providerClass.newInstance(); 111 provider.attachInfoForTesting(context, null); 112 resolver.addProvider(authority, provider); 113 114 return resolver; 115 } 116 } 117