12
12
13
13
import java .util .concurrent .TimeUnit ;
14
14
15
- import static org .mockito .Matchers .any ;
16
- import static org .mockito .Matchers .anyLong ;
17
15
import static org .mockito .Matchers .eq ;
18
16
import static org .mockito .Mockito .mock ;
19
- import static org .mockito .Mockito .never ;
20
17
import static org .mockito .Mockito .verify ;
21
18
22
19
/**
@@ -32,40 +29,27 @@ public HandlerThreadScheduler(Handler handler) {
32
29
33
30
@ Override
34
31
public <T > Subscription schedule (final T state , final Func2 <Scheduler , T , Subscription > action ) {
32
+ return schedule (state , action , 0L , TimeUnit .MILLISECONDS );
33
+ }
34
+
35
+ @ Override
36
+ public <T > Subscription schedule (final T state , final Func2 <Scheduler , T , Subscription > action , long delayTime , TimeUnit unit ) {
35
37
final AtomicObservableSubscription subscription = new AtomicObservableSubscription ();
36
38
final Scheduler _scheduler = this ;
37
-
38
- handler .post (new Runnable () {
39
+ handler .postDelayed (new Runnable () {
39
40
@ Override
40
41
public void run () {
41
42
subscription .wrap (action .call (_scheduler , state ));
42
43
}
43
- });
44
+ }, unit . toMillis ( delayTime ) );
44
45
return subscription ;
45
46
}
46
47
47
- @ Override
48
- public <T > Subscription schedule (final T state , final Func2 <Scheduler , T , Subscription > action , long delayTime , TimeUnit unit ) {
49
- if (delayTime == 0 ) {
50
- return schedule (state , action );
51
- } else {
52
- final AtomicObservableSubscription subscription = new AtomicObservableSubscription ();
53
- final Scheduler _scheduler = this ;
54
- handler .postDelayed (new Runnable () {
55
- @ Override
56
- public void run () {
57
- subscription .wrap (action .call (_scheduler , state ));
58
- }
59
- }, unit .toMillis (delayTime ));
60
- return subscription ;
61
- }
62
- }
63
-
64
48
@ RunWith (AndroidTestRunner .class )
65
49
public static final class UnitTest {
66
50
67
51
@ Test
68
- public void shouldScheduleActionOnHandlerThread () {
52
+ public void shouldScheduleImmediateActionOnHandlerThread () {
69
53
final Handler handler = mock (Handler .class );
70
54
final Object state = new Object ();
71
55
final Func2 <Scheduler , Object , Subscription > action = mock (Func2 .class );
@@ -75,7 +59,7 @@ public void shouldScheduleActionOnHandlerThread() {
75
59
76
60
// verify that we post to the given Handler
77
61
ArgumentCaptor <Runnable > runnable = ArgumentCaptor .forClass (Runnable .class );
78
- verify (handler ).post (runnable .capture ());
62
+ verify (handler ).postDelayed (runnable .capture (), eq ( 0L ));
79
63
80
64
// verify that the given handler delegates to our action
81
65
runnable .getValue ().run ();
@@ -99,21 +83,6 @@ public void shouldScheduleDelayedActionOnHandlerThread() {
99
83
runnable .getValue ().run ();
100
84
verify (action ).call (scheduler , state );
101
85
}
102
-
103
- @ Test
104
- public void scheduleDelayedActionShouldForwardToNormalPostIfDelayIsZero () {
105
- final Handler handler = mock (Handler .class );
106
- final Object state = new Object ();
107
- final Func2 <Scheduler , Object , Subscription > action = mock (Func2 .class );
108
-
109
- Scheduler scheduler = new HandlerThreadScheduler (handler );
110
- scheduler .schedule (state , action , 0L , TimeUnit .SECONDS );
111
-
112
- // verify that we post to the given Handler
113
- verify (handler ).post (any (Runnable .class ));
114
- verify (handler , never ()).postDelayed (any (Runnable .class ), anyLong ());
115
- }
116
-
117
86
}
118
87
}
119
88
0 commit comments