OperatorSampleWithLastWithObservable implements Operator<T, T> { public static <T> OperatorSampleWithLastWithObservable<T, Object> create(Observable<Object> sample) { return new OperatorSampleWithLastWithObservable<>(sample); } OperatorSampleWithLastWithObservable(Observable<U> sampler); static OperatorSampleWithLastWithObservable<T, Object> create(Observable<Object> sample); @Override Subscriber<? super T> call(Subscriber<? super T> child);  }
@Test public void testWhenNameChange_propagateValueOnlyIfButtonClick() throws Exception { final PublishSubject<String> name = PublishSubject.create(); final PublishSubject<Object> buttonClick = PublishSubject.create(); final TestObserver<String> observer = new TestObserver<>(); name .lift(OperatorSampleWithLastWithObservable.<String>create(buttonClick)) .subscribe(observer); name.onNext("ja"); name.onNext("jacek"); buttonClick.onNext(new Object()); assert_().that(observer.getOnNextEvents()).containsExactly("jacek"); }
@Test public void testWhenNameIsNotChanged_propagateOldValue() throws Exception { final PublishSubject<String> name = PublishSubject.create(); final PublishSubject<Object> buttonClick = PublishSubject.create(); final TestObserver<String> observer = new TestObserver<>(); name .lift(OperatorSampleWithLastWithObservable.<String>create(buttonClick)) .subscribe(observer); name.onNext("ja"); buttonClick.onNext(new Object()); assert_().that(observer.getOnNextEvents()).containsExactly("ja"); buttonClick.onNext(new Object()); assert_().that(observer.getOnNextEvents()).containsExactly("ja", "ja"); }
@Test public void testWhenNameChange_propagateNewValue() throws Exception { final PublishSubject<String> name = PublishSubject.create(); final PublishSubject<Object> buttonClick = PublishSubject.create(); final TestObserver<String> observer = new TestObserver<>(); name .lift(OperatorSampleWithLastWithObservable.<String>create(buttonClick)) .subscribe(observer); name.onNext("ja"); buttonClick.onNext(new Object()); assert_().that(observer.getOnNextEvents()).containsExactly("ja"); name.onNext("jacek"); buttonClick.onNext(new Object()); assert_().that(observer.getOnNextEvents()).containsExactly("ja", "jacek"); }
@Test public void testAfterUnsubscribe_sourceIsUnsubscribed() throws Exception { final TestScheduler scheduler = new TestScheduler(); final TestSubject<String> name = TestSubject.create(scheduler); final TestSubject<Object> buttonClick = TestSubject.create(scheduler); final Subscription subscription = name .lift(OperatorSampleWithLastWithObservable.<String>create(buttonClick)) .subscribe(new TestSubscriber<String>()); assert_().that(name.hasObservers()).isTrue(); assert_().that(buttonClick.hasObservers()).isTrue(); subscription.unsubscribe(); assert_().that(name.hasObservers()).isFalse(); assert_().that(buttonClick.hasObservers()).isFalse(); }
@Test public void testPullFromSource_whenBackpressure() throws Exception { final TestSubscriber<Integer> result = new TestSubscriber<>(); result.requestMore(0); Observable.range(1, 100) .lift(OperatorSampleWithLastWithObservable.<Integer>create( Observable.range(1, 2).map(Functions1.toObject()))) .subscribe(result); result.assertNoErrors(); assert_().that(result.getOnNextEvents()).isEmpty(); result.requestMore(1); result.assertNoErrors(); assert_().that(result.getOnNextEvents()).containsExactly(100); result.requestMore(1); result.assertNoErrors(); assert_().that(result.getOnNextEvents()).containsExactly(100, 100); }
@Test public void testIfClickComplete_returnOnCompleted() throws Exception { final PublishSubject<String> name = PublishSubject.create(); final PublishSubject<Object> buttonClick = PublishSubject.create(); final TestSubscriber<String> result = new TestSubscriber<>(); name .lift(OperatorSampleWithLastWithObservable.<String>create(buttonClick)) .subscribe(result); assert_().that(result.getOnCompletedEvents()).isEmpty(); name.onCompleted(); assert_().that(result.getOnCompletedEvents()).isEmpty(); buttonClick.onCompleted(); assert_().that(result.getOnCompletedEvents()).hasSize(1); }