Class ShadowPausedLooper
and @link
LooperMode.Mode.INSTRUMENTATION_TEST
.
This shadow differs from the legacy ShadowLegacyLooper
in the following ways:\ - Has
no connection to Scheduler
. Its APIs are standalone - The main
looper is always paused in PAUSED MODE but can be unpaused in INSTRUMENTATION_TEST mode. When a
looper is paused, posted messages to it are not executed unless idle()
is called. - Just
like in real Android, each looper has its own thread, and posted tasks get executed in that
thread. - - There is only a single SystemClock
value that all loopers read from. Unlike
legacy behavior where each Scheduler
kept their own clock value.
This class should not be used directly; use ShadowLooper
instead.
-
Nested Class Summary
Nested classes/interfaces inherited from class org.robolectric.shadows.ShadowLooper
ShadowLooper.Picker
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
__constructor__
(boolean quitAllowed) protected static Collection<Looper>
Returns theScheduler
that is being used to manage the enqueued tasks.boolean
hasQuit()
void
idle()
Executes all posted tasks scheduled before or at the current time.void
idleConstantly
(boolean shouldIdleConstantly) void
Advances the system clock by the given time, then executes all posted tasks scheduled before or at the given time.void
Helper method to selectively call idle() only if LooperMode is PAUSED.boolean
isIdle()
Returns true if there are no pending tasks scheduled to be executed before current time.boolean
isPaused()
protected static void
loop()
Shadow loop to handle uncaught exceptions.void
pause()
Pause the looper.void
poll
(long timeout) Polls the message queue waiting until a message is posted to the head of the queue.boolean
Enqueue a task to be run later.boolean
postAtFrontOfQueue
(Runnable runnable) Enqueue a task to be run ahead of all other delayed tasks.void
Posts the runnable to the looper and idles until the runnable has been run.protected static void
protected void
quit()
protected void
void
void
reset()
Causes all enqueued tasks to be discarded, and pause state to be resetstatic void
void
Only supported forLooperMode.Mode.LEGACY
.void
Causes only one of the nextRunnable
s that have been scheduled to run while advancing the clock to its start time.void
Runs the current task with the looper paused.void
void
Causes the nextRunnable
(s) that have been scheduled to run while advancing the clock to its start time.static void
setIgnoreUncaughtExceptions
(boolean shouldIgnore) Deprecated.this method only exists to accommodate legacy tests with preexisting issues.boolean
setPaused
(boolean shouldPause) Control the paused state of the Looper.void
unPause()
Not supported for the main Looper inLooperMode.Mode.PAUSED
.Methods inherited from class org.robolectric.shadows.ShadowLooper
assertLooperMode, clearLooperMode, getAllLoopers, getLooperForThread, getShadowMainLooper, idle, idle, idleFor, idleMainLooper, idleMainLooper, idleMainLooper, idleMainLooperConstantly, looperMode, pauseLooper, pauseMainLooper, resetThreadLoopers, runMainLooperOneTask, runMainLooperToNextTask, runUiThreadTasks, runUiThreadTasksIncludingDelayedTasks, shadowMainLooper, unPauseLooper, unPauseMainLooper
-
Constructor Details
-
ShadowPausedLooper
public ShadowPausedLooper()
-
-
Method Details
-
__constructor__
-
getLoopers
-
quitUnchecked
public void quitUnchecked()- Specified by:
quitUnchecked
in classShadowLooper
-
hasQuit
public boolean hasQuit()- Specified by:
hasQuit
in classShadowLooper
-
idle
public void idle()Description copied from class:ShadowLooper
Executes all posted tasks scheduled before or at the current time.- Specified by:
idle
in classShadowLooper
-
idleFor
Description copied from class:ShadowLooper
Advances the system clock by the given time, then executes all posted tasks scheduled before or at the given time.- Specified by:
idleFor
in classShadowLooper
-
isIdle
public boolean isIdle()Description copied from class:ShadowLooper
Returns true if there are no pending tasks scheduled to be executed before current time.- Specified by:
isIdle
in classShadowLooper
-
unPause
public void unPause()Description copied from class:ShadowLooper
Not supported for the main Looper inLooperMode.Mode.PAUSED
.- Specified by:
unPause
in classShadowLooper
-
pause
public void pause()Description copied from class:ShadowLooper
Pause the looper.Has no practical effect for realistic looper, since it is always paused.
- Specified by:
pause
in classShadowLooper
-
isPaused
public boolean isPaused()- Specified by:
isPaused
in classShadowLooper
-
setPaused
public boolean setPaused(boolean shouldPause) Description copied from class:ShadowLooper
Control the paused state of the Looper.Not supported for the main Looper in
LooperMode.Mode.PAUSED
.- Specified by:
setPaused
in classShadowLooper
-
resetScheduler
public void resetScheduler()Description copied from class:ShadowLooper
Only supported forLooperMode.Mode.LEGACY
.- Specified by:
resetScheduler
in classShadowLooper
-
reset
public void reset()Description copied from class:ShadowLooper
Causes all enqueued tasks to be discarded, and pause state to be reset- Specified by:
reset
in classShadowLooper
-
idleIfPaused
public void idleIfPaused()Description copied from class:ShadowLooper
Helper method to selectively call idle() only if LooperMode is PAUSED.Intended for backwards compatibility, to avoid changing behavior for tests still using LEGACY LooperMode.
- Specified by:
idleIfPaused
in classShadowLooper
-
idleConstantly
public void idleConstantly(boolean shouldIdleConstantly) - Specified by:
idleConstantly
in classShadowLooper
-
runToEndOfTasks
public void runToEndOfTasks()Description copied from class:ShadowLooper
Causes all of theRunnable
s that have been scheduled to run while advancing the clock to the start time of the last scheduledRunnable
.- Specified by:
runToEndOfTasks
in classShadowLooper
-
runToNextTask
public void runToNextTask()Description copied from class:ShadowLooper
Causes the nextRunnable
(s) that have been scheduled to run while advancing the clock to its start time. If more than oneRunnable
is scheduled to run at this time then they will all be run.- Specified by:
runToNextTask
in classShadowLooper
-
runOneTask
public void runOneTask()Description copied from class:ShadowLooper
Causes only one of the nextRunnable
s that have been scheduled to run while advancing the clock to its start time. Only oneRunnable
will run even if more than one has been scheduled to run at the same time.- Specified by:
runOneTask
in classShadowLooper
-
post
Description copied from class:ShadowLooper
Enqueue a task to be run later.- Specified by:
post
in classShadowLooper
- Parameters:
runnable
- the task to be rundelayMillis
- how many milliseconds into the (virtual) future to run it- Returns:
- true if the runnable is enqueued
- See Also:
-
postAtFrontOfQueue
Description copied from class:ShadowLooper
Enqueue a task to be run ahead of all other delayed tasks.- Specified by:
postAtFrontOfQueue
in classShadowLooper
- Parameters:
runnable
- the task to be run- Returns:
- true if the runnable is enqueued
- See Also:
-
postSync
Posts the runnable to the looper and idles until the runnable has been run. Generally clients should prefer to useInstrumentation.runOnMainSync(Runnable)
, which will reraise underlying runtime exceptions to the caller. -
runPaused
Description copied from class:ShadowLooper
Runs the current task with the looper paused.When LooperMode is PAUSED, this will execute all pending tasks scheduled before the current time.
- Specified by:
runPaused
in classShadowLooper
-
poll
public void poll(long timeout) Polls the message queue waiting until a message is posted to the head of the queue. This will suspend the thread until a new message becomes available. Returns immediately if the queue is not idle. There's no guarantee that the message queue will not still be idle when returning, but if the message queue becomes not idle it will return immediately.This method is only applicable for the main looper's queue when called on the main thread, as the main looper in Robolectric is processed manually (it doesn't loop)--looper threads are using the native polling of their loopers. Throws an exception if called for another looper's queue. Non-main thread loopers should use
unPause()
.This should be used with care, it can be used to suspend the main (i.e. test) thread while worker threads perform some work, and then resumed by posting to the main looper. Used in a loop to wait on some condition it can process messages on the main looper, simulating the behavior of the real looper, for example:
while (!condition) { shadowMainLooper.poll(timeout); shadowMainLooper.idle(); }
Beware though that a message must be posted to the main thread after the condition is satisfied, or the condition satisfied while idling the main thread, otherwise the main thread will continue to be suspended until the timeout.
- Parameters:
timeout
- Timeout in milliseconds, the maximum time to wait before returning, or 0 to wait indefinitely,
-
getNextScheduledTaskTime
- Specified by:
getNextScheduledTaskTime
in classShadowLooper
- Returns:
- the scheduled time of the next posted task; Duration.ZERO if there is no currently scheduled task.
-
getLastScheduledTaskTime
- Specified by:
getLastScheduledTaskTime
in classShadowLooper
- Returns:
- the scheduled time of the last posted task; Duration.ZERO 0 if there is no currently scheduled task.
-
resetLoopers
-
prepareMainLooper
-
quit
-
quitSafely
-
getScheduler
Description copied from class:ShadowLooper
Returns theScheduler
that is being used to manage the enqueued tasks. This scheduler is managed by the Looper's associated queue.Only supported for
LooperMode.Mode.LEGACY
.- Specified by:
getScheduler
in classShadowLooper
- Returns:
- the
Scheduler
that is being used to manage the enqueued tasks.
-
setIgnoreUncaughtExceptions
Deprecated.this method only exists to accommodate legacy tests with preexisting issues. Silently discarding exceptions is not recommended, and can lead to deadlocks.By default Robolectric will put Loopers that throw uncaught exceptions in their loop method into an error state, where any future posting to the looper's queue will throw an error.This API allows you to disable this behavior. Note this is a permanent setting - it is not reset between tests.
-
loop
Shadow loop to handle uncaught exceptions. Without this logic an uncaught exception on a looper thread will cause idle() to deadlock.
-