Home | History | Annotate | Download | only in test
      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.content.pm.ProviderInfo;
     23 import android.content.res.Resources;
     24 import android.test.mock.MockContentProvider;
     25 import android.test.mock.MockContext;
     26 import android.test.mock.MockContentResolver;
     27 import android.database.DatabaseUtils;
     28 
     29 import java.io.File;
     30 
     31 /**
     32  * This test case class provides a framework for testing a single
     33  * {@link ContentProvider} and for testing your app code with an
     34  * isolated content provider. Instead of using the system map of
     35  * providers that is based on the manifests of other applications, the test
     36  * case creates its own internal map. It then uses this map to resolve providers
     37  * given an authority. This allows you to inject test providers and to null out
     38  * providers that you do not want to use.
     39  * <p>
     40  *      This test case also sets up the following mock objects:
     41  * </p>
     42  * <ul>
     43  *      <li>
     44  *          An {@link android.test.IsolatedContext} that stubs out Context methods that might
     45  *          affect the rest of the running system, while allowing tests to do real file and
     46  *          database work.
     47  *      </li>
     48  *      <li>
     49  *          A {@link android.test.mock.MockContentResolver} that provides the functionality of a
     50  *          regular content resolver, but uses {@link IsolatedContext}. It stubs out
     51  *          {@link ContentResolver#notifyChange(Uri, ContentObserver, boolean)} to
     52  *          prevent the test from affecting the running system.
     53  *      </li>
     54  *      <li>
     55  *          An instance of the provider under test, running in an {@link IsolatedContext}.
     56  *      </li>
     57  * </ul>
     58  * <p>
     59  *      This framework is set up automatically by the base class' {@link #setUp()} method. If you
     60  *      override this method, you must call the super method as the first statement in
     61  *      your override.
     62  * </p>
     63  * <p>
     64  *     In order for their tests to be run, concrete subclasses must provide their own
     65  *     constructor with no arguments. This constructor must call
     66  *     {@link #ProviderTestCase2(Class, String)} as  its first operation.
     67  * </p>
     68  * For more information on content provider testing, please see
     69  * <a href="{@docRoot}tools/testing/contentprovider_testing.html">Content Provider Testing</a>.
     70  */
     71 public abstract class ProviderTestCase2<T extends ContentProvider> extends AndroidTestCase {
     72 
     73     Class<T> mProviderClass;
     74     String mProviderAuthority;
     75 
     76     private IsolatedContext mProviderContext;
     77     private MockContentResolver mResolver;
     78 
     79     private class MockContext2 extends MockContext {
     80 
     81         @Override
     82         public Resources getResources() {
     83             return getContext().getResources();
     84         }
     85 
     86         @Override
     87         public File getDir(String name, int mode) {
     88             // name the directory so the directory will be separated from
     89             // one created through the regular Context
     90             return getContext().getDir("mockcontext2_" + name, mode);
     91         }
     92 
     93         @Override
     94         public Context getApplicationContext() {
     95             return this;
     96         }
     97     }
     98     /**
     99      * Constructor.
    100      *
    101      * @param providerClass The class name of the provider under test
    102      * @param providerAuthority The provider's authority string
    103      */
    104     public ProviderTestCase2(Class<T> providerClass, String providerAuthority) {
    105         mProviderClass = providerClass;
    106         mProviderAuthority = providerAuthority;
    107     }
    108 
    109     private T mProvider;
    110 
    111     /**
    112      * Returns the content provider created by this class in the {@link #setUp()} method.
    113      * @return T An instance of the provider class given as a parameter to the test case class.
    114      */
    115     public T getProvider() {
    116         return mProvider;
    117     }
    118 
    119     /**
    120      * Sets up the environment for the test fixture.
    121      * <p>
    122      * Creates a new
    123      * {@link android.test.mock.MockContentResolver}, a new IsolatedContext
    124      * that isolates the provider's file operations, and a new instance of
    125      * the provider under test within the isolated environment.
    126      * </p>
    127      *
    128      * @throws Exception
    129      */
    130     @Override
    131     protected void setUp() throws Exception {
    132         super.setUp();
    133 
    134         mResolver = new MockContentResolver();
    135         final String filenamePrefix = "test.";
    136         RenamingDelegatingContext targetContextWrapper = new
    137                 RenamingDelegatingContext(
    138                 new MockContext2(), // The context that most methods are
    139                                     //delegated to
    140                 getContext(), // The context that file methods are delegated to
    141                 filenamePrefix);
    142         mProviderContext = new IsolatedContext(mResolver, targetContextWrapper);
    143         mProvider = createProviderForTest(mProviderContext, mProviderClass, mProviderAuthority);
    144         mResolver.addProvider(mProviderAuthority, getProvider());
    145     }
    146 
    147     /**
    148      * Creates and sets up a new instance of the provider.
    149      */
    150     static <T extends ContentProvider> T createProviderForTest(
    151             Context context, Class<T> providerClass, String authority)
    152             throws IllegalAccessException, InstantiationException {
    153         T instance = providerClass.newInstance();
    154         ProviderInfo providerInfo = new ProviderInfo();
    155         providerInfo.authority = authority;
    156         MockContentProvider.attachInfoForTesting(instance, context, providerInfo);
    157         return instance;
    158     }
    159 
    160     /**
    161      * Tears down the environment for the test fixture.
    162      * <p>
    163      * Calls {@link android.content.ContentProvider#shutdown()} on the
    164      * {@link android.content.ContentProvider} represented by mProvider.
    165      */
    166     @Override
    167     protected void tearDown() throws Exception {
    168         mProvider.shutdown();
    169         super.tearDown();
    170     }
    171 
    172     /**
    173      * Gets the {@link MockContentResolver} created by this class during initialization. You
    174      * must use the methods of this resolver to access the provider under test.
    175      *
    176      * @return A {@link MockContentResolver} instance.
    177      */
    178     public MockContentResolver getMockContentResolver() {
    179         return mResolver;
    180     }
    181 
    182     /**
    183      * Gets the {@link IsolatedContext} created by this class during initialization.
    184      * @return The {@link IsolatedContext} instance
    185      */
    186     public IsolatedContext getMockContext() {
    187         return mProviderContext;
    188     }
    189 
    190     /**
    191      * <p>
    192      *      Creates a new content provider of the same type as that passed to the test case class,
    193      *      with an authority name set to the authority parameter, and using an SQLite database as
    194      *      the underlying data source. The SQL statement parameter is used to create the database.
    195      *      This method also creates a new {@link MockContentResolver} and adds the provider to it.
    196      * </p>
    197      * <p>
    198      *      Both the new provider and the new resolver are put into an {@link IsolatedContext}
    199      *      that uses the targetContext parameter for file operations and a {@link MockContext}
    200      *      for everything else. The IsolatedContext prepends the filenamePrefix parameter to
    201      *      file, database, and directory names.
    202      * </p>
    203      * <p>
    204      *      This is a convenience method for creating a "mock" provider that can contain test data.
    205      * </p>
    206      *
    207      * @param targetContext The context to use as the basis of the IsolatedContext
    208      * @param filenamePrefix A string that is prepended to file, database, and directory names
    209      * @param providerClass The type of the provider being tested
    210      * @param authority The authority string to associated with the test provider
    211      * @param databaseName The name assigned to the database
    212      * @param databaseVersion The version assigned to the database
    213      * @param sql A string containing the SQL statements that are needed to create the desired
    214      * database and its tables. The format is the same as that generated by the
    215      * <a href="http://www.sqlite.org/sqlite.html">sqlite3</a> tool's <code>.dump</code> command.
    216      * @return ContentResolver A new {@link MockContentResolver} linked to the provider
    217      *
    218      * @throws IllegalAccessException
    219      * @throws InstantiationException
    220      */
    221     public static <T extends ContentProvider> ContentResolver newResolverWithContentProviderFromSql(
    222             Context targetContext, String filenamePrefix, Class<T> providerClass, String authority,
    223             String databaseName, int databaseVersion, String sql)
    224             throws IllegalAccessException, InstantiationException {
    225         MockContentResolver resolver = new MockContentResolver();
    226         RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext(
    227                 new MockContext(), // The context that most methods are delegated to
    228                 targetContext, // The context that file methods are delegated to
    229                 filenamePrefix);
    230         Context context = new IsolatedContext(resolver, targetContextWrapper);
    231         DatabaseUtils.createDbFromSqlStatements(context, databaseName, databaseVersion, sql);
    232 
    233         T provider = createProviderForTest(context, providerClass, authority);
    234         resolver.addProvider(authority, provider);
    235 
    236         return resolver;
    237     }
    238 }
    239