Package org.robolectric.shadows
Class ShadowLooper
java.lang.Object
org.robolectric.shadows.ShadowLooper
- Direct Known Subclasses:
- ShadowLegacyLooper,- ShadowPausedLooper
@Implements(value=android.os.Looper.class, shadowPicker=Picker.class) public abstract class ShadowLooper extends Object
The base shadow API class for controlling Loopers.
 
It will delegate calls to the appropriate shadow based on the current LooperMode.
- 
Nested Class SummaryNested Classes Modifier and Type Class Description static classShadowLooper.Picker
- 
Constructor SummaryConstructors Constructor Description ShadowLooper()
- 
Method SummaryModifier and Type Method Description static voidassertLooperMode(LooperMode.Mode expectedMode)static voidclearLooperMode()static Collection<Looper>getAllLoopers()Return all created loopers.abstract DurationgetLastScheduledTaskTime()static LoopergetLooperForThread(Thread thread)abstract DurationgetNextScheduledTaskTime()abstract SchedulergetScheduler()Returns theSchedulerthat is being used to manage the enqueued tasks.static ShadowLoopergetShadowMainLooper()Deprecated.UseshadowOf({@link Looper#getMainLooper()})instead.abstract booleanhasQuit()abstract voididle()Executes all posted tasks scheduled before or at the current time.voididle(long intervalMillis)Deprecated.UseidleFor(Duration).voididle(long amount, TimeUnit unit)Deprecated.abstract voididleConstantly(boolean shouldIdleConstantly)abstract voididleFor(long time, TimeUnit timeUnit)Advances the system clock by the given time, then executes all posted tasks scheduled before or at the given time.voididleFor(Duration duration)A variant ofidleFor(long, TimeUnit)that accepts a Duration.abstract voididleIfPaused()Helper method to selectively call idle() only if LooperMode is PAUSED.static voididleMainLooper()static voididleMainLooper(long interval)Deprecated.static voididleMainLooper(long amount, TimeUnit unit)static voididleMainLooperConstantly(boolean shouldIdleConstantly)abstract booleanisIdle()Returns true if there are no pending tasks scheduled to be executed before current time.abstract booleanisPaused()static LooperMode.ModelooperMode()Return the currentLooperMode.abstract voidpause()Pause the looper.static voidpauseLooper(Looper looper)Pauses execution of tasks posted to the ShadowLegacyLooper.static voidpauseMainLooper()Puts the main ShadowLegacyLooper in an "paused" state.abstract booleanpost(Runnable runnable, long delayMillis)Deprecated.Use aHandlerinstance to post to a looper.abstract booleanpostAtFrontOfQueue(Runnable runnable)Deprecated.Use aHandlerinstance to post to a looper.abstract voidquitUnchecked()abstract voidreset()Causes all enqueued tasks to be discarded, and pause state to be resetabstract voidresetScheduler()Only supported forLooperMode.Mode.LEGACY.static voidresetThreadLoopers()Should not be called directly - Robolectric internal use only.static voidrunMainLooperOneTask()static voidrunMainLooperToNextTask()abstract voidrunOneTask()Causes only one of the nextRunnables that have been scheduled to run while advancing the clock to its start time.abstract voidrunPaused(Runnable run)Runs the current task with the looper paused.abstract voidrunToEndOfTasks()abstract voidrunToNextTask()Causes the nextRunnable(s) that have been scheduled to run while advancing the clock to its start time.static voidrunUiThreadTasks()Runs any immediately runnable tasks previously queued on the UI thread, e.g.static voidrunUiThreadTasksIncludingDelayedTasks()Runs all runnable tasks (pending and future) that have been queued on the UI thread.abstract booleansetPaused(boolean shouldPause)Control the paused state of the Looper.static ShadowLoopershadowMainLooper()abstract voidunPause()Not supported for the main Looper inLooperMode.Mode.PAUSED.static voidunPauseLooper(Looper looper)Puts the shadow looper in an "unpaused" state (this is the default state).static voidunPauseMainLooper()Puts the main ShadowLegacyLooper in an "unpaused" state.
- 
Constructor Details- 
ShadowLooperpublic ShadowLooper()
 
- 
- 
Method Details- 
assertLooperMode
- 
getShadowMainLooperDeprecated.UseshadowOf({@link Looper#getMainLooper()})instead.
- 
shadowMainLooper
- 
getLooperForThread
- 
getAllLoopersReturn all created loopers.
- 
resetThreadLooperspublic static void resetThreadLoopers()Should not be called directly - Robolectric internal use only.
- 
looperModeReturn the currentLooperMode.
- 
clearLooperMode
- 
pauseLooperPauses execution of tasks posted to the ShadowLegacyLooper. This means that during tests, tasks sent to the looper will not execute immediately, but will be queued in a way that is similar to how a real looper works. These queued tasks must be executed explicitly by calling#runToEndOftasksor a similar method, otherwise they will not run at all before your test ends.- Parameters:
- looper- the looper to pause
 
- 
unPauseLooperPuts the shadow looper in an "unpaused" state (this is the default state). This means that during tests, tasks sent to the looper will execute inline, immediately, on the calling (main) thread instead of being queued, in a way similar to how Guava's "DirectExecutorService" works. This is likely not to be what you want: it will cause code to be potentially executed in a different order than how it would execute on the device, and if you are using certain Android APIs (such as view animations) that are non-reentrant, they may not work at all or do unpredictable things. For more information, see this discussion.- Parameters:
- looper- the looper to pause
 
- 
pauseMainLooperpublic static void pauseMainLooper()Puts the main ShadowLegacyLooper in an "paused" state.- See Also:
- pauseLooper(android.os.Looper)
 
- 
unPauseMainLooperpublic static void unPauseMainLooper()Puts the main ShadowLegacyLooper in an "unpaused" state.- See Also:
- unPauseLooper(android.os.Looper)
 
- 
idleMainLooperpublic static void idleMainLooper()
- 
idleMainLooper@InlineMe(replacement="ShadowLooper.idleMainLooper(interval, MILLISECONDS)", imports="org.robolectric.shadows.ShadowLooper", staticImports="java.util.concurrent.TimeUnit.MILLISECONDS") @Deprecated public static void idleMainLooper(long interval)Deprecated.
- 
idleMainLooper
- 
idleMainLooperConstantlypublic static void idleMainLooperConstantly(boolean shouldIdleConstantly)
- 
runMainLooperOneTaskpublic static void runMainLooperOneTask()
- 
runMainLooperToNextTaskpublic static void runMainLooperToNextTask()
- 
runUiThreadTaskspublic static void runUiThreadTasks()Runs any immediately runnable tasks previously queued on the UI thread, e.g. byActivity.runOnUiThread(Runnable)orAsyncTask.onPostExecute(Object).**Note:** calling this method does not pause or un-pause the scheduler. - See Also:
- runUiThreadTasksIncludingDelayedTasks()
 
- 
runUiThreadTasksIncludingDelayedTaskspublic static void runUiThreadTasksIncludingDelayedTasks()Runs all runnable tasks (pending and future) that have been queued on the UI thread. Such tasks may be queued by e.g.Activity.runOnUiThread(Runnable)orAsyncTask.onPostExecute(Object).**Note:** calling this method does not pause or un-pause the scheduler, however the clock is advanced as future tasks are run. - See Also:
- runUiThreadTasks()
 
- 
quitUncheckedpublic abstract void quitUnchecked()
- 
hasQuitpublic abstract boolean hasQuit()
- 
idlepublic abstract void idle()Executes all posted tasks scheduled before or at the current time.
- 
idleForAdvances the system clock by the given time, then executes all posted tasks scheduled before or at the given time.
- 
idleForA variant ofidleFor(long, TimeUnit)that accepts a Duration.
- 
isIdlepublic abstract boolean isIdle()Returns true if there are no pending tasks scheduled to be executed before current time.
- 
unPausepublic abstract void unPause()Not supported for the main Looper inLooperMode.Mode.PAUSED.
- 
isPausedpublic abstract boolean isPaused()
- 
setPausedpublic abstract boolean setPaused(boolean shouldPause)Control the paused state of the Looper.Not supported for the main Looper in LooperMode.Mode.PAUSED.
- 
resetSchedulerpublic abstract void resetScheduler()Only supported forLooperMode.Mode.LEGACY.
- 
resetpublic abstract void reset()Causes all enqueued tasks to be discarded, and pause state to be reset
- 
getSchedulerReturns theSchedulerthat is being used to manage the enqueued tasks. This scheduler is managed by the Looper's associated queue.Only supported for LooperMode.Mode.LEGACY.- Returns:
- the Schedulerthat is being used to manage the enqueued tasks.
 
- 
runPausedRuns the current task with the looper paused.When LooperMode is PAUSED, this will execute all pending tasks scheduled before the current time. 
- 
idleIfPausedpublic abstract void idleIfPaused()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. 
- 
idle@Deprecated @InlineMe(replacement="this.idleFor(Duration.ofMillis(intervalMillis))", imports="java.time.Duration") public final void idle(long intervalMillis)Deprecated.UseidleFor(Duration).CausesRunnables that have been scheduled to run within the nextintervalMillismilliseconds to run while advancing the scheduler's clock.
- 
idle@Deprecated @InlineMe(replacement="this.idleFor(amount, unit)") public final void idle(long amount, TimeUnit unit)Deprecated.CausesRunnables that have been scheduled to run within the next specified amount of time to run while advancing the clock.
- 
idleConstantlypublic abstract void idleConstantly(boolean shouldIdleConstantly)
- 
runToEndOfTaskspublic abstract void runToEndOfTasks()
- 
runToNextTaskpublic abstract void runToNextTask()
- 
runOneTaskpublic abstract void runOneTask()
- 
postDeprecated.Use aHandlerinstance to post to a looper.Enqueue a task to be run later.- Parameters:
- runnable- the task to be run
- delayMillis- how many milliseconds into the (virtual) future to run it
- Returns:
- true if the runnable is enqueued
- See Also:
- Handler.postDelayed(Runnable,long)
 
- 
postAtFrontOfQueueDeprecated.Use aHandlerinstance to post to a looper.Enqueue a task to be run ahead of all other delayed tasks.- Parameters:
- runnable- the task to be run
- Returns:
- true if the runnable is enqueued
- See Also:
- Handler.postAtFrontOfQueue(Runnable)
 
- 
pausepublic abstract void pause()Pause the looper.Has no practical effect for realistic looper, since it is always paused. 
- 
getNextScheduledTaskTime- Returns:
- the scheduled time of the next posted task; Duration.ZERO if there is no currently scheduled task.
 
- 
getLastScheduledTaskTime- Returns:
- the scheduled time of the last posted task; Duration.ZERO 0 if there is no currently scheduled task.
 
 
-