Skip to content

Commit 6ddceb4

Browse files
committed
Expose scheduler factory for Handler on HandlerScheduler.
This also renames HandlerThreadScheduler to HandlerSchduler as a HandlerThread is another concept.
1 parent ca2fc48 commit 6ddceb4

File tree

8 files changed

+213
-175
lines changed

8 files changed

+213
-175
lines changed

rxandroid/src/main/java/rx/android/schedulers/AndroidSchedulers.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,16 @@
1818
import android.os.Looper;
1919
import rx.android.plugins.RxAndroidPlugins;
2020

21-
/**
22-
* Schedulers that have Android-specific functionality
23-
*/
21+
/** Android-specific Schedulers. */
2422
public final class AndroidSchedulers {
2523
private AndroidSchedulers() {
2624
throw new AssertionError("No instances");
2725
}
2826

2927
private static final Scheduler MAIN_THREAD_SCHEDULER =
30-
new HandlerThreadScheduler(new Handler(Looper.getMainLooper()));
31-
32-
/**
33-
* {@link Scheduler} which uses the provided {@link Handler} to execute actions.
34-
*/
35-
public static Scheduler handlerThread(final Handler handler) {
36-
return new HandlerThreadScheduler(handler);
37-
}
28+
new HandlerScheduler(new Handler(Looper.getMainLooper()));
3829

39-
/**
40-
* {@link Scheduler} which will execute actions on the Android UI thread.
41-
*/
30+
/** A {@link Scheduler} which executes actions on the Android UI thread. */
4231
public static Scheduler mainThread() {
4332
Scheduler scheduler =
4433
RxAndroidPlugins.getInstance().getSchedulersHook().getMainThreadScheduler();

rxandroid/src/main/java/rx/android/schedulers/HandlerThreadScheduler.java renamed to rxandroid/src/main/java/rx/android/schedulers/HandlerScheduler.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,32 @@
2424
import rx.subscriptions.Subscriptions;
2525
import android.os.Handler;
2626

27-
class HandlerThreadScheduler extends Scheduler {
27+
/** A {@link Scheduler} backed by a {@link Handler}. */
28+
public final class HandlerScheduler extends Scheduler {
29+
/** Create a {@link Scheduler} which uses {@code handler} to execute actions. */
30+
public static HandlerScheduler from(Handler handler) {
31+
if (handler == null) throw new NullPointerException("handler == null");
32+
return new HandlerScheduler(handler);
33+
}
2834

2935
private final Handler handler;
3036

31-
/**
32-
* @deprecated Use {@link AndroidSchedulers#handlerThread}.
33-
*/
34-
@Deprecated
35-
public HandlerThreadScheduler(Handler handler) {
37+
HandlerScheduler(Handler handler) {
3638
this.handler = handler;
3739
}
3840

3941
@Override
4042
public Worker createWorker() {
41-
return new InnerHandlerThreadScheduler(handler);
43+
return new HandlerWorker(handler);
4244
}
4345

44-
private static class InnerHandlerThreadScheduler extends Worker {
46+
static class HandlerWorker extends Worker {
4547

4648
private final Handler handler;
4749

4850
private final CompositeSubscription compositeSubscription = new CompositeSubscription();
4951

50-
public InnerHandlerThreadScheduler(Handler handler) {
52+
HandlerWorker(Handler handler) {
5153
this.handler = handler;
5254
}
5355

@@ -84,6 +86,5 @@ public void call() {
8486
public Subscription schedule(final Action0 action) {
8587
return schedule(action, 0, TimeUnit.MILLISECONDS);
8688
}
87-
8889
}
8990
}

rxandroid/src/test/java/rx/android/plugins/RxAndroidPluginsTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
114
package rx.android.plugins;
215

316
import org.junit.After;

rxandroid/src/test/java/rx/android/schedulers/AndroidSchedulersTest.java

Lines changed: 2 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,19 @@
1313
*/
1414
package rx.android.schedulers;
1515

16-
import android.os.Handler;
17-
18-
import java.util.concurrent.atomic.AtomicReference;
1916
import org.junit.After;
2017
import org.junit.Before;
2118
import org.junit.Test;
2219
import org.junit.runner.RunWith;
23-
import org.mockito.ArgumentCaptor;
24-
import org.mockito.Matchers;
25-
import org.robolectric.Robolectric;
2620
import org.robolectric.RobolectricTestRunner;
2721
import org.robolectric.annotation.Config;
28-
29-
import java.util.concurrent.TimeUnit;
30-
31-
import rx.Observable;
3222
import rx.Scheduler;
33-
import rx.Scheduler.Worker;
34-
import rx.Subscriber;
35-
import rx.Subscription;
3623
import rx.android.plugins.RxAndroidPlugins;
3724
import rx.android.plugins.RxAndroidPluginsTest;
3825
import rx.android.plugins.RxAndroidSchedulersHook;
39-
import rx.functions.Action0;
4026
import rx.schedulers.Schedulers;
4127

4228
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;
4929

5030
@RunWith(RobolectricTestRunner.class)
5131
@Config(manifest=Config.NONE)
@@ -57,87 +37,7 @@ public void setUpAndTearDown() {
5737
}
5838

5939
@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() {
14141
final Scheduler scheduler = Schedulers.immediate();
14242
RxAndroidSchedulersHook hook = new RxAndroidSchedulersHook() {
14343
@Override public Scheduler getMainThreadScheduler() {
@@ -147,35 +47,6 @@ public void shouldNotCallOnSubscribeWhenSubscriptionUnsubscribedBeforeDelay() {
14747
RxAndroidPlugins.getInstance().registerSchedulersHook(hook);
14848

14949
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);
18051
}
18152
}

0 commit comments

Comments
 (0)