Added in API level 3

ProviderTestCase2


abstract class ProviderTestCase2<T : ContentProvider!> : AndroidTestCase
kotlin.Any
   ↳ android.test.AndroidTestCase
   ↳ android.test.ProviderTestCase2

This test case class provides a framework for testing a single 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:

  • An android.test.IsolatedContext that stubs out Context methods that might affect the rest of the running system, while allowing tests to do real file and database work.
  • A android.test.mock.MockContentResolver that provides the functionality of a regular content resolver, but uses IsolatedContext. It stubs out android.content.ContentResolver#notifyChange(Uri,ContentObserver,boolean) to prevent the test from affecting the running system.
  • An instance of the provider under test, running in an IsolatedContext.

This framework is set up automatically by the base class' 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 ProviderTestCase2(java.lang.Class,java.lang.String) as its first operation.

For more information on content provider testing, please see Content Provider Testing.

Summary

Public constructors
ProviderTestCase2(providerClass: Class<T>!, providerAuthority: String!)

Constructor.

Public methods
open MockContentResolver!

Gets the MockContentResolver created by this class during initialization.

open IsolatedContext!

Gets the IsolatedContext created by this class during initialization.

open T

Returns the content provider created by this class in the setUp() method.

open static ContentResolver!
newResolverWithContentProviderFromSql(targetContext: Context!, filenamePrefix: String!, providerClass: Class<T>!, authority: String!, databaseName: String!, databaseVersion: Int, sql: String!)

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.

Protected methods
open Unit

Sets up the environment for the test fixture.

open Unit

Tears down the environment for the test fixture.

Inherited functions
Inherited properties

Public constructors

ProviderTestCase2

Added in API level 3
ProviderTestCase2(
    providerClass: Class<T>!,
    providerAuthority: String!)

Constructor.

Parameters
providerClass Class<T>!: The class name of the provider under test
providerAuthority String!: The provider's authority string

Public methods

getMockContentResolver

Added in API level 3
open fun getMockContentResolver(): MockContentResolver!

Gets the MockContentResolver created by this class during initialization. You must use the methods of this resolver to access the provider under test.

Return
MockContentResolver! A MockContentResolver instance.

getMockContext

Added in API level 3
open fun getMockContext(): IsolatedContext!

Gets the IsolatedContext created by this class during initialization.

Return
IsolatedContext! The IsolatedContext instance

getProvider

Added in API level 3
open fun getProvider(): T

Returns the content provider created by this class in the setUp() method.

Return
T T An instance of the provider class given as a parameter to the test case class.

newResolverWithContentProviderFromSql

Added in API level 3
open static fun <T : ContentProvider!> newResolverWithContentProviderFromSql(
    targetContext: Context!,
    filenamePrefix: String!,
    providerClass: Class<T>!,
    authority: String!,
    databaseName: String!,
    databaseVersion: Int,
    sql: String!
): ContentResolver!

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 MockContentResolver and adds the provider to it.

Both the new provider and the new resolver are put into an IsolatedContext that uses the targetContext parameter for file operations and a 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.

Parameters
targetContext Context!: The context to use as the basis of the IsolatedContext
filenamePrefix String!: A string that is prepended to file, database, and directory names
providerClass Class<T>!: The type of the provider being tested
authority String!: The authority string to associated with the test provider
databaseName String!: The name assigned to the database
databaseVersion Int: The version assigned to the database
sql String!: 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! ContentResolver A new MockContentResolver linked to the provider
Exceptions
java.lang.IllegalAccessException
java.lang.InstantiationException

Protected methods

setUp

Added in API level 3
protected open fun setUp(): Unit

Sets up the environment for the test fixture.

Creates a new 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.

Exceptions
java.lang.Exception

tearDown

Added in API level 3
protected open fun tearDown(): Unit

Tears down the environment for the test fixture.

Calls android.content.ContentProvider#shutdown() on the android.content.ContentProvider represented by mProvider.