Class ShadowLegacyLooper

java.lang.Object
org.robolectric.shadows.ShadowLooper
org.robolectric.shadows.ShadowLegacyLooper

@Implements(value=android.os.Looper.class, isInAndroidSdk=false) public class ShadowLegacyLooper extends ShadowLooper
The shadow Looper implementation for LooperMode.Mode.LEGACY.

Robolectric enqueues posted Runnables to be run (on this thread) later. Runnables that are scheduled to run immediately can be triggered by calling idle().

See Also:
  • Constructor Details

    • ShadowLegacyLooper

      public ShadowLegacyLooper()
  • Method Details

    • resetThreadLoopers

      @Resetter public static void resetThreadLoopers()
    • internalInitializeBackgroundThreadScheduler

      public static void internalInitializeBackgroundThreadScheduler()
      Internal API to initialize background thread scheduler from AndroidTestEnvironment.
    • __constructor__

      @Implementation protected void __constructor__(boolean quitAllowed)
    • getMainLooper

      @Implementation protected static Looper getMainLooper()
    • myLooper

      @Implementation protected static Looper myLooper()
    • loop

      @Implementation protected static void loop()
    • quit

      @Implementation protected void quit()
    • quitSafely

      @Implementation(minSdk=18) protected void quitSafely()
    • quitUnchecked

      public void quitUnchecked()
      Specified by:
      quitUnchecked in class ShadowLooper
    • hasQuit

      public boolean hasQuit()
      Specified by:
      hasQuit in class ShadowLooper
    • getLooperForThread

      public static Looper getLooperForThread(Thread thread)
    • getLoopers

      protected static Collection<Looper> getLoopers()
      Return loopers for all threads including main thread.
    • idle

      public void idle()
      Description copied from class: ShadowLooper
      Executes all posted tasks scheduled before or at the current time.
      Specified by:
      idle in class ShadowLooper
    • idleFor

      public void idleFor(long time, TimeUnit timeUnit)
      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 class ShadowLooper
    • 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 class ShadowLooper
    • 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 class ShadowLooper
    • idleConstantly

      public void idleConstantly(boolean shouldIdleConstantly)
      Specified by:
      idleConstantly in class ShadowLooper
    • runToEndOfTasks

      public void runToEndOfTasks()
      Description copied from class: ShadowLooper
      Causes all of the Runnables that have been scheduled to run while advancing the clock to the start time of the last scheduled Runnable.
      Specified by:
      runToEndOfTasks in class ShadowLooper
    • runToNextTask

      public void runToNextTask()
      Description copied from class: ShadowLooper
      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.
      Specified by:
      runToNextTask in class ShadowLooper
    • runOneTask

      public void runOneTask()
      Description copied from class: ShadowLooper
      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.
      Specified by:
      runOneTask in class ShadowLooper
    • post

      @Deprecated public boolean post(Runnable runnable, long delayMillis)
      Deprecated.
      Use a Handler instance to post to a looper.
      Enqueue a task to be run later.
      Specified by:
      post in class ShadowLooper
      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 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.
      Specified by:
      postAtFrontOfQueue in class ShadowLooper
      Parameters:
      runnable - the task to be run
      Returns:
      true if the runnable is enqueued
      See Also:
    • 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 class ShadowLooper
    • getNextScheduledTaskTime

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

      public Duration getLastScheduledTaskTime()
      Specified by:
      getLastScheduledTaskTime in class ShadowLooper
      Returns:
      the scheduled time of the last posted task; Duration.ZERO 0 if there is no currently scheduled task.
    • unPause

      public void unPause()
      Description copied from class: ShadowLooper
      Not supported for the main Looper in LooperMode.Mode.PAUSED.
      Specified by:
      unPause in class ShadowLooper
    • isPaused

      public boolean isPaused()
      Specified by:
      isPaused in class ShadowLooper
    • 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 class ShadowLooper
    • resetScheduler

      public void resetScheduler()
      Description copied from class: ShadowLooper
      Only supported for LooperMode.Mode.LEGACY.
      Specified by:
      resetScheduler in class ShadowLooper
    • reset

      public void reset()
      Causes all enqueued tasks to be discarded, and pause state to be reset
      Specified by:
      reset in class ShadowLooper
    • getScheduler

      public Scheduler getScheduler()
      Returns the Scheduler that is being used to manage the enqueued tasks. This scheduler is managed by the Looper's associated queue.
      Specified by:
      getScheduler in class ShadowLooper
      Returns:
      the Scheduler that is being used to manage the enqueued tasks.
    • runPaused

      public void runPaused(Runnable r)
      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 class ShadowLooper