Search in sources :

Example 56 with Changes

use of com.pushtorefresh.storio3.contentresolver.Changes in project storio by pushtorefresh.

the class DefaultStorIOSQLiteTest method observeChangesInTables_shouldReceiveIfTableWasChanged.

@Test
public void observeChangesInTables_shouldReceiveIfTableWasChanged() {
    TestSubscriber<Changes> testSubscriber = new TestSubscriber<Changes>();
    Set<String> tables = new HashSet<String>(2);
    tables.add("table1");
    tables.add("table2");
    storIOSQLite.observeChangesInTables(tables, LATEST).subscribe(testSubscriber);
    testSubscriber.assertNoValues();
    Changes changes = Changes.newInstance("table2");
    storIOSQLite.lowLevel().notifyAboutChanges(changes);
    testSubscriber.assertValues(changes);
    testSubscriber.assertNoErrors();
    testSubscriber.dispose();
}
Also used : Changes(com.pushtorefresh.storio3.sqlite.Changes) TestSubscriber(io.reactivex.subscribers.TestSubscriber) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 57 with Changes

use of com.pushtorefresh.storio3.contentresolver.Changes in project storio by pushtorefresh.

the class RxChangesObserverTest method shouldEmitChangesOnSdkVersionLowerThan16.

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Test
public void shouldEmitChangesOnSdkVersionLowerThan16() {
    for (int sdkVersion = MIN_SDK_VERSION; sdkVersion < 16; sdkVersion++) {
        ContentResolver contentResolver = mock(ContentResolver.class);
        final Map<Uri, ContentObserver> contentObservers = new HashMap<Uri, ContentObserver>(3);
        doAnswer(new Answer() {

            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                contentObservers.put((Uri) invocation.getArguments()[0], (ContentObserver) invocation.getArguments()[2]);
                return null;
            }
        }).when(contentResolver).registerContentObserver(any(Uri.class), eq(true), any(ContentObserver.class));
        TestSubscriber<Changes> testSubscriber = new TestSubscriber<Changes>();
        Uri uri1 = mock(Uri.class);
        Uri uri2 = mock(Uri.class);
        Set<Uri> uris = new HashSet<Uri>(2);
        uris.add(uri1);
        uris.add(uri2);
        RxChangesObserver.observeChanges(contentResolver, uris, mock(Handler.class), sdkVersion, BackpressureStrategy.MISSING).subscribe(testSubscriber);
        testSubscriber.assertNotTerminated();
        testSubscriber.assertNoValues();
        // Emulate change of Uris, Flowable should react and emit Changes objects
        contentObservers.get(uri1).onChange(false);
        contentObservers.get(uri2).onChange(false);
        testSubscriber.assertValues(Changes.newInstance(uri1), Changes.newInstance(uri2));
        testSubscriber.dispose();
        testSubscriber.assertNoErrors();
    }
}
Also used : Changes(com.pushtorefresh.storio3.contentresolver.Changes) HashMap(java.util.HashMap) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TestSubscriber(io.reactivex.subscribers.TestSubscriber) ContentObserver(android.database.ContentObserver) HashSet(java.util.HashSet) Test(org.junit.Test) TargetApi(android.annotation.TargetApi)

Example 58 with Changes

use of com.pushtorefresh.storio3.contentresolver.Changes in project storio by pushtorefresh.

the class GetOperationTest method getCursorExecuteAsBlocking.

@Test
public void getCursorExecuteAsBlocking() {
    final TestSubscriber<Changes> changesTestSubscriber = new TestSubscriber<Changes>();
    storIOContentResolver.observeChangesOfUri(TestItem.CONTENT_URI, BackpressureStrategy.MISSING).take(1).subscribe(changesTestSubscriber);
    TestItem testItemToInsert = TestItem.create(null, "value");
    contentResolver.insert(TestItem.CONTENT_URI, testItemToInsert.toContentValues());
    Cursor cursor = storIOContentResolver.get().cursor().withQuery(Query.builder().uri(TestItem.CONTENT_URI).build()).prepare().executeAsBlocking();
    Assertions.assertThat(cursor).hasCount(1);
    cursor.moveToFirst();
    assertThat(testItemToInsert.equalsWithoutId(TestItem.fromCursor(cursor))).isTrue();
    changesTestSubscriber.awaitTerminalEvent(60, SECONDS);
    changesTestSubscriber.assertNoErrors();
    changesTestSubscriber.assertValue(Changes.newInstance(TestItem.CONTENT_URI));
}
Also used : Changes(com.pushtorefresh.storio3.contentresolver.Changes) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Cursor(android.database.Cursor) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)56 TestSubscriber (io.reactivex.subscribers.TestSubscriber)47 Changes (com.pushtorefresh.storio3.contentresolver.Changes)37 Cursor (android.database.Cursor)33 Uri (android.net.Uri)16 Changes (com.pushtorefresh.storio3.sqlite.Changes)16 PutResult (com.pushtorefresh.storio3.contentresolver.operations.put.PutResult)15 Disposable (io.reactivex.disposables.Disposable)11 HashSet (java.util.HashSet)7 DeleteResult (com.pushtorefresh.storio3.contentresolver.operations.delete.DeleteResult)6 AbstractEmissionChecker (com.pushtorefresh.storio3.test.AbstractEmissionChecker)6 LinkedList (java.util.LinkedList)6 ContentResolver (android.content.ContentResolver)5 ContentObserver (android.database.ContentObserver)5 ArrayList (java.util.ArrayList)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Mockito.doAnswer (org.mockito.Mockito.doAnswer)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5 Answer (org.mockito.stubbing.Answer)5 ContentValues (android.content.ContentValues)4