diff --git a/test-runner/src/android/test/ProviderTestCase2.java b/test-runner/src/android/test/ProviderTestCase2.java index 28ecee51af0ae..1fb5538d6ef2f 100644 --- a/test-runner/src/android/test/ProviderTestCase2.java +++ b/test-runner/src/android/test/ProviderTestCase2.java @@ -27,15 +27,44 @@ import android.database.DatabaseUtils; import java.io.File; /** - * This TestCase class provides a framework for isolated testing of a single - * ContentProvider. It uses a {@link android.test.mock.MockContentResolver} to - * access the provider, restricts the provider to an isolated area of the - * filesystem (for safely creating & modifying databases & files), and injects - * {@link android.test.IsolatedContext} to isolate the ContentProvider from the - * rest of the running system. - * - *
This environment is created automatically by {@link #setUp} and {@link - * #tearDown}. + * This test case class provides a framework for testing a single + * {@link ContentProvider} and for testing your app code with an + * isolated content provider. Instead of using the system map of + * providers that is based on the manifests of other applications, the test + * case creates its own internal map. It then uses this map to resolve providers + * given an authority. This allows you to inject test providers and to null out + * providers that you do not want to use. + *
+ * This test case also sets up the following mock objects: + *
+ *+ * This framework is set up automatically by the base class' {@link #setUp()} method. If you + * override this method, you must call the super method as the first statement in + * your override. + *
+ *+ * In order for their tests to be run, concrete subclasses must provide their own + * constructor with no arguments. This constructor must call + * {@link #ProviderTestCase2(Class, String)} as its first operation. + *
+ * For more information on content provider testing, please see + * Content Provider Testing. */ public abstract class ProviderTestCase2+ * Creates a new + * {@link android.test.mock.MockContentResolver}, a new IsolatedContext + * that isolates the provider's file operations, and a new instance of + * the provider under test within the isolated environment. + *
+ * + * @throws Exception + */ @Override protected void setUp() throws Exception { super.setUp(); mResolver = new MockContentResolver(); final String filenamePrefix = "test."; - RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext( - new MockContext2(), // The context that most methods are delegated to + RenamingDelegatingContext targetContextWrapper = new + RenamingDelegatingContext( + new MockContext2(), // The context that most methods are + //delegated to getContext(), // The context that file methods are delegated to filenamePrefix); mProviderContext = new IsolatedContext(mResolver, targetContextWrapper); @@ -92,14 +140,55 @@ public abstract class ProviderTestCase2+ * Creates a new content provider of the same type as that passed to the test case class, + * with an authority name set to the authority parameter, and using an SQLite database as + * the underlying data source. The SQL statement parameter is used to create the database. + * This method also creates a new {@link MockContentResolver} and adds the provider to it. + *
+ *+ * Both the new provider and the new resolver are put into an {@link IsolatedContext} + * that uses the targetContext parameter for file operations and a {@link MockContext} + * for everything else. The IsolatedContext prepends the filenamePrefix parameter to + * file, database, and directory names. + *
+ *+ * This is a convenience method for creating a "mock" provider that can contain test data. + *
+ * + * @param targetContext The context to use as the basis of the IsolatedContext + * @param filenamePrefix A string that is prepended to file, database, and directory names + * @param providerClass The type of the provider being tested + * @param authority The authority string to associated with the test provider + * @param databaseName The name assigned to the database + * @param databaseVersion The version assigned to the database + * @param sql A string containing the SQL statements that are needed to create the desired + * database and its tables. The format is the same as that generated by the + * sqlite3 tool's.dump command.
+ * @return ContentResolver A new {@link MockContentResolver} linked to the provider
+ *
+ * @throws IllegalAccessException
+ * @throws InstantiationException
+ */
public static Lifecycle Support.
- * Every Service is designed to be accessed within a specific sequence of
- * calls. Dependency Injection.
- * Every service has two inherent dependencies, the {@link android.content.Context Context} in
- * which it runs, and the {@link android.app.Application Application} with which it is associated.
- * This framework allows you to inject modified, mock, or isolated replacements for these
- * dependencies, and thus perform a true unit test.
- *
- * If simply run your tests as-is, your Service will be injected with a fully-functional
- * Context, and a generic {@link android.test.mock.MockApplication MockApplication} object.
- * You can create and inject alternatives to either of these by calling
- * {@link AndroidTestCase#setContext(Context) setContext()} or
- * {@link #setApplication setApplication()}. You must do this before calling
- * startService() or bindService(). The test framework provides a
- * number of alternatives for Context, including {link android.test.mock.MockContext MockContext},
- * {@link android.test.RenamingDelegatingContext RenamingDelegatingContext}, and
- * {@link android.content.ContextWrapper ContextWrapper}.
+ *
+ *
+ * Dependency Injection.
+ * A service has two inherent dependencies, its {@link android.content.Context Context} and its
+ * associated {@link android.app.Application Application}. The ServiceTestCase framework
+ * allows you to inject modified, mock, or isolated replacements for these dependencies, and
+ * thus perform unit tests with controlled dependencies in an isolated environment.
+ *
+ * By default, the test case is injected with a full system context and a generic
+ * {@link android.test.mock.MockApplication MockApplication} object. You can inject
+ * alternatives to either of these by invoking
+ * {@link AndroidTestCase#setContext(Context) setContext()} or
+ * {@link #setApplication setApplication()}. You must do this before calling
+ * startService() or bindService(). The test framework provides a
+ * number of alternatives for Context, including
+ * {link android.test.mock.MockContext MockContext},
+ * {@link android.test.RenamingDelegatingContext RenamingDelegatingContext},
+ * {@link android.content.ContextWrapper ContextWrapper}, and
+ * {@link android.test.IsolatedContext}.
*/
public abstract class ServiceTestCase
+ * Starts the service under test, in the same way as if it were started by
+ * {@link android.content.Context#bindService(Intent, ServiceConnection, int)
+ * Context.bindService(Intent, ServiceConnection, flags)} with an
+ * {@link android.content.Intent} that identifies a service.
+ *
+ * Notice that the parameters are different. You do not provide a
+ * {@link android.content.ServiceConnection} object or the flags parameter. Instead,
+ * you only provide the Intent. The method returns an object whose type is a
+ * subclass of {@link android.os.IBinder}, or null if the method fails. An IBinder
+ * object refers to a communication channel between the application and
+ * the service. The flag is assumed to be {@link android.content.Context#BIND_AUTO_CREATE}.
+ *
+ * See Designing a Remote Interface
+ * Using AIDL for more information about the communication channel object returned
+ * by this method.
+ *
+ * Shuts down the service under test. Ensures all resources are cleaned up and
+ * garbage collected before moving on to the next test. This method is called after each
+ * test method.
+ *
+ * Subclasses that override this method must call This only isolates the test code in ways that have proven useful so far. More should be
- * added as they become a problem.
+ *
+ * An extension of {@link android.content.ContentResolver} that is designed for
+ * testing.
+ *
+ * MockContentResolver overrides Android's normal way of resolving providers by
+ * authority. To have access to a provider based on its authority, users of
+ * MockContentResolver first instantiate the provider and
+ * use {@link MockContentResolver#addProvider(String, ContentProvider)}. Resolution of an
+ * authority occurs entirely within MockContentResolver.
+ *
+ * Users can also set an authority's entry in the map to null, so that a provider is completely
+ * mocked out.
+ * ServiceTestCase enforces this protocol:
*
- *
+ *
- *
- * setUp(), you must call
+ * super.setUp() as the first statement in your override.
+ * tearDown(), your must call the
+ * super.tearDown() as the last statement in your override.
+ * super.setUp() as the first statement in your override. The method is
+ * called before each test method is executed.
*/
@Override
protected void setUp() throws Exception {
super.setUp();
-
+
// get the real context, before the individual tests have a chance to muck with it
mSystemContext = getContext();
}
-
+
/**
- * Create the service under test and attach all injected dependencies (Context, Application) to
- * it. This will be called automatically by {@link #startService} or by {@link #bindService}.
- * If you wish to call {@link AndroidTestCase#setContext(Context) setContext()} or
- * {@link #setApplication setApplication()}, you must do so before calling this function.
+ * Creates the service under test and attaches all injected dependencies
+ * (Context, Application) to it. This is called automatically by {@link #startService} or
+ * by {@link #bindService}.
+ * If you need to call {@link AndroidTestCase#setContext(Context) setContext()} or
+ * {@link #setApplication setApplication()}, do so before calling this method.
*/
protected void setupService() {
mService = null;
@@ -131,60 +162,74 @@ public abstract class ServiceTestCasesuper.tearDown() as their
+ * last statement.
+ *