ProviderTestRule.Builder

public static class ProviderTestRule.Builder
extends Object

java.lang.Object
   ↳ android.support.test.rule.provider.ProviderTestRule.Builder


A Builder to ease ProviderTestRule creation. Users can input one or more ContentProviders and their corresponding authority for tests. It also allows to specify the prefix to use when renaming test files for isolation by setPrefix(String). If the ContentProvider under test is implemented based on SQLiteDatabase, users can also pass in database file to restore or database commands to run before tests.

Summary

Public constructors

ProviderTestRule.Builder(Class<T> providerClass, String providerAuth)

The basic builder to use when creating a ProviderTestRule, which allows to specify one ContentProvider and the corresponding authority for tests.

Public methods

<T extends ContentProvider> ProviderTestRule.Builder addProvider(Class<T> providerClass, String providerAuth)

Allows to add additional ContentProvider and the corresponding authority for testing.

ProviderTestRule build()
ProviderTestRule.Builder setDatabaseCommands(String dbName, String... dbCmds)

Allows to pass in specific SQL commands to run against the database with a given name.

ProviderTestRule.Builder setDatabaseCommandsFile(String dbName, File dbCmdFile)

Allows to pass in a file containing commands to run against the database with a given name.

ProviderTestRule.Builder setDatabaseFile(String dbName, File dbDataFile)

Allows to pass in a SQLite database file containing the intended initial data to restore.

ProviderTestRule.Builder setPrefix(String prefix)

Enables users to specify the prefix to use when renaming test files for isolation.

Inherited methods

From class java.lang.Object

Public constructors

ProviderTestRule.Builder

ProviderTestRule.Builder (Class<T> providerClass, 
                String providerAuth)

The basic builder to use when creating a ProviderTestRule, which allows to specify one ContentProvider and the corresponding authority for tests.

Parameters
providerClass Class: The class of ContentProvider under test.

providerAuth String: The authority defined for ContentProvider under test.

Public methods

addProvider

ProviderTestRule.Builder addProvider (Class<T> providerClass, 
                String providerAuth)

Allows to add additional ContentProvider and the corresponding authority for testing. Similarly, setDatabaseFile(String, File), setDatabaseCommands(String, String...), setDatabaseCommandsFile(String, File), and runDatabaseCommands(String, String...) can be used for this ContentProvider.

Parameters
providerClass Class: The class of the added ContentProvider under test.

providerAuth String: The authority defined for the added ContentProvider under test.

Returns
ProviderTestRule.Builder

build

ProviderTestRule build ()

Returns
ProviderTestRule

setDatabaseCommands

ProviderTestRule.Builder setDatabaseCommands (String dbName, 
                String... dbCmds)

Allows to pass in specific SQL commands to run against the database with a given name.

Note: The passed in commands are executed before any sql commands execution in SQLiteOpenHelper's onCreate method defined in ContentProvider under test, also before executing commands, if any, passed in via setDatabaseCommandsFile(String, File), but after restoring the database file, if any, passed in via setDatabaseFile(String, File).

Parameters
dbName String: The name of the underlying database used by the ContentProvider under test.

dbCmds String: The SQL commands to run. Each command will be passed to execSQL(String) to execute.

Returns
ProviderTestRule.Builder

setDatabaseCommandsFile

ProviderTestRule.Builder setDatabaseCommandsFile (String dbName, 
                File dbCmdFile)

Allows to pass in a file containing commands to run against the database with a given name.

Note: Commands in the file are executed before any sql commands execution in SQLiteOpenHelper's onCreate method defined in ContentProvider under test, but after restoring the database file, if any, passed in via setDatabaseFile(String, File) and executing commands, if any, passed in via setDatabaseCommands(String, String...).

Parameters
dbName String: The name of the underlying database used by the ContentProvider under test.

dbCmdFile File: The file that contains line separated database commands to run. Each line will be treated as a separate command and passed to execSQL(String) to execute.

Returns
ProviderTestRule.Builder

setDatabaseFile

ProviderTestRule.Builder setDatabaseFile (String dbName, 
                File dbDataFile)

Allows to pass in a SQLite database file containing the intended initial data to restore.

Note: Restoring the database from the file are executed before any sql commands execution in SQLiteOpenHelper's onCreate method defined in ContentProvider under test, also before running the database commands, if any, passed in via setDatabaseCommands(String, String...) and setDatabaseCommandsFile(String, File).

In the case of the database file with prefixed name already exists, the database restoration will overwrite existing file.

Parameters
dbName String: The name of the underlying database used by the ContentProvider under test.

dbDataFile File: The SQLite database file that contains the data to restore.

Returns
ProviderTestRule.Builder

setPrefix

ProviderTestRule.Builder setPrefix (String prefix)

Enables users to specify the prefix to use when renaming test files for isolation. If not used, this rule will use "#DEFAULT_PREFIX" by default.

Parameters
prefix String: The non-empty prefix to append to test files.

Returns
ProviderTestRule.Builder