- All Implemented Interfaces:
- Enclosing class:
public static enum LooperMode.Mode extends Enum<LooperMode.Mode>
Specifies the different supported Looper modes.
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Enum
Enum.EnumDesc<E extends Enum<E>>
Enum Constant Summary
Modifier and Type Method Description
valueOf(String name)Returns the enum constant of this type with the specified name.
values()Returns an array containing the constants of this enum type, in the order they are declared.
Methods inherited from class java.lang.Enum
clone, compareTo, describeConstable, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
Enum Constant Details
LEGACY@Deprecated public static final LooperMode.Mode LEGACYDeprecated.use LooperMode.PAUSEDRobolectric's default threading model prior to 4.4.
Tasks posted to Loopers are managed via a
Schedulerbehavior can be controlled via
setIdleState(IdleState), with a default of
There is only a single Looper thread - with tests and all posted Looper tasks executing on that thread.
ShadowLooperAPIs can also be used to control posted tasks, but most of those APIs just serve as a facade to
There are multiple problems with this mode. Some of the major ones are:
- The default
UNPAUSEDstate will execute tasks posted to a
Looperinline synchronously. This differs from real Android behaviour, and can cause issues with code that expects/enforces that posted tasks execute in the correct order, such as RecyclerViews.
Schedulerlist of Runnables can get out of sync with the Looper's
MessageQueue, causing deadlocks or other race conditions.
Schedulerkeeps its own time value, which can get out of sync.
Loopertasks execute in the main thread, causing errors for code that enforces that it runs on a non-main
- The default
PAUSEDpublic static final LooperMode.Mode PAUSEDA mode that more accurately models real Android's
Conceptually LooperMode.PAUSED is similar to the LEGACY
Scheduler.IdleState.PAUSEDin the following ways:
- Tests run on the main looper thread
- Tasks posted to the main
Looperare not executed automatically, and must be explicitly executed via
ShadowLooper.idle(). This guarantees execution order correctness
SystemClocktime is frozen, and can be manually advanced via Robolectric APIs.
- Robolectric will warn users if a test fails with unexecuted tasks in the main Looper queue
- Robolectric test APIs, like
ActivityController.setup(), will automatically idle the main
Looperhas its own thread. Tasks posted to background loopers are executed asynchronously in separate threads.
Looperuse the real
MessageQueueto store their queue of pending tasks
- There is only a single clock value, managed via
ShadowSystemClock. This can be explictly incremented via
SchedulerAPIs for the 'foreground' scheduler are currently supported in this mode as well, although it is recommended to switch to use ShadowLooper APIs directly.
- Apply the LooperMode(PAUSED) annotation to your test package/class/method (or remove a LooperMode(LEGACY) annotation)
- Convert any background
Loopers to shadowOf(looper)
- Convert any
- Run your tests. If you see an test failures like 'Main looper has queued unexecuted runnables.', you may need to insert shadowOf(getMainLooper()).idle() calls to your test to drain the main Looper.
valuespublic static LooperMode.Mode values()Returns an array containing the constants of this enum type, in the order they are declared.
- an array containing the constants of this enum type, in the order they are declared
valueOfpublic static LooperMode.Mode valueOf(String name)Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
name- the name of the enum constant to be returned.
- the enum constant with the specified name
IllegalArgumentException- if this enum type has no constant with the specified name
NullPointerException- if the argument is null