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