use of android.content.ContentProvider in project robolectric by robolectric.
the class ShadowContentResolver method query.
@Implementation
protected final Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
ContentProvider provider = getProvider(uri, getContext());
if (provider != null) {
return provider.query(uri, projection, selection, selectionArgs, sortOrder);
} else {
BaseCursor returnCursor = getCursor(uri);
if (returnCursor == null) {
return null;
}
returnCursor.setQuery(uri, projection, selection, selectionArgs, sortOrder);
return returnCursor;
}
}
use of android.content.ContentProvider in project robolectric by robolectric.
the class ShadowContentResolver method insert.
/**
* If a {@link ContentProvider} is registered for the given {@link Uri}, its {@link
* ContentProvider#insert(Uri, ContentValues)} method will be invoked.
*
* Tests can verify that this method was called using {@link #getStatements()} or {@link
* #getInsertStatements()}.
*
* If no appropriate {@link ContentProvider} is found, no action will be taken and a {@link
* Uri} including the incremented value set with {@link #setNextDatabaseIdForInserts(int)} will
* returned.
*/
@Implementation
protected final Uri insert(Uri url, ContentValues values) {
ContentProvider provider = getProvider(url, getContext());
ContentValues valuesCopy = (values == null) ? null : new ContentValues(values);
InsertStatement insertStatement = new InsertStatement(url, provider, valuesCopy);
statements.add(insertStatement);
insertStatements.add(insertStatement);
if (provider != null) {
return provider.insert(url, values);
} else {
return Uri.parse(url.toString() + "/" + ++nextDatabaseIdForInserts);
}
}
use of android.content.ContentProvider in project robolectric by robolectric.
the class ShadowContentResolverTest method shouldDelegateCallsToRegisteredProvider.
@Test
public void shouldDelegateCallsToRegisteredProvider() {
ShadowContentResolver.registerProviderInternal(AUTHORITY, new ContentProvider() {
@Override
public boolean onCreate() {
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
return new BaseCursor();
}
@Override
public Uri insert(Uri uri, ContentValues values) {
return null;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return -1;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
return -1;
}
@Override
public String getType(Uri uri) {
return null;
}
});
final Uri uri = Uri.parse("content://" + AUTHORITY + "/some/path");
final Uri unrelated = Uri.parse("content://unrelated/some/path");
assertThat(contentResolver.query(uri, null, null, null, null)).isNotNull();
assertThat(contentResolver.insert(uri, new ContentValues())).isNull();
assertThat(contentResolver.delete(uri, null, null)).isEqualTo(-1);
assertThat(contentResolver.update(uri, new ContentValues(), null, null)).isEqualTo(-1);
assertThat(contentResolver.query(unrelated, null, null, null, null)).isNull();
assertThat(contentResolver.insert(unrelated, new ContentValues())).isNotNull();
assertThat(contentResolver.delete(unrelated, null, null)).isEqualTo(1);
assertThat(contentResolver.update(unrelated, new ContentValues(), null, null)).isEqualTo(1);
}
use of android.content.ContentProvider in project robolectric by robolectric.
the class ShadowContentResolverTest method acquireUnstableProvider_shouldReturnWithUri.
@Test
public void acquireUnstableProvider_shouldReturnWithUri() {
ContentProvider cp = mock(ContentProvider.class);
ShadowContentResolver.registerProviderInternal(AUTHORITY, cp);
final Uri uri = Uri.parse("content://" + AUTHORITY);
assertThat(contentResolver.acquireUnstableProvider(uri)).isSameInstanceAs(cp.getIContentProvider());
}
use of android.content.ContentProvider in project robolectric by robolectric.
the class ShadowContentResolverTest method getProvider_shouldCreateProviderFromManifest.
@Test
public void getProvider_shouldCreateProviderFromManifest() throws Exception {
Uri uri = Uri.parse("content://org.robolectric.authority1/shadows");
ContentProvider provider = ShadowContentResolver.getProvider(uri);
assertThat(provider).isNotNull();
assertThat(provider.getReadPermission()).isEqualTo("READ_PERMISSION");
assertThat(provider.getWritePermission()).isEqualTo("WRITE_PERMISSION");
assertThat(provider.getPathPermissions()).asList().hasSize(1);
// unfortunately, there is no direct way of testing if authority is set or not
// however, it's checked in ContentProvider.Transport method calls (validateIncomingUri), so
// it's the closest we can test against
// should not throw
provider.getIContentProvider().getType(uri);
}
Aggregations