|
| 1 | +package rx.android.concurrency; |
| 2 | + |
| 3 | +import android.os.Handler; |
| 4 | +import org.junit.Test; |
| 5 | +import org.junit.runner.RunWith; |
| 6 | +import org.mockito.ArgumentCaptor; |
| 7 | +import org.robolectric.RobolectricTestRunner; |
| 8 | +import org.robolectric.annotation.Config; |
| 9 | +import rx.Scheduler; |
| 10 | +import rx.Subscription; |
| 11 | +import rx.operators.AtomicObservableSubscription; |
| 12 | +import rx.util.functions.Func2; |
| 13 | + |
| 14 | +import java.util.concurrent.TimeUnit; |
| 15 | + |
| 16 | +import static org.mockito.Matchers.eq; |
| 17 | +import static org.mockito.Mockito.mock; |
| 18 | +import static org.mockito.Mockito.verify; |
| 19 | + |
| 20 | +/** |
| 21 | + * Schedules actions to run on an Android Handler thread. |
| 22 | + */ |
| 23 | +public class HandlerThreadScheduler extends Scheduler { |
| 24 | + |
| 25 | + private final Handler handler; |
| 26 | + |
| 27 | + /** |
| 28 | + * Constructs a {@link HandlerThreadScheduler} using the given {@link Handler} |
| 29 | + * @param handler {@link Handler} to use when scheduling actions |
| 30 | + */ |
| 31 | + public HandlerThreadScheduler(Handler handler) { |
| 32 | + this.handler = handler; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Calls {@link HandlerThreadScheduler#schedule(Object, rx.util.functions.Func2, long, java.util.concurrent.TimeUnit)} |
| 37 | + * with a delay of zero milliseconds. |
| 38 | + * |
| 39 | + * See {@link #schedule(Object, rx.util.functions.Func2, long, java.util.concurrent.TimeUnit)} |
| 40 | + */ |
| 41 | + @Override |
| 42 | + public <T> Subscription schedule(final T state, final Func2<Scheduler, T, Subscription> action) { |
| 43 | + return schedule(state, action, 0L, TimeUnit.MILLISECONDS); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Calls {@link Handler#postDelayed(Runnable, long)} with a runnable that executes the given action. |
| 48 | + * @param state |
| 49 | + * State to pass into the action. |
| 50 | + * @param action |
| 51 | + * Action to schedule. |
| 52 | + * @param delayTime |
| 53 | + * Time the action is to be delayed before executing. |
| 54 | + * @param unit |
| 55 | + * Time unit of the delay time. |
| 56 | + * @return A Subscription from which one can unsubscribe from. |
| 57 | + */ |
| 58 | + @Override |
| 59 | + public <T> Subscription schedule(final T state, final Func2<Scheduler, T, Subscription> action, long delayTime, TimeUnit unit) { |
| 60 | + final AtomicObservableSubscription subscription = new AtomicObservableSubscription(); |
| 61 | + final Scheduler _scheduler = this; |
| 62 | + handler.postDelayed(new Runnable() { |
| 63 | + @Override |
| 64 | + public void run() { |
| 65 | + subscription.wrap(action.call(_scheduler, state)); |
| 66 | + } |
| 67 | + }, unit.toMillis(delayTime)); |
| 68 | + return subscription; |
| 69 | + } |
| 70 | + |
| 71 | + @RunWith(RobolectricTestRunner.class) |
| 72 | + @Config(manifest=Config.NONE) |
| 73 | + public static final class UnitTest { |
| 74 | + |
| 75 | + @Test |
| 76 | + public void shouldScheduleImmediateActionOnHandlerThread() { |
| 77 | + final Handler handler = mock(Handler.class); |
| 78 | + final Object state = new Object(); |
| 79 | + final Func2<Scheduler, Object, Subscription> action = mock(Func2.class); |
| 80 | + |
| 81 | + Scheduler scheduler = new HandlerThreadScheduler(handler); |
| 82 | + scheduler.schedule(state, action); |
| 83 | + |
| 84 | + // verify that we post to the given Handler |
| 85 | + ArgumentCaptor<Runnable> runnable = ArgumentCaptor.forClass(Runnable.class); |
| 86 | + verify(handler).postDelayed(runnable.capture(), eq(0L)); |
| 87 | + |
| 88 | + // verify that the given handler delegates to our action |
| 89 | + runnable.getValue().run(); |
| 90 | + verify(action).call(scheduler, state); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void shouldScheduleDelayedActionOnHandlerThread() { |
| 95 | + final Handler handler = mock(Handler.class); |
| 96 | + final Object state = new Object(); |
| 97 | + final Func2<Scheduler, Object, Subscription> action = mock(Func2.class); |
| 98 | + |
| 99 | + Scheduler scheduler = new HandlerThreadScheduler(handler); |
| 100 | + scheduler.schedule(state, action, 1L, TimeUnit.SECONDS); |
| 101 | + |
| 102 | + // verify that we post to the given Handler |
| 103 | + ArgumentCaptor<Runnable> runnable = ArgumentCaptor.forClass(Runnable.class); |
| 104 | + verify(handler).postDelayed(runnable.capture(), eq(1000L)); |
| 105 | + |
| 106 | + // verify that the given handler delegates to our action |
| 107 | + runnable.getValue().run(); |
| 108 | + verify(action).call(scheduler, state); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | + |
0 commit comments