| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.internal.operators.observable; |
| 15 |
|
| 16 |
import static org.mockito.ArgumentMatchers.*; |
| 17 |
import static org.mockito.Mockito.*; |
| 18 |
|
| 19 |
import org.junit.Test; |
| 20 |
import org.mockito.InOrder; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.core.*; |
| 23 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 24 |
import io.reactivex.rxjava3.functions.*; |
| 25 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 26 |
import io.reactivex.rxjava3.subjects.PublishSubject; |
| 27 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 28 |
|
| 29 |
public class ObservableSkipWhileTest extends RxJavaTest { |
| 30 |
|
| 31 |
Observer<Integer> w = TestHelper.mockObserver(); |
| 32 |
|
| 33 |
private static final Predicate<Integer> LESS_THAN_FIVE = new Predicate<Integer>() { |
| 34 |
@Override |
| 35 |
public boolean test(Integer v) { |
| 36 |
if (v == 42) { |
| 37 |
throw new RuntimeException("that's not the answer to everything!"); |
| 38 |
} |
| 39 |
return v < 5; |
| 40 |
} |
| 41 |
}; |
| 42 |
|
| 43 |
private static final Predicate<Integer> INDEX_LESS_THAN_THREE = new Predicate<Integer>() { |
| 44 |
int index; |
| 45 |
@Override |
| 46 |
public boolean test(Integer value) { |
| 47 |
return index++ < 3; |
| 48 |
} |
| 49 |
}; |
| 50 |
|
| 51 |
@Test |
| 52 |
public void skipWithIndex() { |
| 53 |
Observable<Integer> src = Observable.just(1, 2, 3, 4, 5); |
| 54 |
src.skipWhile(INDEX_LESS_THAN_THREE).subscribe(w); |
| 55 |
|
| 56 |
InOrder inOrder = inOrder(w); |
| 57 |
inOrder.verify(w, times(1)).onNext(4); |
| 58 |
inOrder.verify(w, times(1)).onNext(5); |
| 59 |
inOrder.verify(w, times(1)).onComplete(); |
| 60 |
inOrder.verify(w, never()).onError(any(Throwable.class)); |
| 61 |
} |
| 62 |
|
| 63 |
@Test |
| 64 |
public void skipEmpty() { |
| 65 |
Observable<Integer> src = Observable.empty(); |
| 66 |
src.skipWhile(LESS_THAN_FIVE).subscribe(w); |
| 67 |
verify(w, never()).onNext(anyInt()); |
| 68 |
verify(w, never()).onError(any(Throwable.class)); |
| 69 |
verify(w, times(1)).onComplete(); |
| 70 |
} |
| 71 |
|
| 72 |
@Test |
| 73 |
public void skipEverything() { |
| 74 |
Observable<Integer> src = Observable.just(1, 2, 3, 4, 3, 2, 1); |
| 75 |
src.skipWhile(LESS_THAN_FIVE).subscribe(w); |
| 76 |
verify(w, never()).onNext(anyInt()); |
| 77 |
verify(w, never()).onError(any(Throwable.class)); |
| 78 |
verify(w, times(1)).onComplete(); |
| 79 |
} |
| 80 |
|
| 81 |
@Test |
| 82 |
public void skipNothing() { |
| 83 |
Observable<Integer> src = Observable.just(5, 3, 1); |
| 84 |
src.skipWhile(LESS_THAN_FIVE).subscribe(w); |
| 85 |
|
| 86 |
InOrder inOrder = inOrder(w); |
| 87 |
inOrder.verify(w, times(1)).onNext(5); |
| 88 |
inOrder.verify(w, times(1)).onNext(3); |
| 89 |
inOrder.verify(w, times(1)).onNext(1); |
| 90 |
inOrder.verify(w, times(1)).onComplete(); |
| 91 |
inOrder.verify(w, never()).onError(any(Throwable.class)); |
| 92 |
} |
| 93 |
|
| 94 |
@Test |
| 95 |
public void skipSome() { |
| 96 |
Observable<Integer> src = Observable.just(1, 2, 3, 4, 5, 3, 1, 5); |
| 97 |
src.skipWhile(LESS_THAN_FIVE).subscribe(w); |
| 98 |
|
| 99 |
InOrder inOrder = inOrder(w); |
| 100 |
inOrder.verify(w, times(1)).onNext(5); |
| 101 |
inOrder.verify(w, times(1)).onNext(3); |
| 102 |
inOrder.verify(w, times(1)).onNext(1); |
| 103 |
inOrder.verify(w, times(1)).onNext(5); |
| 104 |
inOrder.verify(w, times(1)).onComplete(); |
| 105 |
inOrder.verify(w, never()).onError(any(Throwable.class)); |
| 106 |
} |
| 107 |
|
| 108 |
@Test |
| 109 |
public void skipError() { |
| 110 |
Observable<Integer> src = Observable.just(1, 2, 42, 5, 3, 1); |
| 111 |
src.skipWhile(LESS_THAN_FIVE).subscribe(w); |
| 112 |
|
| 113 |
InOrder inOrder = inOrder(w); |
| 114 |
inOrder.verify(w, never()).onNext(anyInt()); |
| 115 |
inOrder.verify(w, never()).onComplete(); |
| 116 |
inOrder.verify(w, times(1)).onError(any(RuntimeException.class)); |
| 117 |
} |
| 118 |
|
| 119 |
@Test |
| 120 |
public void skipManySubscribers() { |
| 121 |
Observable<Integer> src = Observable.range(1, 10).skipWhile(LESS_THAN_FIVE); |
| 122 |
int n = 5; |
| 123 |
for (int i = 0; i < n; i++) { |
| 124 |
Observer<Object> o = TestHelper.mockObserver(); |
| 125 |
InOrder inOrder = inOrder(o); |
| 126 |
|
| 127 |
src.subscribe(o); |
| 128 |
|
| 129 |
for (int j = 5; j < 10; j++) { |
| 130 |
inOrder.verify(o).onNext(j); |
| 131 |
} |
| 132 |
inOrder.verify(o).onComplete(); |
| 133 |
verify(o, never()).onError(any(Throwable.class)); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
@Test |
| 138 |
public void dispose() { |
| 139 |
TestHelper.checkDisposed(PublishSubject.create().skipWhile(Functions.alwaysFalse())); |
| 140 |
} |
| 141 |
|
| 142 |
@Test |
| 143 |
public void doubleOnSubscribe() { |
| 144 |
TestHelper.checkDoubleOnSubscribeObservable(new Function<Observable<Object>, ObservableSource<Object>>() { |
| 145 |
@Override |
| 146 |
public ObservableSource<Object> apply(Observable<Object> o) throws Exception { |
| 147 |
return o.skipWhile(Functions.alwaysFalse()); |
| 148 |
} |
| 149 |
}); |
| 150 |
} |
| 151 |
|
| 152 |
@Test |
| 153 |
public void error() { |
| 154 |
Observable.error(new TestException()) |
| 155 |
.skipWhile(Functions.alwaysFalse()) |
| 156 |
.test() |
| 157 |
.assertFailure(TestException.class); |
| 158 |
} |
| 159 |
} |