@Implements(value=android.os.Looper.class) public class ShadowLooper extends Object
Robolectric enqueues posted Runnable
s to be run (on this thread) later. Runnable
s that are scheduled to run immediately can be triggered by calling idle()
.
ShadowMessageQueue
Constructor and Description |
---|
ShadowLooper() |
Modifier and Type | Method and Description |
---|---|
protected void |
__constructor__(boolean quitAllowed) |
static Looper |
getLooperForThread(Thread thread) |
protected static Looper |
getMainLooper() |
Scheduler |
getScheduler()
Returns the
Scheduler that is being used to manage the enqueued tasks. |
static ShadowLooper |
getShadowMainLooper()
Deprecated.
Use
shadowOf( instead. |
boolean |
hasQuit() |
void |
idle()
Causes
Runnable s that have been scheduled to run immediately to actually run. |
void |
idle(long intervalMillis)
Deprecated.
Use
idle(long, TimeUnit) . |
void |
idle(long amount,
TimeUnit unit)
Causes
Runnable s that have been scheduled to run within the next specified amount of time to run while advancing the scheduler’s clock. |
void |
idleConstantly(boolean shouldIdleConstantly) |
static void |
idleMainLooper() |
static void |
idleMainLooper(long interval)
Deprecated.
|
static void |
idleMainLooper(long amount,
TimeUnit unit) |
static void |
idleMainLooperConstantly(boolean shouldIdleConstantly) |
boolean |
isPaused() |
protected static void |
loop() |
protected static Looper |
myLooper() |
void |
pause() |
static void |
pauseLooper(Looper looper)
Pauses execution of tasks posted to the ShadowLooper.
|
static void |
pauseMainLooper()
Puts the main ShadowLooper in an “paused” state.
|
boolean |
post(Runnable runnable,
long delayMillis)
Deprecated.
Use a
Handler instance to post to a looper. |
boolean |
postAtFrontOfQueue(Runnable runnable)
Deprecated.
Use a
Handler instance to post to a looper. |
protected void |
quit() |
protected void |
quitSafely() |
void |
quitUnchecked() |
void |
reset()
Causes all enqueued tasks to be discarded, and pause state to be reset
|
void |
resetScheduler() |
static void |
resetThreadLoopers() |
static void |
runMainLooperOneTask() |
static void |
runMainLooperToNextTask() |
void |
runOneTask()
Causes only one of the next
Runnable s that have been scheduled to run while advancing the scheduler’s clock to its start time. |
void |
runPaused(Runnable r) |
void |
runToEndOfTasks()
|
void |
runToNextTask()
Causes the next
Runnable (s) that have been scheduled to run while advancing the scheduler’s clock to its start time. |
static void |
runUiThreadTasks()
Runs any immediately runnable tasks previously queued on the UI thread, e.g.
|
static void |
runUiThreadTasksIncludingDelayedTasks()
Runs all runnable tasks (pending and future) that have been queued on the UI thread.
|
boolean |
setPaused(boolean shouldPause) |
void |
unPause() |
static void |
unPauseLooper(Looper looper)
Puts the shadow looper in an “unpaused” state (this is the default state).
|
static void |
unPauseMainLooper()
Puts the main ShadowLooper in an “unpaused” state.
|
@Resetter public static void resetThreadLoopers()
@Implementation protected void __constructor__(boolean quitAllowed)
@Implementation protected static Looper getMainLooper()
@Implementation protected static Looper myLooper()
@Implementation protected static void loop()
@Implementation protected void quit()
@Implementation(minSdk=18) protected void quitSafely()
public void quitUnchecked()
public boolean hasQuit()
@Deprecated public static ShadowLooper getShadowMainLooper()
shadowOf(Looper.getMainLooper()
)
instead.public static void pauseLooper(Looper looper)
Pauses execution of tasks posted to the ShadowLooper. 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.
looper
- the looper to pausepublic 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.
looper
- the looper to pausepublic static void pauseMainLooper()
Puts the main ShadowLooper in an “paused” state.
pauseLooper(android.os.Looper)
public static void unPauseMainLooper()
Puts the main ShadowLooper in an “unpaused” state.
unPauseLooper(android.os.Looper)
public static void idleMainLooper()
@Deprecated public static void idleMainLooper(long interval)
idleMainLooper(long, TimeUnit)
.public static void idleMainLooper(long amount, TimeUnit unit)
public static void idleMainLooperConstantly(boolean shouldIdleConstantly)
public static void runMainLooperOneTask()
public static void runMainLooperToNextTask()
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.
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.
runUiThreadTasks()
public void idle()
Causes Runnable
s that have been scheduled to run immediately to actually run. Does not advance the scheduler’s clock;
@Deprecated public void idle(long intervalMillis)
idle(long, TimeUnit)
.Causes Runnable
s that have been scheduled to run within the next intervalMillis
milliseconds to run while advancing the scheduler’s clock.
public void idle(long amount, TimeUnit unit)
Causes Runnable
s that have been scheduled to run within the next specified amount of time to run while advancing the scheduler’s clock.
public void idleConstantly(boolean shouldIdleConstantly)
public void runToEndOfTasks()
public void runToNextTask()
public void runOneTask()
@Deprecated public boolean post(Runnable runnable, long delayMillis)
Handler
instance to post to a looper.Enqueue a task to be run later.
runnable
- the task to be rundelayMillis
- how many milliseconds into the (virtual) future to run itHandler.postDelayed(Runnable,long)
@Deprecated public boolean postAtFrontOfQueue(Runnable runnable)
Handler
instance to post to a looper.Enqueue a task to be run ahead of all other delayed tasks.
runnable
- the task to be runHandler.postAtFrontOfQueue(Runnable)
public void pause()
public void unPause()
public boolean isPaused()
public boolean setPaused(boolean shouldPause)
public void resetScheduler()
public void reset()
Causes all enqueued tasks to be discarded, and pause state to be reset
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.
Scheduler
that is being used to manage the enqueued tasks.public void runPaused(Runnable r)