3
3
import rx .Observable ;
4
4
import rx .Observer ;
5
5
import rx .Subscription ;
6
+ import rx .util .functions .Action1 ;
7
+ import rx .util .functions .Func0 ;
6
8
7
9
import static org .junit .Assert .assertTrue ;
8
10
@@ -12,14 +14,52 @@ public class UnsubscribeTester
12
14
13
15
public UnsubscribeTester () {}
14
16
15
- public static <T > UnsubscribeTester createOnNext (Observable <T > observable )
17
+ /**
18
+ * Tests the unsubscription semantics of an observable.
19
+ *
20
+ * @param provider Function that when called provides an instance of the observable being tested
21
+ * @param generateOnCompleted Causes an observer generated by @param provider to generate an onCompleted event. Null to not test onCompleted.
22
+ * @param generateOnError Causes an observer generated by @param provider to generate an onError event. Null to not test onError.
23
+ * @param generateOnNext Causes an observer generated by @param provider to generate an onNext event. Null to not test onNext.
24
+ * @param <T> The type of object passed by the Observable
25
+ */
26
+ public static <T , O extends Observable <T >> void test (Func0 <O > provider , Action1 <? super O > generateOnCompleted , Action1 <? super O > generateOnError , Action1 <? super O > generateOnNext )
27
+ {
28
+ if (generateOnCompleted != null ) {
29
+ O observable = provider .call ();
30
+ UnsubscribeTester tester1 = createOnCompleted (observable );
31
+ UnsubscribeTester tester2 = createOnCompleted (observable );
32
+ generateOnCompleted .call (observable );
33
+ tester1 .assertPassed ();
34
+ tester2 .assertPassed ();
35
+ }
36
+ if (generateOnError != null ) {
37
+ O observable = provider .call ();
38
+ UnsubscribeTester tester1 = createOnError (observable );
39
+ UnsubscribeTester tester2 = createOnError (observable );
40
+ generateOnError .call (observable );
41
+ tester1 .assertPassed ();
42
+ tester2 .assertPassed ();
43
+ }
44
+ if (generateOnNext != null ) {
45
+ O observable = provider .call ();
46
+ UnsubscribeTester tester1 = createOnNext (observable );
47
+ UnsubscribeTester tester2 = createOnNext (observable );
48
+ generateOnNext .call (observable );
49
+ tester1 .assertPassed ();
50
+ tester2 .assertPassed ();
51
+ }
52
+ }
53
+
54
+ private static <T > UnsubscribeTester createOnCompleted (Observable <T > observable )
16
55
{
17
56
final UnsubscribeTester test = new UnsubscribeTester ();
18
57
test .setSubscription (observable .subscribe (new Observer <T >()
19
58
{
20
59
@ Override
21
60
public void onCompleted ()
22
61
{
62
+ test .doUnsubscribe ();
23
63
}
24
64
25
65
@ Override
@@ -30,26 +70,25 @@ public void onError(Exception e)
30
70
@ Override
31
71
public void onNext (T args )
32
72
{
33
- test .doUnsubscribe ();
34
73
}
35
74
}));
36
75
return test ;
37
76
}
38
77
39
- public static <T > UnsubscribeTester createOnCompleted (Observable <T > observable )
78
+ private static <T > UnsubscribeTester createOnError (Observable <T > observable )
40
79
{
41
80
final UnsubscribeTester test = new UnsubscribeTester ();
42
81
test .setSubscription (observable .subscribe (new Observer <T >()
43
82
{
44
83
@ Override
45
84
public void onCompleted ()
46
85
{
47
- test .doUnsubscribe ();
48
86
}
49
87
50
88
@ Override
51
89
public void onError (Exception e )
52
90
{
91
+ test .doUnsubscribe ();
53
92
}
54
93
55
94
@ Override
@@ -60,7 +99,7 @@ public void onNext(T args)
60
99
return test ;
61
100
}
62
101
63
- public static <T > UnsubscribeTester createOnError (Observable <T > observable )
102
+ private static <T > UnsubscribeTester createOnNext (Observable <T > observable )
64
103
{
65
104
final UnsubscribeTester test = new UnsubscribeTester ();
66
105
test .setSubscription (observable .subscribe (new Observer <T >()
@@ -73,12 +112,12 @@ public void onCompleted()
73
112
@ Override
74
113
public void onError (Exception e )
75
114
{
76
- test .doUnsubscribe ();
77
115
}
78
116
79
117
@ Override
80
118
public void onNext (T args )
81
119
{
120
+ test .doUnsubscribe ();
82
121
}
83
122
}));
84
123
return test ;
@@ -96,7 +135,7 @@ private void doUnsubscribe()
96
135
subscription .unsubscribe ();
97
136
}
98
137
99
- public void assertPassed ()
138
+ private void assertPassed ()
100
139
{
101
140
assertTrue ("expected notification was received" , subscription == null );
102
141
}
0 commit comments