13
13
*/
14
14
package rx .android .schedulers ;
15
15
16
- import android .os .Handler ;
17
-
18
- import java .util .concurrent .atomic .AtomicReference ;
19
16
import org .junit .After ;
20
17
import org .junit .Before ;
21
18
import org .junit .Test ;
22
19
import org .junit .runner .RunWith ;
23
- import org .mockito .ArgumentCaptor ;
24
- import org .mockito .Matchers ;
25
- import org .robolectric .Robolectric ;
26
20
import org .robolectric .RobolectricTestRunner ;
27
21
import org .robolectric .annotation .Config ;
28
-
29
- import java .util .concurrent .TimeUnit ;
30
-
31
- import rx .Observable ;
32
22
import rx .Scheduler ;
33
- import rx .Scheduler .Worker ;
34
- import rx .Subscriber ;
35
- import rx .Subscription ;
36
23
import rx .android .plugins .RxAndroidPlugins ;
37
24
import rx .android .plugins .RxAndroidPluginsTest ;
38
25
import rx .android .plugins .RxAndroidSchedulersHook ;
39
- import rx .functions .Action0 ;
40
26
import rx .schedulers .Schedulers ;
41
27
42
28
import static org .junit .Assert .assertSame ;
43
- import static org .mockito .Matchers .eq ;
44
- import static org .mockito .Mockito .mock ;
45
- import static org .mockito .Mockito .never ;
46
- import static org .mockito .Mockito .spy ;
47
- import static org .mockito .Mockito .verify ;
48
- import static org .mockito .Mockito .when ;
49
29
50
30
@ RunWith (RobolectricTestRunner .class )
51
31
@ Config (manifest =Config .NONE )
@@ -57,87 +37,7 @@ public void setUpAndTearDown() {
57
37
}
58
38
59
39
@ Test
60
- public void shouldScheduleImmediateActionOnHandlerThread () {
61
- final Handler handler = mock (Handler .class );
62
- @ SuppressWarnings ("unchecked" )
63
- final Action0 action = mock (Action0 .class );
64
-
65
- Scheduler scheduler = AndroidSchedulers .handlerThread (handler );
66
- Worker inner = scheduler .createWorker ();
67
- inner .schedule (action );
68
-
69
- // verify that we post to the given Handler
70
- ArgumentCaptor <Runnable > runnable = ArgumentCaptor .forClass (Runnable .class );
71
- verify (handler ).postDelayed (runnable .capture (), eq (0L ));
72
-
73
- // verify that the given handler delegates to our action
74
- runnable .getValue ().run ();
75
- verify (action ).call ();
76
- }
77
-
78
- @ Test
79
- public void shouldScheduleDelayedActionOnHandlerThread () {
80
- final Handler handler = mock (Handler .class );
81
- @ SuppressWarnings ("unchecked" )
82
- final Action0 action = mock (Action0 .class );
83
-
84
- Scheduler scheduler = AndroidSchedulers .handlerThread (handler );
85
- Worker inner = scheduler .createWorker ();
86
- inner .schedule (action , 1L , TimeUnit .SECONDS );
87
-
88
- // verify that we post to the given Handler
89
- ArgumentCaptor <Runnable > runnable = ArgumentCaptor .forClass (Runnable .class );
90
- verify (handler ).postDelayed (runnable .capture (), eq (1000L ));
91
-
92
- // verify that the given handler delegates to our action
93
- runnable .getValue ().run ();
94
- verify (action ).call ();
95
- }
96
-
97
- @ Test
98
- public void shouldRemoveCallbacksFromHandlerWhenUnsubscribedSubscription () {
99
- final Handler handler = spy (new Handler ());
100
- final Observable .OnSubscribe <Integer > onSubscribe = mock (Observable .OnSubscribe .class );
101
- final Subscription subscription = Observable .create (onSubscribe )
102
- .subscribeOn (AndroidSchedulers .handlerThread (handler ))
103
- .subscribe ();
104
-
105
- verify (onSubscribe ).call (Matchers .any (Subscriber .class ));
106
-
107
- subscription .unsubscribe ();
108
-
109
- verify (handler ).removeCallbacks (Matchers .any (Runnable .class ));
110
- }
111
-
112
- @ Test
113
- public void shouldNotCallOnSubscribeWhenSubscriptionUnsubscribedBeforeDelay () {
114
- final Observable .OnSubscribe <Integer > onSubscribe = mock (Observable .OnSubscribe .class );
115
- final Handler handler = spy (new Handler ());
116
-
117
- final Scheduler scheduler = AndroidSchedulers .handlerThread (handler );
118
- final Worker worker = spy (scheduler .createWorker ());
119
-
120
- final Scheduler spyScheduler = spy (scheduler );
121
- when (spyScheduler .createWorker ()).thenReturn (worker );
122
-
123
- final Subscription subscription = Observable .create (onSubscribe )
124
- .delaySubscription (1 , TimeUnit .MINUTES , spyScheduler )
125
- .subscribe ();
126
-
127
- verify (worker ).schedule (Matchers .any (Action0 .class ),
128
- Matchers .eq (1L ), Matchers .eq (TimeUnit .MINUTES ));
129
- verify (handler ).postDelayed (Matchers .any (Runnable .class ),
130
- Matchers .eq (TimeUnit .MINUTES .toMillis (1L )));
131
-
132
- subscription .unsubscribe ();
133
-
134
- Robolectric .runUiThreadTasksIncludingDelayedTasks ();
135
-
136
- verify (onSubscribe , never ()).call (Matchers .any (Subscriber .class ));
137
- verify (handler ).removeCallbacks (Matchers .any (Runnable .class ));
138
- }
139
-
140
- @ Test public void mainThreadCallsThroughToHook () {
40
+ public void mainThreadCallsThroughToHook () {
141
41
final Scheduler scheduler = Schedulers .immediate ();
142
42
RxAndroidSchedulersHook hook = new RxAndroidSchedulersHook () {
143
43
@ Override public Scheduler getMainThreadScheduler () {
@@ -147,35 +47,6 @@ public void shouldNotCallOnSubscribeWhenSubscriptionUnsubscribedBeforeDelay() {
147
47
RxAndroidPlugins .getInstance ().registerSchedulersHook (hook );
148
48
149
49
Scheduler mainThread = AndroidSchedulers .mainThread ();
150
- assertSame (Schedulers .immediate (), mainThread );
151
- }
152
-
153
- @ Test public void handlerSchedulerCallsThroughToHook () {
154
- final AtomicReference <Action0 > actionRef = new AtomicReference <Action0 >();
155
- RxAndroidPlugins .getInstance ().registerSchedulersHook (new RxAndroidSchedulersHook () {
156
- @ Override public Action0 onSchedule (Action0 action ) {
157
- actionRef .set (action );
158
- return super .onSchedule (action );
159
- }
160
- });
161
-
162
- Handler handler = mock (Handler .class );
163
- @ SuppressWarnings ("unchecked" )
164
- Action0 action = mock (Action0 .class );
165
-
166
- Scheduler scheduler = AndroidSchedulers .handlerThread (handler );
167
- Worker inner = scheduler .createWorker ();
168
- inner .schedule (action );
169
-
170
- // Verify the action was passed through the schedulers hook.
171
- assertSame (action , actionRef .get ());
172
-
173
- // Verify that we post to the given Handler.
174
- ArgumentCaptor <Runnable > runnable = ArgumentCaptor .forClass (Runnable .class );
175
- verify (handler ).postDelayed (runnable .capture (), eq (0L ));
176
-
177
- // Verify that the given handler delegates to our action.
178
- runnable .getValue ().run ();
179
- verify (action ).call ();
50
+ assertSame (scheduler , mainThread );
180
51
}
181
52
}
0 commit comments