OperationSkip { public static <T> OnSubscribeFunc<T> skip(final Observable<? extends T> items, final int num) { return new OnSubscribeFunc<T>() { @Override public Subscription onSubscribe(Observer<? super T> observer) { return new Skip<T>(items, num).onSubscribe(observer); } }; }  static OnSubscribeFunc<T> skip(final Observable<? extends T> items, final int num);  }
@Test public void testSkip1() { Observable<String> w = Observable.from("one", "two", "three"); Observable<String> skip = Observable.create(skip(w, 2)); @SuppressWarnings("unchecked") Observer<String> observer = mock(Observer.class); skip.subscribe(observer); verify(observer, never()).onNext("one"); verify(observer, never()).onNext("two"); verify(observer, times(1)).onNext("three"); verify(observer, never()).onError(any(Throwable.class)); verify(observer, times(1)).onCompleted(); }
@Test public void testSkip2() { Observable<String> w = Observable.from("one", "two", "three"); Observable<String> skip = Observable.create(skip(w, 1)); @SuppressWarnings("unchecked") Observer<String> observer = mock(Observer.class); skip.subscribe(observer); verify(observer, never()).onNext("one"); verify(observer, times(1)).onNext("two"); verify(observer, times(1)).onNext("three"); verify(observer, never()).onError(any(Throwable.class)); verify(observer, times(1)).onCompleted(); }
@Test public void testSkipTimed() { TestScheduler scheduler = new TestScheduler(); PublishSubject<Integer> source = PublishSubject.create(); Observable<Integer> result = source.skip(1, TimeUnit.SECONDS, scheduler); Observer<Object> o = mock(Observer.class); result.subscribe(o); source.onNext(1); source.onNext(2); source.onNext(3); scheduler.advanceTimeBy(1, TimeUnit.SECONDS); source.onNext(4); source.onNext(5); source.onNext(6); source.onCompleted(); InOrder inOrder = inOrder(o); inOrder.verify(o, never()).onNext(1); inOrder.verify(o, never()).onNext(2); inOrder.verify(o, never()).onNext(3); inOrder.verify(o).onNext(4); inOrder.verify(o).onNext(5); inOrder.verify(o).onNext(6); inOrder.verify(o).onCompleted(); inOrder.verifyNoMoreInteractions(); verify(o, never()).onError(any(Throwable.class)); }
@Test public void testSkipTimedFinishBeforeTime() { TestScheduler scheduler = new TestScheduler(); PublishSubject<Integer> source = PublishSubject.create(); Observable<Integer> result = source.skip(1, TimeUnit.SECONDS, scheduler); Observer<Object> o = mock(Observer.class); result.subscribe(o); source.onNext(1); source.onNext(2); source.onNext(3); source.onCompleted(); scheduler.advanceTimeBy(1, TimeUnit.SECONDS); InOrder inOrder = inOrder(o); inOrder.verify(o).onCompleted(); inOrder.verifyNoMoreInteractions(); verify(o, never()).onNext(any()); verify(o, never()).onError(any(Throwable.class)); }
@Test public void testSkipTimedErrorBeforeTime() { TestScheduler scheduler = new TestScheduler(); PublishSubject<Integer> source = PublishSubject.create(); Observable<Integer> result = source.skip(1, TimeUnit.SECONDS, scheduler); Observer<Object> o = mock(Observer.class); result.subscribe(o); source.onNext(1); source.onNext(2); source.onNext(3); source.onError(new CustomException()); scheduler.advanceTimeBy(1, TimeUnit.SECONDS); InOrder inOrder = inOrder(o); inOrder.verify(o).onError(any(CustomException.class)); inOrder.verifyNoMoreInteractions(); verify(o, never()).onNext(any()); verify(o, never()).onCompleted(); }
@Test public void testSkipTimedErrorAfterTime() { TestScheduler scheduler = new TestScheduler(); PublishSubject<Integer> source = PublishSubject.create(); Observable<Integer> result = source.skip(1, TimeUnit.SECONDS, scheduler); Observer<Object> o = mock(Observer.class); result.subscribe(o); source.onNext(1); source.onNext(2); source.onNext(3); scheduler.advanceTimeBy(1, TimeUnit.SECONDS); source.onNext(4); source.onNext(5); source.onNext(6); source.onError(new CustomException()); InOrder inOrder = inOrder(o); inOrder.verify(o, never()).onNext(1); inOrder.verify(o, never()).onNext(2); inOrder.verify(o, never()).onNext(3); inOrder.verify(o).onNext(4); inOrder.verify(o).onNext(5); inOrder.verify(o).onNext(6); inOrder.verify(o).onError(any(CustomException.class)); inOrder.verifyNoMoreInteractions(); verify(o, never()).onCompleted(); }