Search in sources :

Example 6 with Changes

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

the class PutOperationTest method insertObjectAsRxFlowable.

@Test
public void insertObjectAsRxFlowable() {
    TestSubscriber<Changes> changesTestSubscriber = new TestSubscriber<Changes>();
    storIOContentResolver.observeChangesOfUri(TestItem.CONTENT_URI, BackpressureStrategy.MISSING).take(1).subscribe(changesTestSubscriber);
    TestItem testItem = TestItem.create(null, "value");
    PutResult insertResult = storIOContentResolver.put().object(testItem).prepare().asRxFlowable(BackpressureStrategy.MISSING).blockingFirst();
    assertThat(insertResult.wasInserted()).isTrue();
    Cursor cursor = contentResolver.query(TestItem.CONTENT_URI, null, null, null, null);
    Assertions.assertThat(cursor).hasCount(1);
    cursor.moveToFirst();
    assertThat(testItem.equalsWithoutId(TestItem.fromCursor(cursor))).isTrue();
    cursor.close();
    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) PutResult(com.pushtorefresh.storio3.contentresolver.operations.put.PutResult) Test(org.junit.Test)

Example 7 with Changes

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

the class RxChangesObserverTest method shouldEmitChangesOnSdkVersionGreaterThan15.

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Test
public void shouldEmitChangesOnSdkVersionGreaterThan15() {
    for (int sdkVersion = 16; sdkVersion < MAX_SDK_VERSION; sdkVersion++) {
        ContentResolver contentResolver = mock(ContentResolver.class);
        final AtomicReference<ContentObserver> contentObserver = new AtomicReference<ContentObserver>();
        doAnswer(new Answer() {

            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                // Save reference to ContentObserver only once to assert that it was created once
                if (contentObserver.get() == null) {
                    contentObserver.set((ContentObserver) invocation.getArguments()[2]);
                } else if (contentObserver.get() != invocation.getArguments()[2]) {
                    throw new AssertionError("More than one ContentObserver was created");
                }
                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();
        // RxChangesObserver should ignore call to onChange() without Uri on sdkVersion >= 16
        contentObserver.get().onChange(false);
        testSubscriber.assertNoValues();
        // Emulate change of Uris, Flowable should react and emit Changes objects
        contentObserver.get().onChange(false, uri1);
        contentObserver.get().onChange(false, uri2);
        testSubscriber.assertValues(Changes.newInstance(uri1), Changes.newInstance(uri2));
        testSubscriber.dispose();
        testSubscriber.assertNoErrors();
    }
}
Also used : Changes(com.pushtorefresh.storio3.contentresolver.Changes) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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 8 with Changes

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

the class RxChangesObserverTest method shouldRegisterObserverForEachPassedUriAfterSubscribingToFlowableOnSdkVersionLowerThan15.

@Test
public void shouldRegisterObserverForEachPassedUriAfterSubscribingToFlowableOnSdkVersionLowerThan15() {
    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));
        Set<Uri> uris = new HashSet<Uri>(3);
        uris.add(mock(Uri.class));
        uris.add(mock(Uri.class));
        uris.add(mock(Uri.class));
        Flowable<Changes> flowable = RxChangesObserver.observeChanges(contentResolver, uris, mock(Handler.class), sdkVersion, BackpressureStrategy.MISSING);
        // Should not register ContentObserver before subscribing to Flowable
        verify(contentResolver, times(0)).registerContentObserver(any(Uri.class), anyBoolean(), any(ContentObserver.class));
        Disposable disposable = flowable.subscribe();
        for (Uri uri : uris) {
            // Assert that new ContentObserver was registered for each uri
            verify(contentResolver).registerContentObserver(same(uri), eq(true), same(contentObservers.get(uri)));
        }
        assertThat(contentObservers).hasSameSizeAs(uris);
        disposable.dispose();
    }
}
Also used : Changes(com.pushtorefresh.storio3.contentresolver.Changes) Disposable(io.reactivex.disposables.Disposable) HashMap(java.util.HashMap) Handler(android.os.Handler) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ContentObserver(android.database.ContentObserver) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 9 with Changes

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

the class RxChangesObserverTest method contentObserverShouldReturnFalseOnDeliverSelfNotificationsOnAllSdkVersions.

@Test
public void contentObserverShouldReturnFalseOnDeliverSelfNotificationsOnAllSdkVersions() {
    for (int sdkVersion = MIN_SDK_VERSION; sdkVersion < MAX_SDK_VERSION; sdkVersion++) {
        ContentResolver contentResolver = mock(ContentResolver.class);
        Uri uri = mock(Uri.class);
        final AtomicReference<ContentObserver> contentObserver = new AtomicReference<ContentObserver>();
        doAnswer(new Answer() {

            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                contentObserver.set((ContentObserver) invocation.getArguments()[2]);
                return null;
            }
        }).when(contentResolver).registerContentObserver(same(uri), eq(true), any(ContentObserver.class));
        Handler handler = mock(Handler.class);
        Flowable<Changes> flowable = RxChangesObserver.observeChanges(contentResolver, singleton(uri), handler, sdkVersion, BackpressureStrategy.MISSING);
        Disposable disposable = flowable.subscribe();
        assertThat(contentObserver.get().deliverSelfNotifications()).isFalse();
        disposable.dispose();
    }
}
Also used : Changes(com.pushtorefresh.storio3.contentresolver.Changes) Disposable(io.reactivex.disposables.Disposable) Handler(android.os.Handler) AtomicReference(java.util.concurrent.atomic.AtomicReference) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ContentObserver(android.database.ContentObserver) Test(org.junit.Test)

Example 10 with Changes

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

the class RxChangesObserverTest method shouldRegisterOnlyOneContentObserverAfterSubscribingToFlowableOnSdkVersionGreaterThan15.

@Test
public void shouldRegisterOnlyOneContentObserverAfterSubscribingToFlowableOnSdkVersionGreaterThan15() {
    for (int sdkVersion = 16; sdkVersion < MAX_SDK_VERSION; sdkVersion++) {
        ContentResolver contentResolver = mock(ContentResolver.class);
        final AtomicReference<ContentObserver> contentObserver = new AtomicReference<ContentObserver>();
        doAnswer(new Answer() {

            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                // Save reference to ContentObserver only once to assert that it was created once
                if (contentObserver.get() == null) {
                    contentObserver.set((ContentObserver) invocation.getArguments()[2]);
                } else if (contentObserver.get() != invocation.getArguments()[2]) {
                    throw new AssertionError("More than one ContentObserver was created");
                }
                return null;
            }
        }).when(contentResolver).registerContentObserver(any(Uri.class), eq(true), any(ContentObserver.class));
        Set<Uri> uris = new HashSet<Uri>(3);
        uris.add(mock(Uri.class));
        uris.add(mock(Uri.class));
        uris.add(mock(Uri.class));
        Flowable<Changes> flowable = RxChangesObserver.observeChanges(contentResolver, uris, mock(Handler.class), sdkVersion, BackpressureStrategy.MISSING);
        // Should not register ContentObserver before subscribing to Flowable
        verify(contentResolver, times(0)).registerContentObserver(any(Uri.class), anyBoolean(), any(ContentObserver.class));
        Disposable disposable = flowable.subscribe();
        for (Uri uri : uris) {
            // Assert that same ContentObserver was registered for all uris
            verify(contentResolver).registerContentObserver(same(uri), eq(true), same(contentObserver.get()));
        }
        disposable.dispose();
    }
}
Also used : Changes(com.pushtorefresh.storio3.contentresolver.Changes) Disposable(io.reactivex.disposables.Disposable) Handler(android.os.Handler) AtomicReference(java.util.concurrent.atomic.AtomicReference) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ContentObserver(android.database.ContentObserver) HashSet(java.util.HashSet) 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