1 package android.testing; 2 3 import android.content.ContentProvider; 4 import android.content.IContentProvider; 5 6 import androidx.test.InstrumentationRegistry; 7 import androidx.test.filters.SmallTest; 8 9 import org.junit.Assert; 10 import org.junit.Before; 11 import org.junit.Rule; 12 import org.junit.Test; 13 import org.junit.runner.RunWith; 14 import org.mockito.Mockito; 15 16 @SmallTest 17 @RunWith(AndroidTestingRunner.class) 18 public class TestableContentResolverTest { 19 20 @Rule 21 public TestableContext mContext = new TestableContext(InstrumentationRegistry.getContext()); 22 private TestableContentResolver mContentResolver; 23 24 @Before setup()25 public void setup() { 26 mContentResolver = new TestableContentResolver(mContext); 27 mContentResolver.setFallbackToExisting(false); 28 } 29 30 @Test testDefaultContentProvider()31 public void testDefaultContentProvider() { 32 ContentProvider provider = Mockito.mock(ContentProvider.class); 33 IContentProvider iprovider = Mockito.mock(IContentProvider.class); 34 Mockito.when(provider.getIContentProvider()).thenReturn(iprovider); 35 mContentResolver.addProvider("test", provider); 36 37 Assert.assertEquals(iprovider, mContentResolver.acquireProvider(mContext, "test")); 38 Assert.assertEquals(iprovider, mContentResolver.acquireUnstableProvider(mContext, "test")); 39 } 40 41 @Test testStableContentProvider()42 public void testStableContentProvider() { 43 ContentProvider provider = Mockito.mock(ContentProvider.class); 44 IContentProvider iprovider = Mockito.mock(IContentProvider.class); 45 Mockito.when(provider.getIContentProvider()).thenReturn(iprovider); 46 mContentResolver.addProvider("test", provider, TestableContentResolver.STABLE); 47 48 Assert.assertEquals(iprovider, mContentResolver.acquireProvider(mContext, "test")); 49 Assert.assertNull(mContentResolver.acquireUnstableProvider(mContext, "test")); 50 } 51 52 @Test testUnstableContentProvider()53 public void testUnstableContentProvider() { 54 ContentProvider provider = Mockito.mock(ContentProvider.class); 55 IContentProvider iprovider = Mockito.mock(IContentProvider.class); 56 Mockito.when(provider.getIContentProvider()).thenReturn(iprovider); 57 mContentResolver.addProvider("test", provider, TestableContentResolver.UNSTABLE); 58 59 Assert.assertEquals(iprovider, mContentResolver.acquireUnstableProvider(mContext, "test")); 60 Assert.assertNull(mContentResolver.acquireProvider(mContext, "test")); 61 } 62 } 63