1 package org.robolectric.android.controller; 2 3 import static com.google.common.truth.Truth.assertThat; 4 5 import android.app.Application; 6 import android.content.ContentProviderClient; 7 import android.content.ContentResolver; 8 import android.content.pm.PathPermission; 9 import android.content.pm.ProviderInfo; 10 import android.net.Uri; 11 import androidx.test.core.app.ApplicationProvider; 12 import androidx.test.ext.junit.runners.AndroidJUnit4; 13 import org.junit.Before; 14 import org.junit.Test; 15 import org.junit.runner.RunWith; 16 import org.robolectric.Robolectric; 17 import org.robolectric.shadows.testing.TestContentProvider1; 18 19 @RunWith(AndroidJUnit4.class) 20 public class ContentProviderControllerTest { 21 private final ContentProviderController<TestContentProvider1> controller = Robolectric.buildContentProvider(TestContentProvider1.class); 22 private ContentResolver contentResolver; 23 24 @Before setUp()25 public void setUp() throws Exception { 26 contentResolver = ApplicationProvider.getApplicationContext().getContentResolver(); 27 } 28 29 @Test shouldSetBaseContext()30 public void shouldSetBaseContext() throws Exception { 31 TestContentProvider1 myContentProvider = controller.create().get(); 32 assertThat(myContentProvider.getContext()) 33 .isEqualTo(((Application) ApplicationProvider.getApplicationContext()).getBaseContext()); 34 } 35 36 @Test shouldInitializeFromManifestProviderInfo()37 public void shouldInitializeFromManifestProviderInfo() throws Exception { 38 TestContentProvider1 myContentProvider = controller.create().get(); 39 assertThat(myContentProvider.getReadPermission()).isEqualTo("READ_PERMISSION"); 40 assertThat(myContentProvider.getWritePermission()).isEqualTo("WRITE_PERMISSION"); 41 42 assertThat(myContentProvider.getPathPermissions()).asList().hasSize(1); 43 PathPermission pathPermission = myContentProvider.getPathPermissions()[0]; 44 assertThat(pathPermission.getPath()).isEqualTo("/path/*"); 45 assertThat(pathPermission.getType()).isEqualTo(PathPermission.PATTERN_SIMPLE_GLOB); 46 assertThat(pathPermission.getReadPermission()).isEqualTo("PATH_READ_PERMISSION"); 47 assertThat(pathPermission.getWritePermission()).isEqualTo("PATH_WRITE_PERMISSION"); 48 } 49 50 @Test shouldRegisterWithContentResolver()51 public void shouldRegisterWithContentResolver() throws Exception { 52 controller.create().get(); 53 54 ContentProviderClient client = 55 contentResolver.acquireContentProviderClient( 56 "org.robolectric.authority1"); 57 client.query(Uri.parse("something"), new String[]{"title"}, "*", new String[]{}, "created"); 58 assertThat(controller.get().transcript).containsExactly("onCreate", "query for something"); 59 } 60 61 @Test whenNoProviderManifestEntryFound_shouldStillInitialize()62 public void whenNoProviderManifestEntryFound_shouldStillInitialize() throws Exception { 63 TestContentProvider1 myContentProvider = Robolectric.buildContentProvider(NotInManifestContentProvider.class).create().get(); 64 assertThat(myContentProvider.getReadPermission()).isNull(); 65 assertThat(myContentProvider.getWritePermission()).isNull(); 66 assertThat(myContentProvider.getPathPermissions()).isNull(); 67 } 68 69 @Test create_shouldCallOnCreate()70 public void create_shouldCallOnCreate() throws Exception { 71 TestContentProvider1 myContentProvider = controller.create().get(); 72 assertThat(myContentProvider.transcript).containsExactly("onCreate"); 73 } 74 75 @Test shutdown_shouldCallShutdown()76 public void shutdown_shouldCallShutdown() throws Exception { 77 TestContentProvider1 myContentProvider = controller.shutdown().get(); 78 assertThat(myContentProvider.transcript).containsExactly("shutdown"); 79 } 80 81 @Test withoutManifest_shouldRegisterWithContentResolver()82 public void withoutManifest_shouldRegisterWithContentResolver() throws Exception { 83 ProviderInfo providerInfo = new ProviderInfo(); 84 providerInfo.authority = "some-authority"; 85 controller.create(providerInfo); 86 87 ContentProviderClient client = contentResolver.acquireContentProviderClient(providerInfo.authority); 88 client.query(Uri.parse("something"), new String[]{"title"}, "*", new String[]{}, "created"); 89 assertThat(controller.get().transcript).containsExactly("onCreate", "query for something"); 90 } 91 92 @Test contentProviderShouldBeCreatedBeforeBeingRegistered()93 public void contentProviderShouldBeCreatedBeforeBeingRegistered() throws Exception { 94 XContentProvider xContentProvider = Robolectric.setupContentProvider(XContentProvider.class, "x-authority"); 95 assertThat(xContentProvider.transcript).containsExactly("x-authority not registered yet"); 96 ContentProviderClient contentProviderClient = contentResolver.acquireContentProviderClient("x-authority"); 97 assertThat(contentProviderClient.getLocalContentProvider()).isSameAs(xContentProvider); 98 } 99 100 //////////////////// 101 102 static class XContentProvider extends TestContentProvider1 { 103 @Override onCreate()104 public boolean onCreate() { 105 ContentProviderClient contentProviderClient = 106 ApplicationProvider.getApplicationContext() 107 .getContentResolver() 108 .acquireContentProviderClient("x-authority"); 109 transcript.add(contentProviderClient == null ? "x-authority not registered yet" : "x-authority is registered"); 110 return false; 111 } 112 } 113 114 static class NotInManifestContentProvider extends TestContentProvider1 {} 115 }