Skip to content

Commit eb27d75

Browse files
authored
Merge pull request ReactiveX#358 from hzsweers/hooks
Add handler getters to match RxJavaPlugins API
2 parents 0482e41 + ca22269 commit eb27d75

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

rxandroid/src/main/java/io/reactivex/android/plugins/RxAndroidPlugins.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,22 @@ public static Scheduler onMainThreadScheduler(Scheduler scheduler) {
5757
return apply(f, scheduler);
5858
}
5959

60+
/**
61+
* Returns the current hook function.
62+
* @return the hook function, may be null
63+
*/
64+
public static Function<Callable<Scheduler>, Scheduler> getInitMainThreadSchedulerHandler() {
65+
return onInitMainThreadHandler;
66+
}
67+
68+
/**
69+
* Returns the current hook function.
70+
* @return the hook function, may be null
71+
*/
72+
public static Function<Scheduler, Scheduler> getOnMainThreadSchedulerHandler() {
73+
return onMainThreadHandler;
74+
}
75+
6076
/**
6177
* Removes all handlers and resets the default behavior.
6278
*/

rxandroid/src/test/java/io/reactivex/android/plugins/RxAndroidPluginsTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
import io.reactivex.Scheduler;
2727
import io.reactivex.android.testutil.EmptyScheduler;
2828
import io.reactivex.functions.Function;
29+
import io.reactivex.schedulers.Schedulers;
2930

3031
import static junit.framework.TestCase.fail;
3132
import static org.junit.Assert.assertEquals;
33+
import static org.junit.Assert.assertNull;
3234
import static org.junit.Assert.assertSame;
3335

3436
@RunWith(RobolectricTestRunner.class)
@@ -158,4 +160,37 @@ public void overrideInitMainSchedulerThrowsWhenSchedulerCallableReturnsNull() {
158160
}
159161
}
160162

163+
@Test
164+
public void getInitMainThreadSchedulerHandlerReturnsHandler() {
165+
Function<Callable<Scheduler>, Scheduler> handler = new Function<Callable<Scheduler>, Scheduler>() {
166+
@Override public Scheduler apply(Callable<Scheduler> schedulerCallable) throws Exception {
167+
return Schedulers.trampoline();
168+
}
169+
};
170+
RxAndroidPlugins.setInitMainThreadSchedulerHandler(handler);
171+
assertSame(handler, RxAndroidPlugins.getInitMainThreadSchedulerHandler());
172+
}
173+
174+
@Test
175+
public void getMainThreadSchedulerHandlerReturnsHandler() {
176+
Function<Scheduler, Scheduler> handler = new Function<Scheduler, Scheduler>() {
177+
@Override public Scheduler apply(Scheduler scheduler) {
178+
return Schedulers.trampoline();
179+
}
180+
};
181+
RxAndroidPlugins.setMainThreadSchedulerHandler(handler);
182+
assertSame(handler, RxAndroidPlugins.getOnMainThreadSchedulerHandler());
183+
}
184+
185+
@Test
186+
public void getInitMainThreadSchedulerHandlerReturnsNullIfNotSet() {
187+
RxAndroidPlugins.reset();
188+
assertNull(RxAndroidPlugins.getInitMainThreadSchedulerHandler());
189+
}
190+
191+
@Test
192+
public void getMainThreadSchedulerHandlerReturnsNullIfNotSet() {
193+
RxAndroidPlugins.reset();
194+
assertNull(RxAndroidPlugins.getOnMainThreadSchedulerHandler());
195+
}
161196
}

0 commit comments

Comments
 (0)