Search in sources :

Example 26 with TestObserver

use of io.reactivex.observers.TestObserver in project retrofit by square.

the class AsyncTest method throwingInOnCompleteDeliveredToPlugin.

@Test
public void throwingInOnCompleteDeliveredToPlugin() throws InterruptedException {
    server.enqueue(new MockResponse());
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> errorRef = new AtomicReference<>();
    RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {

        @Override
        public void accept(Throwable throwable) throws Exception {
            if (!errorRef.compareAndSet(null, throwable)) {
                // Don't swallow secondary errors!
                throw Exceptions.propagate(throwable);
            }
            latch.countDown();
        }
    });
    TestObserver<Void> observer = new TestObserver<>();
    final RuntimeException e = new RuntimeException();
    service.completable().subscribe(new ForwardingCompletableObserver(observer) {

        @Override
        public void onComplete() {
            throw e;
        }
    });
    latch.await(1, SECONDS);
    assertThat(errorRef.get()).isSameAs(e);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) CompositeException(io.reactivex.exceptions.CompositeException) IOException(java.io.IOException) TestObserver(io.reactivex.observers.TestObserver) ForwardingCompletableObserver(retrofit2.adapter.rxjava2.CompletableThrowingTest.ForwardingCompletableObserver) Test(org.junit.Test)

Example 27 with TestObserver

use of io.reactivex.observers.TestObserver in project RxPermissions by tbruyelle.

the class RxPermissionsTest method eachSubscriptionCombined_trigger_granted.

@Test
@TargetApi(Build.VERSION_CODES.M)
public void eachSubscriptionCombined_trigger_granted() {
    TestObserver<Permission> sub = new TestObserver<>();
    String permission = Manifest.permission.READ_PHONE_STATE;
    when(mRxPermissions.isGranted(permission)).thenReturn(false);
    int[] result = new int[] { PackageManager.PERMISSION_GRANTED };
    PublishSubject<Object> trigger = PublishSubject.create();
    trigger.compose(mRxPermissions.ensureEachCombined(permission)).subscribe(sub);
    trigger.onNext(1);
    mRxPermissions.onRequestPermissionsResult(new String[] { permission }, result);
    sub.assertNoErrors();
    sub.assertNotTerminated();
    sub.assertValue(new Permission(permission, true));
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test) TargetApi(android.annotation.TargetApi)

Example 28 with TestObserver

use of io.reactivex.observers.TestObserver in project RxPermissions by tbruyelle.

the class RxPermissionsTest method subscription_trigger_granted.

@Test
@TargetApi(Build.VERSION_CODES.M)
public void subscription_trigger_granted() {
    TestObserver<Boolean> sub = new TestObserver<>();
    String permission = Manifest.permission.READ_PHONE_STATE;
    when(mRxPermissions.isGranted(permission)).thenReturn(false);
    int[] result = new int[] { PackageManager.PERMISSION_GRANTED };
    PublishSubject<Object> trigger = PublishSubject.create();
    trigger.compose(mRxPermissions.ensure(permission)).subscribe(sub);
    trigger.onNext(1);
    mRxPermissions.onRequestPermissionsResult(new String[] { permission }, result);
    sub.assertNoErrors();
    sub.assertNotTerminated();
    sub.assertValue(true);
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test) TargetApi(android.annotation.TargetApi)

Example 29 with TestObserver

use of io.reactivex.observers.TestObserver in project RxPermissions by tbruyelle.

the class RxPermissionsTest method eachSubscription_trigger_granted.

@Test
@TargetApi(Build.VERSION_CODES.M)
public void eachSubscription_trigger_granted() {
    TestObserver<Permission> sub = new TestObserver<>();
    String permission = Manifest.permission.READ_PHONE_STATE;
    when(mRxPermissions.isGranted(permission)).thenReturn(false);
    int[] result = new int[] { PackageManager.PERMISSION_GRANTED };
    PublishSubject<Object> trigger = PublishSubject.create();
    trigger.compose(mRxPermissions.ensureEach(permission)).subscribe(sub);
    trigger.onNext(1);
    mRxPermissions.onRequestPermissionsResult(new String[] { permission }, result);
    sub.assertNoErrors();
    sub.assertNotTerminated();
    sub.assertValue(new Permission(permission, true));
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test) TargetApi(android.annotation.TargetApi)

Example 30 with TestObserver

use of io.reactivex.observers.TestObserver in project RxPermissions by tbruyelle.

the class RxPermissionsTest method subscription_granted.

@Test
@TargetApi(Build.VERSION_CODES.M)
public void subscription_granted() {
    TestObserver<Boolean> sub = new TestObserver<>();
    String permission = Manifest.permission.READ_PHONE_STATE;
    when(mRxPermissions.isGranted(permission)).thenReturn(false);
    int[] result = new int[] { PackageManager.PERMISSION_GRANTED };
    trigger().compose(mRxPermissions.ensure(permission)).subscribe(sub);
    mRxPermissions.onRequestPermissionsResult(new String[] { permission }, result);
    sub.assertNoErrors();
    sub.assertTerminated();
    sub.assertValue(true);
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test) TargetApi(android.annotation.TargetApi)

Aggregations

TestObserver (io.reactivex.observers.TestObserver)158 Test (org.junit.Test)128 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)31 TargetApi (android.annotation.TargetApi)21 Matchers.anyString (org.mockito.Matchers.anyString)21 StorIOException (com.pushtorefresh.storio3.StorIOException)19 TestException (io.reactivex.exceptions.TestException)19 Observable (io.reactivex.Observable)12 BaseTest (io.rx_cache2.internal.common.BaseTest)12 Completable (io.reactivex.Completable)11 Disposable (io.reactivex.disposables.Disposable)11 InOrder (org.mockito.InOrder)10 TestScheduler (io.reactivex.schedulers.TestScheduler)9 StorIOSQLite (com.pushtorefresh.storio3.sqlite.StorIOSQLite)8 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)7 Schedulers (io.reactivex.schedulers.Schedulers)6 Activity (android.app.Activity)5 Instrumentation (android.app.Instrumentation)5 ContentValues (android.content.ContentValues)5 Context (android.content.Context)5