Java Code Examples for io.reactivex.rxjava3.observers.TestObserver#assertValues()

The following examples show how to use io.reactivex.rxjava3.observers.TestObserver#assertValues() . 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: RxMobiusLoopTest.java    From mobius with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSupportStartingALoopWithAnInit() throws Exception {
  MobiusLoop.Builder<String, Integer, Boolean> withInit =
      builder.init(
          new Init<String, Boolean>() {
            @Nonnull
            @Override
            public First<String, Boolean> init(String model) {
              return First.first(model + "-init");
            }
          });

  ObservableTransformer<Integer, String> transformer = RxMobius.loopFrom(withInit, "hi");

  final TestObserver<String> observer = Observable.just(10).compose(transformer).test();

  observer.awaitCount(2);
  observer.assertValues("hi-init", "hi-init10");
}
 
Example 2
Source File: ReplayRelayTest.java    From RxRelay with Apache License 2.0 6 votes vote down vote up
@Test
public void reentrantDrain() {
    TestScheduler scheduler = new TestScheduler();

    final ReplayRelay<Integer>
        rp = ReplayRelay.createWithTimeAndSize(1, TimeUnit.SECONDS, scheduler, 2);

    TestObserver<Integer> ts = new TestObserver<Integer>() {
        @Override
        public void onNext(Integer t) {
            if (t == 1) {
                rp.accept(2);
            }
            super.onNext(t);
        }
    };

    rp.subscribe(ts);

    rp.accept(1);

    ts.assertValues(1, 2);
}
 
Example 3
Source File: AndroidLifecycleActivityTest.java    From RxLifecycle with Apache License 2.0 6 votes vote down vote up
private void testLifecycle(ActivityController<? extends LifecycleOwner> controller) {
    LifecycleProvider<Lifecycle.Event> provider = AndroidLifecycle.createLifecycleProvider(controller.get());

    TestObserver<Lifecycle.Event> testObserver = provider.lifecycle().test();

    controller.create();
    controller.start();
    controller.resume();
    controller.pause();
    controller.stop();
    controller.destroy();

    testObserver.assertValues(
            Lifecycle.Event.ON_CREATE,
            Lifecycle.Event.ON_START,
            Lifecycle.Event.ON_RESUME,
            Lifecycle.Event.ON_PAUSE,
            Lifecycle.Event.ON_STOP,
            Lifecycle.Event.ON_DESTROY
    );
}
 
Example 4
Source File: ReplayingShareObservableTest.java    From RxReplayingShare with Apache License 2.0 6 votes vote down vote up
@Test public void initialValueToNewSubscriberAfterUnsubscribe() {
  PublishSubject<String> subject = PublishSubject.create();
  Observable<String> observable = subject.compose(ReplayingShare.<String>instance());

  TestObserver<String> observer1 = new TestObserver<>();
  observable.subscribe(observer1);
  observer1.assertNoValues();

  subject.onNext("Foo");
  observer1.assertValues("Foo");
  observer1.dispose();

  TestObserver<String> observer2 = new TestObserver<>();
  observable.subscribe(observer2);
  observer2.assertValues("Foo");
}
 
Example 5
Source File: ReplayingShareObservableTest.java    From RxReplayingShare with Apache License 2.0 6 votes vote down vote up
@Ignore("No backpressure in Observable")
@Test public void backpressureHonoredWhenCached() {
  PublishSubject<String> subject = PublishSubject.create();
  Observable<String> observable = subject.compose(ReplayingShare.<String>instance());

  TestObserver<String> observer1 = new TestObserver<>();
  observable.subscribe(observer1);
  observer1.assertNoValues();

  subject.onNext("Foo");
  observer1.assertValues("Foo");

  TestObserver<String> observer2 = new TestObserver<>(/*0*/);
  observable.subscribe(observer2);
  observer2.assertNoValues();

  subject.onNext("Bar"); // Replace the cached value...
  observer1.assertValues("Foo", "Bar");

  //observer2.requestMore(1); // ...and ensure new requests see it.
  observer2.assertValues("Bar");
}
 
Example 6
Source File: ReplayingShareObservableTest.java    From RxReplayingShare with Apache License 2.0 6 votes vote down vote up
@Test public void streamsDoNotShareInstances() {
  PublishSubject<String> subjectA = PublishSubject.create();
  Observable<String> observableA = subjectA.compose(ReplayingShare.<String>instance());
  TestObserver<String> observerA1 = new TestObserver<>();
  observableA.subscribe(observerA1);

  PublishSubject<String> subjectB = PublishSubject.create();
  Observable<String> observableB = subjectB.compose(ReplayingShare.<String>instance());
  TestObserver<String> observerB1 = new TestObserver<>();
  observableB.subscribe(observerB1);

  subjectA.onNext("Foo");
  observerA1.assertValues("Foo");
  subjectB.onNext("Bar");
  observerB1.assertValues("Bar");

  TestObserver<String> observerA2 = new TestObserver<>();
  observableA.subscribe(observerA2);
  observerA2.assertValues("Foo");

  TestObserver<String> observerB2 = new TestObserver<>();
  observableB.subscribe(observerB2);
  observerB2.assertValues("Bar");
}
 
Example 7
Source File: ReplayingShareObservableTest.java    From RxReplayingShare with Apache License 2.0 6 votes vote down vote up
@Test public void errorClearsCacheAndResubscribes() {
  List<String> start = new ArrayList<>();
  start.add("initA");

  PublishSubject<String> upstream = PublishSubject.create();
  Observable<String> replayed = upstream.startWithIterable(start).compose(ReplayingShare.<String>instance());

  TestObserver<String> observer1 = new TestObserver<>();
  replayed.subscribe(observer1);
  observer1.assertValues("initA");

  TestObserver<String> observer2 = new TestObserver<>();
  replayed.subscribe(observer2);
  observer1.assertValues("initA");

  RuntimeException r = new RuntimeException();
  upstream.onError(r);
  observer1.assertError(r);
  observer2.assertError(r);

  start.set(0, "initB");

  TestObserver<String> observer3 = new TestObserver<>();
  replayed.subscribe(observer3);
  observer3.assertValues("initB");
}
 
Example 8
Source File: ReplayingShareObservableTest.java    From RxReplayingShare with Apache License 2.0 6 votes vote down vote up
@Test public void completeClearsCacheAndResubscribesStartingWithDefault() {
  List<String> start = new ArrayList<>();
  start.add("initA");

  PublishSubject<String> upstream = PublishSubject.create();
  Observable<String> replayed =
      upstream.startWithIterable(start).compose(ReplayingShare.createWithDefault("default"));

  TestObserver<String> observer1 = new TestObserver<>();
  replayed.subscribe(observer1);
  observer1.assertValues("default", "initA");

  TestObserver<String> observer2 = new TestObserver<>();
  replayed.subscribe(observer2);
  observer1.assertValues("default", "initA");

  upstream.onComplete();
  observer1.assertComplete();
  observer2.assertComplete();

  start.set(0, "initB");

  TestObserver<String> observer3 = new TestObserver<>();
  replayed.subscribe(observer3);
  observer3.assertValues("default", "initB");
}
 
Example 9
Source File: ReplayRelayTest.java    From RxRelay with Apache License 2.0 6 votes vote down vote up
@Test
public void reentrantDrain() {
    TestScheduler scheduler = new TestScheduler();

    final ReplayRelay<Integer>
        rp = ReplayRelay.createWithTimeAndSize(1, TimeUnit.SECONDS, scheduler, 2);

    TestObserver<Integer> ts = new TestObserver<Integer>() {
        @Override
        public void onNext(Integer t) {
            if (t == 1) {
                rp.accept(2);
            }
            super.onNext(t);
        }
    };

    rp.subscribe(ts);

    rp.accept(1);

    ts.assertValues(1, 2);
}
 
Example 10
Source File: UntilLifecycleTransformerObservableTest.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
@Test
public void noEvent() {
    TestObserver<String> testObserver = stream
        .compose(RxLifecycle.<String, String>bind(lifecycle))
        .test();

    stream.onNext("1");
    stream.onNext("2");
    testObserver.assertValues("1", "2");
    testObserver.assertNotComplete();
}
 
Example 11
Source File: RxFragmentLifecycleTest.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
private void testLifecycle(LifecycleProvider<FragmentEvent> provider) {
    Fragment fragment = (Fragment) provider;
    FragmentTestUtil.startFragment(fragment);

    TestObserver<FragmentEvent> testObserver = provider.lifecycle().skip(1).test();

    fragment.onAttach(null);
    fragment.onCreate(null);
    fragment.onViewCreated(null, null);
    fragment.onStart();
    fragment.onResume();
    fragment.onPause();
    fragment.onStop();
    fragment.onDestroyView();
    fragment.onDestroy();
    fragment.onDetach();

    testObserver.assertValues(
        FragmentEvent.ATTACH,
        FragmentEvent.CREATE,
        FragmentEvent.CREATE_VIEW,
        FragmentEvent.START,
        FragmentEvent.RESUME,
        FragmentEvent.PAUSE,
        FragmentEvent.STOP,
        FragmentEvent.DESTROY_VIEW,
        FragmentEvent.DESTROY,
        FragmentEvent.DETACH
    );
}
 
Example 12
Source File: UntilCorrespondingEventTransformerObservableTest.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
@Test
public void twoOpenEvents() {
    TestObserver<String> testObserver = stream
        .compose(RxLifecycle.<String, String>bind(lifecycle, CORRESPONDING_EVENTS))
        .test();

    lifecycle.onNext("create");
    stream.onNext("1");
    lifecycle.onNext("start");
    stream.onNext("2");

    testObserver.assertValues("1", "2");
    testObserver.assertNotComplete();
}
 
Example 13
Source File: UntilEventTransformerObservableTest.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
@Test
public void noEvents() {
    TestObserver<String> testObserver = stream
        .compose(RxLifecycle.<String, String>bindUntilEvent(lifecycle, "stop"))
        .test();

    stream.onNext("1");
    stream.onNext("2");
    testObserver.assertValues("1", "2");
    testObserver.assertNotComplete();
}
 
Example 14
Source File: PublishRelayTest.java    From RxRelay with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("CheckReturnValue")
public void subscribedTo() {
    PublishRelay<Integer> pp = PublishRelay.create();
    PublishRelay<Integer> pp2 = PublishRelay.create();

    pp.subscribe(pp2);

    TestObserver<Integer> ts = pp2.test();

    pp.accept(1);
    pp.accept(2);

    ts.assertValues(1, 2);
}
 
Example 15
Source File: ReplayingShareObservableTest.java    From RxReplayingShare with Apache License 2.0 5 votes vote down vote up
@Test public void errorClearsCacheAndResubscribesStartingWithDefault() {
  List<String> start = new ArrayList<>();
  start.add("initA");

  PublishSubject<String> upstream = PublishSubject.create();
  Observable<String> replayed =
      upstream.startWithIterable(start).compose(ReplayingShare.createWithDefault("default"));

  TestObserver<String> observer1 = new TestObserver<>();
  replayed.subscribe(observer1);
  observer1.assertValues("default", "initA");

  TestObserver<String> observer2 = new TestObserver<>();
  replayed.subscribe(observer2);
  observer1.assertValues("default", "initA");

  RuntimeException r = new RuntimeException();
  upstream.onError(r);
  observer1.assertError(r);
  observer2.assertError(r);

  start.set(0, "initB");

  TestObserver<String> observer3 = new TestObserver<>();
  replayed.subscribe(observer3);
  observer3.assertValues("default", "initB");
}
 
Example 16
Source File: UntilCorrespondingEventTransformerObservableTest.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
@Test
public void noEvents() {
    TestObserver<String> testObserver = stream
        .compose(RxLifecycle.<String, String>bind(lifecycle, CORRESPONDING_EVENTS))
        .test();

    stream.onNext("1");
    stream.onNext("2");
    testObserver.assertValues("1", "2");
    testObserver.assertNotComplete();
}
 
Example 17
Source File: ReplayingShareObservableTest.java    From RxReplayingShare with Apache License 2.0 5 votes vote down vote up
@Test public void defaultValueOnSubscribe() {
  PublishSubject<String> subject = PublishSubject.create();
  Observable<String> observable = subject.compose(ReplayingShare.createWithDefault("default"));

  TestObserver<String> observer1 = new TestObserver<>();
  observable.subscribe(observer1);
  observer1.assertValues("default");

  subject.onNext("Foo");
  observer1.assertValues("default", "Foo");
}
 
Example 18
Source File: UntilCorrespondingEventTransformerObservableTest.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
@Test
public void openAndCloseEvent() {
    TestObserver<String> testObserver = stream
        .compose(RxLifecycle.<String, String>bind(lifecycle, CORRESPONDING_EVENTS))
        .test();

    lifecycle.onNext("create");
    stream.onNext("1");
    lifecycle.onNext("destroy");
    stream.onNext("2");

    testObserver.assertValues("1");
    testObserver.assertComplete();
}
 
Example 19
Source File: UntilCorrespondingEventTransformerObservableTest.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
@Test
public void oneStartEvent() {
    TestObserver<String> testObserver = stream
        .compose(RxLifecycle.<String, String>bind(lifecycle, CORRESPONDING_EVENTS))
        .test();

    lifecycle.onNext("create");
    stream.onNext("1");
    stream.onNext("2");

    testObserver.assertValues("1", "2");
    testObserver.assertNotComplete();
}
 
Example 20
Source File: PublishRelayTest.java    From RxRelay with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("CheckReturnValue")
public void subscribedTo() {
    PublishRelay<Integer> pp = PublishRelay.create();
    PublishRelay<Integer> pp2 = PublishRelay.create();

    pp.subscribe(pp2);

    TestObserver<Integer> ts = pp2.test();

    pp.accept(1);
    pp.accept(2);

    ts.assertValues(1, 2);
}