org.robolectric.shadows.ShadowContentResolver Java Examples

The following examples show how to use org.robolectric.shadows.ShadowContentResolver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ItemSyncAdapterTest.java    From materialistic with Apache License 2.0 6 votes vote down vote up
@Test
public void testWifiChange() {
    setNetworkType(ConnectivityManager.TYPE_MOBILE);
    new ItemSyncWifiReceiver()
            .onReceive(service, new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
    assertThat(ShadowContentResolver.getStatus(createSyncAccount(),
            SyncContentProvider.PROVIDER_AUTHORITY, true).syncRequests).isEqualTo(0);

    setNetworkType(ConnectivityManager.TYPE_WIFI);
    new ItemSyncWifiReceiver().onReceive(service, new Intent());
    assertThat(ShadowContentResolver.getStatus(createSyncAccount(),
            SyncContentProvider.PROVIDER_AUTHORITY, true).syncRequests).isEqualTo(0);

    setNetworkType(ConnectivityManager.TYPE_WIFI);
    new ItemSyncWifiReceiver()
            .onReceive(service, new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
    assertThat(ShadowContentResolver.getStatus(createSyncAccount(),
            SyncContentProvider.PROVIDER_AUTHORITY, true).syncRequests).isGreaterThan(0);
}
 
Example #2
Source File: Assert.java    From fdroidclient with GNU General Public License v3.0 6 votes vote down vote up
public static void assertIsInstalledVersionInDb(ShadowContentResolver resolver,
                                                String appId, int versionCode, String versionName) {
    Uri uri = InstalledAppProvider.getAppUri(appId);

    String[] projection = {
            InstalledAppTable.Cols.Package.NAME,
            InstalledAppTable.Cols.VERSION_CODE,
            InstalledAppTable.Cols.VERSION_NAME,
            InstalledAppTable.Cols.APPLICATION_LABEL,
    };

    Cursor cursor = resolver.query(uri, projection, null, null, null);

    assertNotNull(cursor);
    assertEquals("App \"" + appId + "\" not installed", 1, cursor.getCount());

    cursor.moveToFirst();

    assertEquals(appId, cursor.getString(cursor.getColumnIndex(InstalledAppTable.Cols.Package.NAME)));
    assertEquals(versionCode, cursor.getInt(cursor.getColumnIndex(InstalledAppTable.Cols.VERSION_CODE)));
    assertEquals(versionName, cursor.getString(cursor.getColumnIndex(InstalledAppTable.Cols.VERSION_NAME)));
    cursor.close();
}
 
Example #3
Source File: ItemSyncAdapterTest.java    From materialistic with Apache License 2.0 5 votes vote down vote up
@Test
public void testSyncDeferred() throws IOException {
    Call<HackerNewsItem> call = mock(Call.class);
    when(call.execute()).thenThrow(IOException.class);
    when(TestRestServiceFactory.hnRestService.cachedItem(any())).thenReturn(call);
    when(TestRestServiceFactory.hnRestService.networkItem(any())).thenReturn(call);

    syncPreferences.edit().putBoolean("1", true).putBoolean("2", true).apply();
    syncScheduler.scheduleSync(service, null);
    adapter.onPerformSync(mock(Account.class), getLastSyncExtras(), null, null, null);
    ShadowContentResolver.Status syncStatus = ShadowContentResolver.getStatus(
            new Account("Materialistic", BuildConfig.APPLICATION_ID),
            SyncContentProvider.PROVIDER_AUTHORITY);
    assertThat(syncStatus.syncRequests).isEqualTo(3); // original + 2 deferred
}
 
Example #4
Source File: SimpleProviderTest.java    From simpleprovider with MIT License 5 votes vote down vote up
@Before
public void setUp() {
    // fetch references to all the stuff we need like ContentProvider
    mProvider = new TestProvider();
    mContentResolver = Robolectric.application.getContentResolver();
    mPostsUri = Uri.parse("content://" + TestProvider.AUTHORITY + "/posts");

    // create and register the provider
    mProvider.onCreate();
    ShadowContentResolver.registerProvider(TestProvider.AUTHORITY, mProvider);
}
 
Example #5
Source File: OllieTest.java    From Ollie with Apache License 2.0 5 votes vote down vote up
@Before
public void initialize() {
	ContentProvider contentProvider = new OllieSampleProvider();
	contentProvider.onCreate();

	ShadowLog.stream = System.out;
	ShadowContentResolver.registerProvider("com.example.ollie", contentProvider);

	Ollie.with(Robolectric.application)
			.setName("OllieSample.db")
			.setLogLevel(LogLevel.FULL)
			.init();
}
 
Example #6
Source File: ContentProviderTest.java    From droitatedDB with Apache License 2.0 5 votes vote down vote up
@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    CommentContentProvider provider = new CommentContentProvider();
    provider.onCreate();
    ShadowContentResolver.registerProvider("org.droitateddb.test.data.generated.provider.comment", provider);
    SingleContentProvider p = new SingleContentProvider();
    p.onCreate();
    ShadowContentResolver.registerProvider("org.droitateddb.test.data.generated.provider.single", p);

}
 
Example #7
Source File: ItemSyncAdapterTest.java    From materialistic with Apache License 2.0 5 votes vote down vote up
@Test
public void testSyncDisabled() {
    PreferenceManager.getDefaultSharedPreferences(service)
            .edit().clear().apply();
    syncScheduler.scheduleSync(service, "1");
    assertNull(ShadowContentResolver.getStatus(createSyncAccount(),
            SyncContentProvider.PROVIDER_AUTHORITY));
}
 
Example #8
Source File: Assert.java    From fdroidclient with GNU General Public License v3.0 4 votes vote down vote up
public static void assertInvalidUri(ShadowContentResolver resolver, String uri) {
    assertInvalidUri(resolver, Uri.parse(uri));
}
 
Example #9
Source File: Assert.java    From fdroidclient with GNU General Public License v3.0 4 votes vote down vote up
public static void assertValidUri(ShadowContentResolver resolver, String uri, String[] projection) {
    assertValidUri(resolver, Uri.parse(uri), projection);
}
 
Example #10
Source File: Assert.java    From fdroidclient with GNU General Public License v3.0 4 votes vote down vote up
public static void assertInvalidUri(ShadowContentResolver resolver, Uri uri) {
    Cursor cursor = resolver.query(uri, new String[]{}, null, null, null);
    assertNull(cursor);
}
 
Example #11
Source File: Assert.java    From fdroidclient with GNU General Public License v3.0 4 votes vote down vote up
public static void assertValidUri(ShadowContentResolver resolver, Uri uri, String[] projection) {
    Cursor cursor = resolver.query(uri, projection, null, null, null);
    assertNotNull(cursor);
    cursor.close();
}
 
Example #12
Source File: Assert.java    From fdroidclient with GNU General Public License v3.0 4 votes vote down vote up
public static void assertValidUri(ShadowContentResolver resolver, Uri actualUri, String expectedUri,
                                  String[] projection) {
    assertValidUri(resolver, actualUri, projection);
    assertEquals(expectedUri, actualUri.toString());
}
 
Example #13
Source File: Assert.java    From fdroidclient with GNU General Public License v3.0 4 votes vote down vote up
public static void assertResultCount(ShadowContentResolver resolver, int expectedCount, Uri uri) {
    assertResultCount(resolver, expectedCount, uri, new String[]{});
}
 
Example #14
Source File: Assert.java    From fdroidclient with GNU General Public License v3.0 4 votes vote down vote up
public static void assertResultCount(ShadowContentResolver resolver, int expectedCount, Uri uri,
                                     String[] projection) {
    Cursor cursor = resolver.query(uri, projection, null, null, null);
    assertResultCount(expectedCount, cursor);
    cursor.close();
}
 
Example #15
Source File: AppProviderTest.java    From fdroidclient with GNU General Public License v3.0 4 votes vote down vote up
public static App insertApp(ShadowContentResolver contentResolver, Context context, String id, String name, ContentValues additionalValues) {
    return insertApp(contentResolver, context, id, name, additionalValues, 1);
}
 
Example #16
Source File: AppProviderTest.java    From fdroidclient with GNU General Public License v3.0 4 votes vote down vote up
public static App insertApp(ShadowContentResolver contentResolver, Context context, String id, String name, ContentValues additionalValues, long repoId) {

        ContentValues values = new ContentValues();
        values.put(Cols.Package.PACKAGE_NAME, id);
        values.put(Cols.REPO_ID, repoId);
        values.put(Cols.NAME, name);

        // Required fields (NOT NULL in the database).
        values.put(Cols.SUMMARY, "test summary");
        values.put(Cols.DESCRIPTION, "test description");
        values.put(Cols.LICENSE, "GPL?");
        values.put(Cols.IS_COMPATIBLE, 1);

        values.put(Cols.PREFERRED_SIGNER, "eaa1d713b9c2a0475234a86d6539f910");

        values.putAll(additionalValues);

        Uri uri = AppProvider.getContentUri();

        contentResolver.insert(uri, values);

        AppProvider.Helper.recalculatePreferredMetadata(context);

        return AppProvider.Helper.findSpecificApp(context.getContentResolver(), id, repoId, Cols.ALL);
    }
 
Example #17
Source File: ItemSyncAdapterTest.java    From materialistic with Apache License 2.0 4 votes vote down vote up
private Bundle getLastSyncExtras() {
    return ShadowContentResolver.getStatus(createSyncAccount(),
            SyncContentProvider.PROVIDER_AUTHORITY).syncExtras;
}
 
Example #18
Source File: TestUtils.java    From fdroidclient with GNU General Public License v3.0 3 votes vote down vote up
/**
 * The way that Robolectric has to implement shadows for Android classes
 * such as {@link android.content.ContentProvider} is by using a special
 * annotation that means the classes will implement the correct methods at
 * runtime.  However this means that the shadow of a content provider does
 * not actually extend {@link android.content.ContentProvider}. As such,
 * we need to do some special mocking using Mockito in order to provide a
 * {@link ContextWrapper} which is able to return a proper content
 * resolver that delegates to the Robolectric shadow object.
 */
public static ContextWrapper createContextWithContentResolver(ShadowContentResolver contentResolver) {
    final ContentResolver resolver = mock(ContentResolver.class, AdditionalAnswers.delegatesTo(contentResolver));
    return new ContextWrapper(RuntimeEnvironment.application.getApplicationContext()) {
        @Override
        public ContentResolver getContentResolver() {
            return resolver;
        }
    };
}