Search in sources :

Example 46 with ContentProvider

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;
    }
}
Also used : BaseCursor(org.robolectric.fakes.BaseCursor) IContentProvider(android.content.IContentProvider) ContentProvider(android.content.ContentProvider) Implementation(org.robolectric.annotation.Implementation)

Example 47 with ContentProvider

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);
    }
}
Also used : ContentValues(android.content.ContentValues) IContentProvider(android.content.IContentProvider) ContentProvider(android.content.ContentProvider) Implementation(org.robolectric.annotation.Implementation)

Example 48 with ContentProvider

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);
}
Also used : ContentValues(android.content.ContentValues) BaseCursor(org.robolectric.fakes.BaseCursor) ContentProvider(android.content.ContentProvider) Cursor(android.database.Cursor) MatrixCursor(android.database.MatrixCursor) BaseCursor(org.robolectric.fakes.BaseCursor) Uri(android.net.Uri) Test(org.junit.Test)

Example 49 with ContentProvider

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());
}
Also used : ContentProvider(android.content.ContentProvider) Uri(android.net.Uri) Test(org.junit.Test)

Example 50 with ContentProvider

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);
}
Also used : ContentProvider(android.content.ContentProvider) Uri(android.net.Uri) Test(org.junit.Test)

Aggregations

ContentProvider (android.content.ContentProvider)54 Uri (android.net.Uri)29 IContentProvider (android.content.IContentProvider)16 Test (org.junit.Test)13 Implementation (org.robolectric.annotation.Implementation)11 ContentValues (android.content.ContentValues)8 Context (android.content.Context)7 ApplicationInfo (android.content.pm.ApplicationInfo)7 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)7 RemoteException (android.os.RemoteException)7 AndroidRuntimeException (android.util.AndroidRuntimeException)7 ComponentName (android.content.ComponentName)6 IBinder (android.os.IBinder)6 BaseCursor (org.robolectric.fakes.BaseCursor)6 ContentProviderOperation (android.content.ContentProviderOperation)4 OperationApplicationException (android.content.OperationApplicationException)4 ContentResolver (android.content.ContentResolver)3 ProviderInfo (android.content.pm.ProviderInfo)3 Cursor (android.database.Cursor)3 MatrixCursor (android.database.MatrixCursor)3