Debian Codegraph @

src/test/java/io/reactivex/rxjava3/observers/DisposableObserverTest.java

raw
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.observers;
15
16 import static org.junit.Assert.*;
17
18 import java.util.*;
19
20 import org.junit.Test;
21
22 import io.reactivex.rxjava3.core.Observable;
23 import io.reactivex.rxjava3.core.RxJavaTest;
24 import io.reactivex.rxjava3.disposables.*;
25 import io.reactivex.rxjava3.internal.util.EndConsumerHelper;
26 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
27 import io.reactivex.rxjava3.testsupport.TestHelper;
28
29 public class DisposableObserverTest extends RxJavaTest {
30
31 static final class TestDisposableObserver<T> extends DisposableObserver<T> {
32
33 int start;
34
35 final List<T> values = new ArrayList<>();
36
37 final List<Throwable> errors = new ArrayList<>();
38
39 int completions;
40
41 @Override
42 protected void onStart() {
43 super.onStart();
44
45 start++;
46 }
47
48 @Override
49 public void onNext(T value) {
50 values.add(value);
51 }
52
53 @Override
54 public void onError(Throwable e) {
55 errors.add(e);
56 }
57
58 @Override
59 public void onComplete() {
60 completions++;
61 }
62 }
63
64 @Test
65 public void normal() {
66 TestDisposableObserver<Integer> tc = new TestDisposableObserver<>();
67
68 assertFalse(tc.isDisposed());
69 assertEquals(0, tc.start);
70 assertTrue(tc.values.isEmpty());
71 assertTrue(tc.errors.isEmpty());
72
73 Observable.just(1).subscribe(tc);
74
75 assertFalse(tc.isDisposed());
76 assertEquals(1, tc.start);
77 assertEquals(1, tc.values.get(0).intValue());
78 assertTrue(tc.errors.isEmpty());
79 }
80
81 @Test
82 public void startOnce() {
83
84 List<Throwable> error = TestHelper.trackPluginErrors();
85
86 try {
87 TestDisposableObserver<Integer> tc = new TestDisposableObserver<>();
88
89 tc.onSubscribe(Disposable.empty());
90
91 Disposable d = Disposable.empty();
92
93 tc.onSubscribe(d);
94
95 assertTrue(d.isDisposed());
96
97 assertEquals(1, tc.start);
98
99 TestHelper.assertError(error, 0, IllegalStateException.class, EndConsumerHelper.composeMessage(tc.getClass().getName()));
100 } finally {
101 RxJavaPlugins.reset();
102 }
103 }
104
105 @Test
106 public void dispose() {
107 TestDisposableObserver<Integer> tc = new TestDisposableObserver<>();
108
109 assertFalse(tc.isDisposed());
110
111 tc.dispose();
112
113 assertTrue(tc.isDisposed());
114
115 Disposable d = Disposable.empty();
116
117 tc.onSubscribe(d);
118
119 assertTrue(d.isDisposed());
120
121 assertEquals(0, tc.start);
122 }
123 }