Skip to content

Instantly share code, notes, and snippets.

@hborders
Last active January 2, 2016 09:49
Show Gist options
  • Select an option

  • Save hborders/8285441 to your computer and use it in GitHub Desktop.

Select an option

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
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();
}
}
}
@KoljaTM

KoljaTM commented Jan 19, 2014

Copy link
Copy Markdown

Awesome! Thanks a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment