OperationDistinctUntilChanged { public static <T> OnSubscribeFunc<T> distinctUntilChanged(Observable<? extends T> source, Comparator<T> equalityComparator) { return new DistinctUntilChanged<T, T>(source, Functions.<T> identity(), equalityComparator); }  static OnSubscribeFunc<T> distinctUntilChanged(Observable<? extends T> source, Comparator<T> equalityComparator); static OnSubscribeFunc<T> distinctUntilChanged(Observable<? extends T> source, Func1<? super T, ? extends U> keySelector, Comparator<U> equalityComparator); static OnSubscribeFunc<T> distinctUntilChanged(Observable<? extends T> source, Func1<? super T, ? extends U> keySelector); static OnSubscribeFunc<T> distinctUntilChanged(Observable<? extends T> source);  }
@Test public void testDistinctUntilChangedOfNone() { Observable<String> src = Observable.empty(); Observable.create(distinctUntilChanged(src)).subscribe(w); verify(w, never()).onNext(anyString()); verify(w, never()).onError(any(Throwable.class)); verify(w, times(1)).onCompleted(); }
@Test public void testDistinctUntilChangedOfNoneWithKeySelector() { Observable<String> src = Observable.empty(); Observable.create(distinctUntilChanged(src, TO_UPPER_WITH_EXCEPTION)).subscribe(w); verify(w, never()).onNext(anyString()); verify(w, never()).onError(any(Throwable.class)); verify(w, times(1)).onCompleted(); }
@Test public void testDistinctUntilChangedOfNormalSource() { Observable<String> src = Observable.from("a", "b", "c", "c", "c", "b", "b", "a", "e"); Observable.create(distinctUntilChanged(src)).subscribe(w); InOrder inOrder = inOrder(w); inOrder.verify(w, times(1)).onNext("a"); inOrder.verify(w, times(1)).onNext("b"); inOrder.verify(w, times(1)).onNext("c"); inOrder.verify(w, times(1)).onNext("b"); inOrder.verify(w, times(1)).onNext("a"); inOrder.verify(w, times(1)).onNext("e"); inOrder.verify(w, times(1)).onCompleted(); inOrder.verify(w, never()).onNext(anyString()); verify(w, never()).onError(any(Throwable.class)); }
@Test public void testDistinctUntilChangedOfNormalSourceWithKeySelector() { Observable<String> src = Observable.from("a", "b", "c", "C", "c", "B", "b", "a", "e"); Observable.create(distinctUntilChanged(src, TO_UPPER_WITH_EXCEPTION)).subscribe(w); InOrder inOrder = inOrder(w); inOrder.verify(w, times(1)).onNext("a"); inOrder.verify(w, times(1)).onNext("b"); inOrder.verify(w, times(1)).onNext("c"); inOrder.verify(w, times(1)).onNext("B"); inOrder.verify(w, times(1)).onNext("a"); inOrder.verify(w, times(1)).onNext("e"); inOrder.verify(w, times(1)).onCompleted(); inOrder.verify(w, never()).onNext(anyString()); verify(w, never()).onError(any(Throwable.class)); }
@Test public void testDistinctUntilChangedOfSourceWithNulls() { Observable<String> src = Observable.from(null, "a", "a", null, null, "b", null, null); Observable.create(distinctUntilChanged(src)).subscribe(w); InOrder inOrder = inOrder(w); inOrder.verify(w, times(1)).onNext(null); inOrder.verify(w, times(1)).onNext("a"); inOrder.verify(w, times(1)).onNext(null); inOrder.verify(w, times(1)).onNext("b"); inOrder.verify(w, times(1)).onNext(null); inOrder.verify(w, times(1)).onCompleted(); inOrder.verify(w, never()).onNext(anyString()); verify(w, never()).onError(any(Throwable.class)); }
@Test public void testDistinctUntilChangedOfSourceWithExceptionsFromKeySelector() { Observable<String> src = Observable.from("a", "b", null, "c"); Observable.create(distinctUntilChanged(src, TO_UPPER_WITH_EXCEPTION)).subscribe(w); InOrder inOrder = inOrder(w); inOrder.verify(w, times(1)).onNext("a"); inOrder.verify(w, times(1)).onNext("b"); verify(w, times(1)).onError(any(NullPointerException.class)); inOrder.verify(w, never()).onNext(anyString()); inOrder.verify(w, never()).onCompleted(); }
@Test public void testDistinctUntilChangedWithComparator() { Observable<String> src = Observable.from("a", "b", "c", "aa", "bb", "c", "ddd"); Observable.create(distinctUntilChanged(src, COMPARE_LENGTH)).subscribe(w); InOrder inOrder = inOrder(w); inOrder.verify(w, times(1)).onNext("a"); inOrder.verify(w, times(1)).onNext("aa"); inOrder.verify(w, times(1)).onNext("c"); inOrder.verify(w, times(1)).onNext("ddd"); inOrder.verify(w, times(1)).onCompleted(); inOrder.verify(w, never()).onNext(anyString()); verify(w, never()).onError(any(Throwable.class)); }
@Test public void testDistinctUntilChangedWithComparatorAndKeySelector() { Observable<String> src = Observable.from("a", "b", "x", "aa", "bb", "c", "ddd"); Observable.create(distinctUntilChanged(src, TO_UPPER_WITH_EXCEPTION, COMPARE_LENGTH)).subscribe(w); InOrder inOrder = inOrder(w); inOrder.verify(w, times(1)).onNext("a"); inOrder.verify(w, times(1)).onNext("x"); inOrder.verify(w, times(1)).onNext("c"); inOrder.verify(w, times(1)).onNext("ddd"); inOrder.verify(w, times(1)).onCompleted(); inOrder.verify(w, never()).onNext(anyString()); verify(w, never()).onError(any(Throwable.class)); }
@Test public void testDistinctUntilChangedWithComparatorAndKeySelectorandTwoSubscriptions() { Observable<String> src = Observable.from("a", "b", "x", "aa", "bb", "c", "ddd"); Observable.create(distinctUntilChanged(src, TO_UPPER_WITH_EXCEPTION, COMPARE_LENGTH)).subscribe(w); InOrder inOrder = inOrder(w); inOrder.verify(w, times(1)).onNext("a"); inOrder.verify(w, times(1)).onNext("x"); Observable.create(distinctUntilChanged(src, TO_UPPER_WITH_EXCEPTION, COMPARE_LENGTH)).subscribe(w2); inOrder.verify(w, times(1)).onNext("c"); inOrder.verify(w, times(1)).onNext("ddd"); inOrder.verify(w, times(1)).onCompleted(); inOrder.verify(w, never()).onNext(anyString()); verify(w, never()).onError(any(Throwable.class)); InOrder inOrder2 = inOrder(w2); inOrder2.verify(w2, times(1)).onNext("a"); inOrder2.verify(w2, times(1)).onNext("x"); inOrder2.verify(w2, times(1)).onNext("c"); inOrder2.verify(w2, times(1)).onNext("ddd"); inOrder2.verify(w2, times(1)).onCompleted(); inOrder2.verify(w2, never()).onNext(anyString()); verify(w2, never()).onError(any(Throwable.class)); }