Last active
January 2, 2016 09:49
-
-
Save hborders/8285441 to your computer and use it in GitHub Desktop.
A new ShadowLooper to workaround https://github.com/robolectric/robolectric/pull/895
Include it in your unit tests along with a top-level configuration file named: org.robolectric.Config.properties with the following contents: shadows=org.github.gist.hborders.Fixed895ShadowLooper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package org.github.gist.hborders; | |
| /** | |
| To automatically configure this Shadow for all tests, create a top-level configuration file named: org.robolectric.Config.properties with the following contents: | |
| shadows=org.github.gist.hborders.Fixed895ShadowLooper | |
| */ | |
| @SuppressWarnings({"UnusedDeclaration"}) | |
| @Implements(Looper.class) | |
| public class Fixed895ShadowLooper extends ShadowLooper { | |
| private static final Thread MAIN_THREAD = Thread.currentThread(); | |
| public void __constructor__() { | |
| } | |
| @Implementation | |
| public static Looper getMainLooper() { | |
| ShadowApplication shadowApplication = Robolectric.getShadowApplication(); | |
| if ((shadowApplication == null) && (Thread.currentThread() == MAIN_THREAD)) { | |
| Looper mainLooper = myLooper(); | |
| return mainLooper; | |
| } else { | |
| // might still throw NullPointerException | |
| // better than returning null because this fails early. | |
| return shadowApplication.getMainLooper(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome! Thanks a lot