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

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

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.