MockContentResolver


public class MockContentResolver
extends ContentResolver

java.lang.Object
   ↳ android.content.ContentResolver
     ↳ android.test.mock.MockContentResolver


An extension of 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 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.

Developer Guides

For more information about application testing, read the Testing developer guide.

Summary

Inherited constants

String ANY_CURSOR_ITEM_TYPE

This is the Android platform's generic MIME type to match any MIME type of the form "CURSOR_ITEM_BASE_TYPE/SUB_TYPE".

String CURSOR_DIR_BASE_TYPE

This is the Android platform's base MIME type for a content: URI containing a Cursor of zero or more items.

String CURSOR_ITEM_BASE_TYPE

This is the Android platform's base MIME type for a content: URI containing a Cursor of a single item.

String EXTRA_HONORED_ARGS

Allows provider to report back to client which query keys are honored in a Cursor.

String EXTRA_REFRESH_SUPPORTED

An extra boolean describing whether a particular provider supports refresh or not.

String EXTRA_SIZE

An extra Point describing the optimal size for a requested image resource, in pixels.

String EXTRA_TOTAL_COUNT

Added to Cursor extras Bundle to indicate total row count of recordset when paging is supported.

int NOTIFY_DELETE

Flag for notifyChange(android.net.Uri, android.database.ContentObserver, int): typically set by a ContentProvider to indicate that this notification is the result of a ContentProvider.delete call.

int NOTIFY_INSERT

Flag for notifyChange(android.net.Uri, android.database.ContentObserver, int): typically set by a ContentProvider to indicate that this notification is the result of an ContentProvider.insert call.

int NOTIFY_SKIP_NOTIFY_FOR_DESCENDANTS

Flag for notifyChange(android.net.Uri, android.database.ContentObserver, int): if set, this notification will be skipped if it is being delivered to the root URI of a ContentObserver that is using "notify for descendants." The purpose of this is to allow the provide to send a general notification of "something under X" changed that observers of that specific URI can receive, while also sending a specific URI under X.

int NOTIFY_SYNC_TO_NETWORK

Flag for notifyChange(android.net.Uri, android.database.ContentObserver, int): attempt to sync the change to the network.

int NOTIFY_UPDATE

Flag for notifyChange(android.net.Uri, android.database.ContentObserver, int): typically set by a ContentProvider to indicate that this notification is the result of an ContentProvider.update call.

String QUERY_ARG_GROUP_COLUMNS

Specifies the list of columns (stored as a String[]) against which to group results.

String QUERY_ARG_LIMIT

Specifies the max number of rows to include in a Cursor.

String QUERY_ARG_OFFSET

Specifies the offset row index within a Cursor.

String QUERY_ARG_SORT_COLLATION

Allows client to specify a hint to the provider declaring which collation to use when sorting values.

String QUERY_ARG_SORT_COLUMNS

Specifies the list of columns (stored as a String[]) against which to sort results.

String QUERY_ARG_SORT_DIRECTION

Specifies desired sort order.

String QUERY_ARG_SORT_LOCALE

Allows client to specify a hint to the provider declaring which locale to use when sorting values.

String QUERY_ARG_SQL_GROUP_BY

Key for an SQL style GROUP BY string that may be present in the query Bundle argument passed to ContentProvider.query(Uri, String[], Bundle, CancellationSignal).

String QUERY_ARG_SQL_HAVING

Key for an SQL style HAVING string that may be present in the query Bundle argument passed to ContentProvider.query(Uri, String[], Bundle, CancellationSignal).

String QUERY_ARG_SQL_LIMIT

Key for an SQL style LIMIT string that may be present in the query Bundle argument passed to ContentProvider.query(Uri, String[], Bundle, CancellationSignal).

String QUERY_ARG_SQL_SELECTION

Key for an SQL style selection string that may be present in the query Bundle argument passed to ContentProvider.query(Uri, String[], Bundle, CancellationSignal) when called by a legacy client.

String QUERY_ARG_SQL_SELECTION_ARGS

Key for SQL selection string arguments list.

String QUERY_ARG_SQL_SORT_ORDER

Key for an SQL style sort string that may be present in the query Bundle argument passed to ContentProvider.query(Uri, String[], Bundle, CancellationSignal) when called by a legacy client.

int QUERY_SORT_DIRECTION_ASCENDING

int QUERY_SORT_DIRECTION_DESCENDING

String SCHEME_ANDROID_RESOURCE

String SCHEME_CONTENT

String SCHEME_FILE

String SYNC_EXTRAS_ACCOUNT

This constant was deprecated in API level 15. instead use requestSync(android.accounts.Account, java.lang.String, android.os.Bundle)

String SYNC_EXTRAS_DISCARD_LOCAL_DELETIONS

Indicates that the sync adapter should not proceed with the delete operations, if it determines that there are too many.

String SYNC_EXTRAS_DO_NOT_RETRY

If this extra is set to true then the request will not be retried if it fails.

String SYNC_EXTRAS_EXPEDITED

If this extra is set to true, the sync request will be scheduled at the front of the sync request queue, but it is still subject to JobScheduler quota and throttling due to App Standby buckets.

String SYNC_EXTRAS_FORCE

This constant was deprecated in API level 15. instead use SYNC_EXTRAS_MANUAL

String SYNC_EXTRAS_IGNORE_BACKOFF

If this extra is set to true then any backoffs for the initial attempt (e.g. due to retries) are ignored by the sync scheduler.

String SYNC_EXTRAS_IGNORE_SETTINGS

If this extra is set to true then the sync settings (like getSyncAutomatically()) are ignored by the sync scheduler.

String SYNC_EXTRAS_INITIALIZE

Set by the SyncManager to request that the SyncAdapter initialize itself for the given account/authority pair.

String SYNC_EXTRAS_MANUAL

Setting this extra is the equivalent of setting both SYNC_EXTRAS_IGNORE_SETTINGS and SYNC_EXTRAS_IGNORE_BACKOFF

String SYNC_EXTRAS_OVERRIDE_TOO_MANY_DELETIONS

Indicates that the sync adapter should proceed with the delete operations, even if it determines that there are too many.

String SYNC_EXTRAS_REQUIRE_CHARGING

If this extra is set to true, the sync request will be scheduled only when the device is plugged in.

String SYNC_EXTRAS_SCHEDULE_AS_EXPEDITED_JOB

Run this sync operation as an "expedited job" (see JobInfo.Builder.setExpedited(boolean)).

String SYNC_EXTRAS_UPLOAD

Indicates that this sync is intended to only upload local changes to the server.

int SYNC_OBSERVER_TYPE_ACTIVE

int SYNC_OBSERVER_TYPE_PENDING

int SYNC_OBSERVER_TYPE_SETTINGS

Public constructors

MockContentResolver()

Creates a local map of providers.

MockContentResolver(Context context)

Creates a local map of providers.

Public methods

void addProvider(String name, ContentProvider provider)

Adds access to a provider based on its authority

void notifyChange(Collection<Uri> uris, ContentObserver observer, int flags)

Overrides the behavior from the parent class to completely ignore any content notifications sent to this object.

void notifyChange(Uri uri, ContentObserver observer)

Overrides the behavior from the parent class to completely ignore any content notifications sent to this object.

void notifyChange(Uri uri, ContentObserver observer, int flags)

Overrides the behavior from the parent class to completely ignore any content notifications sent to this object.

void notifyChange(Uri uri, ContentObserver observer, boolean syncToNetwork)

This method is deprecated. callers should consider migrating to notifyChange(android.net.Uri, android.database.ContentObserver, int), as it offers support for many more options than just ContentResolver.NOTIFY_SYNC_TO_NETWORK.

Inherited methods

final ContentProviderClient acquireContentProviderClient(Uri uri)

Returns a ContentProviderClient that is associated with the ContentProvider that services the content at uri, starting the provider if necessary.

final ContentProviderClient acquireContentProviderClient(String name)

Returns a ContentProviderClient that is associated with the ContentProvider with the authority of name, starting the provider if necessary.

final ContentProviderClient acquireUnstableContentProviderClient(String name)

Like acquireContentProviderClient(java.lang.String), but for use when you do not trust the stability of the target content provider.

final ContentProviderClient acquireUnstableContentProviderClient(Uri uri)

Like acquireContentProviderClient(android.net.Uri), but for use when you do not trust the stability of the target content provider.

static void addPeriodicSync(Account account, String authority, Bundle extras, long pollFrequency)

Specifies that a sync should be requested with the specified the account, authority, and extras at the given frequency.

static Object addStatusChangeListener(int mask, SyncStatusObserver callback)

Request notifications when the different aspects of the SyncManager change.

ContentProviderResult[] applyBatch(String authority, ArrayList<ContentProviderOperation> operations)

Applies each of the ContentProviderOperation objects and returns an array of their results.

final int bulkInsert(Uri url, ContentValues[] values)

Inserts multiple rows into a table at the given URL.

final Bundle call(Uri uri, String method, String arg, Bundle extras)

Call a provider-defined method.

final Bundle call(String authority, String method, String arg, Bundle extras)

void cancelSync(Uri uri)

This method was deprecated in API level 15. instead use cancelSync(android.accounts.Account, java.lang.String)

static void cancelSync(SyncRequest request)

Remove the specified sync.

static void cancelSync(Account account, String authority)

Cancel any active or pending syncs that match account and authority.

final Uri canonicalize(Uri url)

Transform the given url to a canonical representation of its referenced resource, which can be used across devices, persisted, backed up and restored, etc.

final int delete(Uri url, String where, String[] selectionArgs)

Deletes row(s) specified by a content URI.

final int delete(Uri url, Bundle extras)

Deletes row(s) specified by a content URI.

static SyncInfo getCurrentSync()

This method was deprecated in API level 15. Since multiple concurrent syncs are now supported you should use getCurrentSyncs() to get the accurate list of current syncs. This method returns the first item from the list of current syncs or null if there are none.

static List<SyncInfo> getCurrentSyncs()

Returns a list with information about all the active syncs.

static int getIsSyncable(Account account, String authority)

Check if this account/provider is syncable.

static boolean getMasterSyncAutomatically()

Gets the global auto-sync setting that applies to all the providers and accounts.

List<UriPermission> getOutgoingPersistedUriPermissions()

Return list of all persisted URI permission grants that are hosted by the calling app.

static List<PeriodicSync> getPeriodicSyncs(Account account, String authority)

Get the list of information about the periodic syncs for the given account and authority.

List<UriPermission> getPersistedUriPermissions()

Return list of all URI permission grants that have been persisted by the calling app.

String[] getStreamTypes(Uri url, String mimeTypeFilter)

Query for the possible MIME types for the representations the given content URL can be returned when opened as as stream with openTypedAssetFileDescriptor(Uri, String, Bundle).

static SyncAdapterType[] getSyncAdapterTypes()

Get information about the SyncAdapters that are known to the system.

static boolean getSyncAutomatically(Account account, String authority)

Check if the provider should be synced when a network tickle is received

This method requires the caller to hold the permission Manifest.permission.READ_SYNC_SETTINGS.

final String getType(Uri url)

Return the MIME type of the given content URL.

final ContentResolver.MimeTypeInfo getTypeInfo(String mimeType)

Return a detailed description of the given MIME type, including an icon and label that describe the type.

final Uri insert(Uri url, ContentValues values, Bundle extras)

Inserts a row into a table at the given URL.

final Uri insert(Uri url, ContentValues values)

Inserts a row into a table at the given URL.

static boolean isSyncActive(Account account, String authority)

Returns true if there is currently a sync operation for the given account or authority actively being processed.

static boolean isSyncPending(Account account, String authority)

Return true if the pending status is true of any matching authorities.

Bitmap loadThumbnail(Uri uri, Size size, CancellationSignal signal)

Convenience method that efficiently loads a visual thumbnail for the given Uri.

void notifyChange(Collection<Uri> uris, ContentObserver observer, int flags)

Notify registered observers that several rows have been updated.

void notifyChange(Uri uri, ContentObserver observer, boolean syncToNetwork)

This method was deprecated in API level 30. callers should consider migrating to notifyChange(android.net.Uri, android.database.ContentObserver, int), as it offers support for many more options than just NOTIFY_SYNC_TO_NETWORK.

void notifyChange(Uri uri, ContentObserver observer, int flags)

Notify registered observers that a row was updated.

void notifyChange(Uri uri, ContentObserver observer)

Notify registered observers that a row was updated and attempt to sync changes to the network.

final AssetFileDescriptor openAssetFile(Uri uri, String mode, CancellationSignal signal)

final AssetFileDescriptor openAssetFileDescriptor(Uri uri, String mode, CancellationSignal cancellationSignal)

Open a raw file descriptor to access data under a URI.

final AssetFileDescriptor openAssetFileDescriptor(Uri uri, String mode)

Open a raw file descriptor to access data under a URI.

final ParcelFileDescriptor openFile(Uri uri, String mode, CancellationSignal signal)

final ParcelFileDescriptor openFileDescriptor(Uri uri, String mode, CancellationSignal cancellationSignal)

Open a raw file descriptor to access data under a URI.

final ParcelFileDescriptor openFileDescriptor(Uri uri, String mode)

Open a raw file descriptor to access data under a URI.

final InputStream openInputStream(Uri uri)

Open a stream on to the content associated with a content URI.

final OutputStream openOutputStream(Uri uri)

Synonym for openOutputStream(uri, "w").

final OutputStream openOutputStream(Uri uri, String mode)

Open a stream on to the content associated with a content URI.

final AssetFileDescriptor openTypedAssetFile(Uri uri, String mimeTypeFilter, Bundle opts, CancellationSignal signal)

final AssetFileDescriptor openTypedAssetFileDescriptor(Uri uri, String mimeType, Bundle opts, CancellationSignal cancellationSignal)

Open a raw file descriptor to access (potentially type transformed) data from a "content:" URI.

final AssetFileDescriptor openTypedAssetFileDescriptor(Uri uri, String mimeType, Bundle opts)

Open a raw file descriptor to access (potentially type transformed) data from a "content:" URI.

final Cursor query(Uri uri, String[] projection, Bundle queryArgs, CancellationSignal cancellationSignal)

Query the given URI, returning a Cursor over the result set with support for cancellation.

final Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, CancellationSignal cancellationSignal)

Query the given URI, returning a Cursor over the result set with optional support for cancellation.

final Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)

Query the given URI, returning a Cursor over the result set.

final boolean refresh(Uri url, Bundle extras, CancellationSignal cancellationSignal)

This allows clients to request an explicit refresh of content identified by uri.

final void registerContentObserver(Uri uri, boolean notifyForDescendants, ContentObserver observer)

Register an observer class that gets callbacks when data identified by a given content URI changes.

void releasePersistableUriPermission(Uri uri, int modeFlags)

Relinquish a persisted URI permission grant.

static void removePeriodicSync(Account account, String authority, Bundle extras)

Remove a periodic sync.

static void removeStatusChangeListener(Object handle)

Remove a previously registered status change listener.

static void requestSync(Account account, String authority, Bundle extras)

Start an asynchronous sync operation.

static void requestSync(SyncRequest request)

Register a sync with the SyncManager.

static void setIsSyncable(Account account, String authority, int syncable)

Set whether this account/provider is syncable.

static void setMasterSyncAutomatically(boolean sync)

Sets the global auto-sync setting that applies to all the providers and accounts.

static void setSyncAutomatically(Account account, String authority, boolean sync)

Set whether or not the provider is synced when it receives a network tickle.

void startSync(Uri uri, Bundle extras)

This method was deprecated in API level 15. instead use requestSync(android.accounts.Account, java.lang.String, android.os.Bundle)

void takePersistableUriPermission(Uri uri, int modeFlags)

Take a persistable URI permission grant that has been offered.

final Uri uncanonicalize(Uri url)

Given a canonical Uri previously generated by canonicalize(Uri), convert it to its local non-canonical form.

final void unregisterContentObserver(ContentObserver observer)

Unregisters a change observer.

final int update(Uri uri, ContentValues values, Bundle extras)

Update row(s) in a content URI.

final int update(Uri uri, ContentValues values, String where, String[] selectionArgs)

Update row(s) in a content URI.

static void validateSyncExtrasBundle(Bundle extras)

Check that only values of the following types are in the Bundle:

  • Integer
  • Long
  • Boolean
  • Float
  • Double
  • String
  • Account
  • null

static ContentResolver wrap(ContentProviderClient wrapped)

Create a ContentResolver instance that redirects all its methods to the given ContentProviderClient.

static ContentResolver wrap(ContentProvider wrapped)

Create a ContentResolver instance that redirects all its methods to the given ContentProvider.

Object clone()

Creates and returns a copy of this object.

boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

final Class<?> getClass()

Returns the runtime class of this Object.

int hashCode()

Returns a hash code value for the object.

final void notify()

Wakes up a single thread that is waiting on this object's monitor.

final void notifyAll()

Wakes up all threads that are waiting on this object's monitor.

String toString()

Returns a string representation of the object.

final void wait(long timeoutMillis, int nanos)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait(long timeoutMillis)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait()

Causes the current thread to wait until it is awakened, typically by being notified or interrupted.

Public constructors

MockContentResolver

Added in API level 1
public MockContentResolver ()

Creates a local map of providers. This map is used instead of the global map when an API call tries to acquire a provider.

MockContentResolver

Added in API level 18
public MockContentResolver (Context context)

Creates a local map of providers. This map is used instead of the global map when an API call tries to acquire a provider.

Parameters
context Context

Public methods

addProvider

Added in API level 1
public void addProvider (String name, 
                ContentProvider provider)

Adds access to a provider based on its authority

Parameters
name String: The authority name associated with the provider.

provider ContentProvider: An instance of ContentProvider or one of its subclasses, or null.

notifyChange

Added in API level 30
public void notifyChange (Collection<Uri> uris, 
                ContentObserver observer, 
                int flags)

Overrides the behavior from the parent class to completely ignore any content notifications sent to this object. This effectively hides clients from observers elsewhere in the system.

Parameters
uris Collection: This value cannot be null.

observer ContentObserver: This value may be null.

flags int: Value is either 0 or a combination of ContentResolver.NOTIFY_SYNC_TO_NETWORK, ContentResolver.NOTIFY_SKIP_NOTIFY_FOR_DESCENDANTS, ContentResolver.NOTIFY_INSERT, ContentResolver.NOTIFY_UPDATE, and ContentResolver.NOTIFY_DELETE

notifyChange

Added in API level 1
public void notifyChange (Uri uri, 
                ContentObserver observer)

Overrides the behavior from the parent class to completely ignore any content notifications sent to this object. This effectively hides clients from observers elsewhere in the system.

Parameters
uri Uri: This value cannot be null.

observer ContentObserver: This value may be null.

notifyChange

Added in API level 24
public void notifyChange (Uri uri, 
                ContentObserver observer, 
                int flags)

Overrides the behavior from the parent class to completely ignore any content notifications sent to this object. This effectively hides clients from observers elsewhere in the system.

Parameters
uri Uri: This value cannot be null.

observer ContentObserver: This value may be null.

flags int: Value is either 0 or a combination of ContentResolver.NOTIFY_SYNC_TO_NETWORK, ContentResolver.NOTIFY_SKIP_NOTIFY_FOR_DESCENDANTS, ContentResolver.NOTIFY_INSERT, ContentResolver.NOTIFY_UPDATE, and ContentResolver.NOTIFY_DELETE

notifyChange

Added in API level 1
public void notifyChange (Uri uri, 
                ContentObserver observer, 
                boolean syncToNetwork)

This method is deprecated.
callers should consider migrating to notifyChange(android.net.Uri, android.database.ContentObserver, int), as it offers support for many more options than just ContentResolver.NOTIFY_SYNC_TO_NETWORK.

Overrides the behavior from the parent class to completely ignore any content notifications sent to this object. This effectively hides clients from observers elsewhere in the system.

Parameters
uri Uri: This value cannot be null.

observer ContentObserver: This value may be null.

syncToNetwork boolean: If true, same as ContentResolver.NOTIFY_SYNC_TO_NETWORK.