Skip to content

AndroidSchedulers.mainThread() has Android dependencies in RxAndroid 2.0.0-RC2 #332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
peter-tackage opened this issue Sep 20, 2016 · 2 comments

Comments

@peter-tackage
Copy link
Contributor

I've just started experimenting with RxJava/RxAndroid 2.0.0-RC2 on an Android app and when attempting to write a (JVM) unit test which replaces AndroidSchedulers.mainThread() with a Scheduler defined via RxAndroidPlugins it seems that this is not possible as it requires Android Handler and Looper dependencies.

This is the Exception that is reported when I attempt to execute the unit tests:

java.lang.ExceptionInInitializerError
    at <blah, blah>
Caused by: java.lang.RuntimeException: Method getMainLooper in android.os.Looper not mocked. See http://g.co/androidstudio/not-mocked for details.
    at android.os.Looper.getMainLooper(Looper.java)
    at io.reactivex.android.schedulers.AndroidSchedulers.<clinit>(AndroidSchedulers.java:24)
    ... 32 more

And this is the code in question from AndroidSchedulers in RxAndroid 2.0.0-RC2:

    // snip
    private static final Scheduler MAIN_THREAD = RxAndroidPlugins.initMainThreadScheduler(
        new HandlerScheduler(new Handler(Looper.getMainLooper())));

    /** A {@link Scheduler} which executes actions on the Android main thread. */
    public static Scheduler mainThread() {
        return RxAndroidPlugins.onMainThreadScheduler(MAIN_THREAD);
    }
    //snip 

Looking at the previous AndroidSchedulers definition from Rx 1.x, this was not the case as it would return the plugin's instance without attempting to evaluate the statement with Android dependencies on demand: the RxAndroidPlugins instance would take preference.

So a few questions. Is my evaluation correct or am I missing something? Secondly, is this the intended behavior?

@peter-tackage peter-tackage changed the title AndroidSchedulers.mainThread() has Android dependencies AndroidSchedulers.mainThread() has Android dependencies in RxAndroid 2.0.0-RC2 Sep 20, 2016
@JakeWharton
Copy link
Contributor

The behavior matches what RxJava 2 does for all of its schedulers which is supplying the original to the plugin functions.

We could change it to Function<Scheduler, Callable<Scheduler>> to lazy init. Wonder if RxJava would be interested in that API change as well.

@JakeWharton
Copy link
Contributor

I've filed an issue on RxJava. We'll see what they say before taking any action here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants