Serializable
, Comparable<LooperMode.Mode>
public static enum LooperMode.Mode extends Enum<LooperMode.Mode>
Enum Constant | Description |
---|---|
LEGACY |
Deprecated.
use LooperMode.PAUSED
|
PAUSED |
A mode that more accurately models real Android's
Looper behavior. |
Modifier and Type | Method | Description |
---|---|---|
static LooperMode.Mode |
valueOf(String name) |
Returns the enum constant of this type with the specified name.
|
static LooperMode.Mode[] |
values() |
Returns an array containing the constants of this enum type, in
the order they are declared.
|
@Deprecated public static final LooperMode.Mode LEGACY
Tasks posted to Loopers are managed via a Scheduler
. Scheduler
behavior can be controlled via setIdleState(IdleState)
, with a default of UNPAUSED
.
There is only a single Looper thread - with tests and all posted Looper tasks executing on that thread.
ShadowLooper
APIs can also be used to control posted
tasks, but most of those APIs just serve as a facade to Scheduler
APIs.
There are multiple problems with this mode. Some of the major ones are:
UNPAUSED
state
will execute tasks posted to a Looper
inline 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.
Scheduler
list of Runnables can get out of sync with
the Looper's MessageQueue
, causing deadlocks or other race
conditions.
Scheduler
keeps its own time value, which can get out
of sync.
Looper
tasks execute in the main thread, causing errors
for code that enforces that it runs on a non-main Looper
thread.
public static final LooperMode.Mode PAUSED
Looper
behavior.
Conceptually LooperMode.PAUSED is similar to the LEGACY Scheduler.IdleState.PAUSED
in the following ways:
Looper
are not executed automatically, and
must be explicitly executed via ShadowLooper
APIs like
ShadowLooper.idle()
. This guarantees execution order
correctness
SystemClock
time is frozen, and can be manually advanced via
Robolectric APIs.
ActivityController.setup()
, will automatically idle
the main Looper
Looper
has its own thread. Tasks posted to background loopers
are executed asynchronously in separate threads.
Looper
use the real MessageQueue
to store their
queue of pending tasks
ShadowSystemClock
. This can be explictly incremented via
SystemClock.setCurrentTimeMillis(long)
, or ShadowLooper.idleFor(Duration)
.
Scheduler
APIs for the 'foreground' scheduler
are currently supported in this mode as well, although it is recommended to switch to use
ShadowLooper APIs directly.
To use:
Scheduler
for controlling Looper
s to shadowOf(looper)
RoboExecutorService
usages
to PausedExecutorService
or InlineExecutorService
public static LooperMode.Mode[] values()
for (LooperMode.Mode c : LooperMode.Mode.values()) System.out.println(c);
public static LooperMode.Mode valueOf(String name)
name
- the name of the enum constant to be returned.IllegalArgumentException
- if this enum type has no constant with the specified nameNullPointerException
- if the argument is null