Enum LooperMode.Mode
- All Implemented Interfaces:
Serializable,Comparable<LooperMode.Mode>,java.lang.constant.Constable
- Enclosing class:
LooperMode
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Enum
Enum.EnumDesc<E extends Enum<E>> -
Enum Constant Summary
Enum ConstantsEnum ConstantDescriptionA mode that simulates an android instrumentation test threading model, which has a separate test thread distinct from the main looper thread.Deprecated.use LooperMode.PAUSEDA mode that more accurately models real Android'sLooperbehavior. -
Method Summary
Modifier and TypeMethodDescriptionstatic LooperMode.ModeReturns 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.
-
Enum Constant Details
-
LEGACY
Deprecated.use LooperMode.PAUSEDRobolectric's default threading model prior to 4.4.Tasks posted to Loopers are managed via a
.invalid reference
org.robolectric.util.Schedulerbehavior can be controlled viainvalid reference
org.robolectric.util.Scheduler, with a default ofinvalid reference
setIdleState(IdleState).invalid reference
UNPAUSEDThere is only a single Looper thread - with tests and all posted Looper tasks executing on that thread.
APIs can also be used to control posted tasks, but most of those APIs just serve as a facade toinvalid reference
org.robolectric.shadows.ShadowLooperAPIs.invalid reference
org.robolectric.util.SchedulerThere are multiple problems with this mode. Some of the major ones are:
- The default
state will execute tasks posted to a
invalid reference
UNPAUSEDLooperinline 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. - The
list of Runnables can get out of sync with the Looper's
invalid reference
org.robolectric.util.SchedulerMessageQueue, causing deadlocks or other race conditions. - Each
keeps its own time value, which can get out of sync.
invalid reference
org.robolectric.util.Scheduler - Background
Loopertasks execute in the main thread, causing errors for code that enforces that it runs on a non-mainLooperthread.
- The default
-
PAUSED
A mode that more accurately models real Android'sLooperbehavior.Conceptually LooperMode.PAUSED is similar to the LEGACY
in the following ways:invalid reference
org.robolectric.util.Scheduler.IdleState#PAUSED- Tests run on the main looper thread
- Tasks posted to the main
Looperare not executed automatically, and must be explicitly executed viaAPIs likeinvalid reference
org.robolectric.shadows.ShadowLooper. This guarantees execution order correctnessinvalid reference
org.robolectric.shadows.ShadowLooper#idle() 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
, will automatically idle the main
invalid reference
org.robolectric.android.controller.ActivityController#setup()Looper - Each
Looperhas its own thread. Tasks posted to background loopers are executed asynchronously in separate threads. Looperuse the realMessageQueueto store their queue of pending tasks- There is only a single clock value, managed via
. This can be explictly incremented via
invalid reference
org.robolectric.shadows.ShadowSystemClockSystemClock.setCurrentTimeMillis(long), or.invalid reference
org.robolectric.shadows.ShadowLooper#idleFor(Duration)
APIs for the 'foreground' scheduler are currently supported in this mode as well, although it is recommended to switch to use ShadowLooper APIs directly.invalid reference
org.robolectric.util.SchedulerTo use:
- Apply the LooperMode(PAUSED) annotation to your test package/class/method (or remove a LooperMode(LEGACY) annotation)
- Convert any background
for controlling
invalid reference
org.robolectric.util.SchedulerLoopers to shadowOf(looper) - Convert any
usages to
invalid reference
org.robolectric.android.util.concurrent.RoboExecutorServiceorinvalid reference
org.robolectric.android.util.concurrent.PausedExecutorServiceinvalid reference
org.robolectric.android.util.concurrent.InlineExecutorService - 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.
-
INSTRUMENTATION_TEST
A mode that simulates an android instrumentation test threading model, which has a separate test thread distinct from the main looper thread.Otherwise it is quite similar to PAUSED mode. The clock time is still fixed, and you can use shadowLooper methods to pause, unpause, and wait for any looper to be idle.
It is recommended to use this mode in tests that mostly use androidx.test APIs, which will support being called directly on the main thread or on the test thread. Most org.robolectric APIs that interact with the android UI (e.g. ActivityController) will raise an exception if called off the main thread.
-
-
Method Details
-
values
Returns an array containing the constants of this enum type, in the order they are declared.- Returns:
- an array containing the constants of this enum type, in the order they are declared
-
valueOf
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.)- Parameters:
name- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException- if this enum type has no constant with the specified nameNullPointerException- if the argument is null
-