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.

  • Constructor Details

    • ShadowLooper

      public ShadowLooper()
  • Method Details

    • assertLooperMode

      public static void assertLooperMode(LooperMode.Mode expectedMode)
    • getShadowMainLooper

      @Deprecated public static ShadowLooper getShadowMainLooper()
      Deprecated.
      Use shadowOf({@link Looper#getMainLooper()}) instead.
    • shadowMainLooper

      public static ShadowLooper shadowMainLooper()
    • getLooperForThread

      public static Looper getLooperForThread(Thread thread)
    • getAllLoopers

      public static Collection<Looper> getAllLoopers()
      Return all created loopers.
    • resetThreadLoopers

      public static void resetThreadLoopers()
      Should not be called directly - Robolectric internal use only.
    • looperMode

      public static LooperMode.Mode looperMode()
      Return the current LooperMode.
    • clearLooperMode

      @Resetter public static void clearLooperMode()
    • pauseLooper

      public static void pauseLooper(Looper looper)
      Pauses 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 #runToEndOftasks or a similar method, otherwise they will not run at all before your test ends.
      Parameters:
      looper - the looper to pause
    • unPauseLooper

      public static void unPauseLooper(Looper looper)
      Puts 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
    • pauseMainLooper

      public static void pauseMainLooper()
      Puts the main ShadowLegacyLooper in an "paused" state.
      See Also:
    • unPauseMainLooper

      public static void unPauseMainLooper()
      Puts the main ShadowLegacyLooper in an "unpaused" state.
      See Also:
    • idleMainLooper

      public 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)
    • idleMainLooper

      public static void idleMainLooper(long amount, TimeUnit unit)
    • idleMainLooperConstantly

      public static void idleMainLooperConstantly(boolean shouldIdleConstantly)
    • runMainLooperOneTask

      public static void runMainLooperOneTask()
    • runMainLooperToNextTask

      public static void runMainLooperToNextTask()
    • runUiThreadTasks

      public static void runUiThreadTasks()
      Runs any immediately runnable tasks previously queued on the UI thread, e.g. by Activity.runOnUiThread(Runnable) or AsyncTask.onPostExecute(Object).

      **Note:** calling this method does not pause or un-pause the scheduler.

      See Also:
    • runUiThreadTasksIncludingDelayedTasks

      public 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) or AsyncTask.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:
    • quitUnchecked

      public abstract void quitUnchecked()
    • hasQuit

      public abstract boolean hasQuit()
    • idle

      public abstract void idle()
      Executes all posted tasks scheduled before or at the current time.
    • idleFor

      public abstract void idleFor(long time, TimeUnit timeUnit)
      Advances the system clock by the given time, then executes all posted tasks scheduled before or at the given time.
    • idleFor

      public void idleFor(Duration duration)
      A variant of idleFor(long, TimeUnit) that accepts a Duration.
    • isIdle

      public abstract boolean isIdle()
      Returns true if there are no pending tasks scheduled to be executed before current time.
    • unPause

      public abstract void unPause()
      Not supported for the main Looper in LooperMode.Mode.PAUSED.
    • isPaused

      public abstract boolean isPaused()
    • setPaused

      public abstract boolean setPaused(boolean shouldPause)
      Control the paused state of the Looper.

      Not supported for the main Looper in LooperMode.Mode.PAUSED.

    • resetScheduler

      public abstract void resetScheduler()
      Only supported for LooperMode.Mode.LEGACY.
    • reset

      public abstract void reset()
      Causes all enqueued tasks to be discarded, and pause state to be reset
    • getScheduler

      public abstract Scheduler getScheduler()
      Returns the Scheduler 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.

      Returns:
      the Scheduler that is being used to manage the enqueued tasks.
    • runPaused

      public abstract void runPaused(Runnable run)
      Runs the current task with the looper paused.

      When LooperMode is PAUSED, this will execute all pending tasks scheduled before the current time.

    • idleIfPaused

      public 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.
      Causes Runnables that have been scheduled to run within the next intervalMillis milliseconds 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.
      Causes Runnables that have been scheduled to run within the next specified amount of time to run while advancing the clock.
    • idleConstantly

      public abstract void idleConstantly(boolean shouldIdleConstantly)
    • runToEndOfTasks

      public abstract void runToEndOfTasks()
      Causes all of the Runnables that have been scheduled to run while advancing the clock to the start time of the last scheduled Runnable.
    • runToNextTask

      public abstract void runToNextTask()
      Causes the next Runnable(s) that have been scheduled to run while advancing the clock to its start time. If more than one Runnable is scheduled to run at this time then they will all be run.
    • runOneTask

      public abstract void runOneTask()
      Causes only one of the next Runnables that have been scheduled to run while advancing the clock to its start time. Only one Runnable will run even if more than one has been scheduled to run at the same time.
    • post

      @Deprecated public abstract boolean post(Runnable runnable, long delayMillis)
      Deprecated.
      Use a Handler instance 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:
    • postAtFrontOfQueue

      @Deprecated public abstract boolean postAtFrontOfQueue(Runnable runnable)
      Deprecated.
      Use a Handler instance 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:
    • pause

      public abstract void pause()
      Pause the looper.

      Has no practical effect for realistic looper, since it is always paused.

    • getNextScheduledTaskTime

      public abstract Duration getNextScheduledTaskTime()
      Returns:
      the scheduled time of the next posted task; Duration.ZERO if there is no currently scheduled task.
    • getLastScheduledTaskTime

      public abstract Duration getLastScheduledTaskTime()
      Returns:
      the scheduled time of the last posted task; Duration.ZERO 0 if there is no currently scheduled task.