Home | History | Annotate | Download | only in shadows
      1 package org.robolectric.shadows;
      2 
      3 import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
      4 import static org.assertj.core.api.Assertions.assertThat;
      5 import static org.assertj.core.api.Assertions.fail;
      6 import static org.mockito.Mockito.verify;
      7 import static org.mockito.MockitoAnnotations.initMocks;
      8 import static org.robolectric.Shadows.shadowOf;
      9 
     10 import android.content.ContentProvider;
     11 import android.content.ContentProviderClient;
     12 import android.content.ContentProviderOperation;
     13 import android.content.ContentResolver;
     14 import android.content.ContentValues;
     15 import android.net.Uri;
     16 import android.os.Bundle;
     17 import android.os.CancellationSignal;
     18 import java.util.ArrayList;
     19 import org.junit.Before;
     20 import org.junit.Test;
     21 import org.junit.runner.RunWith;
     22 import org.mockito.Mock;
     23 import org.robolectric.RobolectricTestRunner;
     24 import org.robolectric.RuntimeEnvironment;
     25 import org.robolectric.annotation.Config;
     26 
     27 @RunWith(RobolectricTestRunner.class)
     28 public class ShadowContentProviderClientTest {
     29 
     30   private static final String AUTHORITY = "org.robolectric";
     31   private final Uri URI = Uri.parse("content://" + AUTHORITY);
     32   private final ContentValues VALUES = new ContentValues();
     33   private static final String[] PROJECTION = null;
     34   private static final String SELECTION = "1=?";
     35   private static final String[] SELECTION_ARGS = {"1"};
     36   private static final String SORT_ORDER = "DESC";
     37   private static final String MIME_TYPE = "application/octet-stream";
     38 
     39   @Mock ContentProvider provider;
     40   ContentResolver contentResolver = RuntimeEnvironment.application.getContentResolver();
     41 
     42   @Before
     43   public void setUp() {
     44     initMocks(this);
     45     ShadowContentResolver.registerProviderInternal(AUTHORITY, provider);
     46   }
     47 
     48   @Test
     49   public void acquireContentProviderClient_isStable() {
     50     ContentProviderClient client = contentResolver.acquireContentProviderClient(AUTHORITY);
     51     assertThat(shadowOf(client).isStable()).isTrue();
     52   }
     53 
     54   @Test
     55   public void acquireUnstableContentProviderClient_isUnstable() {
     56     ContentProviderClient client = contentResolver.acquireUnstableContentProviderClient(AUTHORITY);
     57     assertThat(shadowOf(client).isStable()).isFalse();
     58   }
     59 
     60   @Test
     61   public void release_shouldRelease() {
     62     ContentProviderClient client = contentResolver.acquireContentProviderClient(AUTHORITY);
     63     ShadowContentProviderClient shadow = shadowOf(client);
     64     assertThat(shadow.isReleased()).isFalse();
     65     client.release();
     66     assertThat(shadow.isReleased()).isTrue();
     67   }
     68 
     69   @Test(expected = IllegalStateException.class)
     70   public void release_shouldFailWhenCalledTwice() {
     71     ContentProviderClient client = contentResolver.acquireContentProviderClient(AUTHORITY);
     72     client.release();
     73     client.release();
     74     fail("client.release() was called twice and did not throw");
     75   }
     76 
     77   @Test
     78   @Config(minSdk = JELLY_BEAN_MR2)
     79   public void shouldDelegateToContentProvider() throws Exception {
     80     ContentProviderClient client = contentResolver.acquireContentProviderClient(AUTHORITY);
     81 
     82     client.query(URI, PROJECTION, SELECTION, SELECTION_ARGS, SORT_ORDER);
     83     verify(provider).query(URI, PROJECTION, SELECTION, SELECTION_ARGS, SORT_ORDER);
     84 
     85     CancellationSignal signal = new CancellationSignal();
     86     client.query(URI, PROJECTION, SELECTION, SELECTION_ARGS, SORT_ORDER, signal);
     87     verify(provider).query(URI, PROJECTION, SELECTION, SELECTION_ARGS, SORT_ORDER, signal);
     88 
     89     client.insert(URI, VALUES);
     90     verify(provider).insert(URI, VALUES);
     91 
     92     client.update(URI, VALUES, SELECTION, SELECTION_ARGS);
     93     verify(provider).update(URI, VALUES, SELECTION, SELECTION_ARGS);
     94 
     95     client.delete(URI, SELECTION, SELECTION_ARGS);
     96     verify(provider).delete(URI, SELECTION, SELECTION_ARGS);
     97 
     98     client.getType(URI);
     99     verify(provider).getType(URI);
    100 
    101     client.openFile(URI, "rw");
    102     verify(provider).openFile(URI, "rw");
    103 
    104     client.openAssetFile(URI, "r");
    105     verify(provider).openAssetFile(URI, "r");
    106 
    107     final Bundle opts = new Bundle();
    108     client.openTypedAssetFileDescriptor(URI, MIME_TYPE, opts);
    109     verify(provider).openTypedAssetFile(URI, MIME_TYPE, opts);
    110 
    111     client.getStreamTypes(URI, MIME_TYPE);
    112     verify(provider).getStreamTypes(URI, MIME_TYPE);
    113 
    114     final ArrayList<ContentProviderOperation> ops = new ArrayList<>();
    115     client.applyBatch(ops);
    116     verify(provider).applyBatch(ops);
    117 
    118     final ContentValues[] values = {VALUES};
    119     client.bulkInsert(URI, values);
    120     verify(provider).bulkInsert(URI, values);
    121 
    122     final String method = "method";
    123     final String arg = "arg";
    124     final Bundle extras = new Bundle();
    125     client.call(method, arg, extras);
    126     verify(provider).call(method, arg, extras);
    127   }
    128 }
    129